diff options
Diffstat (limited to 'keyboard/preonic/beeps.c')
| -rw-r--r-- | keyboard/preonic/beeps.c | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/keyboard/preonic/beeps.c b/keyboard/preonic/beeps.c new file mode 100644 index 000000000..13e46e1da --- /dev/null +++ b/keyboard/preonic/beeps.c | |||
| @@ -0,0 +1,238 @@ | |||
| 1 | #include "beeps.h" | ||
| 2 | #include <math.h> | ||
| 3 | #include <avr/pgmspace.h> | ||
| 4 | #include <avr/interrupt.h> | ||
| 5 | #include <avr/io.h> | ||
| 6 | |||
| 7 | #define PI 3.14159265 | ||
| 8 | #define CHANNEL OCR1C | ||
| 9 | |||
| 10 | volatile uint16_t sample; | ||
| 11 | uint16_t lastSample; | ||
| 12 | |||
| 13 | const int sounddata_length=200; | ||
| 14 | |||
| 15 | const unsigned char sounddata_data[] PROGMEM = {128, | ||
| 16 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 17 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 18 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 19 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 20 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 21 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 22 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 23 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 24 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, | ||
| 25 | 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 129, 127, 129, 128, 127, 133, | ||
| 26 | 117, 109, 125, 121, 116, 132, 140, 126, 114, 114, 116, 120, 114, 93, 73, 66, 76, 116, 142, 129, | ||
| 27 | 128, 129, 120, 119, 118, 104, 87, 123, 181, 194, 196, 198, 189, 176, 160, 162, 172, 164, 164, 183, | ||
| 28 | 197, 188, 168, 167, 170, 165, 185, 209, 206, 196, 196, 199, 185, 162, 156, 167, 176, 173, 170, 166, | ||
| 29 | 151, 142, 140, 134, 130, 127, 113, 86, 67, 66, 69, 75, 73, 75, 86, 90, 91, 84, 65, 48, | ||
| 30 | 41, 30, 26, 56, 91, 88, 72, 70, 73, 82, 89, 73, 57, 60, 74, 89, 92, 77, 63, 60, | ||
| 31 | 53, 47, 56, 64, 63, 61, 56, 54, 52, 36, 16, 22, 51, 66, 67, 70, 76, 88, 99, 92, | ||
| 32 | 77, 74, 85, 100, 106, 97, 83, 85, 96, 108, 133, 160, 164}; | ||
| 33 | |||
| 34 | void delay_us(int count) { | ||
| 35 | while(count--) { | ||
| 36 | _delay_us(1); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | void beeps() { | ||
| 41 | // DDRB |= (1<<7); | ||
| 42 | // PORTB &= ~(1<<7); | ||
| 43 | |||
| 44 | // // Use full 16-bit resolution. | ||
| 45 | // ICR1 = 0xFFFF; | ||
| 46 | |||
| 47 | // // I could write a wall of text here to explain... but TL;DW | ||
| 48 | // // Go read the ATmega32u4 datasheet. | ||
| 49 | // // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on | ||
| 50 | |||
| 51 | // // Pin PB7 = OCR1C (Timer 1, Channel C) | ||
| 52 | // // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0 | ||
| 53 | // // (i.e. start high, go low when counter matches.) | ||
| 54 | // // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0 | ||
| 55 | // // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1 | ||
| 56 | |||
| 57 | // TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010; | ||
| 58 | // TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; | ||
| 59 | |||
| 60 | |||
| 61 | // // Turn off PWM control on PB7, revert to output low. | ||
| 62 | // // TCCR1A &= ~(_BV(COM1C1)); | ||
| 63 | // // CHANNEL = ((1 << level) - 1); | ||
| 64 | |||
| 65 | // // Turn on PWM control of PB7 | ||
| 66 | // TCCR1A |= _BV(COM1C1); | ||
| 67 | // // CHANNEL = level << OFFSET | 0x0FFF; | ||
| 68 | // // CHANNEL = 0b1010101010101010; | ||
| 69 | |||
| 70 | // float x = 12; | ||
| 71 | // float y = 24; | ||
| 72 | // float length = 50; | ||
| 73 | // float scale = 1; | ||
| 74 | |||
| 75 | // // int f1 = 1000000/440; | ||
| 76 | // // int f2 = 1000000/880; | ||
| 77 | // // for (uint32_t i = 0; i < length * 1000; i++) { | ||
| 78 | // // // int frequency = 1/((sin(PI*2*i*scale*pow(2, x/12.0))*.5+1 + sin(PI*2*i*scale*pow(2, y/12.0))*.5+1) / 2); | ||
| 79 | |||
| 80 | // // ICR1 = f1; // Set max to the period | ||
| 81 | // // OCR1C = f1 >> 1; // Set compare to half the period | ||
| 82 | // // // _delay_us(10); | ||
| 83 | // // } | ||
| 84 | // int frequency = 1000000/440; | ||
| 85 | // ICR1 = frequency; // Set max to the period | ||
| 86 | // OCR1C = frequency >> 1; // Set compare to half the period | ||
| 87 | // _delay_us(500000); | ||
| 88 | |||
| 89 | // TCCR1A &= ~(_BV(COM1C1)); | ||
| 90 | // CHANNEL = 0; | ||
| 91 | play_notes(); | ||
| 92 | |||
| 93 | |||
| 94 | // play_note(55*pow(2, 0/12.0), 1); | ||
| 95 | // play_note(55*pow(2, 12/12.0), 1); | ||
| 96 | // play_note(55*pow(2, 24/12.0), 1); | ||
| 97 | // play_note(55*pow(2, 0/12.0), 1); | ||
| 98 | // play_note(55*pow(2, 12/12.0), 1); | ||
| 99 | // play_note(55*pow(2, 24/12.0), 1); | ||
| 100 | |||
| 101 | // play_note(0, 4); | ||
| 102 | |||
| 103 | // play_note(55*pow(2, 0/12.0), 8); | ||
| 104 | // play_note(55*pow(2, 12/12.0), 4); | ||
| 105 | // play_note(55*pow(2, 10/12.0), 4); | ||
| 106 | // play_note(55*pow(2, 12/12.0), 8); | ||
| 107 | // play_note(55*pow(2, 10/12.0), 4); | ||
| 108 | // play_note(55*pow(2, 7/12.0), 2); | ||
| 109 | // play_note(55*pow(2, 8/12.0), 2); | ||
| 110 | // play_note(55*pow(2, 7/12.0), 16); | ||
| 111 | // play_note(0, 4); | ||
| 112 | // play_note(55*pow(2, 3/12.0), 8); | ||
| 113 | // play_note(55*pow(2, 5/12.0), 4); | ||
| 114 | // play_note(55*pow(2, 7/12.0), 4); | ||
| 115 | // play_note(55*pow(2, 7/12.0), 8); | ||
| 116 | // play_note(55*pow(2, 5/12.0), 4); | ||
| 117 | // play_note(55*pow(2, 3/12.0), 4); | ||
| 118 | // play_note(55*pow(2, 2/12.0), 16); | ||
| 119 | |||
| 120 | |||
| 121 | } | ||
| 122 | |||
| 123 | void play_note(float freq, int length) { | ||
| 124 | DDRB |= (1<<7); | ||
| 125 | PORTB &= ~(1<<7); | ||
| 126 | |||
| 127 | if (freq > 0) { | ||
| 128 | int frequency = 1000000/freq; | ||
| 129 | ICR1 = frequency; // Set max to the period | ||
| 130 | OCR1C = frequency >> 1; // Set compare to half the period | ||
| 131 | |||
| 132 | TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010; | ||
| 133 | TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; | ||
| 134 | } | ||
| 135 | |||
| 136 | for (int i = 0; i < length; i++) { | ||
| 137 | _delay_us(50000); | ||
| 138 | } | ||
| 139 | |||
| 140 | TCCR1A &= ~(_BV(COM1C1)); | ||
| 141 | } | ||
| 142 | |||
| 143 | // This is called at 8000 Hz to load the next sample. | ||
| 144 | ISR(TIMER1_COMPA_vect) { | ||
| 145 | if (sample >= sounddata_length) { | ||
| 146 | if (sample == sounddata_length + lastSample) { | ||
| 147 | TIMSK1 &= ~_BV(OCIE1A); | ||
| 148 | |||
| 149 | // Disable the per-sample timer completely. | ||
| 150 | TCCR1B &= ~_BV(CS10); | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | OCR1C = sounddata_length + lastSample - sample; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | OCR1C = pgm_read_byte(&sounddata_data[sample]); | ||
| 158 | } | ||
| 159 | |||
| 160 | ++sample; | ||
| 161 | } | ||
| 162 | |||
| 163 | void play_notes() { | ||
| 164 | |||
| 165 | |||
| 166 | // Set up Timer 2 to do pulse width modulation on the speaker | ||
| 167 | // pin. | ||
| 168 | |||
| 169 | DDRB |= (1<<7); | ||
| 170 | PORTB &= ~(1<<7); | ||
| 171 | |||
| 172 | // Use internal clock (datasheet p.160) | ||
| 173 | // ASSR &= ~(_BV(EXCLK) | _BV(AS2)); | ||
| 174 | |||
| 175 | // Set fast PWM mode (p.157) | ||
| 176 | TCCR1A |= _BV(WGM21) | _BV(WGM20); | ||
| 177 | TCCR1B &= ~_BV(WGM22); | ||
| 178 | |||
| 179 | // Do non-inverting PWM on pin OC2A (p.155) | ||
| 180 | // On the Arduino this is pin 11. | ||
| 181 | TCCR1A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0); | ||
| 182 | TCCR1A &= ~(_BV(COM2B1) | _BV(COM2B0)); | ||
| 183 | // No prescaler (p.158) | ||
| 184 | TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10); | ||
| 185 | |||
| 186 | // Set initial pulse width to the first sample. | ||
| 187 | OCR1A = pgm_read_byte(&sounddata_data[0]); | ||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | cli(); | ||
| 193 | |||
| 194 | // Set CTC mode (Clear Timer on Compare Match) (p.133) | ||
| 195 | // Have to set OCR1A *after*, otherwise it gets reset to 0! | ||
| 196 | TCCR2B = (TCCR2B & ~_BV(WGM13)) | _BV(WGM12); | ||
| 197 | TCCR2A = TCCR2A & ~(_BV(WGM11) | _BV(WGM10)); | ||
| 198 | |||
| 199 | // No prescaler (p.134) | ||
| 200 | TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10); | ||
| 201 | |||
| 202 | // Set the compare register (OCR1A). | ||
| 203 | // OCR1A is a 16-bit register, so we have to do this with | ||
| 204 | // interrupts disabled to be safe. | ||
| 205 | // OCR2A = F_CPU / SAMPLE_RATE; // 16e6 / 8000 = 2000 | ||
| 206 | OCR2A = 2000; | ||
| 207 | |||
| 208 | // Enable interrupt when TCNT1 == OCR1A (p.136) | ||
| 209 | TIMSK1 |= _BV(OCIE2A); | ||
| 210 | |||
| 211 | sample = 0; | ||
| 212 | sei(); | ||
| 213 | } | ||
| 214 | |||
| 215 | void note(int x, float length) { | ||
| 216 | DDRB |= (1<<1); | ||
| 217 | int t = (int)(440*pow(2,-x/12.0)); // starting note | ||
| 218 | for (int y = 0; y < length*1000/t; y++) { // note length | ||
| 219 | PORTB |= (1<<1); | ||
| 220 | delay_us(t); | ||
| 221 | PORTB &= ~(1<<1); | ||
| 222 | delay_us(t); | ||
| 223 | } | ||
| 224 | PORTB &= ~(1<<1); | ||
| 225 | } | ||
| 226 | |||
| 227 | void true_note(float x, float y, float length) { | ||
| 228 | for (uint32_t i = 0; i < length * 50; i++) { | ||
| 229 | uint32_t v = (uint32_t) (round(sin(PI*2*i*640000*pow(2, x/12.0))*.5+1 + sin(PI*2*i*640000*pow(2, y/12.0))*.5+1) / 2 * pow(2, 8)); | ||
| 230 | for (int u = 0; u < 8; u++) { | ||
| 231 | if (v & (1 << u) && !(PORTB&(1<<1))) | ||
| 232 | PORTB |= (1<<1); | ||
| 233 | else if (PORTB&(1<<1)) | ||
| 234 | PORTB &= ~(1<<1); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | PORTB &= ~(1<<1); | ||
| 238 | } \ No newline at end of file | ||
