aboutsummaryrefslogtreecommitdiff
path: root/esp32
diff options
context:
space:
mode:
Diffstat (limited to 'esp32')
-rw-r--r--esp32/lego-piano.ino84
1 files changed, 62 insertions, 22 deletions
diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino
index d67a75a..bd1236b 100644
--- a/esp32/lego-piano.ino
+++ b/esp32/lego-piano.ino
@@ -16,19 +16,65 @@
so that given one of 12|13 and one of 14|15, one LED is identified
*/
+#include <driver/dac.h>
+
+hw_timer_t * timer = NULL;
+portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
+
+bool play = false;
+int wave_pos = 0;
+int wave_period = 1000;
+
+// stupid square wave
+int wave() {
+ if (wave_pos < wave_period/2) {
+ return 255;
+ }
+ else {
+ return 0;
+ }
+}
+
+void IRAM_ATTR onTimer() {
+ portENTER_CRITICAL_ISR(&timerMux);
+
+ if (play) {
+ dac_output_voltage(DAC_CHANNEL_1, wave());
+ wave_pos = (wave_pos+1)%wave_period;
+ }
+
+ portEXIT_CRITICAL_ISR(&timerMux);
+}
+
+
int currentLed = 0;
int lastSeen = 100;
+const int rows[] = { 5, 23, 19, 18, 26 };
+const int cols[] = { 34, 33, 35, 21, 22 };
+const int adc[] = { 2, 4, 12, 27, 14 };
+const int ampEnable = 32;
+
void setup() {
Serial.begin(19200);
- pinMode(12, OUTPUT);
- pinMode(13, OUTPUT);
- pinMode(14, OUTPUT);
- pinMode(15, OUTPUT);
- pinMode(A1, INPUT);
- pinMode(A2, INPUT);
+
+ pinMode(ampEnable, OUTPUT);
+
+ for (int i=0;i<5;++i) {
+ pinMode(rows[i], OUTPUT);
+ pinMode(cols[i], OUTPUT);
+ pinMode(adc[i], INPUT);
+ }
+
currentLed = 0;
lastSeen = 100;
+
+ timer = timerBegin(0, 2, true); // use a prescaler of 2
+ timerAttachInterrupt(timer, &onTimer, true);
+ timerAlarmWrite(timer, 5000, true);
+ timerAlarmEnable(timer);
+
+ play=true;
}
void tristate(int pin) {
@@ -48,32 +94,26 @@ void ground(int pin) {
}
void enableLed(int led) {
- int row = led/2;
- int col = led%2;
+ int row = led/5;
+ int col = led%5;
/* Serial.print("enabling "); */
/* Serial.print(row); */
/* Serial.print(" "); */
/* Serial.println(col); */
- if (row==0) {
- power(12); ground(13);
- }
- else {
- ground(12); power(13);
- }
+ for (int i=0;i<5;++i) {
+ if (i==row) { power(rows[i]); }
+ else { ground(rows[i]); }
- if (col==0) {
- ground(14); tristate(15);
- }
- else {
- tristate(14); ground(15);
+ if (i==col) { ground(cols[i]); }
+ else { tristate(cols[i]); }
}
}
int sense(int led) {
- int row = led/2;
+ int row = led/5;
- int value = analogRead(row == 0 ? A1 : A2);
+ int value = analogRead(adc[row]);
//Serial.println(value);
return value < 500;
@@ -96,5 +136,5 @@ void loop() {
lastSeen = 100;
}
- currentLed = (currentLed+1)%4;
+ currentLed = (currentLed+1)%25;
} \ No newline at end of file