From dad952c9a57bd774355d854ea3f5ead45dda9b90 Mon Sep 17 00:00:00 2001 From: Zepheris Date: Thu, 15 Oct 2020 06:48:23 -0600 Subject: [PATCH] Added Joystick.h Setup PCF buttons to activate via joystick Todo: Finish rotary encoder to work via joystick Todo: make code more efficient --- platformio.ini | 1 + src/main.cpp | 110 ++++++++++++++++++++----------------------------- 2 files changed, 46 insertions(+), 65 deletions(-) diff --git a/platformio.ini b/platformio.ini index b26178f..a0c81fc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,3 +15,4 @@ framework = arduino lib_deps = xreef/PCF8574 library@^2.2.0 ellsclytn/Rotary@0.0.0-alpha+sha.d1fef10209 + mheironimus/Joystick@^2.0.7 diff --git a/src/main.cpp b/src/main.cpp index 8734c76..fa478b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,36 +1,42 @@ -//Need to create an array to house all of the button states. Define the array using a variable so I can just change a number at the top of the code. #include "Arduino.h" #include "PCF8574.h" #include "Rotary.h" - -//Setup I/O Expander Chips -PCF8574 pcf1(0x20); -//PCF8574 pcf2(0x38); -//Setup Rotary Encoders -Rotary r1 = Rotary(4, 5); +#include "Joystick.h" //setup variables //numPCFButtons is greater than 8, its expected to be using 2 PCF8572 chips (multiples of 8) -int numPCFButtons = 8; -//int numPCFButtons = 16; -unsigned long btnTime[32] = {0}; -unsigned long btnPressedTime[8] = {0}; -int btnState[32] = {0}; +const int numPCFButtons = 8; +const int numRotaries = 3; +unsigned long btnTime[numPCFButtons] = {0}; +unsigned long rotTime[numRotaries] = {0}; +unsigned long btnPressedTime[numPCFButtons] = {0}; +unsigned long rotPressedTime[numRotaries] = {0}; +int btnState[numPCFButtons] = {0}; +int rotState[numRotaries] = {0}; const unsigned long gButtonDelta = 15; const unsigned long gButtonPressedDelta = 100; -/* - #define int RELEASED 0 - #define int PRESSSED 1 - #define int HOLD 1 -*/ +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); + +//Setup Joystick +Joystick_ joy(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, + 32, 0, // Button Count, Hat Switch Count + true, true, false, // X and Y, but no Z Axis + false, false, false, // No Rx, Ry, or Rz + false, false, // No rudder or throttle + false, false, false); // No accelerator, brake, or steering void setup() { pinMode(LED_BUILTIN, OUTPUT); //debug LED - - Serial.begin(9600); - while (!Serial); // Leonardo needs this in order to see serial output at startup //INIT Rotary Encoder r1.begin(true); @@ -47,8 +53,11 @@ void setup() { for (int i = 0; i < 8; i++) { pcf1.digitalWrite(i, LOW); } + + //Joystick start + joy.begin(); + - Serial.println("Ready"); } void processBtn1(int i) { @@ -72,8 +81,9 @@ void processBtn1(int i) { btnTime[i] = millis() + gButtonDelta; //Are we still pressing the button? If so, skip checking for the button state if (btnState[i] == 0) { - //joystick.button(i).push + //joystick.button(i).push <--psudo code //Start Debug Code + joy.pressButton(i); digitalWrite(LED_BUILTIN, HIGH); Serial.write("Button: "); Serial.print(i); @@ -93,8 +103,9 @@ void processBtn1(int i) { // btnState[i] = 0; // break; default: - //joystick.button(i).letgo + //joystick.button(i).letgo <--psudo code //Start Debug Code + joy.releaseButton(i); digitalWrite(LED_BUILTIN, LOW); Serial.write("Button: "); Serial.print(i); @@ -133,7 +144,6 @@ void specialBtnOnStart(int i) { } } - void specialBtnAlwaysOn(int i) { //This is a sample function that shows how to simulate a button press. //This is mainly for always on switches. When you turn the switch on, it will simulate a button press @@ -157,69 +167,39 @@ void specialBtnAlwaysOn(int i) { } } - void loop() { //Start PCF8574 Button pressed detection section for (int i = 0; i < numPCFButtons; 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"); - btnState[9] = 1; - btnTime[9] = millis() + gButtonDelta; - digitalWrite(LED_BUILTIN, LOW); + 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"); - btnState[9] = 1; - btnTime[9] = millis() + gButtonDelta; - digitalWrite(LED_BUILTIN, LOW); + 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 (btnState[9] == 1) { - if (millis() >= btnTime[9]) { + if (rotState[1] == 1) { + if (millis() >= rotTime[1]) { Serial.println("Encoder Off"); digitalWrite(LED_BUILTIN,LOW); - btnState[9] = 0; + rotState[1] = 0; } } //End PCF8574 Button Pressed detection section -} - - - - - - - - - - - - - -/* - time = millis(); - Serial.println(time); - delay(500); -*/ - -/* - for (int i = 0; i<8; i++) { - if (pcf1.digitalRead(i) == HIGH){ - digitalWrite(LED_BUILTIN,HIGH); - delay(50); - } else { - digitalWrite(LED_BUILTIN,LOW); - } - } -*/ \ No newline at end of file +} \ No newline at end of file