aboutsummaryrefslogtreecommitdiff
path: root/esp32/lego-piano.ino
blob: 7826f31818438754ea75c6c268c24b25ca045fae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
  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 <driver/dac.h>
#define TSF_IMPLEMENTATION
#define TSF_NO_STDIO
#include "../ESP8266Audio/src/libtinysoundfont/tsf.h"
 
int currentLed = 0;
 
#define DEBUG 0
 
const size_t row_count = 5;
const size_t col_count = 5;
 
const int rows[row_count] = { 523191826 };
const int cols[col_count] = { 1733162122 };
const int adc[row_count] = { 24122714 };
const int ampEnable = 32;
 
const int octave_shift = 2;
 
char pressed[row_count*col_count] = { 0 };
 
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
tsf* g_TinySoundFont = 0;
bool playing = false;
 
//This is a minimal SoundFont with a single loopin saw-wave sample/instrument/preset (484 bytes) 
const static unsigned char MinimalSoundFont[] PROGMEM =
{
#define TEN0 0,0,0,0,0,0,0,0,0,0
'R','I','F','F',220,1,0,0,'s','f','b','k',
'L','I','S','T',88,1,0,0,'p','d','t','a',
'p','h','d','r',76,TEN0,TEN0,TEN0,TEN0,0,0,0,0,TEN0,0,0,0,0,0,0,0,255,0,255,0,1,TEN0,0,0,0,
'p','b','a','g',8,0,0,0,0,0,0,0,1,0,0,0,'p','m','o','d',10,TEN0,0,0,0,'p','g','e','n',8,0,0,0,41,0,0,0,0,0,0,0,
'i','n','s','t',44,TEN0,TEN0,0,0,0,0,0,0,0,0,TEN0,0,0,0,0,0,0,0,1,0,
'i','b','a','g',8,0,0,0,0,0,0,0,2,0,0,0,'i','m','o','d',10,TEN0,0,0,0,
'i','g','e','n',12,0,0,0,54,0,1,0,53,0,0,0,0,0,0,0,
's','h','d','r',92,TEN0,TEN0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,49,0,0,0,34,86,0,0,60,0,0,0,1,TEN0,TEN0,TEN0,TEN0,0,0,0,0,0,0,0,
'L','I','S','T',112,0,0,0,'s','d','t','a','s','m','p','l',100,0,0,0,86,0,119,3,31,7,147,10,43,14,169,17,58,21,189,24,73,28,204,31,73,35,249,38,46,42,71,46,250,48,150,53,242,55,126,60,151,63,108,66,126,72,207,
70,86,83,100,72,74,100,163,39,241,163,59,175,59,179,9,179,134,187,6,186,2,194,5,194,15,200,6,202,96,206,159,209,35,213,213,216,45,220,221,223,76,227,221,230,91,234,242,237,105,241,8,245,118,248,32,252
};
 
class DblBuffer {
  const static size_t _size = 1000;
  short samples[2][_size];
  size_t readPtr;
  char which;
  bool has_data[2];
 
 public:
  DblBuffer() :which(0), readPtr(0{
    has_data[0]=false;
    has_data[1]=false;
  }
 
  bool needsWriting() {
    return !has_data[1-which];
  }
  size_t size() { return _size; }
  short* ptr() { return samples[1-which]; }
  void writingDone() {
    has_data[1-which]=true;
  }
 
  short pop() {
    if (readPtr >= _size) {
      has_data[which]=false;
      which=1-which;readPtr=0;
    }
    if (!has_data[which]) return 0;
 
    return samples[which][readPtr++];
  }
} buffer;
 
void IRAM_ATTR onTimer() {
  return;
  if (!g_TinySoundFont) return;
 
  portENTER_CRITICAL_ISR(&timerMux);
 
  dac_output_voltage(DAC_CHANNEL_1, buffer.pop() >> 8);
 
  portEXIT_CRITICAL_ISR(&timerMux);
}
 
void setup() {
  Serial.begin(115200);
 
  pinMode(ampEnable, OUTPUT);
  digitalWrite(ampEnable, LOW);
 
  dac_i2s_disable();
  dac_output_enable(DAC_CHANNEL_1);
 
  for (int i=0;i<row_count;++i) {
    pinMode(rows[i], OUTPUT|PULLUP);
    pinMode(adc[i], INPUT);
  }
  for (int i=0;i<col_count;++i) {
    pinMode(cols[i], OUTPUT|PULLDOWN);
  }
 
  currentLed = 0;
  playing = false;
 
  g_TinySoundFont = tsf_load_memory(MinimalSoundFont, sizeof(MinimalSoundFont));
  if (g_TinySoundFont) {
    // render at 20kHz 
    tsf_set_output(g_TinySoundFont, TSF_MONO, 200000);
 
    // pre-scaling by 4k, then calling on every 1 tick, should give us 20kHz 
    timer = timerBegin(01000true);
    timerAttachInterrupt(timer, &onTimer, true);
    timerAlarmWrite(timer, 4true);
    timerAlarmEnable(timer);
  }
  else {
    Serial.println("failed to start tsf");
  }
}
 
void tristate(int pin) {
  pinMode(pin,OUTPUT|PULLDOWN);
  digitalWrite(pin,LOW);
  pinMode(pin,INPUT);
}
 
void power(int pin) {
  pinMode(pin,OUTPUT|PULLUP);
  digitalWrite(pin,HIGH);
}
 
void ground(int pin) {
  pinMode(pin,OUTPUT|PULLDOWN);
  digitalWrite(pin,LOW);
}
 
void enableLed(int led) {
  int row = (led/col_count) % row_count ;
  int col = led%col_count;
 
#if DEBUG & 0x02
  Serial.print("enabling ");
  Serial.print(row);
  Serial.print(" ");
  Serial.println(col);
#endif
 
  for (int i=0;i<row_count;++i) {
    if (i==row) { power(rows[i]); }
    else { ground(rows[i]); }
  }
  for (int i=0;i<col_count;++i) {
    if (i==col) { ground(cols[i]); }
    else { tristate(cols[i]); }
  }
}
 
int sense(int led) {
  int row = (led/col_count)%row_count;
 
  int value = analogRead(adc[row]);
 
#if DEBUG & 0x04
  Serial.println(value);
#endif
 
  return value < 500;
}
 
void loop() {
#if DEBUG & 0x01
  Serial.print("current led ");
  Serial.println(currentLed);
#endif
 
  enableLed(currentLed);
  delay(5);
  if (sense(currentLed)) {
    if (!pressed[currentLed]) {
      Serial.print(currentLed);
      Serial.println(" proximity!");
 
      pressed[currentLed]=1;
      if (g_TinySoundFont) {
        portENTER_CRITICAL_ISR(&timerMux);
        tsf_note_on(g_TinySoundFont, 060 + currentLed, 1.0f);
        portEXIT_CRITICAL_ISR(&timerMux);
      }
    }
  }
  else if (pressed[currentLed]) {
    pressed[currentLed]=0;
    if (g_TinySoundFont) {
      portENTER_CRITICAL_ISR(&timerMux);
      tsf_note_off(g_TinySoundFont, 060 + currentLed);
      portEXIT_CRITICAL_ISR(&timerMux);
    }
  }
 
  currentLed = (currentLed+1)%(row_count*col_count);
 
  if (buffer.needsWriting()) {
    tsf_render_short(g_TinySoundFont, buffer.ptr(), buffer.size(), 0);
    buffer.writingDone();
  }
}