commit 8ba82c0262c6bda3cad12df03d19f757c38cecc6 Author: Zepheris Date: Sun Oct 11 18:38:53 2020 -0600 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..b26178f --- /dev/null +++ b/platformio.ini @@ -0,0 +1,17 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:leonardo] +platform = atmelavr +board = leonardo +framework = arduino +lib_deps = + xreef/PCF8574 library@^2.2.0 + ellsclytn/Rotary@0.0.0-alpha+sha.d1fef10209 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8734c76 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,225 @@ +//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); + +//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 unsigned long gButtonDelta = 15; +const unsigned long gButtonPressedDelta = 100; +/* + #define int RELEASED 0 + #define int PRESSSED 1 + #define int HOLD 1 +*/ + +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); + + //INIT PCF8574 + pcf1.begin(); + + //Set pins to input + for (int i = 0; i < 8; i++) { + pcf1.pinMode(i, INPUT); + } + + //Ensure all pins are set to LOW + for (int i = 0; i < 8; i++) { + pcf1.digitalWrite(i, LOW); + } + + Serial.println("Ready"); +} + +void processBtn1(int i) { + //wait a bit before checking the button state again + if (millis() >= btnTime[i]) { + //is a button being pressed? + uint8_t val = pcf1.digitalRead(i); + if (val == HIGH) { + switch (i) { + // case 0: + // specialBtnAlwaysOn(i); + // break; + // case 1: + // specialBtnAlwaysOn(i); + // break; + // case 2: + // specialBtnAlwaysOn(i); + // break; + default: + //Set the timer + gButtonDelta milliseconds (to prevent checking button state too often) + 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 + //Start Debug Code + digitalWrite(LED_BUILTIN, HIGH); + Serial.write("Button: "); + Serial.print(i); + Serial.println(" PRESSED"); + //End Debug Code + //set btnState to show we've pressed the button + btnState[i] = 1; + } + break; + } + //If the button is past its check time and the button isn't pressed + } else if ((btnState[i] != 0) && (millis() >= btnTime[i])) { + switch (i) { + //case 0: + // Serial.println("Special BTN LOW"); + // digitalWrite(LED_BUILTIN, LOW); + // btnState[i] = 0; + // break; + default: + //joystick.button(i).letgo + //Start Debug Code + digitalWrite(LED_BUILTIN, LOW); + Serial.write("Button: "); + Serial.print(i); + Serial.println(" DEPRESSED"); + //End Debug Code + //Reset variables to 0 + } + btnTime[i] = 0; + btnState[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. + //****NEEDS TO BE VERIFIED**** + + btnTime[i] = millis() + gButtonDelta; + + if (btnState[i] == 0) { + //joystick.button(i).push + //Start Debug Code + digitalWrite(LED_BUILTIN, HIGH); + Serial.println("Special BTN HIGH"); + //set the button state to indicate the button is pressed + btnState[i] = 2; + btnPressedTime[i] = millis() + gButtonPressedDelta; + } + + if ((btnState[i] == 2) && (millis() >= btnPressedTime[i])) { + digitalWrite(LED_BUILTIN, LOW); + Serial.println("Special BTN DONE"); + btnState[i] = 3; + } +} + + +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 + //****NEEDS TO BE VERIFIED**** + btnTime[i] = millis() + gButtonDelta; + + if (btnState[i] == 0) { + //joystick.button(i).push + //Start Debug Code + digitalWrite(LED_BUILTIN, HIGH); + Serial.println("Special BTN HIGH"); + //set the button state to indicate the button is pressed + btnState[i] = 2; + btnPressedTime[i] = millis() + gButtonPressedDelta; + } + + if ((btnState[i] == 2) && (millis() >= btnPressedTime[i])) { + digitalWrite(LED_BUILTIN, LOW); + Serial.println("Special BTN DONE"); + btnState[i] = 3; + } +} + + +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); + digitalWrite(LED_BUILTIN, HIGH); + } else { + Serial.println("LEFT"); + btnState[9] = 1; + btnTime[9] = millis() + gButtonDelta; + digitalWrite(LED_BUILTIN, LOW); + digitalWrite(LED_BUILTIN, HIGH); + } + } + + if (btnState[9] == 1) { + if (millis() >= btnTime[9]) { + Serial.println("Encoder Off"); + digitalWrite(LED_BUILTIN,LOW); + btnState[9] = 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 diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html