summaryrefslogtreecommitdiff
path: root/notes.c
diff options
context:
space:
mode:
authorVineet K <git@vineetk.net>2024-04-21 17:22:43 -0400
committerShokara Kou <kou@13f0.net>2024-04-21 17:23:57 -0400
commit21992cfc5410263750b481044ea77c431adff8d0 (patch)
treeef6da522bea314aa23913436f002ab4520df7fe5 /notes.c
parentd129c76966fc961672831a3058c79e76c93d7019 (diff)
read and play notes from serial
Diffstat (limited to 'notes.c')
-rw-r--r--notes.c46
1 files changed, 32 insertions, 14 deletions
diff --git a/notes.c b/notes.c
index 3f93cc5..7902efe 100644
--- a/notes.c
+++ b/notes.c
@@ -38,12 +38,12 @@ unsigned short freqs[] = {
38 FREQ(1750), 38 FREQ(1750),
39 FREQ(2000), 39 FREQ(2000),
40 40
41 [255] = 0 // used as a 'silent' note 41 [32] = 0 // used as a 'silent' note
42}; 42};
43unsigned char note = 0; 43unsigned char note = 0;
44// 0 = buttons write note+cycles+\xff to UART 44// 0 = buttons write note+cycles+\xff to UART
45// 1 = buttons don't do anything, speaker reads note+cycles+\xff from UART 45// 1 = buttons don't do anything, speaker reads note+cycles+\xff from UART
46unsigned char mode = 0; 46unsigned char mode = '0';
47 47
48ISR (PCINT0_vect) 48ISR (PCINT0_vect)
49{ 49{
@@ -82,7 +82,7 @@ void
82usart_init(void) 82usart_init(void)
83{ 83{
84 // 115200 bps, RX/TX enabled 84 // 115200 bps, RX/TX enabled
85 UBRR0 = 8; 85 UBRR0 = 103;
86 UCSR0B = (1 << RXEN0) | (1 << TXEN0); 86 UCSR0B = (1 << RXEN0) | (1 << TXEN0);
87 UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); 87 UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
88} 88}
@@ -94,6 +94,13 @@ usart_send(unsigned char c)
94 UDR0 = c; 94 UDR0 = c;
95} 95}
96 96
97void
98usart_send_str(const char *c)
99{
100 for (unsigned char i = 0; c[i] != '\0'; i++)
101 usart_send(c[i]);
102}
103
97unsigned char 104unsigned char
98usart_read(void) 105usart_read(void)
99{ 106{
@@ -125,9 +132,14 @@ main(void)
125 // Sends Square wave to the Speaker When a Button is Pressed 132 // Sends Square wave to the Speaker When a Button is Pressed
126 unsigned short cycles = 0; 133 unsigned short cycles = 0;
127 unsigned char tmp = 0; 134 unsigned char tmp = 0;
128 for (;;) { 135 for (unsigned char n = 0;; n = (n + 1) % 2) {
136 if (UCSR0A & (1 << RXC0)) {
137 mode = UDR0;
138 PORTD &= ~(1 << P_SPKR);
139 usart_send(mode);
140 }
129 // check if any buttons are pressed, otherwise don't play a note 141 // check if any buttons are pressed, otherwise don't play a note
130 if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) { 142 if (mode != '1' && ((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) {
131 // if a button was released, then send note and cycles played 143 // if a button was released, then send note and cycles played
132 if (cycles > 0) { 144 if (cycles > 0) {
133 usart_send(tmp); 145 usart_send(tmp);
@@ -135,16 +147,11 @@ main(void)
135 usart_send(LOW(cycles)); 147 usart_send(LOW(cycles));
136 usart_send(0xff); 148 usart_send(0xff);
137 cycles = 0; 149 cycles = 0;
138 } else {
139 if (UCSR0A & (1 << RXC0)) {
140 mode = UDR0;
141 usart_send(mode);
142 }
143 } 150 }
144 continue; 151 continue;
145 } 152 }
146 153
147 if (mode == 0) { 154 if (mode == '0') {
148 // Prepare Timer/Counter 155 // Prepare Timer/Counter
149 TCNT1 = 1; 156 TCNT1 = 1;
150 tmp = note; 157 tmp = note;
@@ -159,15 +166,26 @@ main(void)
159 TIFR1 |= 1 << OCF1A; 166 TIFR1 |= 1 << OCF1A;
160 TCCR1B &= ~(1 << CS11); 167 TCCR1B &= ~(1 << CS11);
161 cycles = (cycles + 1) % 0xffff; 168 cycles = (cycles + 1) % 0xffff;
162 } else if (mode == 0xb0) { 169 } else if (mode == '1') {
163 note = usart_read(); 170 note = usart_read();
171 if (note >= '0') note -= '0';
164 tmp = usart_read(); 172 tmp = usart_read();
165 cycles = tmp << 8; 173 cycles = tmp << 8;
166 tmp = usart_read(); 174 tmp = usart_read();
167 cycles |= tmp; 175 cycles |= tmp;
168 (void)usart_read(); 176 tmp = usart_read();
177
178 // reset to default mode when an invalid note is read
179 if (tmp != 'z' || note > '@') {
180 mode = '0';
181 continue;
182 }
183
184 for (unsigned short i = 0; i < cycles; i++) {
185 usart_send(HIGH(cycles));
186 usart_send(LOW(cycles));
187 usart_send(0);
169 188
170 for (unsigned char i = 0; i < cycles; i++) {
171 TCNT1 = 1; 189 TCNT1 = 1;
172 OCR1A = freqs[note]; 190 OCR1A = freqs[note];
173 TCCR1B |= 1 << CS11; 191 TCCR1B |= 1 << CS11;