From 6d2c2bdbed64ce83532c800eeef179bcd1e0288e Mon Sep 17 00:00:00 2001 From: Alex Wulff Date: Fri, 15 Jul 2022 11:39:26 -0400 Subject: [PATCH] changing to float32 model inputs --- pico-light-voice/.#README.md | 1 + pico-light-voice/CMakeLists.txt | 1 + pico-light-voice/source/.#lights 2.cpp | 1 + pico-light-voice/source/.#lights 3.cpp | 1 + pico-light-voice/source/lights.cpp | 110 +++++++++++++++++++++++ pico-light-voice/source/lights.h | 1 + pico-light-voice/source/main.cpp | 118 +++++++------------------ 7 files changed, 148 insertions(+), 85 deletions(-) create mode 120000 pico-light-voice/.#README.md create mode 120000 pico-light-voice/source/.#lights 2.cpp create mode 120000 pico-light-voice/source/.#lights 3.cpp create mode 100644 pico-light-voice/source/lights.cpp create mode 100644 pico-light-voice/source/lights.h diff --git a/pico-light-voice/.#README.md b/pico-light-voice/.#README.md new file mode 120000 index 0000000..ad8cb69 --- /dev/null +++ b/pico-light-voice/.#README.md @@ -0,0 +1 @@ +alex@Alexs-MBP.home.64612 \ No newline at end of file diff --git a/pico-light-voice/CMakeLists.txt b/pico-light-voice/CMakeLists.txt index e497e57..e8eeec5 100755 --- a/pico-light-voice/CMakeLists.txt +++ b/pico-light-voice/CMakeLists.txt @@ -13,6 +13,7 @@ pico_sdk_init() add_executable(pico-voice source/main.cpp + source/lights.cpp ) include(${MODEL_FOLDER}/edge-impulse-sdk/cmake/utils.cmake) diff --git a/pico-light-voice/source/.#lights 2.cpp b/pico-light-voice/source/.#lights 2.cpp new file mode 120000 index 0000000..f3b8eda --- /dev/null +++ b/pico-light-voice/source/.#lights 2.cpp @@ -0,0 +1 @@ +alex@Alexs-MacBook-Pro.local.64766 \ No newline at end of file diff --git a/pico-light-voice/source/.#lights 3.cpp b/pico-light-voice/source/.#lights 3.cpp new file mode 120000 index 0000000..f3b8eda --- /dev/null +++ b/pico-light-voice/source/.#lights 3.cpp @@ -0,0 +1 @@ +alex@Alexs-MacBook-Pro.local.64766 \ No newline at end of file diff --git a/pico-light-voice/source/lights.cpp b/pico-light-voice/source/lights.cpp new file mode 100644 index 0000000..38426cb --- /dev/null +++ b/pico-light-voice/source/lights.cpp @@ -0,0 +1,110 @@ +#define PIN 7 + +#include +#include +#include "Adafruit_NeoPixel.hpp" + +bool update_state(uint32_t *state, bool strip_on) { + // check for new state information + if (multicore_fifo_rvalid()) { + uint32_t val = multicore_fifo_pop_blocking(); + + // turn lights off + if (val == 2) { + printf("Turning lights off\n"); + multicore_fifo_push_blocking(0); + return false; + } + + // increment state by 1 + else { + if (!strip_on) { + printf("Turning lights on\n"); + multicore_fifo_push_blocking(0); + return true; + } + + // SET NUM STATES HERE + *state = (*state+1) % 3; + printf("Incrementing state to %u\n", *state); + multicore_fifo_push_blocking(0); + return true; + } + } + + // if no data available just leave as it was + return strip_on; +} + +void core1_entry() { + Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, + NEO_GRB + NEO_KHZ800); + strip.begin(); + //strip.setBrightness(64); + strip.show(); + + // tell the other core we're ready for data + multicore_fifo_push_blocking(0); + + uint32_t state = 0; + bool lights_on = false; + + while (1) { + lights_on = update_state(&state, lights_on); + + if (!lights_on) { + strip.clear(); + strip.show(); + sleep_ms(200); + } + + // rainbow state + else if (state == 0) { + uint16_t i, j; + + for(j=0; j<256; j++) { + for(i=0; i< strip.numPixels(); i++) { + uint8_t WheelPos = ((i * 256 / strip.numPixels()) + j) & 255; + uint32_t c = 0; + + WheelPos = 255 - WheelPos; + + if(WheelPos < 85) { + c = strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); + } + + else if(WheelPos < 170) { + WheelPos -= 85; + c = strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); + } + + else { + WheelPos -= 170; + c = strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); + } + + strip.setPixelColor(i, c); + } + + // check if state has changed + uint32_t last_state = state; + lights_on = update_state(&state, lights_on); + if (last_state != state || !lights_on) break; + + strip.show(); + sleep_ms(10); + } + } + + // boring + else if (state == 1) { + for(int i=0; i< strip.numPixels(); i++) { + strip.setPixelColor(i, strip.Color(10,10,5)); + } + + strip.show(); + sleep_ms(100); + } + } +} + diff --git a/pico-light-voice/source/lights.h b/pico-light-voice/source/lights.h new file mode 100644 index 0000000..970ab30 --- /dev/null +++ b/pico-light-voice/source/lights.h @@ -0,0 +1 @@ +void core1_entry(); diff --git a/pico-light-voice/source/main.cpp b/pico-light-voice/source/main.cpp index 2527776..3d1a48f 100644 --- a/pico-light-voice/source/main.cpp +++ b/pico-light-voice/source/main.cpp @@ -1,5 +1,5 @@ #include "ei_run_classifier.h" -#include "Adafruit_NeoPixel.hpp" +#include "lights.h" #include #include @@ -12,102 +12,30 @@ // ############ ADC and Model Stuff ############ -#define NSAMP 5000 +#define NSAMP 4000 // set this to determine sample rate // 0 = 500,000 Hz // 960 = 50,000 Hz // 9600 = 5,000 Hz -#define CLOCK_DIV 9600 +#define CLOCK_DIV 12000 #define CAPTURE_CHANNEL 0 #define LED_PIN 25 float features[NSAMP]; uint16_t capture_buf[NSAMP]; -// ############ Lights Stuff ############ -#define PIN 7 - // ############ Functions ############ int raw_feature_get_data(size_t offset, size_t length, float *out_ptr) { memcpy(out_ptr, features + offset, length * sizeof(float)); return 0; } -void core1_entry() { - Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, - NEO_GRB + NEO_KHZ800); - strip.begin(); - strip.setBrightness(64); - strip.show(); - - // tell the other core we're ready for data - multicore_fifo_push_blocking(0); - - uint32_t state = 0; - bool sent_req = false; - uint32_t loop_idx = 0; - while (1) { - // check for new state information - if (multicore_fifo_rvalid()) { - uint32_t val = multicore_fifo_pop_blocking(); - - // 0 means state unchanged - if (val != 0) { - state = val; - } - - sent_req = false; - } - - else if (!sent_req) { - // tell the other core we're ready for data - multicore_fifo_push_blocking(0); - // make sure we don't fill up the other queue - sent_req = true; - } - - // turn on - if (state == 1) { - uint16_t i, j; - - for(j=0; j<256; j++) { // 5 cycles of all colors on wheel - for(i=0; i< strip.numPixels(); i++) { - uint8_t WheelPos = ((i * 256 / strip.numPixels()) + j) & 255; - uint32_t c = 0; - - WheelPos = 255 - WheelPos; - if(WheelPos < 85) { - c = strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); - } - if(WheelPos < 170) { - WheelPos -= 85; - c = strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); - } - WheelPos -= 170; - c = strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); - - strip.setPixelColor(i, c); - } - - strip.show(); - sleep_ms(10); - } - } - - // turn off - else if (state == 2) { - strip.clear(); - strip.show(); - sleep_ms(200); - } - } -} - int main() { stdio_usb_init(); stdio_init_all(); - + + // function lives in lights.cpp multicore_launch_core1(core1_entry); gpio_init(LED_PIN); @@ -179,22 +107,26 @@ int main() if (EI_CLASSIFIER_HAS_ANOMALY == 1) printf("Anomaly!\n"); - const float thresh = 0.9; + const float thresh = 0.7; - uint32_t state = 0; + uint32_t state = 0; for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) { if (ix == 0 && result.classification[ix].value > thresh) { - printf("GO\n"); - state = 1; + //printf("EXTINGUISH\n"); + //state = 1; } - if (ix == 2 && result.classification[ix].value > thresh) { - printf("STOP\n"); - state = 2; + if (ix == 1 && result.classification[ix].value > thresh) { + //printf("ILLUMINATE\n"); + //state = 2; } + + printf("%0.2f, ",result.classification[ix].value); } - if (multicore_fifo_rvalid()) { + printf("\n"); + + if (multicore_fifo_rvalid() && state != 0) { multicore_fifo_pop_blocking(); multicore_fifo_push_blocking(state); } @@ -204,6 +136,10 @@ int main() // copy everything to feature buffer to run model // this is probably slow, idk + + // copy everything to feature buffer to run model + // this is probably slow, idk + /* uint64_t sum = 0; for (uint32_t i=0; i max) max = capture_buf[i]; + if (capture_buf[i] < min) min = capture_buf[i]; + } + + for (uint32_t i=0; i