diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 7bd9288..116f515 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -19,7 +19,6 @@ typedef struct note { // 61 Notes from C2 to C7 const int NUM_NOTES = 61; - const Note notes[] = { {"C2", 36}, {"C#2", 37}, {"D2", 38}, {"D#2", 39}, {"E2", 40}, {"F2", 41}, {"F#2", 42}, {"G2", 43}, {"G#2", 44}, {"A2", 45}, {"A#2", 46}, {"B2", 47}, {"C3", 48}, {"C#3", 49}, {"D3", 50}, {"D#3", 51}, {"E3", 52}, {"F3", 53}, {"F#3", 54}, {"G3", 55}, {"G#3", 56}, {"A3", 57}, {"A#3", 58}, {"B3", 59}, @@ -30,8 +29,13 @@ const Note notes[] = { }; // 4 Multiplexers: GPIO pins for each analog multiplexer that handles 16 sensors +// Notes are connected in order: Mux #1 (0-15), Mux #2 (16-31), Mux #3 (32-47), Mux #4 (48-61) const int NUM_MUX = 4; -const int mux[] = {35, 34, 36, 39}; +const int PINS_PER_MUX = 16; +const int mux_in[] = {35, 34, 36, 39}; // Analog signal input +// Select pins for every multiplexer, each multiplexer has 4 select pins, all sel0 are connected to 14, etc. +const int NUM_MUX_SEL = 4; +const int mux_sel[4] = {14, 27, 16, 17}; int lasts[NUM_NOTES]; // variable to store the value coming from the sensor u64 last_hit_times[NUM_NOTES]; @@ -43,10 +47,15 @@ int active_threshold = 400; // Minimum value to be considered as a hit void setup() { // Initialize pin and serial - for (int pin : pins) pinMode(pin, INPUT); + for (int pin: mux_in) pinMode(pin, INPUT); + for (int pin: mux_sel) pinMode(pin, OUTPUT); Serial.begin(9600); Serial.printf("Initialized\r\n"); + // Testing pin + pinMode(2, OUTPUT); + analogWrite(2, 255); + start_time = timeMillis(); } @@ -58,9 +67,11 @@ void setup() void on_sensor_update(int id, u64 time, int last, int current) { // If the last value is larger than the current value, check timeout - if (last > current && last > active_threshold) { + if (last > current && last > active_threshold) + { u64 elapsed = time - last_hit_times[id]; - if (elapsed < 150) { + if (elapsed < 150) + { // If the last hit is too close, ignore this hit return; } @@ -68,25 +79,47 @@ void on_sensor_update(int id, u64 time, int last, int current) // The last value is a local maximum, // and we read it as the hit strength of our note. Send midi command to the host. // /hit - Serial.printf("/hit %d %d\r\n", notes[id], min((last - active_threshold) * 127 / (max_threshold - active_threshold), 127)); + Serial.printf("/hit %d %d\r\n", notes[id].midi, + min((last - active_threshold) * 127 / (max_threshold - active_threshold), 127)); // Update last hit time last_hit_times[id] = time; } } -void loop() { +void loop() +{ u64 time = timeMillis(); + u64 elapsed = time - start_time; - // Read sensor value from each pin - for (int i = 0; i < NUM_SENSORS; i++) { - int v = analogRead(pins[i]); - if (v != lasts[i]) { - u64 elapsed = time - start_time; - Serial.printf("Sensor #%d - %" PRIu64 ": %d\r\n", i, elapsed, v); + Serial.printf("%" PRIu64 "=============\r\n", elapsed); - on_sensor_update(i, time, lasts[i], v); + // Loop through each multiplexer state + for (int i = 0; i < PINS_PER_MUX; i++) + { + // Set select pins + for (int j = 0; j < NUM_MUX_SEL; j++) + { + // i >> j is the jth bit of i + digitalWrite(mux_sel[j], (i >> j) & 1); + } + + // Read four input pins from the multiplexer + for (int j = 0; j < NUM_MUX; j++) + { + int note_id = j * PINS_PER_MUX + i; + if (note_id >= NUM_NOTES) break; + + // Read the analog input + int v = analogRead(mux_in[j]); + if (v != lasts[note_id]) + { + // Serial prints are really slow, so don't use them in debug mode + // Serial.printf("#%d %d\r\n", note_id, v); + on_sensor_update(note_id, time, lasts[note_id], v); + } + lasts[note_id] = v; } - lasts[i] = v; } +// Serial.printf("Loop %" PRIu64 "\r\n", elapsed); } \ No newline at end of file