aboutsummaryrefslogtreecommitdiff
path: root/esp32/lego-piano.ino
blob: 575f55e39eada0e398287fd2bccf29fd1a034cc7 (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
/*
  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>
 
int currentLed = 0;
int lastSeen = -1;
 
#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;
 
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;
  lastSeen = -1;
}
 
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: octave_shift*freq,
  offset: 0,
  };
 
  dac_cw_generator_config(&wave_config);
  dac_cw_generator_enable();
 
  digitalWrite(ampEnable, HIGH);
}
 
void mute() {
  digitalWrite(ampEnable, LOW);
  dac_cw_generator_disable();
}
 
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;
}
 
uint32_t notes[] = {
                    261// C4 
                    277,
                    294// D4 
                    311,
                    330// E4 
                    349// F4 
                    370,
                    392// G4 
                    415,
                    440// A4 
                    466,
                    494// B4 
 
                    523// C5 
                    554,
                    587// D5 
                    622,
                    659// E5 
                    698// F5 
                    740,
                    783// G5 
                    831,
                    880// A5 
                    932,
                    988// B5 
 
                    1046// C6 
};
 
void loop() {
#if DEBUG & 0x01
  Serial.print("current led ");
  Serial.println(currentLed);
#endif
 
  enableLed(currentLed);
  delay(5);
  if (sense(currentLed)) {
    if (lastSeen != currentLed) {
      lastSeen = currentLed;
      Serial.print(currentLed);
      Serial.println(" proximity!");
      play(notes[currentLed]);
    }
  }
  else if (lastSeen == currentLed) {
    lastSeen = -1;
    mute();
  }
 
  currentLed = (currentLed+1)%(row_count*col_count);
}