From 3d76839cbcd3104ab22c5f7c7496a2090da60688 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 21 Aug 2020 16:12:49 +0100 Subject: using arduino-esp32 branch esp32s, it plays! that branch exposes the "cosine wave generator", so I don't have to --- esp32/lego-piano.ino | 73 ++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index ce11990..42c7e1a 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -18,35 +18,6 @@ #include -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_2, wave()); - wave_pos = (wave_pos+1)%wave_period; - } - - portEXIT_CRITICAL_ISR(&timerMux); -} - - int currentLed = 0; int lastSeen = 100; @@ -55,11 +26,13 @@ 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); + Serial.begin(115200); pinMode(ampEnable, OUTPUT); - dac_output_enable(DAC_CHANNEL_2); + dac_i2s_disable(); + dac_output_enable(DAC_CHANNEL_1); for (int i=0;i<5;++i) { pinMode(rows[i], OUTPUT); @@ -70,12 +43,27 @@ void setup() { currentLed = 0; lastSeen = 100; - timer = timerBegin(0, 2, true); // use a prescaler of 2 - timerAttachInterrupt(timer, &onTimer, true); - timerAlarmWrite(timer, 5000, true); - timerAlarmEnable(timer); + digitalWrite(ampEnable, LOW); +} + +void play(uint32_t freq) { + dac_cw_config_t wave_config = { + en_ch: DAC_CHANNEL_1, + scale: DAC_CW_SCALE_2, + phase: DAC_CW_PHASE_0, + freq: freq, + offset: 0, + }; + + dac_cw_generator_config(&wave_config); + dac_cw_generator_enable(); - play=true; + digitalWrite(ampEnable, HIGH); +} + +void mute() { + digitalWrite(ampEnable, LOW); + dac_cw_generator_disable(); } void tristate(int pin) { @@ -120,6 +108,10 @@ int sense(int led) { return value < 500; } +int count=0; +uint32_t notes[] = { 440, 880, 1200 }; +int currentNote = 0; + void loop() { /* Serial.print("current led "); */ /* Serial.println(currentLed); */ @@ -138,4 +130,13 @@ void loop() { } currentLed = (currentLed+1)%25; + + if (count == 450) { + //mute(); + } + else if (count == 0) { + play(notes[currentNote]); + currentNote = (currentNote+1)%3; + } + count = (count+1)%500; } \ No newline at end of file -- cgit v1.2.3