1
0
mirror of https://github.com/limyz/573controller synced 2024-11-23 22:51:00 +01:00

Updated _gfxg

Minor button tweaks for out-of-box-experience with GITADORA PC version
This commit is contained in:
limyz 2020-08-04 16:46:12 +08:00 committed by GitHub
parent c2566adb82
commit f2815c0797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,125 +1,122 @@
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// For use with a 573 or GH Guitar Controller // For use with a 573 or GH Guitar Controller
// //
// This sketch maps a LIS3LV02DL accelerometer via I2C and uses 7 digital // This sketch maps a LIS3LV02DL accelerometer via I2C and uses 7 digital
// inputs - 5 buttons and a 2 hat switches for the pick. // inputs - 5 buttons and a 2 hat switches for the pick.
// //
// All digital pins are grounded when they are pressed. // All digital pins are grounded when they are pressed.
// //
// NOTE: This sketch file is for use with Arduino Leonardo and // NOTE: This sketch file is for use with Arduino Leonardo and
// Micro only. // Micro only.
// //
// by lyzzz // by lyzzz
// 2020-03-31 // 2020-03-31
//-------------------------------------------------------------------- //--------------------------------------------------------------------
#include <Joystick.h> #include <Joystick.h>
#include <Wire.h> #include <Wire.h>
// LIS3LV02DL Definitions // LIS3LV02DL Definitions
int _slave_id = 0x1D; int _slave_id = 0x1D;
#define CTRL_REG1 0x20 #define CTRL_REG1 0x20
#define CTRL_REG2 0x21 #define CTRL_REG2 0x21
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
5, 1, // Button Count, Hat Switch Count 5, 1, // Button Count, Hat Switch Count
true, true, true, // X, Y and Z Axis true, true, true, // X, Y and Z Axis
false, false, false, // No Rx, Ry, or Rz false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering false, false, false); // No accelerator, brake, or steering
byte i2c_read(byte address) { byte i2c_read(byte address) {
Wire.beginTransmission(_slave_id); Wire.beginTransmission(_slave_id);
Wire.write(address); Wire.write(address);
Wire.endTransmission(); Wire.endTransmission();
Wire.requestFrom(_slave_id, 1); Wire.requestFrom(_slave_id, 1);
while(Wire.available()) { while(Wire.available()) {
return Wire.read(); return Wire.read();
} }
} }
void setup() { void setup() {
// Initialize Pins // Initialize Pins
// Pins 2 and 3 -> Guitar Accelerometer (I2C) // Pins 2 and 3 -> Guitar Accelerometer (I2C)
pinMode(2, INPUT_PULLUP); pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP); pinMode(3, INPUT_PULLUP);
// Pins 5 to 9 -> Guitar R, G, B, Y, P Buttons // Pins 5 to 9 -> Guitar R, G, B, Y, P Buttons
pinMode(4, INPUT_PULLUP); pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP); pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP); pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP); pinMode(8, INPUT_PULLUP);
// Pins 9 to 10 -> Guitar Pick // Pins 9 to 10 -> Guitar Pick
pinMode(9, INPUT_PULLUP); pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP); pinMode(10, INPUT_PULLUP);
// Initialise I2C for LIS3LV02DL // Initialise I2C for LIS3LV02DL
Wire.begin(); Wire.begin();
Wire.beginTransmission(_slave_id); Wire.beginTransmission(_slave_id);
Wire.write(CTRL_REG1); Wire.write(CTRL_REG1);
Wire.write(0xF7); Wire.write(0xF7);
Wire.endTransmission(); Wire.endTransmission();
// Initialize Joystick Library // Initialize Joystick Library
Joystick.begin(); Joystick.begin();
Joystick.setXAxisRange(-2048, 2048); Joystick.setXAxisRange(-2048, 2048);
Joystick.setYAxisRange(-2048, 2048); Joystick.setYAxisRange(-2048, 2048);
Joystick.setZAxisRange(-2048, 2048); Joystick.setZAxisRange(-2048, 2048);
} }
// Constant that maps the phyical pin to the joystick button. byte keys[] = {6, 4, 5, 7, 8};
const int pinToButtonMap = 4; byte pick[] = {9 ,10};
const int pinToHatSwitchMap = 9; int lastButtonState[5] = {0,0,0,0,0};
int lastHatSwitchState[2] = {0,0};
// Last state of the buttons
int lastButtonState[5] = {0,0,0,0,0}; void loop() {
int lastHatSwitchState[2] = {0,0}; #define OUTX_L 0x28
#define OUTX_H 0x29
void loop() { #define OUTY_L 0x2A
#define OUTX_L 0x28 #define OUTY_H 0x2B
#define OUTX_H 0x29 #define OUTZ_L 0x2C
#define OUTY_L 0x2A #define OUTZ_H 0x2D
#define OUTY_H 0x2B
#define OUTZ_L 0x2C bool valueChanged = false;
#define OUTZ_H 0x2D byte x_val_l = i2c_read(OUTX_L), y_val_l = i2c_read(OUTY_L), z_val_l = i2c_read(OUTZ_L);
byte x_val_h = i2c_read(OUTX_H), y_val_h = i2c_read(OUTY_H), z_val_h = i2c_read(OUTZ_H);
bool valueChanged = false;
byte x_val_l = i2c_read(OUTX_L), y_val_l = i2c_read(OUTY_L), z_val_l = i2c_read(OUTZ_L); // Read pin values
byte x_val_h = i2c_read(OUTX_H), y_val_h = i2c_read(OUTY_H), z_val_h = i2c_read(OUTZ_H); for (int index = 0; index < 5; index++) {
int currentButtonState = !digitalRead(keys[index]);
// Read pin values if (currentButtonState != lastButtonState[index]) {
for (int index = 0; index < 5; index++) { Joystick.setButton(index, currentButtonState);
int currentButtonState = !digitalRead(index + pinToButtonMap); lastButtonState[index] = currentButtonState;
if (currentButtonState != lastButtonState[index]) { }
Joystick.setButton(index, currentButtonState); }
lastButtonState[index] = currentButtonState;
} for (int index = 0; index < 2; index++) {
} int currentHatSwitchState = digitalRead(pick[index]);
if (currentHatSwitchState != lastHatSwitchState[index]) {
for (int index = 0; index < 2; index++) { valueChanged = true;
int currentHatSwitchState = digitalRead(index + pinToHatSwitchMap); lastHatSwitchState[index] = currentHatSwitchState;
if (currentHatSwitchState != lastHatSwitchState[index]) { }
valueChanged = true; }
lastHatSwitchState[index] = currentHatSwitchState;
} if (valueChanged) {
} if ((lastHatSwitchState[0] == 0) && (lastHatSwitchState[1] == 0)) {
Joystick.setHatSwitch(0, -1);
if (valueChanged) { }
if ((lastHatSwitchState[0] == 0) && (lastHatSwitchState[1] == 0)) { if (lastHatSwitchState[0] == 1) {
Joystick.setHatSwitch(0, -1); Joystick.setHatSwitch(0, 0);
} }
if (lastHatSwitchState[0] == 1) { if (lastHatSwitchState[1] == 1) {
Joystick.setHatSwitch(0, 0); Joystick.setHatSwitch(0, 180);
} }
if (lastHatSwitchState[1] == 1) { }
Joystick.setHatSwitch(0, 180);
} Joystick.setXAxis(x_val_l | x_val_h << 8);
} Joystick.setYAxis(y_val_l | y_val_h << 8);
Joystick.setZAxis(z_val_l | z_val_h << 8);
Joystick.setXAxis(x_val_l | x_val_h << 8); }
Joystick.setYAxis(y_val_l | y_val_h << 8);
Joystick.setZAxis(z_val_l | z_val_h << 8);
}