|
| 1 | +#include <Adafruit_NeoPixel.h> |
| 2 | +#include <Adafruit_LIS3DH.h> |
| 3 | +#include <Adafruit_Sensor.h> |
| 4 | +#include <107-Arduino-Servo-RP2040.h> |
| 5 | + |
| 6 | +Adafruit_NeoPixel strip(60, PIN_EXTERNAL_NEOPIXELS, NEO_GRB + NEO_KHZ800); |
| 7 | + |
| 8 | +Adafruit_LIS3DH lis = Adafruit_LIS3DH(); |
| 9 | + |
| 10 | +static _107_::Servo servo_0; |
| 11 | + |
| 12 | +uint8_t x = 0; |
| 13 | + |
| 14 | +void setup() { |
| 15 | + // core1 setup |
| 16 | + Serial.begin(115200); |
| 17 | + |
| 18 | + if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address |
| 19 | + Serial.println("Couldnt start LIS3DH"); |
| 20 | + while (1) yield(); |
| 21 | + } |
| 22 | + |
| 23 | + lis.setRange(LIS3DH_RANGE_2_G); |
| 24 | + |
| 25 | + pinMode(PIN_EXTERNAL_POWER, OUTPUT); |
| 26 | + digitalWrite(PIN_EXTERNAL_POWER, HIGH); |
| 27 | + |
| 28 | + strip.begin(); |
| 29 | + strip.show(); |
| 30 | + strip.setBrightness(50); |
| 31 | + |
| 32 | + pinMode(PIN_EXTERNAL_BUTTON, INPUT_PULLUP); |
| 33 | + |
| 34 | + servo_0.attach(PIN_EXTERNAL_SERVO); |
| 35 | +} |
| 36 | + |
| 37 | +void loop() { |
| 38 | + |
| 39 | + delay(10); |
| 40 | + x++; |
| 41 | + for(int32_t i=0; i< strip.numPixels(); i++) { |
| 42 | + strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + x) & 255)); |
| 43 | + } |
| 44 | + strip.show(); |
| 45 | + // Print X Y & Z accelerometer data |
| 46 | + if (x % 10 == 0) { |
| 47 | + // every 100ms |
| 48 | + sensors_event_t event; |
| 49 | + lis.getEvent(&event); |
| 50 | + /* Display the results (acceleration is measured in m/s^2) */ |
| 51 | + Serial.print("Accel X: "); Serial.print(event.acceleration.x); |
| 52 | + Serial.print(" \tY: "); Serial.print(event.acceleration.y); |
| 53 | + Serial.print(" \tZ: "); Serial.print(event.acceleration.z); |
| 54 | + Serial.println(" m/s^2 "); |
| 55 | + Serial.println(x); |
| 56 | + } |
| 57 | + // external button press disable external power |
| 58 | + if (! digitalRead(PIN_EXTERNAL_BUTTON)) { |
| 59 | + Serial.println("External button pressed"); |
| 60 | + digitalWrite(PIN_EXTERNAL_POWER, LOW); |
| 61 | + } |
| 62 | + else { |
| 63 | + digitalWrite(PIN_EXTERNAL_POWER, HIGH); |
| 64 | + } |
| 65 | + |
| 66 | + if (x < 128) { |
| 67 | + // forward |
| 68 | + servo_0.writeMicroseconds(map(x, 0, 127, 1000, 2000)); |
| 69 | + } else { |
| 70 | + // and back |
| 71 | + servo_0.writeMicroseconds(map(x-128, 0, 127, 2000, 1000)); |
| 72 | + } |
| 73 | + return; |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +uint32_t Wheel(byte WheelPos) { |
| 78 | + WheelPos = 255 - WheelPos; |
| 79 | + if(WheelPos < 85) { |
| 80 | + return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); |
| 81 | + } |
| 82 | + if(WheelPos < 170) { |
| 83 | + WheelPos -= 85; |
| 84 | + return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); |
| 85 | + } |
| 86 | + WheelPos -= 170; |
| 87 | + return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); |
| 88 | +} |
| 89 | + |
| 90 | +// audio runs on core 2! |
| 91 | + |
| 92 | +#include <I2S.h> |
| 93 | + |
| 94 | +#include "boot.h" |
| 95 | +#include "hithere.h" |
| 96 | + |
| 97 | +struct { |
| 98 | + const uint8_t *data; |
| 99 | + uint32_t len; |
| 100 | + uint32_t rate; |
| 101 | +} sound[] = { |
| 102 | + hithereAudioData, sizeof(hithereAudioData), hithereSampleRate, |
| 103 | + bootAudioData , sizeof(bootAudioData) , bootSampleRate, |
| 104 | +}; |
| 105 | +#define N_SOUNDS (sizeof(sound) / sizeof(sound[0])) |
| 106 | + |
| 107 | +I2S i2s(OUTPUT); |
| 108 | + |
| 109 | +uint8_t sndIdx = 0; |
| 110 | + |
| 111 | + |
| 112 | +void setup1(void) { |
| 113 | + i2s.setBCLK(PIN_I2S_BIT_CLOCK); |
| 114 | + i2s.setDATA(PIN_I2S_DATA); |
| 115 | + i2s.setBitsPerSample(16); |
| 116 | +} |
| 117 | + |
| 118 | +void loop1() { |
| 119 | + Serial.printf("Core #2 Playing audio clip #%d\n", sndIdx); |
| 120 | + play_i2s(sound[sndIdx].data, sound[sndIdx].len, sound[sndIdx].rate); |
| 121 | + delay(5000); |
| 122 | + if(++sndIdx >= N_SOUNDS) sndIdx = 0; |
| 123 | +} |
| 124 | + |
| 125 | +void play_i2s(const uint8_t *data, uint32_t len, uint32_t rate) { |
| 126 | + |
| 127 | + // start I2S at the sample rate with 16-bits per sample |
| 128 | + if (!i2s.begin(rate)) { |
| 129 | + Serial.println("Failed to initialize I2S!"); |
| 130 | + delay(500); |
| 131 | + i2s.end(); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + for(uint32_t i=0; i<len; i++) { |
| 136 | + uint16_t sample = (uint16_t)data[i] * 32; |
| 137 | + // write the same sample twice, once for left and once for the right channel |
| 138 | + i2s.write(sample); |
| 139 | + i2s.write(sample); |
| 140 | + } |
| 141 | + i2s.end(); |
| 142 | +} |
0 commit comments