kopia lustrzana https://github.com/AlexFWulff/awulff-pico-playground
Porównaj commity
5 Commity
a5b2f83603
...
363cf68326
Autor | SHA1 | Data |
---|---|---|
Alex Wulff | 363cf68326 | |
Alex Wulff | 50320e8126 | |
Alex Wulff | 6289393253 | |
Alex Wulff | e96c7b7417 | |
Alex Wulff | 6d2c2bdbed |
|
@ -14,15 +14,15 @@
|
|||
// 0 = 500,000 Hz
|
||||
// 960 = 50,000 Hz
|
||||
// 9600 = 5,000 Hz
|
||||
#define CLOCK_DIV 9600
|
||||
#define CLOCK_DIV 12000
|
||||
|
||||
// Channel 0 is GPIO26
|
||||
#define CAPTURE_CHANNEL 0
|
||||
#define LED_PIN 25
|
||||
#define NSAMP 20000
|
||||
#define NSAMP 10000
|
||||
|
||||
uint16_t capture_buf[NSAMP];
|
||||
uint16_t sending_buf[NSAMP];
|
||||
float sending_buf[NSAMP];
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
|
@ -77,16 +77,26 @@ int main() {
|
|||
gpio_put(LED_PIN, 1);
|
||||
adc_run(true);
|
||||
|
||||
// first transmission will be garbage since we haven't filled the buffer yet
|
||||
// first transmission will be garbage since we haven't filled buf
|
||||
std::string encoded =
|
||||
base64_encode((unsigned char const *)sending_buf, NSAMP*2);
|
||||
base64_encode((unsigned char const *)sending_buf, NSAMP*4);
|
||||
|
||||
printf("%s", encoded.c_str());
|
||||
|
||||
gpio_put(LED_PIN, 0);
|
||||
|
||||
dma_channel_wait_for_finish_blocking(dma_chan);
|
||||
|
||||
|
||||
memcpy(sending_buf, capture_buf, NSAMP*2);
|
||||
uint16_t min = 32768;
|
||||
uint16_t max = 0;
|
||||
|
||||
for (uint32_t i=0; i<NSAMP; i++) {
|
||||
if (capture_buf[i] > max) max = capture_buf[i];
|
||||
if (capture_buf[i] < min) min = capture_buf[i];
|
||||
}
|
||||
|
||||
for (uint32_t i=0; i<NSAMP; i++) {
|
||||
sending_buf[i] = ((float)capture_buf[i]-(float)min)/((float)max-(float)min)*2-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ import numpy as np
|
|||
import base64
|
||||
|
||||
if __name__=="__main__":
|
||||
infile = "/Users/alex/Desktop/pico/other-3"
|
||||
outfile = "/Users/alex/Desktop/pico/other.wav"
|
||||
infile = "/Users/alex/Desktop/4khz/extinguish"
|
||||
outfile = "/Users/alex/Desktop/4khz/extinguish.wav"
|
||||
|
||||
f = open(infile, "r")
|
||||
|
||||
|
@ -28,7 +28,7 @@ if __name__=="__main__":
|
|||
audio = AudioSegment(
|
||||
data.tobytes(),
|
||||
sample_width=2,
|
||||
frame_rate=5000,
|
||||
frame_rate=4000,
|
||||
channels=1
|
||||
)
|
||||
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
from pydub import AudioSegment
|
||||
import scipy.io.wavfile
|
||||
import numpy as np
|
||||
import base64
|
||||
import io
|
||||
|
||||
if __name__=="__main__":
|
||||
infile = "/Users/alex/Desktop/float-test/noise"
|
||||
outfile = "/Users/alex/Desktop/float-test/noise.wav"
|
||||
|
||||
f = open(infile, "r")
|
||||
|
||||
byte_data = bytearray()
|
||||
vals = f.read(1000)
|
||||
# Reading all at once didn't work for some reason
|
||||
while vals:
|
||||
byte_data.extend(base64.b64decode(vals))
|
||||
vals = f.read(1000)
|
||||
if len(vals) != 1000: break
|
||||
f.close()
|
||||
|
||||
# Enforce little endian
|
||||
dt = np.dtype(np.float32)
|
||||
dt = dt.newbyteorder('<')
|
||||
|
||||
data = np.zeros(int(len(byte_data)/4), dtype=dt)
|
||||
num_idx = 0
|
||||
byte_idx = 0
|
||||
while byte_idx < len(byte_data)-4:
|
||||
num = np.frombuffer(byte_data[byte_idx:byte_idx+4], dtype=dt)
|
||||
if (num > 1 or num < -1):
|
||||
print("Skipped one")
|
||||
byte_idx = byte_idx + 3
|
||||
else:
|
||||
data[num_idx] = num[0]
|
||||
num_idx = num_idx+1
|
||||
byte_idx = byte_idx + 4
|
||||
|
||||
wav_io = io.BytesIO()
|
||||
scipy.io.wavfile.write(wav_io, 4000, data)
|
||||
wav_io.seek(0)
|
||||
sound = AudioSegment.from_wav(wav_io)
|
||||
|
||||
sound.export(outfile, format="wav")
|
|
@ -0,0 +1 @@
|
|||
alex@Alexs-MBP.home.64612
|
|
@ -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)
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
#define PIN 7
|
||||
|
||||
#include <pico/multicore.h>
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
void core1_entry();
|
|
@ -1,5 +1,5 @@
|
|||
#include "ei_run_classifier.h"
|
||||
#include "Adafruit_NeoPixel.hpp"
|
||||
#include "lights.h"
|
||||
|
||||
#include <hardware/gpio.h>
|
||||
#include <hardware/uart.h>
|
||||
|
@ -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<NSAMP; i++) {
|
||||
sum += capture_buf[i];
|
||||
|
@ -213,5 +149,17 @@ int main()
|
|||
for (uint32_t i=0; i<NSAMP; i++) {
|
||||
features[i] = (float)capture_buf[i]-dc_offset;
|
||||
}
|
||||
*/
|
||||
uint16_t min = 32768;
|
||||
uint16_t max = 0;
|
||||
|
||||
for (uint32_t i=0; i<NSAMP; i++) {
|
||||
if (capture_buf[i] > max) max = capture_buf[i];
|
||||
if (capture_buf[i] < min) min = capture_buf[i];
|
||||
}
|
||||
|
||||
for (uint32_t i=0; i<NSAMP; i++) {
|
||||
features[i] = ((float)capture_buf[i]-(float)min)/((float)max-(float)min)*2-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue