forked from ElectricLaboratory/MiniStylophone-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
styl.c
316 lines (283 loc) · 7.21 KB
/
styl.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* styl - Nano Stylophone for attiny13a
* Copyright (C) 2011 Bob Clough <[email protected]>
* Copyright (C) 2011 Charles Yarnold <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define F_CPU 8000000UL
#define USART_BAUDRATE 9600
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include "macros.h"
#include "notes.h"
#include "styl.h"
int recording, saved, playing, reading, noteSlot;
void setup(void)
{
// Set PORTB pins 2 and 3 as outputs
bit_set(DDRB, LED_TOUCH);
bit_set(DDRB, LED_REC);
// Turn Touch LED on, Record LED off
bit_clear(PORTB, LED_TOUCH);
bit_set(PORTB, LED_REC);
// Activate PWM for output, ADC for probe input
pwm_init();
adc_init();
// We aren't recording
recording = FALSE;
// We are at the beginning of a recording
noteSlot = 0;
}
void loop(void)
{
//if we are in playback mode
if (playing)
{
// fetch the next note from the eeprom
reading = eeprom_read_byte((uint8_t*) noteSlot);
reading = reading * 4;
}
else
{
// fetch the next note from the probe
reading = adc_read();
}
// if nothing is being pressed
if (reading < 128)
{
// turn the touch LED On
bit_clear(PORTB, LED_TOUCH);
// Stop playing any notes we might be playing
dontplay();
// if we are recording
if (recording)
{
// turn the REC LED on
bit_clear(PORTB, LED_REC);
_delay_ms(10);
if (adc_read() == reading)
{
saved = 0;
}
}
}
else
{
// if we are recording and the note hasn't changed
if (recording && !saved)
{
// crop off the top two bits to make the note fit in an 8 bit memory address
int readingLess = reading / 4;
// store the note in the eeprom
eeprom_write_byte((uint8_t *) noteSlot, (uint8_t) readingLess);
// move to the next eeprom location
noteSlot++;
// if the eeprom is full
if (noteSlot > 60)
{
// stop recording & reset to the beginning of the recording
recording = FALSE;
noteSlot = 0;
// turn the recording LED Off
bit_set(PORTB, LED_REC);
}
else
{
// mark the current note as saved
saved = 1;
}
}
// turn the TOUCH LED off
bit_set(PORTB, LED_TOUCH);
// if we are recording, turn the RED LED off (why here?)
if (recording) bit_set(PORTB, LED_REC);
// average out the reading over a few samples to make sure it is correct
_delay_ms(50);
reading = reading + adc_read() + adc_read() + adc_read() / 4;
if ((reading <= 310)) // Record button was pressed
{
// Toggle Recording Mode
recording = !recording;
// If we are finished recording
if (!recording)
{
// add a '0' to the end of the recording, to make sure the playback is turned off
noteSlot++;
eeprom_write_byte((uint8_t *) noteSlot, (uint8_t) 0);
}
// reset our 'pointer' to the beginning of the eeprom
noteSlot = 0;
// wait for the button to be released
while (adc_read() > 128);
}
else if ((reading <= 340)) // Play button was pressed
{
// we cant play while recording
if (!recording)
{
// start playing back from the beginning of the eeprom
playing = TRUE;
noteSlot = 0;
// turn on the REC LED
bit_clear(PORTB, LED_REC);
// wait for the button to be released
while (adc_read() > 128);
}
}
else if ((reading <= 359))
{
playnote(A1);
}
else if (reading <= 380)
{
playnote(AS1);
}
else if (reading <= 387)
{
playnote(B1);
}
else if (reading <= 402)
{
playnote(C1);
}
else if (reading <= 418)
{
playnote(CS1);
}
else if (reading <= 436)
{
playnote(D1);
}
else if (reading <= 455)
{
playnote(DS1);
}
else if (reading <= 477)
{
playnote(E1);
}
else if (reading <= 500)
{
playnote(F1);
}
else if (reading <= 525)
{
playnote(FS1);
}
else if (reading <= 554)
{
playnote(G1);
}
else if (reading <= 586)
{
playnote(GS1);
}
else if (reading <= 621)
{
playnote(A2);
}
else if (reading <= 661)
{
playnote(AS2);
}
else if (reading <= 707)
{
playnote(B2);
}
else if (reading <= 760)
{
playnote(C2);
}
else if (reading <= 821)
{
playnote(CS2);
}
else if (reading <= 892)
{
playnote(D2);
}
else if (reading <= 977)
{
playnote(DS2);
}
else if (reading > 977)
{
playnote(E2);
}
}
if (playing)
{
// wait for time
_delay_ms(200);
dontplay();
_delay_ms(200);
noteSlot++;
if (noteSlot > 60 || reading == 0)
{
noteSlot = 0;
playing = FALSE;
bit_set(PORTB, LED_REC);
}
}
else
{
// loop quickly
_delay_us(255);
}
}
void playnote(uint8_t a)
{
OCR0A = a;
OCR0B = 0;
}
void dontplay()
{
playnote(0);
}
void pwm_init(void)
{
DDRB |= BIT(1);
TCCR0A = BIT(COM0B1); // Clear OC0A on Compare Match
TCCR0A |= BIT(WGM00); // PWM mode, 8 bit
TCCR0B = BIT(WGM02); // TOP = OCRA
TCCR0B |= BIT(CS01) | BIT(CS00); // 64x prescaler
}
void adc_init(void)
{
ADMUX = BIT(MUX1);
ADCSRA = BIT(ADEN);
}
int adc_read()
{
ADCSRA |= BIT(ADSC);
while (bit_get(ADCSRA, BIT(ADSC)) != 0)
{
};
return ADC;
}
/**
* Main to allow arduino-style setup and main functions
*/
int main(void)
{
setup();
while (1)
{
loop();
}
}