diff options
| author | Vineet K <git@vineetk.net> | 2024-04-21 15:22:20 -0400 |
|---|---|---|
| committer | Shokara Kou <kou@13f0.net> | 2024-04-21 15:23:02 -0400 |
| commit | d129c76966fc961672831a3058c79e76c93d7019 (patch) | |
| tree | 4dca475486b1460439ab16eb89056b6b8c724aac | |
| parent | c2fe984ed4e6e0077747b1eb5ffa76e5a7c2fe5f (diff) | |
prepare for uart commands
| -rw-r--r-- | notes.c | 64 |
1 files changed, 47 insertions, 17 deletions
| @@ -41,6 +41,9 @@ unsigned short freqs[] = { | |||
| 41 | [255] = 0 // used as a 'silent' note | 41 | [255] = 0 // used as a 'silent' note |
| 42 | }; | 42 | }; |
| 43 | unsigned char note = 0; | 43 | unsigned char note = 0; |
| 44 | // 0 = buttons write note+cycles+\xff to UART | ||
| 45 | // 1 = buttons don't do anything, speaker reads note+cycles+\xff from UART | ||
| 46 | unsigned char mode = 0; | ||
| 44 | 47 | ||
| 45 | ISR (PCINT0_vect) | 48 | ISR (PCINT0_vect) |
| 46 | { | 49 | { |
| @@ -121,33 +124,60 @@ main(void) | |||
| 121 | 124 | ||
| 122 | // Sends Square wave to the Speaker When a Button is Pressed | 125 | // Sends Square wave to the Speaker When a Button is Pressed |
| 123 | unsigned short cycles = 0; | 126 | unsigned short cycles = 0; |
| 124 | unsigned char last_note = 0; | 127 | unsigned char tmp = 0; |
| 125 | for (unsigned char n = 0;; n = (n + 1) % 2) { | 128 | for (;;) { |
| 126 | // check if any buttons are pressed, otherwise don't play a note | 129 | // check if any buttons are pressed, otherwise don't play a note |
| 127 | if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) { | 130 | if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) { |
| 128 | // if a button was released, then send note and cycles played | 131 | // if a button was released, then send note and cycles played |
| 129 | if (cycles > 0) { | 132 | if (cycles > 0) { |
| 130 | usart_send(last_note); | 133 | usart_send(tmp); |
| 131 | usart_send(HIGH(cycles)); | 134 | usart_send(HIGH(cycles)); |
| 132 | usart_send(LOW(cycles)); | 135 | usart_send(LOW(cycles)); |
| 133 | usart_send('\xff'); | 136 | usart_send(0xff); |
| 134 | cycles = 0; | 137 | cycles = 0; |
| 138 | } else { | ||
| 139 | if (UCSR0A & (1 << RXC0)) { | ||
| 140 | mode = UDR0; | ||
| 141 | usart_send(mode); | ||
| 142 | } | ||
| 135 | } | 143 | } |
| 136 | continue; | 144 | continue; |
| 137 | } | 145 | } |
| 138 | 146 | ||
| 139 | // Prepare Timer/Counter | 147 | if (mode == 0) { |
| 140 | TCNT1 = 1; | 148 | // Prepare Timer/Counter |
| 141 | last_note = note; | 149 | TCNT1 = 1; |
| 142 | OCR1A = freqs[note]; | 150 | tmp = note; |
| 143 | TCCR1B |= 1 << CS11; | 151 | OCR1A = freqs[note]; |
| 144 | 152 | TCCR1B |= 1 << CS11; | |
| 145 | while ((TIFR1 & (1 << OCF1A)) == 0); // Monitor OutPut Compare Flag | 153 | |
| 146 | 154 | // Monitor OutPut Compare Flag | |
| 147 | // Toggles port For Speaker | 155 | while ((TIFR1 & (1 << OCF1A)) == 0); |
| 148 | PORTD ^= 1 << P_SPKR; | 156 | |
| 149 | TIFR1 |= 1 << OCF1A; | 157 | // Toggles port For Speaker |
| 150 | TCCR1B &= ~(1 << CS11); | 158 | PORTD ^= 1 << P_SPKR; |
| 151 | cycles = (cycles + 1) % 0xffff; | 159 | TIFR1 |= 1 << OCF1A; |
| 160 | TCCR1B &= ~(1 << CS11); | ||
| 161 | cycles = (cycles + 1) % 0xffff; | ||
| 162 | } else if (mode == 0xb0) { | ||
| 163 | note = usart_read(); | ||
| 164 | tmp = usart_read(); | ||
| 165 | cycles = tmp << 8; | ||
| 166 | tmp = usart_read(); | ||
| 167 | cycles |= tmp; | ||
| 168 | (void)usart_read(); | ||
| 169 | |||
| 170 | for (unsigned char i = 0; i < cycles; i++) { | ||
| 171 | TCNT1 = 1; | ||
| 172 | OCR1A = freqs[note]; | ||
| 173 | TCCR1B |= 1 << CS11; | ||
| 174 | |||
| 175 | while ((TIFR1 & (1 << OCF1A)) == 0); | ||
| 176 | |||
| 177 | PORTD ^= 1 << P_SPKR; | ||
| 178 | TIFR1 |= 1 << OCF1A; | ||
| 179 | TCCR1B &= ~(1 << CS11); | ||
| 180 | } | ||
| 181 | } | ||
| 152 | } | 182 | } |
| 153 | } | 183 | } |
