| 1 | // Adafruit Circuit Playground speaker library |
|---|
| 2 | // by Phil Burgess / Paint Your Dragon. |
|---|
| 3 | |
|---|
| 4 | #include <Arduino.h> |
|---|
| 5 | #include "Adafruit_CPlay_Speaker.h" |
|---|
| 6 | |
|---|
| 7 | /**************************************************************************/ |
|---|
| 8 | /*! |
|---|
| 9 | @brief Sets up Circuit Playground speaker for PWM audio output: enables 48 KHz |
|---|
| 10 | high-speed PWM mode, configures Timer/Counter 4, sets PWM duty cycle to |
|---|
| 11 | 50% (speaker idle position). |
|---|
| 12 | */ |
|---|
| 13 | /**************************************************************************/ |
|---|
| 14 | void Adafruit_CPlay_Speaker::begin(void) { |
|---|
| 15 | if(!started) { |
|---|
| 16 | #ifdef __AVR__ |
|---|
| 17 | // Set up Timer4 for fast PWM on !OC4A |
|---|
| 18 | PLLFRQ = (PLLFRQ & 0xCF) | 0x30; // Route PLL to async clk |
|---|
| 19 | TCCR4A = _BV(COM4A0) | _BV(PWM4A); // Clear on match, PWMA on |
|---|
| 20 | TCCR4B = _BV(PWM4X) | _BV(CS40); // PWM invert, 1:1 prescale |
|---|
| 21 | TCCR4D = 0; // Fast PWM mode |
|---|
| 22 | TCCR4E = 0; // Not enhanced mode |
|---|
| 23 | DT4 = 0; // No dead time |
|---|
| 24 | noInterrupts(); // TC4H accesses must be atomic |
|---|
| 25 | TC4H = 0; // Not 10-bit mode |
|---|
| 26 | OCR4C = 255; // TOP |
|---|
| 27 | TC4H = 0; |
|---|
| 28 | OCR4A = 127; // 50% duty (idle position) to start |
|---|
| 29 | interrupts(); |
|---|
| 30 | pinMode(5, OUTPUT); // Enable output |
|---|
| 31 | #else |
|---|
| 32 | pinMode(CPLAY_SPEAKER_SHUTDOWN, OUTPUT); |
|---|
| 33 | digitalWrite(CPLAY_SPEAKER_SHUTDOWN, HIGH); |
|---|
| 34 | // PWM/timer not needed on CPlay Express, has true analog out. |
|---|
| 35 | // Set analogWrite resolution to 8 bits to match AVR calls. |
|---|
| 36 | analogWriteResolution(8); |
|---|
| 37 | pinMode(A0, OUTPUT); // Enable output |
|---|
| 38 | #endif |
|---|
| 39 | started = true; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /**************************************************************************/ |
|---|
| 44 | /*! |
|---|
| 45 | @brief enable or disable the speaker. This function only works on 'Express' boards. |
|---|
| 46 | @param e pass true to enable, false to disable |
|---|
| 47 | */ |
|---|
| 48 | /**************************************************************************/ |
|---|
| 49 | |
|---|
| 50 | void Adafruit_CPlay_Speaker::enable(bool e) { |
|---|
| 51 | #if !defined(__AVR__) // circuit playground express has nicer amp w/shutdown |
|---|
| 52 | digitalWrite(CPLAY_SPEAKER_SHUTDOWN, e); |
|---|
| 53 | #endif |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /**************************************************************************/ |
|---|
| 57 | /*! |
|---|
| 58 | @brief Turns off PWM output to the speaker. |
|---|
| 59 | */ |
|---|
| 60 | /**************************************************************************/ |
|---|
| 61 | void Adafruit_CPlay_Speaker::end(void) { |
|---|
| 62 | if(started) { |
|---|
| 63 | #ifdef __AVR__ |
|---|
| 64 | TCCR4A = 0; // PWMA off |
|---|
| 65 | pinMode(5, INPUT); |
|---|
| 66 | #else |
|---|
| 67 | pinMode(A0, INPUT); |
|---|
| 68 | enable(false); |
|---|
| 69 | #endif |
|---|
| 70 | started = false; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /**************************************************************************/ |
|---|
| 75 | /*! |
|---|
| 76 | @brief Sets speaker position, enables PWM output if needed. |
|---|
| 77 | @param value the value to set (0-255; 127=idle) |
|---|
| 78 | */ |
|---|
| 79 | /**************************************************************************/ |
|---|
| 80 | void Adafruit_CPlay_Speaker::set(uint8_t value) { |
|---|
| 81 | if(!started) begin(); |
|---|
| 82 | #ifdef __AVR__ |
|---|
| 83 | OCR4A = value; |
|---|
| 84 | #else |
|---|
| 85 | analogWrite(A0, value); |
|---|
| 86 | #endif |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /**************************************************************************/ |
|---|
| 90 | /*! |
|---|
| 91 | @brief Plays digitized 8-bit audio (optionally 10 bits on Express board) from |
|---|
| 92 | a PROGMEM (flash memory) buffer. Maybe 1-3 seconds tops depending on |
|---|
| 93 | sampling rate (e.g. 8000 Hz = 8 Kbytes/second). Max ~20K space avail on |
|---|
| 94 | Circuit Playground, lots more on Circuit Playground Express. |
|---|
| 95 | This function currently "blocks" -- it will not play sounds in the |
|---|
| 96 | background while other code runs. |
|---|
| 97 | @param data pointer to the audio data to play |
|---|
| 98 | @param len the length of the data in samples |
|---|
| 99 | @param sampleRate the sample rate of the data in samples per second |
|---|
| 100 | @param tenBit Optional flag if true 10-bit mode is enabled. AVR ONLY |
|---|
| 101 | */ |
|---|
| 102 | /**************************************************************************/ |
|---|
| 103 | void Adafruit_CPlay_Speaker::playSound( |
|---|
| 104 | const uint8_t *data, uint32_t len, uint16_t sampleRate, bool tenBit) { |
|---|
| 105 | |
|---|
| 106 | uint32_t i; |
|---|
| 107 | |
|---|
| 108 | if(!started) begin(); |
|---|
| 109 | |
|---|
| 110 | #ifdef __AVR__ |
|---|
| 111 | uint16_t interval = 1000000L / sampleRate; |
|---|
| 112 | #else |
|---|
| 113 | uint32_t r2 = sampleRate / 2; |
|---|
| 114 | uint32_t startTime = micros(); |
|---|
| 115 | #endif |
|---|
| 116 | |
|---|
| 117 | if(tenBit) { // 10-bit audio samples? |
|---|
| 118 | uint8_t loIdx = 4; |
|---|
| 119 | #ifdef __AVR__ |
|---|
| 120 | // Because it uses 8-bit PWM for output, the AVR code must filter |
|---|
| 121 | // 10-bit data down to 8 bits. This is ONLY here for compatibility |
|---|
| 122 | // with sketches with 10-bit samples. If targeting a project for AVR, |
|---|
| 123 | // it's best to produce and use optimized 8-bit audio, else it's just |
|---|
| 124 | // wasted space! Timer/Counter 4 DOES offer a 10-bit mode, but it's |
|---|
| 125 | // not used in this library, just not worth it in the limited flash |
|---|
| 126 | // space of the 32U4 chip. |
|---|
| 127 | uint16_t idx = 0; |
|---|
| 128 | uint8_t hiBits; |
|---|
| 129 | for(i=0; i<len; i++) { |
|---|
| 130 | if(++loIdx >= 4) { |
|---|
| 131 | hiBits = pgm_read_byte(&data[idx++]); |
|---|
| 132 | loIdx = 0; |
|---|
| 133 | } |
|---|
| 134 | OCR4A = ((hiBits & 0xC0) | (pgm_read_byte(&data[idx++]) >> 2)); |
|---|
| 135 | hiBits <<= 2; // Do this after write, because of masking op above |
|---|
| 136 | delayMicroseconds(interval); |
|---|
| 137 | } |
|---|
| 138 | OCR4A = 127; |
|---|
| 139 | #else |
|---|
| 140 | // Circuit Playground Express -- use 10-bit analogWrite() |
|---|
| 141 | uint32_t idx = 0; |
|---|
| 142 | uint16_t hiBits; |
|---|
| 143 | analogWriteResolution(10); |
|---|
| 144 | for(i=0; i<len; i++) { |
|---|
| 145 | if(++loIdx >= 4) { |
|---|
| 146 | hiBits = (uint16_t)pgm_read_byte(&data[idx++]); |
|---|
| 147 | loIdx = 0; |
|---|
| 148 | } |
|---|
| 149 | hiBits <<= 2; // Do this before write, because of masking op below |
|---|
| 150 | analogWrite(A0, (hiBits & 0x300) | pgm_read_byte(&data[idx++])); |
|---|
| 151 | while(((micros()-startTime+50)/100) < ((i*10000UL+r2)/sampleRate)); |
|---|
| 152 | } |
|---|
| 153 | analogWriteResolution(8); // Return to 8 bits for set() compatibility |
|---|
| 154 | analogWrite(A0, 127); |
|---|
| 155 | #endif |
|---|
| 156 | |
|---|
| 157 | } else { // 8-bit audio samples |
|---|
| 158 | |
|---|
| 159 | #ifdef __AVR__ |
|---|
| 160 | for(i=0; i<len; i++) { |
|---|
| 161 | OCR4A = pgm_read_byte(&data[i]); |
|---|
| 162 | delayMicroseconds(interval); |
|---|
| 163 | } |
|---|
| 164 | OCR4A = 127; |
|---|
| 165 | #else |
|---|
| 166 | for(i=0; i<len; i++) { |
|---|
| 167 | analogWrite(A0, pgm_read_byte(&data[i])); |
|---|
| 168 | while(((micros()-startTime+50)/100) < ((i*10000UL+r2)/sampleRate)); |
|---|
| 169 | } |
|---|
| 170 | analogWrite(A0, 127); |
|---|
| 171 | #endif |
|---|
| 172 | } |
|---|
| 173 | } |
|---|