Add project files

This commit is contained in:
2024-07-18 16:53:48 +02:00
commit afcb5957e6
16 changed files with 3223 additions and 0 deletions

5
attiny_firmware/.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

10
attiny_firmware/.vscode/extensions.json vendored Normal file
View File

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

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 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,36 @@
; 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:attiny85]
platform = atmelavr
board = attiny85
framework = arduino
upload_protocol = custom
upload_port = /dev/ttyACM0
upload_speed = 19200
upload_flags =
-C
${platformio.packages_dir}/tool-avrdude/avrdude.conf
-p
$BOARD_MCU
-P
$UPLOAD_PORT
-b
$UPLOAD_SPEED
-c
stk500v1
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i
lib_deps = connornishijima/TinySnore@^1.0.1
monitor_speed=115200
board_build.f_cpu=1000000L
board_fuses.lfuse = 0x62
board_fuses.hfuse = 0xD7
board_fuses.efuse = 0xFF

View File

@ -0,0 +1,78 @@
#include "tinysnore.h"
#include <Arduino.h>
// Define to enable serial debug
// messages on pin 1
#define debug
// Pin connected to the ESP-01's CH_PD pin (Pin 3)
#define esp_enable_pin 4
// Pin connected to the ESP-01's feedback pin (UTXD / Pin 1)
#define esp_status_pin 3
// Wake up the ESP-01 every 5 minutes
const long update_interval = 1000L * 60L * 5L;
// Cut power to the ESP-01 if the update takes longer than 30 seconds
const long update_timeout = 1000L * 30L;
#ifdef debug
#include <SoftwareSerial.h>
SoftwareSerial Debug(PB0, PB1);
#endif
void setup() {
#ifdef debug
Debug.begin(9600);
Debug.println("-> Reset");
#endif
pinMode(esp_enable_pin, OUTPUT);
pinMode(esp_status_pin, INPUT_PULLUP);
}
unsigned long runESP() {
// Reset the ESP
digitalWrite(esp_enable_pin, LOW);
snore(100);
digitalWrite(esp_enable_pin, HIGH);
// Wait 0.2s for the ESP to boot up
snore(200);
// Wait until either the status pin gets pulled HIGH
// by the ESP or `update_timeout` milliseconds have passed
unsigned long elapsed_time_ms = 0;
while (digitalRead(esp_status_pin) == LOW &&
elapsed_time_ms < update_timeout) {
snore(100);
elapsed_time_ms += 100;
}
// Turn off the ESP
digitalWrite(esp_enable_pin, LOW);
// Return time the mcu was asleep
return 100L + 200L + elapsed_time_ms;
}
void loop() {
// Store the current time
unsigned long start_time = millis();
#ifdef debug
Debug.print("Start time: ");
Debug.println(start_time);
Debug.println("-> Running ESP");
#endif
// Run the ESP and store the sleep time
long elapsed_asleep = runESP();
// Calculate elapsed time the mcu was awake
long elapsed_awake = millis() - start_time;
// Calculate the total elapsed time
long delta_time = elapsed_awake + elapsed_asleep;
// Sleep until next interval
long sleep_time = max(0, update_interval - delta_time);
#ifdef debug
Debug.println("-> ESP Done");
Debug.print("Took: ");
Debug.println(delta_time);
Debug.print("-> Sleeping for ");
Debug.println(sleep_time);
#endif
snore(sleep_time);
}

View File

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner 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/en/latest/advanced/unit-testing/index.html