From 4f9a699823c4c613871c9b23f7753b1dcc35074b Mon Sep 17 00:00:00 2001 From: 987123879113 <63495610+987123879113@users.noreply.github.com> Date: Thu, 4 Jun 2020 17:06:22 +0900 Subject: [PATCH] Optimize code slightly --- _gfxg/_gfxg.ino | 24 ++++-------------------- _gfxg_adxl345/_gfxg_adxl345.ino | 26 +++++--------------------- 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/_gfxg/_gfxg.ino b/_gfxg/_gfxg.ino index 057a554..cb39db3 100644 --- a/_gfxg/_gfxg.ino +++ b/_gfxg/_gfxg.ino @@ -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; } } diff --git a/_gfxg_adxl345/_gfxg_adxl345.ino b/_gfxg_adxl345/_gfxg_adxl345.ino index 255baac..1979d7b 100644 --- a/_gfxg_adxl345/_gfxg_adxl345.ino +++ b/_gfxg_adxl345/_gfxg_adxl345.ino @@ -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; } }