commit 5f0a0feed1ff083b2402eeb85b526701b1e86a36 Author: true Date: Wed Apr 2 21:37:50 2025 -0700 initial commit of in-progress platformio/arduino code diff --git a/fw/HackSpaceCon/.vscode/extensions.json b/fw/HackSpaceCon/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/fw/HackSpaceCon/.vscode/extensions.json @@ -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" + ] +} diff --git a/fw/HackSpaceCon/include/README b/fw/HackSpaceCon/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/fw/HackSpaceCon/include/README @@ -0,0 +1,37 @@ + +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 convention is to give header files names that end with `.h'. + +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/fw/HackSpaceCon/lib/README b/fw/HackSpaceCon/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/fw/HackSpaceCon/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 the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for 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 + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/fw/HackSpaceCon/platformio.ini b/fw/HackSpaceCon/platformio.ini new file mode 100644 index 0000000..152b39f --- /dev/null +++ b/fw/HackSpaceCon/platformio.ini @@ -0,0 +1,32 @@ +; 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] +platform = atmelmegaavr +board = ATtiny402 +framework = arduino + +board_build.f_cpu = 8000000L + + +[env:HackSpaceCon_Wand_Upload] +# before you can debug, you need to install pymcuprog in platformio virtual environment. +# python -m pip install pymcuprog +upload_speed = 115200 +upload_flags = + --tool + uart + --device + attiny402 + --uart + $UPLOAD_PORT + --clk + $UPLOAD_SPEED +upload_command = pymcuprog write --erase $UPLOAD_FLAGS --filename $SOURCE diff --git a/fw/HackSpaceCon/src/main.cpp b/fw/HackSpaceCon/src/main.cpp new file mode 100644 index 0000000..a85ff6a --- /dev/null +++ b/fw/HackSpaceCon/src/main.cpp @@ -0,0 +1,103 @@ +#include +#include +#include + +#include "rgbled.h" + + + +#include + +tinyNeoPixel rgb = tinyNeoPixel(RGB_COUNT, PIN_PA1, NEO_GRB, rgbled); + + + +uint8_t run_rgb_program = 0; + + + +void setup() { + // configure PA3 as falling edge interrupt input for button + PORTA.DIRCLR = PIN3_bm; + PORTA.PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; + + // configure other hardware pins as appropriate + pinMode(PIN_PA1, INPUT_PULLUP); // unused + pinMode(PIN_PA7, INPUT_PULLUP); // unused + + digitalWrite(PIN_PA6, LOW); + pinMode(PIN_PA6, OUTPUT); // LED boost regulator enable + + digitalWrite(PIN_PA2, LOW); + pinMode(PIN_PA2, OUTPUT); // LED data + + // set up the RGB ~61Hz periodic timer + conf_rgb_timer(); + + // enable global interrupts (though they may already be enabled? fuck if I know) + sei(); +} + +void loop() { + switch (run_rgb_program) { + case 1: { // just started running a program + digitalWrite(PIN_PA6, HIGH); // enable LED power supply, + delay(10); // wait a moment for LEDs to stabilize, + enable_rgb_timer(); // then start the RGB program timebase. + + rgb_program[0](1); // initialize the program, + run_rgb_program++; // and set to running mode. + + // we can idle the CPU after running the program + SLPCTRL.CTRLA = SLPCTRL_SMODE_IDLE_gc | SLPCTRL_SEN_bm; + __asm("sleep"); + + break; + } + case 2: { // continuing to run a program + // send updates to the led + rgb.show(); + + // then process the next program frame + if (!rgb_program[0](0)) { + // until the program says it's done + run_rgb_program = 0; + break; + } + + // we can idle the CPU after running the program + SLPCTRL.CTRLA = SLPCTRL_SMODE_IDLE_gc | SLPCTRL_SEN_bm; + __asm("sleep"); + + break; + } + default: { // no longer running a program + disable_rgb_timer(); // disable RGB program timer, + digitalWrite(PIN_PA6, LOW); // disable LED power supply, + run_rgb_program = 0; // and clear run_rgb_program. + + // finally, go to sleep in standby mode + SLPCTRL.CTRLA = SLPCTRL_SMODE_STDBY_gc | SLPCTRL_SEN_bm; + __asm("sleep"); + + break; + } + } +} + +// button interrupt +ISR(PORTA_PORT_vect) +{ + // reset the INTFLAGS - necessary on this series + uint8_t intflags = PORTA.INTFLAGS; + PORTA.INTFLAGS = intflags; + + // was our pin changed? + if (intflags & PIN3_bm) { + // is the pin low? + if (!digitalRead(PIN_PA3)) { + // start running a program if one isn't running already + if (!run_rgb_program) run_rgb_program = 1; + } + } +} \ No newline at end of file diff --git a/fw/HackSpaceCon/src/rgbled.cpp b/fw/HackSpaceCon/src/rgbled.cpp new file mode 100644 index 0000000..631ac15 --- /dev/null +++ b/fw/HackSpaceCon/src/rgbled.cpp @@ -0,0 +1,82 @@ +#include +#include "rgbled.h" + + + +uint8_t rgbp_rainbow(uint8_t init); + +uint8_t (*rgb_program[1])(uint8_t) = { + rgbp_rainbow +}; + + + +#define RGB_COUNT 5 +uint8_t rgbled[3 * RGB_COUNT]; + + +// configures and enables the 50Hz timer interrupt that is used for RGB program updates +void conf_rgb_timer() +{ + // this timer will run at half speed. + // so 8MHz / 2 (prescale) / 1 (CLK_PER) = 4MHz + // this will allow a full cycle time of ~61Hz. + + _PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, 0); // disable CLK_PER divider + + disable_rgb_timer(); + + TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc; // prescale timer to run at half speed + TCB0.CCMP = 0xffff; // count to full + TCB0.CNT = 0; +} + +ISR(TCB0_INT_vect) +{ + // reset the INTFLAGS - necessary on this series + uint8_t intflags = TCB0.INTFLAGS; + TCB0.INTFLAGS = intflags; + + // we don't care why we interrupted. + // we'll now return to executing loop() +} + + +// rgb program 0: rainbow puke +#define RAINBOW_HUE_INC 50 +#define RAINBOW_OFFSET (1536/5) +#define RAINBOW_TIMEOUT 240 + +uint16_t rainbow_hue = 0; +uint16_t rainbow_timeout; + +uint8_t rgbp_rainbow(uint8_t init) +{ + uint8_t i; + uint16_t hue; + + if (init) { + rainbow_timeout = RAINBOW_TIMEOUT; + } + + if (--rainbow_timeout) { + hue = rainbow_hue; + + for (i = 0; i < RGB_COUNT; i++) { + hue += RAINBOW_OFFSET; + if (hue >= 1536) hue -= 1536; + + // apply color + // TODO + } + + // move the hue wheel for the next cycle through the program + rainbow_hue += RAINBOW_HUE_INC; + if (rainbow_hue > 1536) rainbow_hue -= 1536; + + return 1; + } + + // done with program + return 0; +} \ No newline at end of file diff --git a/fw/HackSpaceCon/src/rgbled.h b/fw/HackSpaceCon/src/rgbled.h new file mode 100644 index 0000000..b0c4e65 --- /dev/null +++ b/fw/HackSpaceCon/src/rgbled.h @@ -0,0 +1,19 @@ +#include + + + +#define RGB_COUNT 5 + + + +#define enable_rgb_timer() {TCB0.CTRLA |= 1;}; +#define disable_rgb_timer() {TCB0.CTRLA &= ~1;}; + + + +extern uint8_t rgbled[3 * RGB_COUNT]; +extern uint8_t (*rgb_program[1])(uint8_t); + + + +void conf_rgb_timer(); diff --git a/fw/HackSpaceCon/test/README b/fw/HackSpaceCon/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/fw/HackSpaceCon/test/README @@ -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