From 252dfca2b25446b3caa02779ca8cbab553c2d0f6 Mon Sep 17 00:00:00 2001 From: Ghost <31184695+GHOSCHT@users.noreply.github.com> Date: Sun, 27 Dec 2020 16:54:53 +0100 Subject: [PATCH] Transfer Code to PIO --- Arduino/ArduinoControl.ino | 108 ----------------------- Arduino/Control/.gitignore | 5 ++ Arduino/Control/.vscode/extensions.json | 7 ++ Arduino/Control/include/README | 39 +++++++++ Arduino/Control/lib/README | 46 ++++++++++ Arduino/Control/platformio.ini | 16 ++++ Arduino/Control/src/main.cpp | 109 ++++++++++++++++++++++++ Arduino/Control/test/README | 11 +++ 8 files changed, 233 insertions(+), 108 deletions(-) delete mode 100644 Arduino/ArduinoControl.ino create mode 100644 Arduino/Control/.gitignore create mode 100644 Arduino/Control/.vscode/extensions.json create mode 100644 Arduino/Control/include/README create mode 100644 Arduino/Control/lib/README create mode 100644 Arduino/Control/platformio.ini create mode 100644 Arduino/Control/src/main.cpp create mode 100644 Arduino/Control/test/README diff --git a/Arduino/ArduinoControl.ino b/Arduino/ArduinoControl.ino deleted file mode 100644 index e8bb2d3..0000000 --- a/Arduino/ArduinoControl.ino +++ /dev/null @@ -1,108 +0,0 @@ -#include - -const int bjtCount = 4; - -const int bjtPin[bjtCount] = {6, 5, 3, 10}; -const int btnPin[bjtCount] = {7, 8, 9, 4}; -int bjtState[bjtCount] = {255, 255, 255, 255}; //255 -> bjt max "open" - -byte btnState[bjtCount]; -byte lastbtnState[bjtCount]; -String receivedData; -String pendingData; - -void setup() -{ - Serial.begin(9600); - Serial.setTimeout(5); - - for (int i = 0; i < bjtCount; i++) - { - pinMode(btnPin[i], INPUT_PULLUP); - pinMode(bjtPin[i], OUTPUT); - bjtState[i] = EEPROM.read(i); - digitalWrite(bjtPin[i], bjtState[i]); - } -} - -void toggleLED(int i) -{ - if (bjtState[i] != 0) - { - bjtState[i] = 0; - } - else - { - bjtState[i] = 255; - } -} - -void absoluteLED(String data, int i) -{ - if (data == "off") - { - bjtState[i] = 0; - } - - if (data == "on") - { - bjtState[i] = 255; - } -} - -void loop() -{ - while (Serial.available()) - { - receivedData = Serial.readString(); //FORMAT: index of light + t oggle, i ncrease, d ecrease -> eg. 0t - Serial.flush(); - } - - for (int i = 0; i < bjtCount; i++) - { - btnState[i] = digitalRead(btnPin[i]); - - if ((btnState[i] == LOW && btnState[i] != lastbtnState[i])) - { - toggleLED(i); - } - - if (receivedData.indexOf(String(i)) == 0 && receivedData.length() == 2) - { - if (receivedData.indexOf("t") >= 0) - { - toggleLED(i); - } - else if (receivedData.indexOf("i") >= 0) - { - bjtState[i] += 5; - } - else if (receivedData.indexOf("d") >= 0) - { - bjtState[i] -= 5; - } - } - - absoluteLED(receivedData, i); - - //clamp state between 0 and 255 - if (bjtState[i] > 255) - { - bjtState[i] = 255; - } - - if (bjtState[i] < 0) - { - bjtState[i] = 0; - } - - analogWrite(bjtPin[i], bjtState[i]); - EEPROM.update(i, bjtState[i]); - lastbtnState[i] = btnState[i]; - pendingData += String(bjtState[i]) + ","; - } - - Serial.println(pendingData); - pendingData = ""; - receivedData = ""; -} \ No newline at end of file diff --git a/Arduino/Control/.gitignore b/Arduino/Control/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/Arduino/Control/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/Arduino/Control/.vscode/extensions.json b/Arduino/Control/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/Arduino/Control/.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/Arduino/Control/include/README b/Arduino/Control/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/Arduino/Control/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/Arduino/Control/lib/README b/Arduino/Control/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/Arduino/Control/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/Arduino/Control/platformio.ini b/Arduino/Control/platformio.ini new file mode 100644 index 0000000..416a9f7 --- /dev/null +++ b/Arduino/Control/platformio.ini @@ -0,0 +1,16 @@ +; 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:control] +platform = atmelavr +board = nanoatmega328 +framework = arduino +upload_port = COM6 +monitor_port = COM6 diff --git a/Arduino/Control/src/main.cpp b/Arduino/Control/src/main.cpp new file mode 100644 index 0000000..f4dd98f --- /dev/null +++ b/Arduino/Control/src/main.cpp @@ -0,0 +1,109 @@ +#include +#include + +const int bjtCount = 4; + +const int bjtPin[bjtCount] = {6, 5, 3, 10}; +const int btnPin[bjtCount] = {7, 8, 9, 4}; +int bjtState[bjtCount] = {255, 255, 255, 255}; //255 -> bjt max "open" + +byte btnState[bjtCount]; +byte lastbtnState[bjtCount]; +String receivedData; +String pendingData; + +void setup() +{ + Serial.begin(9600); + Serial.setTimeout(5); + + for (int i = 0; i < bjtCount; i++) + { + pinMode(btnPin[i], INPUT_PULLUP); + pinMode(bjtPin[i], OUTPUT); + bjtState[i] = EEPROM.read(i); + digitalWrite(bjtPin[i], bjtState[i]); + } +} + +void toggleLED(int i) +{ + if (bjtState[i] != 0) + { + bjtState[i] = 0; + } + else + { + bjtState[i] = 255; + } +} + +void absoluteLED(String data, int i) +{ + if (data == "off") + { + bjtState[i] = 0; + } + + if (data == "on") + { + bjtState[i] = 255; + } +} + +void loop() +{ + while (Serial.available()) + { + receivedData = Serial.readString(); //FORMAT: index of light + t oggle, i ncrease, d ecrease -> eg. 0t + Serial.flush(); + } + + for (int i = 0; i < bjtCount; i++) + { + btnState[i] = digitalRead(btnPin[i]); + + if ((btnState[i] == LOW && btnState[i] != lastbtnState[i])) + { + toggleLED(i); + } + + if (receivedData.indexOf(String(i)) == 0 && receivedData.length() == 2) + { + if (receivedData.indexOf("t") >= 0) + { + toggleLED(i); + } + else if (receivedData.indexOf("i") >= 0) + { + bjtState[i] += 5; + } + else if (receivedData.indexOf("d") >= 0) + { + bjtState[i] -= 5; + } + } + + absoluteLED(receivedData, i); + + //clamp state between 0 and 255 + if (bjtState[i] > 255) + { + bjtState[i] = 255; + } + + if (bjtState[i] < 0) + { + bjtState[i] = 0; + } + + analogWrite(bjtPin[i], bjtState[i]); + EEPROM.update(i, bjtState[i]); + lastbtnState[i] = btnState[i]; + pendingData += String(bjtState[i]) + ","; + } + + Serial.println(pendingData); + pendingData = ""; + receivedData = ""; +} \ No newline at end of file diff --git a/Arduino/Control/test/README b/Arduino/Control/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/Arduino/Control/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