Transfer Code to PIO

This commit is contained in:
GHOSCHT 2020-12-27 16:54:53 +01:00
parent 88cf657869
commit 252dfca2b2
8 changed files with 233 additions and 108 deletions

View file

@ -1,108 +0,0 @@
#include <EEPROM.h>
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 = "";
}

5
Arduino/Control/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View file

@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}

View file

@ -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

View file

@ -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 <Foo.h>
#include <Bar.h>
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

View file

@ -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

View file

@ -0,0 +1,109 @@
#include <Arduino.h>
#include <EEPROM.h>
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 = "";
}

View file

@ -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