/* matrix-scan a set of LEDs pins: "rows" 12 & 13 go to a 220Ω resistor, then to 2 LEDs each (positive / long stem side); also to a 10kΩ resistor then to 2 phototransistors (collector / long stem side) also, from between the 10kΩ and the phototransistors, wire goes to A1 & A2 "columns" 14 & 15 go to 2 LEDs each (negative / short stem side) so that given one of 12|13 and one of 14|15, one LED is identified */ #include #include #define TSF_IMPLEMENTATION #define TSF_NO_STDIO #include "../ESP8266Audio/src/libtinysoundfont/tsf.h" #include "../RingBuffer/src/RingBuf.h" #include "font.h" int currentLed = 0; #define DEBUG 0 const size_t row_count = 5; const size_t col_count = 5; const int rows[row_count] = { 5, 23, 19, 18, 26 }; const int cols[col_count] = { 17, 33, 16, 21, 22 }; const int adc[row_count] = { 2, 4, 12, 27, 14 }; const int ampEnable = 32; const int octave_shift = 2; const int sampleRate = 11025; char pressed[row_count*col_count] = { 0 }; tsf* g_TinySoundFont = 0; const i2s_port_t i2sPort = I2S_NUM_0; void setup() { Serial.begin(115200); pinMode(ampEnable, OUTPUT); digitalWrite(ampEnable, LOW); dac_i2s_enable(); dac_output_enable(DAC_CHANNEL_1); static const i2s_config_t i2s_config = { .mode = (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN ), .sample_rate = sampleRate, .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority .dma_buf_count = 8, .dma_buf_len = 64, .use_apll = false }; i2s_driver_install(i2sPort, &i2s_config, 0, NULL); i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN); // only dac1 for (int i=0;i buffer; short intermediateBuffer[bufferSize]; void loop() { #if DEBUG & 0x01 Serial.print("current led "); Serial.println(currentLed); #endif enableLed(currentLed); unsigned long t = millis(); size_t written=100; short value; // fill the I2S buffer while (written > 0) { // we don't want to drop an element from the buffer if we then // can't write it! so we peek at the buffer, try to write, and pop // if successful value=buffer[0] + 32768; // tsf produces *signed* shorts, i2s wants *unsigned* i2s_write(i2sPort, &value, sizeof(value), &written, 0); if (written > 0) buffer.pop(value); } int toFill = buffer.maxSize() - buffer.size(); if (toFill > 100) { tsf_render_short(g_TinySoundFont, intermediateBuffer, toFill, 0); for (int i=0;i