PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
POKITTO_HW/HWSound.cpp@43:6183b12dd99c, 2018-05-01 (annotated)
- Committer:
- Pokitto
- Date:
- Tue May 01 18:42:56 2018 +0000
- Revision:
- 43:6183b12dd99c
New volume control;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 43:6183b12dd99c | 1 | /**************************************************************************/ |
Pokitto | 43:6183b12dd99c | 2 | /*! |
Pokitto | 43:6183b12dd99c | 3 | @file HWSound.cpp |
Pokitto | 43:6183b12dd99c | 4 | @author Jonne Valola |
Pokitto | 43:6183b12dd99c | 5 | |
Pokitto | 43:6183b12dd99c | 6 | @section LICENSE |
Pokitto | 43:6183b12dd99c | 7 | |
Pokitto | 43:6183b12dd99c | 8 | Software License Agreement (BSD License) |
Pokitto | 43:6183b12dd99c | 9 | |
Pokitto | 43:6183b12dd99c | 10 | Copyright (c) 2016, Jonne Valola |
Pokitto | 43:6183b12dd99c | 11 | All rights reserved. |
Pokitto | 43:6183b12dd99c | 12 | |
Pokitto | 43:6183b12dd99c | 13 | Redistribution and use in source and binary forms, with or without |
Pokitto | 43:6183b12dd99c | 14 | modification, are permitted provided that the following conditions are met: |
Pokitto | 43:6183b12dd99c | 15 | 1. Redistributions of source code must retain the above copyright |
Pokitto | 43:6183b12dd99c | 16 | notice, this list of conditions and the following disclaimer. |
Pokitto | 43:6183b12dd99c | 17 | 2. Redistributions in binary form must reproduce the above copyright |
Pokitto | 43:6183b12dd99c | 18 | notice, this list of conditions and the following disclaimer in the |
Pokitto | 43:6183b12dd99c | 19 | documentation and/or other materials provided with the distribution. |
Pokitto | 43:6183b12dd99c | 20 | 3. Neither the name of the copyright holders nor the |
Pokitto | 43:6183b12dd99c | 21 | names of its contributors may be used to endorse or promote products |
Pokitto | 43:6183b12dd99c | 22 | derived from this software without specific prior written permission. |
Pokitto | 43:6183b12dd99c | 23 | |
Pokitto | 43:6183b12dd99c | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
Pokitto | 43:6183b12dd99c | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Pokitto | 43:6183b12dd99c | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Pokitto | 43:6183b12dd99c | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
Pokitto | 43:6183b12dd99c | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Pokitto | 43:6183b12dd99c | 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Pokitto | 43:6183b12dd99c | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
Pokitto | 43:6183b12dd99c | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Pokitto | 43:6183b12dd99c | 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Pokitto | 43:6183b12dd99c | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Pokitto | 43:6183b12dd99c | 34 | */ |
Pokitto | 43:6183b12dd99c | 35 | /**************************************************************************/ |
Pokitto | 43:6183b12dd99c | 36 | |
Pokitto | 43:6183b12dd99c | 37 | #include "mbed.h" |
Pokitto | 43:6183b12dd99c | 38 | #include "HWSound.h" |
Pokitto | 43:6183b12dd99c | 39 | //#include "MCP4018.h" |
Pokitto | 43:6183b12dd99c | 40 | #include "SoftwareI2C.h" |
Pokitto | 43:6183b12dd99c | 41 | #include "Pokitto_settings.h" |
Pokitto | 43:6183b12dd99c | 42 | #include "PokittoDisk.h" |
Pokitto | 43:6183b12dd99c | 43 | #include "PokittoGlobs.h" |
Pokitto | 43:6183b12dd99c | 44 | #include "Synth.h" |
Pokitto | 43:6183b12dd99c | 45 | #include "timer_11u6x.h" |
Pokitto | 43:6183b12dd99c | 46 | #include "clock_11u6x.h" |
Pokitto | 43:6183b12dd99c | 47 | #include "HWLCD.h" |
Pokitto | 43:6183b12dd99c | 48 | //#include "beat_11025.h" |
Pokitto | 43:6183b12dd99c | 49 | |
Pokitto | 43:6183b12dd99c | 50 | |
Pokitto | 43:6183b12dd99c | 51 | Pokitto::Sound __shw; |
Pokitto | 43:6183b12dd99c | 52 | |
Pokitto | 43:6183b12dd99c | 53 | using namespace Pokitto; |
Pokitto | 43:6183b12dd99c | 54 | |
Pokitto | 43:6183b12dd99c | 55 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 56 | #if POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 57 | pwmout_t* obj = &audiopwm; |
Pokitto | 43:6183b12dd99c | 58 | #endif |
Pokitto | 43:6183b12dd99c | 59 | #endif |
Pokitto | 43:6183b12dd99c | 60 | |
Pokitto | 43:6183b12dd99c | 61 | /** Sound Variables **/ |
Pokitto | 43:6183b12dd99c | 62 | #if (POK_STREAMING_MUSIC > 0) |
Pokitto | 43:6183b12dd99c | 63 | unsigned char buffers[4][BUFFER_SIZE]; |
Pokitto | 43:6183b12dd99c | 64 | volatile int currentBuffer = 0, oldBuffer = 0; |
Pokitto | 43:6183b12dd99c | 65 | volatile int bufindex = 0, vol=1; |
Pokitto | 43:6183b12dd99c | 66 | volatile unsigned char * currentPtr; |
Pokitto | 43:6183b12dd99c | 67 | volatile unsigned char * endPtr; |
Pokitto | 43:6183b12dd99c | 68 | int8_t streamvol=3; |
Pokitto | 43:6183b12dd99c | 69 | uint32_t streamcounter=0; |
Pokitto | 43:6183b12dd99c | 70 | uint8_t streamstep=0; |
Pokitto | 43:6183b12dd99c | 71 | #endif |
Pokitto | 43:6183b12dd99c | 72 | |
Pokitto | 43:6183b12dd99c | 73 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 74 | pwmout_t Pokitto::audiopwm; // this way (instead of PwmOut class) pwm doesn't start screaming until it is initialized ! |
Pokitto | 43:6183b12dd99c | 75 | //Ticker Pokitto::audio; |
Pokitto | 43:6183b12dd99c | 76 | |
Pokitto | 43:6183b12dd99c | 77 | using namespace Pokitto; |
Pokitto | 43:6183b12dd99c | 78 | |
Pokitto | 43:6183b12dd99c | 79 | |
Pokitto | 43:6183b12dd99c | 80 | /** stream output and status */ |
Pokitto | 43:6183b12dd99c | 81 | uint8_t Pokitto::streambyte,Pokitto::streamon; |
Pokitto | 43:6183b12dd99c | 82 | |
Pokitto | 43:6183b12dd99c | 83 | uint8_t soundbuf[SBUFSIZE]; |
Pokitto | 43:6183b12dd99c | 84 | uint8_t Pokitto::HWvolume=0; |
Pokitto | 43:6183b12dd99c | 85 | uint16_t soundbufindex; |
Pokitto | 43:6183b12dd99c | 86 | uint8_t* soundbufptr; |
Pokitto | 43:6183b12dd99c | 87 | |
Pokitto | 43:6183b12dd99c | 88 | bool volpotError=false; //test for broken MCP4018 |
Pokitto | 43:6183b12dd99c | 89 | |
Pokitto | 43:6183b12dd99c | 90 | uint16_t soundbyte; |
Pokitto | 43:6183b12dd99c | 91 | |
Pokitto | 43:6183b12dd99c | 92 | |
Pokitto | 43:6183b12dd99c | 93 | #if POK_USE_DAC > 0 |
Pokitto | 43:6183b12dd99c | 94 | #if POK_BOARDREV == 1 |
Pokitto | 43:6183b12dd99c | 95 | /** 2-layer board rev 1.3 **/ |
Pokitto | 43:6183b12dd99c | 96 | DigitalOut dac0(P1_6); |
Pokitto | 43:6183b12dd99c | 97 | DigitalOut dac1(P1_0); |
Pokitto | 43:6183b12dd99c | 98 | DigitalOut dac2(P1_16); |
Pokitto | 43:6183b12dd99c | 99 | DigitalOut dac3(P0_19); |
Pokitto | 43:6183b12dd99c | 100 | DigitalOut dac4(P0_17); |
Pokitto | 43:6183b12dd99c | 101 | DigitalOut dac5(P1_12); |
Pokitto | 43:6183b12dd99c | 102 | DigitalOut dac6(P1_15); |
Pokitto | 43:6183b12dd99c | 103 | DigitalOut dac7(P1_8); |
Pokitto | 43:6183b12dd99c | 104 | #else |
Pokitto | 43:6183b12dd99c | 105 | /** 4-layer board rev 2.1 **/ |
Pokitto | 43:6183b12dd99c | 106 | DigitalOut dac0(P1_28); |
Pokitto | 43:6183b12dd99c | 107 | DigitalOut dac1(P1_29); |
Pokitto | 43:6183b12dd99c | 108 | DigitalOut dac2(P1_30); |
Pokitto | 43:6183b12dd99c | 109 | DigitalOut dac3(P1_31); |
Pokitto | 43:6183b12dd99c | 110 | /* has daniel made a mistake ?*/ |
Pokitto | 43:6183b12dd99c | 111 | DigitalOut dac4(P2_20); |
Pokitto | 43:6183b12dd99c | 112 | DigitalOut dac5(P2_21); |
Pokitto | 43:6183b12dd99c | 113 | DigitalOut dac6(P2_22); |
Pokitto | 43:6183b12dd99c | 114 | DigitalOut dac7(P2_23); |
Pokitto | 43:6183b12dd99c | 115 | |
Pokitto | 43:6183b12dd99c | 116 | DigitalOut amp(P1_17); |
Pokitto | 43:6183b12dd99c | 117 | |
Pokitto | 43:6183b12dd99c | 118 | #endif // POK_BOARDREV |
Pokitto | 43:6183b12dd99c | 119 | #endif // POK_USE_DAC |
Pokitto | 43:6183b12dd99c | 120 | |
Pokitto | 43:6183b12dd99c | 121 | #if POK_BOARDREV == 2 |
Pokitto | 43:6183b12dd99c | 122 | //MCP4018 volpot(P0_5,P0_4); |
Pokitto | 43:6183b12dd99c | 123 | |
Pokitto | 43:6183b12dd99c | 124 | /** |
Pokitto | 43:6183b12dd99c | 125 | * @brief Handle interrupt from 32-bit timer 0 |
Pokitto | 43:6183b12dd99c | 126 | * @return Nothing |
Pokitto | 43:6183b12dd99c | 127 | */ |
Pokitto | 43:6183b12dd99c | 128 | |
Pokitto | 43:6183b12dd99c | 129 | uint32_t p=0; |
Pokitto | 43:6183b12dd99c | 130 | uint8_t pixx=0, pixy=0; |
Pokitto | 43:6183b12dd99c | 131 | |
Pokitto | 43:6183b12dd99c | 132 | extern "C" void TIMER32_0_IRQHandler(void) |
Pokitto | 43:6183b12dd99c | 133 | { |
Pokitto | 43:6183b12dd99c | 134 | if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) { |
Pokitto | 43:6183b12dd99c | 135 | Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1); |
Pokitto | 43:6183b12dd99c | 136 | //pokSoundBufferedIRQ(); |
Pokitto | 43:6183b12dd99c | 137 | #if POK_GBSOUND > 0 |
Pokitto | 43:6183b12dd99c | 138 | /** GAMEBUINO SOUND **/ |
Pokitto | 43:6183b12dd99c | 139 | Pokitto::audio_IRQ(); |
Pokitto | 43:6183b12dd99c | 140 | #else |
Pokitto | 43:6183b12dd99c | 141 | /** NOT GAMEBUINO SOUND **/ |
Pokitto | 43:6183b12dd99c | 142 | #if POK_SOUND_BUFFERED |
Pokitto | 43:6183b12dd99c | 143 | pokSoundBufferedIRQ(); |
Pokitto | 43:6183b12dd99c | 144 | #else |
Pokitto | 43:6183b12dd99c | 145 | pokSoundIRQ(); |
Pokitto | 43:6183b12dd99c | 146 | #endif |
Pokitto | 43:6183b12dd99c | 147 | #endif |
Pokitto | 43:6183b12dd99c | 148 | } |
Pokitto | 43:6183b12dd99c | 149 | } |
Pokitto | 43:6183b12dd99c | 150 | |
Pokitto | 43:6183b12dd99c | 151 | |
Pokitto | 43:6183b12dd99c | 152 | void initHWvolumecontrol() { |
Pokitto | 43:6183b12dd99c | 153 | HWvolume=0; |
Pokitto | 43:6183b12dd99c | 154 | volpotError=true; |
Pokitto | 43:6183b12dd99c | 155 | //if (volpot.put(HWvolume)) volpotError=true; //try if MCP4018 answers |
Pokitto | 43:6183b12dd99c | 156 | setHWvolume(discrete_vol_hw_levels[VOLUME_STARTUP>>5]); |
Pokitto | 43:6183b12dd99c | 157 | } |
Pokitto | 43:6183b12dd99c | 158 | |
Pokitto | 43:6183b12dd99c | 159 | int Pokitto::setHWvolume(uint8_t v) { |
Pokitto | 43:6183b12dd99c | 160 | HWvolume=v; |
Pokitto | 43:6183b12dd99c | 161 | if (HWvolume>127) HWvolume=127; |
Pokitto | 43:6183b12dd99c | 162 | //HWvolume = 0x7F&v; |
Pokitto | 43:6183b12dd99c | 163 | //if (!volpotError) return volpot.put(HWvolume); //use normal I2C |
Pokitto | 43:6183b12dd99c | 164 | /* fallback method for broken MCP4018 */ |
Pokitto | 43:6183b12dd99c | 165 | SoftwareI2C swvolpot(P0_4, P0_5); //swapped SDA,SCL |
Pokitto | 43:6183b12dd99c | 166 | swvolpot.write(0x5e,HWvolume); |
Pokitto | 43:6183b12dd99c | 167 | return HWvolume; |
Pokitto | 43:6183b12dd99c | 168 | } |
Pokitto | 43:6183b12dd99c | 169 | |
Pokitto | 43:6183b12dd99c | 170 | uint8_t Pokitto::getHWvolume() { |
Pokitto | 43:6183b12dd99c | 171 | return HWvolume; |
Pokitto | 43:6183b12dd99c | 172 | } |
Pokitto | 43:6183b12dd99c | 173 | |
Pokitto | 43:6183b12dd99c | 174 | void Pokitto::changeHWvolume(int8_t v) { |
Pokitto | 43:6183b12dd99c | 175 | int temp = HWvolume + v; |
Pokitto | 43:6183b12dd99c | 176 | if (temp < 0) temp = 0; //prevent volume "looparound" than can make a massive crack |
Pokitto | 43:6183b12dd99c | 177 | if (temp > 127) temp = 127; |
Pokitto | 43:6183b12dd99c | 178 | setHWvolume(temp); |
Pokitto | 43:6183b12dd99c | 179 | } |
Pokitto | 43:6183b12dd99c | 180 | |
Pokitto | 43:6183b12dd99c | 181 | uint8_t Pokitto::ampIsOn() { |
Pokitto | 43:6183b12dd99c | 182 | return amp; |
Pokitto | 43:6183b12dd99c | 183 | } |
Pokitto | 43:6183b12dd99c | 184 | |
Pokitto | 43:6183b12dd99c | 185 | void Pokitto::ampEnable(uint8_t v) { |
Pokitto | 43:6183b12dd99c | 186 | if (v>1) v=1; // limit against funny values |
Pokitto | 43:6183b12dd99c | 187 | amp=v; |
Pokitto | 43:6183b12dd99c | 188 | } |
Pokitto | 43:6183b12dd99c | 189 | #endif // POK_BOARDREV == 2 |
Pokitto | 43:6183b12dd99c | 190 | |
Pokitto | 43:6183b12dd99c | 191 | void Pokitto::dac_write(uint8_t value) { |
Pokitto | 43:6183b12dd99c | 192 | #if POK_USE_DAC > 0 |
Pokitto | 43:6183b12dd99c | 193 | #if POK_BOARDREV == 1 // was 1 |
Pokitto | 43:6183b12dd99c | 194 | if (value & 1) SET_DAC0 else CLR_DAC0; |
Pokitto | 43:6183b12dd99c | 195 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 196 | if (value & 1) SET_DAC1 else CLR_DAC1; |
Pokitto | 43:6183b12dd99c | 197 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 198 | if (value & 1) SET_DAC2 else CLR_DAC2; |
Pokitto | 43:6183b12dd99c | 199 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 200 | if (value & 1) SET_DAC3 else CLR_DAC3; |
Pokitto | 43:6183b12dd99c | 201 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 202 | if (value & 1) SET_DAC4 else CLR_DAC4; |
Pokitto | 43:6183b12dd99c | 203 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 204 | if (value & 1) SET_DAC5 else CLR_DAC5; |
Pokitto | 43:6183b12dd99c | 205 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 206 | if (value & 1) SET_DAC6 else CLR_DAC6; |
Pokitto | 43:6183b12dd99c | 207 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 208 | if (value & 1) SET_DAC7 else CLR_DAC7; |
Pokitto | 43:6183b12dd99c | 209 | #else |
Pokitto | 43:6183b12dd99c | 210 | |
Pokitto | 43:6183b12dd99c | 211 | //val = value<<28; //lower 4 bits go higher - because port mask is used, no AND is needed to clear bits |
Pokitto | 43:6183b12dd99c | 212 | //val += value<<(15-4); //higher 4 bits go lower. No need to shift by 15 because bits are in the higher nibble |
Pokitto | 43:6183b12dd99c | 213 | /* daniel has made a mistake with ports */ |
Pokitto | 43:6183b12dd99c | 214 | //val = ((value&0x70)<<(28-4)); //higher 4 bits go higher - because port mask is used, no AND is needed to clear bits |
Pokitto | 43:6183b12dd99c | 215 | //val += value<<(15); //lower 4 bits go lower. No need to shift by 15 because bits are in the higher nibble |
Pokitto | 43:6183b12dd99c | 216 | //SET_MASK_DAC; |
Pokitto | 43:6183b12dd99c | 217 | //LPC_GPIO_PORT->MPIN[1] = val; // write bits to port |
Pokitto | 43:6183b12dd99c | 218 | //CLR_MASK_DAC; |
Pokitto | 43:6183b12dd99c | 219 | /* fixed here */ |
Pokitto | 43:6183b12dd99c | 220 | #define MASKED_DAC 0 |
Pokitto | 43:6183b12dd99c | 221 | #if MASKED_DAC |
Pokitto | 43:6183b12dd99c | 222 | uint32_t val; |
Pokitto | 43:6183b12dd99c | 223 | val=value; |
Pokitto | 43:6183b12dd99c | 224 | SET_MASK_DAC_LO; |
Pokitto | 43:6183b12dd99c | 225 | LPC_GPIO_PORT->MPIN[1] = val<<28; // write lower 4 bits to port |
Pokitto | 43:6183b12dd99c | 226 | CLR_MASK_DAC_LO; |
Pokitto | 43:6183b12dd99c | 227 | SET_MASK_DAC_HI; |
Pokitto | 43:6183b12dd99c | 228 | LPC_GPIO_PORT->MPIN[2] = val<<(20-4); // write bits to port |
Pokitto | 43:6183b12dd99c | 229 | CLR_MASK_DAC_HI; |
Pokitto | 43:6183b12dd99c | 230 | #else |
Pokitto | 43:6183b12dd99c | 231 | if (value & 1) SET_DAC0 else CLR_DAC0; |
Pokitto | 43:6183b12dd99c | 232 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 233 | if (value & 1) SET_DAC1 else CLR_DAC1; |
Pokitto | 43:6183b12dd99c | 234 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 235 | if (value & 1) SET_DAC2 else CLR_DAC2; |
Pokitto | 43:6183b12dd99c | 236 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 237 | if (value & 1) SET_DAC3 else CLR_DAC3; |
Pokitto | 43:6183b12dd99c | 238 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 239 | if (value & 1) SET_DAC4 else CLR_DAC4; |
Pokitto | 43:6183b12dd99c | 240 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 241 | if (value & 1) SET_DAC5 else CLR_DAC5; |
Pokitto | 43:6183b12dd99c | 242 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 243 | if (value & 1) SET_DAC6 else CLR_DAC6; |
Pokitto | 43:6183b12dd99c | 244 | value >>= 1; |
Pokitto | 43:6183b12dd99c | 245 | if (value & 1) SET_DAC7 else CLR_DAC7; |
Pokitto | 43:6183b12dd99c | 246 | #endif //MASKED_DAC |
Pokitto | 43:6183b12dd99c | 247 | //CLR_MASK_DAC; |
Pokitto | 43:6183b12dd99c | 248 | #endif // BOARDREV |
Pokitto | 43:6183b12dd99c | 249 | #endif |
Pokitto | 43:6183b12dd99c | 250 | } |
Pokitto | 43:6183b12dd99c | 251 | |
Pokitto | 43:6183b12dd99c | 252 | /** SOUND INIT **/ |
Pokitto | 43:6183b12dd99c | 253 | void Pokitto::soundInit() { |
Pokitto | 43:6183b12dd99c | 254 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 255 | uint32_t timerFreq; |
Pokitto | 43:6183b12dd99c | 256 | #if POK_USE_PWM |
Pokitto | 43:6183b12dd99c | 257 | |
Pokitto | 43:6183b12dd99c | 258 | pwmout_init(&audiopwm,POK_AUD_PIN); |
Pokitto | 43:6183b12dd99c | 259 | pwmout_period_us(&audiopwm,POK_AUD_PWM_US); //was 31us |
Pokitto | 43:6183b12dd99c | 260 | pwmout_write(&audiopwm,0.1f); |
Pokitto | 43:6183b12dd99c | 261 | #endif |
Pokitto | 43:6183b12dd99c | 262 | |
Pokitto | 43:6183b12dd99c | 263 | //#if POK_GBSOUND > 0 |
Pokitto | 43:6183b12dd99c | 264 | /** GAMEBUINO SOUND **/ |
Pokitto | 43:6183b12dd99c | 265 | //audio.attach_us(&audio_IRQ, 1000000/(POK_AUD_FREQ>>0)); |
Pokitto | 43:6183b12dd99c | 266 | //#else |
Pokitto | 43:6183b12dd99c | 267 | /** NOT GAMEBUINO SOUND **/ |
Pokitto | 43:6183b12dd99c | 268 | //audio.attach_us(&pokSoundBufferedIRQ, 1000000/(POK_AUD_FREQ>>0)); |
Pokitto | 43:6183b12dd99c | 269 | /* Initialize 32-bit timer 0 clock */ |
Pokitto | 43:6183b12dd99c | 270 | Chip_TIMER_Init(LPC_TIMER32_0); |
Pokitto | 43:6183b12dd99c | 271 | |
Pokitto | 43:6183b12dd99c | 272 | /* Timer rate is system clock rate */ |
Pokitto | 43:6183b12dd99c | 273 | timerFreq = Chip_Clock_GetSystemClockRate(); |
Pokitto | 43:6183b12dd99c | 274 | |
Pokitto | 43:6183b12dd99c | 275 | /* Timer setup for match and interrupt at TICKRATE_HZ */ |
Pokitto | 43:6183b12dd99c | 276 | Chip_TIMER_Reset(LPC_TIMER32_0); |
Pokitto | 43:6183b12dd99c | 277 | |
Pokitto | 43:6183b12dd99c | 278 | /* Enable both timers to generate interrupts when time matches */ |
Pokitto | 43:6183b12dd99c | 279 | Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1); |
Pokitto | 43:6183b12dd99c | 280 | |
Pokitto | 43:6183b12dd99c | 281 | /* Setup 32-bit timer's duration (32-bit match time) */ |
Pokitto | 43:6183b12dd99c | 282 | Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (timerFreq / POK_AUD_FREQ)); |
Pokitto | 43:6183b12dd99c | 283 | |
Pokitto | 43:6183b12dd99c | 284 | /* Setup both timers to restart when match occurs */ |
Pokitto | 43:6183b12dd99c | 285 | Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1); |
Pokitto | 43:6183b12dd99c | 286 | |
Pokitto | 43:6183b12dd99c | 287 | /* Start both timers */ |
Pokitto | 43:6183b12dd99c | 288 | Chip_TIMER_Enable(LPC_TIMER32_0); |
Pokitto | 43:6183b12dd99c | 289 | |
Pokitto | 43:6183b12dd99c | 290 | /* Clear both timers of any pending interrupts */ |
Pokitto | 43:6183b12dd99c | 291 | #define TIMER_32_0_IRQn 18 |
Pokitto | 43:6183b12dd99c | 292 | NVIC_ClearPendingIRQ((IRQn_Type)TIMER_32_0_IRQn); |
Pokitto | 43:6183b12dd99c | 293 | |
Pokitto | 43:6183b12dd99c | 294 | /* Redirect IRQ vector - Jonne*/ |
Pokitto | 43:6183b12dd99c | 295 | NVIC_SetVector((IRQn_Type)TIMER_32_0_IRQn, (uint32_t)&TIMER32_0_IRQHandler); |
Pokitto | 43:6183b12dd99c | 296 | |
Pokitto | 43:6183b12dd99c | 297 | /* Enable both timer interrupts */ |
Pokitto | 43:6183b12dd99c | 298 | NVIC_EnableIRQ((IRQn_Type)TIMER_32_0_IRQn); |
Pokitto | 43:6183b12dd99c | 299 | //#endif // POK_GAMEBUINO_SUPPORT |
Pokitto | 43:6183b12dd99c | 300 | |
Pokitto | 43:6183b12dd99c | 301 | //emptySong(); |
Pokitto | 43:6183b12dd99c | 302 | //emptyOscillators(); |
Pokitto | 43:6183b12dd99c | 303 | //emptyBlocks(); |
Pokitto | 43:6183b12dd99c | 304 | //emptyPatches(); |
Pokitto | 43:6183b12dd99c | 305 | #ifdef TEST_SOUND |
Pokitto | 43:6183b12dd99c | 306 | testOsc(); |
Pokitto | 43:6183b12dd99c | 307 | #endif // TEST_SOUND |
Pokitto | 43:6183b12dd99c | 308 | #if POK_BOARDREV == 2 |
Pokitto | 43:6183b12dd99c | 309 | //initHWvolumecontrol(); |
Pokitto | 43:6183b12dd99c | 310 | #endif |
Pokitto | 43:6183b12dd99c | 311 | |
Pokitto | 43:6183b12dd99c | 312 | #if POK_ENABLE_SYNTH |
Pokitto | 43:6183b12dd99c | 313 | emptyOscillators(); |
Pokitto | 43:6183b12dd99c | 314 | #endif |
Pokitto | 43:6183b12dd99c | 315 | |
Pokitto | 43:6183b12dd99c | 316 | |
Pokitto | 43:6183b12dd99c | 317 | #endif // POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 318 | |
Pokitto | 43:6183b12dd99c | 319 | } |
Pokitto | 43:6183b12dd99c | 320 | |
Pokitto | 43:6183b12dd99c | 321 | |
Pokitto | 43:6183b12dd99c | 322 | uint8_t Pokitto::streamPaused() { |
Pokitto | 43:6183b12dd99c | 323 | return !streamon; |
Pokitto | 43:6183b12dd99c | 324 | } |
Pokitto | 43:6183b12dd99c | 325 | |
Pokitto | 43:6183b12dd99c | 326 | void Pokitto::pauseStream() { |
Pokitto | 43:6183b12dd99c | 327 | streamon=0; |
Pokitto | 43:6183b12dd99c | 328 | } |
Pokitto | 43:6183b12dd99c | 329 | |
Pokitto | 43:6183b12dd99c | 330 | void Pokitto::playStream() { |
Pokitto | 43:6183b12dd99c | 331 | streamon=1; |
Pokitto | 43:6183b12dd99c | 332 | } |
Pokitto | 43:6183b12dd99c | 333 | |
Pokitto | 43:6183b12dd99c | 334 | |
Pokitto | 43:6183b12dd99c | 335 | void pokPauseStream() { |
Pokitto | 43:6183b12dd99c | 336 | streamon=0; |
Pokitto | 43:6183b12dd99c | 337 | } |
Pokitto | 43:6183b12dd99c | 338 | |
Pokitto | 43:6183b12dd99c | 339 | void pokPlayStream() { |
Pokitto | 43:6183b12dd99c | 340 | streamon=1; |
Pokitto | 43:6183b12dd99c | 341 | } |
Pokitto | 43:6183b12dd99c | 342 | |
Pokitto | 43:6183b12dd99c | 343 | |
Pokitto | 43:6183b12dd99c | 344 | |
Pokitto | 43:6183b12dd99c | 345 | inline void pokSoundBufferedIRQ() { |
Pokitto | 43:6183b12dd99c | 346 | uint32_t output = soundbuf[soundbufindex+=Pokitto::streamon]; |
Pokitto | 43:6183b12dd99c | 347 | if (soundbufindex==SBUFSIZE) soundbufindex=0; |
Pokitto | 43:6183b12dd99c | 348 | //if (p==sizeof(beat_11025_raw)) p=0; |
Pokitto | 43:6183b12dd99c | 349 | //soundbuf[soundbufindex++] = output; |
Pokitto | 43:6183b12dd99c | 350 | //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float |
Pokitto | 43:6183b12dd99c | 351 | //obj->pwm->MATCHREL1 = t_on; |
Pokitto | 43:6183b12dd99c | 352 | output *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 353 | output >>= 8; |
Pokitto | 43:6183b12dd99c | 354 | dac_write(output); |
Pokitto | 43:6183b12dd99c | 355 | |
Pokitto | 43:6183b12dd99c | 356 | //setDRAMpoint(pixx, pixy); |
Pokitto | 43:6183b12dd99c | 357 | } |
Pokitto | 43:6183b12dd99c | 358 | |
Pokitto | 43:6183b12dd99c | 359 | inline void pokSoundIRQ() { |
Pokitto | 43:6183b12dd99c | 360 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 361 | //#define TICKY 0xFFFF //160 |
Pokitto | 43:6183b12dd99c | 362 | //#define INCY 409 |
Pokitto | 43:6183b12dd99c | 363 | uint32_t output=0;uint32_t op; |
Pokitto | 43:6183b12dd99c | 364 | streamon=1; |
Pokitto | 43:6183b12dd99c | 365 | //if (test==TICKY) test=0; |
Pokitto | 43:6183b12dd99c | 366 | //if (test<(TICKY/2)) { tpin=1; pwmout_write(&audiopwm,(float)0/(float)255);}//dac_write(0);} |
Pokitto | 43:6183b12dd99c | 367 | //else {tpin=0; pwmout_write(&audiopwm,(float)255/(float)255);}//dac_write(64);} |
Pokitto | 43:6183b12dd99c | 368 | //test+=INCY; |
Pokitto | 43:6183b12dd99c | 369 | //return; |
Pokitto | 43:6183b12dd99c | 370 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 371 | #if POK_USE_PWM |
Pokitto | 43:6183b12dd99c | 372 | pwmout_t* obj = &audiopwm; |
Pokitto | 43:6183b12dd99c | 373 | #endif |
Pokitto | 43:6183b12dd99c | 374 | #endif |
Pokitto | 43:6183b12dd99c | 375 | #if POK_STREAMING_MUSIC > 0 |
Pokitto | 43:6183b12dd99c | 376 | #if POK_STREAMFREQ_HALVE |
Pokitto | 43:6183b12dd99c | 377 | streamstep = 1-streamstep; |
Pokitto | 43:6183b12dd99c | 378 | #else |
Pokitto | 43:6183b12dd99c | 379 | streamstep = 1; |
Pokitto | 43:6183b12dd99c | 380 | #endif // POK_STREAMFREQ_HALVE |
Pokitto | 43:6183b12dd99c | 381 | streamstep &= streamon; // streamon is used to toggle SD music streaming on and off |
Pokitto | 43:6183b12dd99c | 382 | if (streamstep) { |
Pokitto | 43:6183b12dd99c | 383 | output = (*currentPtr++); |
Pokitto | 43:6183b12dd99c | 384 | if(streamvol && streamon) { |
Pokitto | 43:6183b12dd99c | 385 | output >>= 3-streamvol; |
Pokitto | 43:6183b12dd99c | 386 | streambyte = output; |
Pokitto | 43:6183b12dd99c | 387 | } else { |
Pokitto | 43:6183b12dd99c | 388 | streambyte = 0; // duty cycle |
Pokitto | 43:6183b12dd99c | 389 | output = 0; |
Pokitto | 43:6183b12dd99c | 390 | } |
Pokitto | 43:6183b12dd99c | 391 | if (currentPtr >= endPtr) |
Pokitto | 43:6183b12dd99c | 392 | { |
Pokitto | 43:6183b12dd99c | 393 | currentBuffer++; |
Pokitto | 43:6183b12dd99c | 394 | if (currentBuffer==4) currentBuffer=0; |
Pokitto | 43:6183b12dd99c | 395 | currentPtr = buffers[currentBuffer]; |
Pokitto | 43:6183b12dd99c | 396 | endPtr = currentPtr + BUFFER_SIZE; |
Pokitto | 43:6183b12dd99c | 397 | } |
Pokitto | 43:6183b12dd99c | 398 | } |
Pokitto | 43:6183b12dd99c | 399 | #endif // POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 400 | |
Pokitto | 43:6183b12dd99c | 401 | /** DO ADDITIONAL SOUND PROCESSING (NOT STREAM) OF SOUND HERE **/ |
Pokitto | 43:6183b12dd99c | 402 | |
Pokitto | 43:6183b12dd99c | 403 | #if POK_ENABLE_SYNTH |
Pokitto | 43:6183b12dd99c | 404 | /** if song is being played from sd **/ |
Pokitto | 43:6183b12dd99c | 405 | if (playing) { |
Pokitto | 43:6183b12dd99c | 406 | notetick++; |
Pokitto | 43:6183b12dd99c | 407 | updatePlayback(); |
Pokitto | 43:6183b12dd99c | 408 | } |
Pokitto | 43:6183b12dd99c | 409 | /** oscillators update **/ |
Pokitto | 43:6183b12dd99c | 410 | osc1.count += osc1.cinc + (osc1.pitchbend >> 4); // counts to 65535 and overflows to zero WAS 8 ! |
Pokitto | 43:6183b12dd99c | 411 | osc2.count += osc2.cinc + (osc2.pitchbend >> 4); // counts to 65535 and overflows to zero |
Pokitto | 43:6183b12dd99c | 412 | osc3.count += osc3.cinc + (osc3.pitchbend >> 4); // counts to 65535 and overflows to zero |
Pokitto | 43:6183b12dd99c | 413 | Marr[tick](); // call mixing function |
Pokitto | 43:6183b12dd99c | 414 | --tick; |
Pokitto | 43:6183b12dd99c | 415 | |
Pokitto | 43:6183b12dd99c | 416 | /** mixing oscillator output **/ |
Pokitto | 43:6183b12dd99c | 417 | |
Pokitto | 43:6183b12dd99c | 418 | op = (uint32_t) ((osc1.output)*(osc1.vol>>8))>>9;// >> 2 osc1.vol Marr; |
Pokitto | 43:6183b12dd99c | 419 | op += (uint32_t) ((osc2.output)*(osc2.vol>>8))>>9;// >> 2 osc1.vol Marr; |
Pokitto | 43:6183b12dd99c | 420 | op += (uint32_t) ((osc3.output)*(osc3.vol>>8))>>9;// >> 2 osc1.vol Marr; |
Pokitto | 43:6183b12dd99c | 421 | op *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 422 | op >>= 8; |
Pokitto | 43:6183b12dd99c | 423 | output = op & 0xFF; |
Pokitto | 43:6183b12dd99c | 424 | |
Pokitto | 43:6183b12dd99c | 425 | #endif // POK_ENABLE_SYNTH |
Pokitto | 43:6183b12dd99c | 426 | |
Pokitto | 43:6183b12dd99c | 427 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 428 | /** HARDWARE **/ |
Pokitto | 43:6183b12dd99c | 429 | #if POK_ENABLE_SOUND > 0 |
Pokitto | 43:6183b12dd99c | 430 | #if POK_STREAMING_MUSIC > 0 |
Pokitto | 43:6183b12dd99c | 431 | /** sound is enabled, streaming is enabled */ |
Pokitto | 43:6183b12dd99c | 432 | #if POK_STREAM_TO_DAC > 0 |
Pokitto | 43:6183b12dd99c | 433 | /** stream goes to DAC */ |
Pokitto | 43:6183b12dd99c | 434 | #if POK_USE_DAC > 0 |
Pokitto | 43:6183b12dd99c | 435 | uint32_t sbyte = streambyte; |
Pokitto | 43:6183b12dd99c | 436 | sbyte *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 437 | sbyte >>= 8; |
Pokitto | 43:6183b12dd99c | 438 | dac_write((uint8_t)sbyte); // duty cycle |
Pokitto | 43:6183b12dd99c | 439 | #endif // POK_USE_DAC |
Pokitto | 43:6183b12dd99c | 440 | #else |
Pokitto | 43:6183b12dd99c | 441 | /** stream goes to PWM */ |
Pokitto | 43:6183b12dd99c | 442 | if (streamstep) { |
Pokitto | 43:6183b12dd99c | 443 | //pwmout_write(&audiopwm,(float)streambyte/(float)255); |
Pokitto | 43:6183b12dd99c | 444 | #if POK_USE_PWM |
Pokitto | 43:6183b12dd99c | 445 | uint32_t sbyte = streambyte; |
Pokitto | 43:6183b12dd99c | 446 | sbyte *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 447 | sbyte >>= 8; |
Pokitto | 43:6183b12dd99c | 448 | uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*sbyte)>>8)); //cut out float |
Pokitto | 43:6183b12dd99c | 449 | obj->pwm->MATCHREL1 = t_on; |
Pokitto | 43:6183b12dd99c | 450 | #endif |
Pokitto | 43:6183b12dd99c | 451 | //dac_write((uint8_t)streambyte); // duty cycle |
Pokitto | 43:6183b12dd99c | 452 | } |
Pokitto | 43:6183b12dd99c | 453 | #endif // POK_STREAM_TO_DAC |
Pokitto | 43:6183b12dd99c | 454 | #endif // POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 455 | #if POK_STREAM_TO_DAC > 0 |
Pokitto | 43:6183b12dd99c | 456 | /** synth goes to PWM */ |
Pokitto | 43:6183b12dd99c | 457 | //pwmout_write(&audiopwm,(float)output/(float)255); |
Pokitto | 43:6183b12dd99c | 458 | #if POK_USE_PWM |
Pokitto | 43:6183b12dd99c | 459 | op = output; |
Pokitto | 43:6183b12dd99c | 460 | op *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 461 | op >>= 8; |
Pokitto | 43:6183b12dd99c | 462 | uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*op)>>8)); //cut out float |
Pokitto | 43:6183b12dd99c | 463 | obj->pwm->MATCHREL1 = t_on; |
Pokitto | 43:6183b12dd99c | 464 | #endif |
Pokitto | 43:6183b12dd99c | 465 | #else // POK_STREAMING_MUSIC |
Pokitto | 43:6183b12dd99c | 466 | op = output; |
Pokitto | 43:6183b12dd99c | 467 | op *= discrete_vol_multipliers[discrete_vol]; |
Pokitto | 43:6183b12dd99c | 468 | op >>= 8; |
Pokitto | 43:6183b12dd99c | 469 | dac_write((uint8_t)op); // SYNTH to DAC |
Pokitto | 43:6183b12dd99c | 470 | #endif |
Pokitto | 43:6183b12dd99c | 471 | soundbyte = (output+streambyte)>>1; |
Pokitto | 43:6183b12dd99c | 472 | soundbuf[soundbufindex++]=soundbyte; |
Pokitto | 43:6183b12dd99c | 473 | if (soundbufindex==256) soundbufindex=0; |
Pokitto | 43:6183b12dd99c | 474 | #endif //POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 475 | #endif // HARDWARE |
Pokitto | 43:6183b12dd99c | 476 | #endif //POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 477 | } |
Pokitto | 43:6183b12dd99c | 478 | |
Pokitto | 43:6183b12dd99c | 479 | |
Pokitto | 43:6183b12dd99c | 480 | void Pokitto::updateSDAudioStream() { |
Pokitto | 43:6183b12dd99c | 481 | #ifndef NOPETITFATFS |
Pokitto | 43:6183b12dd99c | 482 | if (streamPaused()) return; |
Pokitto | 43:6183b12dd99c | 483 | |
Pokitto | 43:6183b12dd99c | 484 | #if POK_STREAMING_MUSIC > 0 |
Pokitto | 43:6183b12dd99c | 485 | if (oldBuffer != currentBuffer) { |
Pokitto | 43:6183b12dd99c | 486 | if (currentBuffer==0) fileReadBytes(&buffers[3][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 487 | else if (currentBuffer==1) fileReadBytes(&buffers[0][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 488 | else if (currentBuffer==2) fileReadBytes(&buffers[1][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 489 | else fileReadBytes(&buffers[2][0],BUFFER_SIZE); |
Pokitto | 43:6183b12dd99c | 490 | oldBuffer = currentBuffer; |
Pokitto | 43:6183b12dd99c | 491 | streamcounter += BUFFER_SIZE; |
Pokitto | 43:6183b12dd99c | 492 | } else return; |
Pokitto | 43:6183b12dd99c | 493 | |
Pokitto | 43:6183b12dd99c | 494 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 495 | if ( streamcounter > fs.fsize - (BUFFER_SIZE*6)) { |
Pokitto | 43:6183b12dd99c | 496 | #else |
Pokitto | 43:6183b12dd99c | 497 | if ( streamcounter > getFileLength() - (BUFFER_SIZE*6)) { |
Pokitto | 43:6183b12dd99c | 498 | #endif |
Pokitto | 43:6183b12dd99c | 499 | streamcounter=0; |
Pokitto | 43:6183b12dd99c | 500 | #if POK_STREAM_LOOP |
Pokitto | 43:6183b12dd99c | 501 | fileRewind(); |
Pokitto | 43:6183b12dd99c | 502 | #else |
Pokitto | 43:6183b12dd99c | 503 | pokPauseStream(); |
Pokitto | 43:6183b12dd99c | 504 | #endif |
Pokitto | 43:6183b12dd99c | 505 | } |
Pokitto | 43:6183b12dd99c | 506 | #endif |
Pokitto | 43:6183b12dd99c | 507 | #endif // NOPETITFATFS |
Pokitto | 43:6183b12dd99c | 508 | } |
Pokitto | 43:6183b12dd99c | 509 | |
Pokitto | 43:6183b12dd99c | 510 | |
Pokitto | 43:6183b12dd99c | 511 | #endif // POK_ENABLE_SOUND |
Pokitto | 43:6183b12dd99c | 512 | |
Pokitto | 43:6183b12dd99c | 513 | |
Pokitto | 43:6183b12dd99c | 514 |