diff --git a/src/main.cpp b/src/main.cpp index fa478b0..e566729 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,9 @@ //numPCFButtons is greater than 8, its expected to be using 2 PCF8572 chips (multiples of 8) const int numPCFButtons = 8; const int numRotaries = 3; +const int rotJoy[numRotaries] = { + 32,30,28 +}; unsigned long btnTime[numPCFButtons] = {0}; unsigned long rotTime[numRotaries] = {0}; unsigned long btnPressedTime[numPCFButtons] = {0}; @@ -16,15 +19,16 @@ int btnState[numPCFButtons] = {0}; int rotState[numRotaries] = {0}; const unsigned long gButtonDelta = 15; const unsigned long gButtonPressedDelta = 100; -bool debug = false; //Setup I/O Expander Chips PCF8574 pcf1(0x20); //Setup Rotary Encoders -Rotary r1 = Rotary(4, 5); -Rotary r2 = Rotary(6, 7); -Rotary r3 = Rotary(8, 9); +Rotary r[numRotaries] = { +Rotary (4, 5), +Rotary (6, 7), +Rotary (8, 9) +}; //Setup Joystick Joystick_ joy(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, @@ -39,7 +43,9 @@ void setup() { pinMode(LED_BUILTIN, OUTPUT); //debug LED //INIT Rotary Encoder - r1.begin(true); + for (int i = 0; i < numRotaries; i++){ + r[i].begin(true); + } //INIT PCF8574 pcf1.begin(); @@ -56,8 +62,7 @@ void setup() { //Joystick start joy.begin(); - - + } void processBtn1(int i) { @@ -120,6 +125,40 @@ void processBtn1(int i) { } } +void processRot1(int i) { + unsigned char en1 = r[i].process(); + if (en1) { + //Serial.println(en1 == DIR_CW ? "Right" : "Left"); + if (en1 == DIR_CW) { + Serial.println("RIGHT"); + rotState[i] = 1; + rotTime[i] = millis() + gButtonDelta; + joy.releaseButton(rotJoy[i]); + joy.pressButton(rotJoy[i]); + digitalWrite(LED_BUILTIN, LOW); //This can be written for joystick.letgo. This is in case the button is still being pressed, it will let go before pressing it again + digitalWrite(LED_BUILTIN, HIGH); + } else { + Serial.println("LEFT"); + rotState[i] = 1; + rotTime[i] = millis() + gButtonDelta; + joy.releaseButton(rotJoy[i-1]); + joy.pressButton(rotJoy[i-1]); + digitalWrite(LED_BUILTIN, LOW); //This can be written for joystick.letgo. This is in case the button is still being pressed, it will let go before pressing it again + digitalWrite(LED_BUILTIN, HIGH); + } + } + + if (rotState[i] == 1) { + if (millis() >= rotTime[1]) { + Serial.println("Encoder Off"); + digitalWrite(LED_BUILTIN,LOW); + joy.releaseButton(rotJoy[i]); + joy.releaseButton(rotJoy[i-1]); + rotState[i] = 0; + } + } +} + void specialBtnOnStart(int i) { //this sample assumes the button is pressed during startup //once the button is not pressed any more, it will simulate a button press. @@ -171,35 +210,12 @@ void loop() { //Start PCF8574 Button pressed detection section for (int i = 0; i < numPCFButtons; i++) { - processBtn1(i); + processBtn1(i); } - - //Rotary Encoder - unsigned char en1 = r1.process(); - if (en1) { - //Serial.println(en1 == DIR_CW ? "Right" : "Left"); - if (en1 == DIR_CW) { - Serial.println("RIGHT"); - rotState[1] = 1; - rotTime[1] = millis() + gButtonDelta; - digitalWrite(LED_BUILTIN, LOW); //This can be written for joystick.letgo. This is in case the button is still being pressed, it will let go before pressing it again - digitalWrite(LED_BUILTIN, HIGH); - } else { - Serial.println("LEFT"); - rotState[1] = 1; - rotTime[1] = millis() + gButtonDelta; - digitalWrite(LED_BUILTIN, LOW); //This can be written for joystick.letgo. This is in case the button is still being pressed, it will let go before pressing it again - digitalWrite(LED_BUILTIN, HIGH); - } - } - - if (rotState[1] == 1) { - if (millis() >= rotTime[1]) { - Serial.println("Encoder Off"); - digitalWrite(LED_BUILTIN,LOW); - rotState[1] = 0; - } - } - //End PCF8574 Button Pressed detection section + + for (int i = 0; i < numRotaries; i++) { + processRot1(i); + } + } \ No newline at end of file