1
0
mirror of https://github.com/limyz/573controller synced 2024-11-30 18:24:28 +01:00

Merge pull request #1 from 987123879113/optimization

Remove case/conditional code for slight optimization
This commit is contained in:
limyz 2020-06-04 16:23:15 +08:00 committed by GitHub
commit e9c8f6cab3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 41 deletions

View File

@ -1,7 +1,7 @@
//--------------------------------------------------------------------
// 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.
//
// All digital pins are grounded when they are pressed.
@ -32,7 +32,7 @@ byte i2c_read(byte address) {
Wire.beginTransmission(_slave_id);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(_slave_id, 1);
while(Wire.available()) {
return Wire.read();
@ -89,28 +89,12 @@ void loop() {
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);
byte x_val_h = i2c_read(OUTX_H), y_val_h = i2c_read(OUTY_H), z_val_h = i2c_read(OUTZ_H);
// Read pin values
for (int index = 0; index < 5; index++) {
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (currentButtonState != lastButtonState[index]) {
switch (index) {
case 0:
Joystick.setButton(0, currentButtonState);
break;
case 1:
Joystick.setButton(1, currentButtonState);
break;
case 2:
Joystick.setButton(2, currentButtonState);
break;
case 3:
Joystick.setButton(3, currentButtonState);
break;
case 4:
Joystick.setButton(4, currentButtonState);
break;
}
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}

View File

@ -1,7 +1,7 @@
//--------------------------------------------------------------------
// For use with a 573 or GH Guitar Controller
//
// This sketch maps a ADXL345 accelerometer via I2C and uses 7 digital
// This sketch maps a ADXL345 accelerometer via I2C and uses 7 digital
// inputs - 5 buttons and a 2 hat switches for the pick.
//
// All digital pins are grounded when they are pressed.
@ -70,12 +70,12 @@ int lastButtonState[5] = {0,0,0,0,0};
int lastHatSwitchState[2] = {0,0};
void loop() {
/* Get a new sensor event */
sensors_event_t event;
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
bool valueChanged = false;
Joystick.setXAxis(event.acceleration.x);
Joystick.setYAxis(event.acceleration.y);
Joystick.setZAxis(event.acceleration.z);
@ -84,23 +84,7 @@ void loop() {
for (int index = 0; index < 5; index++) {
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (currentButtonState != lastButtonState[index]) {
switch (index) {
case 0:
Joystick.setButton(0, currentButtonState);
break;
case 1:
Joystick.setButton(1, currentButtonState);
break;
case 2:
Joystick.setButton(2, currentButtonState);
break;
case 3:
Joystick.setButton(3, currentButtonState);
break;
case 4:
Joystick.setButton(4, currentButtonState);
break;
}
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}