From 02aea42207469c430f94b924cbf6de2f4d2d94a6 Mon Sep 17 00:00:00 2001 From: Thomas Schmid Date: Sun, 26 Jun 2022 00:23:50 +0200 Subject: [PATCH 1/2] add acceleration sensor Signed-off-by: Thomas Schmid --- firmware/include/zauberstab.h | 11 +++++++++-- firmware/platformio.ini | 4 +++- firmware/src/zauberstab.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/firmware/include/zauberstab.h b/firmware/include/zauberstab.h index 57a875b..ae449e2 100644 --- a/firmware/include/zauberstab.h +++ b/firmware/include/zauberstab.h @@ -2,12 +2,19 @@ #include "Arduino.h" #include +#include +#include + +#define ADXL345_I2CADDR 0x53 // 0x1D if SDO = HIGH #define LED_PIN 12 -#define NUM_LEDS 144 +#define NUM_LEDS 48 #define MIC_PIN 15 extern CRGB leds[NUM_LEDS]; +extern ADXL345_WE myAcc; int zauberstab_init(); -float get_sample(); \ No newline at end of file +float get_sample(); +void switch_app(); +bool acc_has_event(); \ No newline at end of file diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 22305b5..ad2ded7 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -10,7 +10,9 @@ [env] framework = arduino -lib_deps = fastled/FastLED @ ^3.5.0 +lib_deps = + fastled/FastLED @ ^3.5.0 + wollewald/ADXL345_WE @ ^2.1.4 [env:esp32doit-devkit-v1] platform = espressif32 diff --git a/firmware/src/zauberstab.cpp b/firmware/src/zauberstab.cpp index 51a3489..c65e1bb 100644 --- a/firmware/src/zauberstab.cpp +++ b/firmware/src/zauberstab.cpp @@ -4,16 +4,42 @@ DcCancelation dc_blocker{0.95}; CRGB leds[NUM_LEDS]; static int16_t mic_offset = 0; +static bool acc_event = false; +ADXL345_WE myAcc = ADXL345_WE(ADXL345_I2CADDR); static uint16_t read_mic() { return analogRead(MIC_PIN); } +void double_tab_int() { + acc_event = true; +} + +bool acc_has_event() { + return acc_event; +} + int zauberstab_init() { FastLED.addLeds(leds, NUM_LEDS); + FastLED.addLeds(leds, NUM_LEDS); + FastLED.addLeds(leds, NUM_LEDS); // FastLED.setMaxPowerInVoltsAndMilliamps(5, 300); + + Wire.begin(); + if (!myAcc.init()){ + Serial.println("Ooops, no ADXL345 detected ... Check your wiring!"); + return -1; + } + + myAcc.setDataRate(ADXL345_DATA_RATE_200); + myAcc.setRange(ADXL345_RANGE_8G); + myAcc.setGeneralTapParameters(ADXL345_XY0, 3.0, 30, 100.0); + myAcc.setAdditionalDoubleTapParameters(false, 250); + myAcc.setInterrupt(ADXL345_DOUBLE_TAP, INT_PIN_1); + attachInterrupt(digitalPinToInterrupt(4), double_tab_int, RISING); + return 0; } From 9f3c929fcd48fa26db1a1311e3f4864dde0faa09 Mon Sep 17 00:00:00 2001 From: Thomas Schmid Date: Sun, 26 Jun 2022 00:24:44 +0200 Subject: [PATCH 2/2] update main app handling to include an off state Signed-off-by: Thomas Schmid --- firmware/src/main.cpp | 57 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 683f862..8463305 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -22,25 +22,72 @@ std::vector> apps = { std::ref(fackel_app) }; +<<<<<<< HEAD unsigned int current_app = 1; unsigned int next_app; +======= +static unsigned int current_app = 0; +static unsigned int next_app; +static bool init_successfull = false; +static bool sleep_active=false; + +void switch_app() { + if (!sleep_active) { + next_app = current_app + 1; + } else { + sleep_active = false; + next_app = 0; + } + + if (next_app >= apps.size()) { + next_app = 0; + // Turn off leds before going to sleep + fadeToBlackBy(leds, NUM_LEDS, 0xFF); + FastLED.show(); + + //configure wakeup source + esp_sleep_enable_ext0_wakeup(GPIO_NUM_4, 1); + + //bedtime + sleep_active = true; + esp_deep_sleep_start(); + } + + next_app = next_app % apps.size(); +} +>>>>>>> update main app handling to include an off state void setup() { next_app = current_app; +<<<<<<< HEAD zauberstab_init(); //Serial.begin(115200); +======= + if (zauberstab_init() != 0) { + return; + } + Serial.begin(115200); +>>>>>>> update main app handling to include an off state + init_successfull = true; apps[current_app].get().init(); } void loop() { -/* EVERY_N_SECONDS(30) - { - next_app++; - next_app = next_app % apps.size(); - } */ + if (!init_successfull) { + return; + } + + if (acc_has_event()) { + String axes = myAcc.getActTapStatusAsString(); + byte intSource = myAcc.readAndClearInterrupts(); + + if (myAcc.checkInterrupt(intSource, ADXL345_DOUBLE_TAP)) { + switch_app(); + } + } if (next_app != current_app) {