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

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Tue Jan 30 10:41:47 2018 +0000
Revision:
31:f4b9b85c7b62
Sound output improvements added:  louder, clearer, faster!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 31:f4b9b85c7b62 1 /**************************************************************************/
Pokitto 31:f4b9b85c7b62 2 /*!
Pokitto 31:f4b9b85c7b62 3 @file PokittoSound.h
Pokitto 31:f4b9b85c7b62 4 @author Jonne Valola
Pokitto 31:f4b9b85c7b62 5
Pokitto 31:f4b9b85c7b62 6 @section LICENSE
Pokitto 31:f4b9b85c7b62 7
Pokitto 31:f4b9b85c7b62 8 Software License Agreement (BSD License)
Pokitto 31:f4b9b85c7b62 9
Pokitto 31:f4b9b85c7b62 10 Copyright (c) 2016, Jonne Valola
Pokitto 31:f4b9b85c7b62 11 All rights reserved.
Pokitto 31:f4b9b85c7b62 12
Pokitto 31:f4b9b85c7b62 13 Redistribution and use in source and binary forms, with or without
Pokitto 31:f4b9b85c7b62 14 modification, are permitted provided that the following conditions are met:
Pokitto 31:f4b9b85c7b62 15 1. Redistributions of source code must retain the above copyright
Pokitto 31:f4b9b85c7b62 16 notice, this list of conditions and the following disclaimer.
Pokitto 31:f4b9b85c7b62 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 31:f4b9b85c7b62 18 notice, this list of conditions and the following disclaimer in the
Pokitto 31:f4b9b85c7b62 19 documentation and/or other materials provided with the distribution.
Pokitto 31:f4b9b85c7b62 20 3. Neither the name of the copyright holders nor the
Pokitto 31:f4b9b85c7b62 21 names of its contributors may be used to endorse or promote products
Pokitto 31:f4b9b85c7b62 22 derived from this software without specific prior written permission.
Pokitto 31:f4b9b85c7b62 23
Pokitto 31:f4b9b85c7b62 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 31:f4b9b85c7b62 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 31:f4b9b85c7b62 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 31:f4b9b85c7b62 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 31:f4b9b85c7b62 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 31:f4b9b85c7b62 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 31:f4b9b85c7b62 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 31:f4b9b85c7b62 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 31:f4b9b85c7b62 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 31:f4b9b85c7b62 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 31:f4b9b85c7b62 34 */
Pokitto 31:f4b9b85c7b62 35 /**************************************************************************/
Pokitto 31:f4b9b85c7b62 36
Pokitto 31:f4b9b85c7b62 37
Pokitto 31:f4b9b85c7b62 38 #ifndef POKITTOSOUND_H
Pokitto 31:f4b9b85c7b62 39 #define POKITTOSOUND_H
Pokitto 31:f4b9b85c7b62 40
Pokitto 31:f4b9b85c7b62 41 #include <stdint.h>
Pokitto 31:f4b9b85c7b62 42 #include "Pokitto_settings.h"
Pokitto 31:f4b9b85c7b62 43 #include "GBcompatibility.h"
Pokitto 31:f4b9b85c7b62 44 #include "PokittoFakeavr.h"
Pokitto 31:f4b9b85c7b62 45 #include "PokittoGlobs.h"
Pokitto 31:f4b9b85c7b62 46
Pokitto 31:f4b9b85c7b62 47 extern void pokPauseStream();
Pokitto 31:f4b9b85c7b62 48 extern void pokPlayStream();
Pokitto 31:f4b9b85c7b62 49 extern uint8_t pokStreamPaused();
Pokitto 31:f4b9b85c7b62 50
Pokitto 31:f4b9b85c7b62 51 //volume levels
Pokitto 31:f4b9b85c7b62 52 #define GLOBVOL_SHIFT 5 //shift global volume to allow for finer increments
Pokitto 31:f4b9b85c7b62 53 #ifndef MAX_VOL_TEST
Pokitto 31:f4b9b85c7b62 54 #define VOLUME_SPEAKER_MAX 255 //((8<<GLOBVOL_SHIFT)-1)
Pokitto 31:f4b9b85c7b62 55 #define VOLUME_HEADPHONE_MAX (1<<GLOBVOL_SHIFT)
Pokitto 31:f4b9b85c7b62 56 #define VOLUME_STARTUP ((1<<GLOBVOL_SHIFT)/2)
Pokitto 31:f4b9b85c7b62 57 #else
Pokitto 31:f4b9b85c7b62 58 #define VOLUME_SPEAKER_MAX ((8<<GLOBVOL_SHIFT)-1)
Pokitto 31:f4b9b85c7b62 59 #define VOLUME_HEADPHONE_MAX VOLUME_SPEAKER_MAX
Pokitto 31:f4b9b85c7b62 60 #define VOLUME_STARTUP VOLUME_SPEAKER_MAX
Pokitto 31:f4b9b85c7b62 61 #endif // MAXVOLTEST
Pokitto 31:f4b9b85c7b62 62
Pokitto 31:f4b9b85c7b62 63 #ifdef POK_SIM
Pokitto 31:f4b9b85c7b62 64 #define VOLUME_STEP 1
Pokitto 31:f4b9b85c7b62 65 #else
Pokitto 31:f4b9b85c7b62 66 #define VOLUME_STEP 8
Pokitto 31:f4b9b85c7b62 67 #endif
Pokitto 31:f4b9b85c7b62 68
Pokitto 31:f4b9b85c7b62 69
Pokitto 31:f4b9b85c7b62 70 //commands
Pokitto 31:f4b9b85c7b62 71 #define CMD_VOLUME 0
Pokitto 31:f4b9b85c7b62 72 #define CMD_INSTRUMENT 1
Pokitto 31:f4b9b85c7b62 73 #define CMD_SLIDE 2
Pokitto 31:f4b9b85c7b62 74 #define CMD_ARPEGGIO 3
Pokitto 31:f4b9b85c7b62 75 #define CMD_TREMOLO 4
Pokitto 31:f4b9b85c7b62 76
Pokitto 31:f4b9b85c7b62 77 #define STR_PLAYING 0x1
Pokitto 31:f4b9b85c7b62 78 #define STR_VISUALIZER 0x2
Pokitto 31:f4b9b85c7b62 79 #define STR_LOOP 0x4
Pokitto 31:f4b9b85c7b62 80 #define STR_PAUSED 0x8
Pokitto 31:f4b9b85c7b62 81
Pokitto 31:f4b9b85c7b62 82 namespace Pokitto {
Pokitto 31:f4b9b85c7b62 83
Pokitto 31:f4b9b85c7b62 84 /** Sound class.
Pokitto 31:f4b9b85c7b62 85 * The Sound class is an API-compatible version of the Gamebuino Sound API by Aurelien Rodot.
Pokitto 31:f4b9b85c7b62 86 * Because it is a class consisting of only static members, there is only 1 instance running,
Pokitto 31:f4b9b85c7b62 87 * no matter how many Sound classes are declared (see example below). This means sound can
Pokitto 31:f4b9b85c7b62 88 * be used through a simple Sound class object or as a member of the Core class.
Pokitto 31:f4b9b85c7b62 89 *
Pokitto 31:f4b9b85c7b62 90 */
Pokitto 31:f4b9b85c7b62 91
Pokitto 31:f4b9b85c7b62 92 extern void audio_IRQ(); // audio interrupt
Pokitto 31:f4b9b85c7b62 93
Pokitto 31:f4b9b85c7b62 94 class Sound {
Pokitto 31:f4b9b85c7b62 95 private:
Pokitto 31:f4b9b85c7b62 96 static uint16_t volumeMax;
Pokitto 31:f4b9b85c7b62 97 public:
Pokitto 31:f4b9b85c7b62 98 static void begin();
Pokitto 31:f4b9b85c7b62 99
Pokitto 31:f4b9b85c7b62 100 // Headphonemode
Pokitto 31:f4b9b85c7b62 101 static void setMaxVol(int16_t);
Pokitto 31:f4b9b85c7b62 102 static uint16_t getMaxVol();
Pokitto 31:f4b9b85c7b62 103 static void volumeUp();
Pokitto 31:f4b9b85c7b62 104 static void volumeDown();
Pokitto 31:f4b9b85c7b62 105
Pokitto 31:f4b9b85c7b62 106 // Original functions
Pokitto 31:f4b9b85c7b62 107 static void updateStream();
Pokitto 31:f4b9b85c7b62 108 static void playTone(uint8_t os, int frq, uint8_t amp, uint8_t wav,uint8_t arpmode);
Pokitto 31:f4b9b85c7b62 109 static void playTone(uint8_t os, uint16_t freq, uint8_t volume, uint32_t duration);
Pokitto 31:f4b9b85c7b62 110 static uint8_t ampIsOn();
Pokitto 31:f4b9b85c7b62 111 static void ampEnable(uint8_t);
Pokitto 31:f4b9b85c7b62 112 static int playMusicStream(char* filename, uint8_t options);
Pokitto 31:f4b9b85c7b62 113 static int playMusicStream(char* filename);
Pokitto 31:f4b9b85c7b62 114 static int playMusicStream();
Pokitto 31:f4b9b85c7b62 115 static void pauseMusicStream();
Pokitto 31:f4b9b85c7b62 116
Pokitto 31:f4b9b85c7b62 117 // GB compatibility functions
Pokitto 31:f4b9b85c7b62 118 static void playTrack(const uint16_t* track, uint8_t channel);
Pokitto 31:f4b9b85c7b62 119 static void updateTrack(uint8_t channel);
Pokitto 31:f4b9b85c7b62 120 static void updateTrack();
Pokitto 31:f4b9b85c7b62 121 static void stopTrack(uint8_t channel);
Pokitto 31:f4b9b85c7b62 122 static void stopTrack();
Pokitto 31:f4b9b85c7b62 123 static void changePatternSet(const uint16_t* const* patterns, uint8_t channel);
Pokitto 31:f4b9b85c7b62 124 static bool trackIsPlaying[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 125
Pokitto 31:f4b9b85c7b62 126 static void playPattern(const uint16_t* pattern, uint8_t channel);
Pokitto 31:f4b9b85c7b62 127 static void changeInstrumentSet(const uint16_t* const* instruments, uint8_t channel);
Pokitto 31:f4b9b85c7b62 128 static void updatePattern(uint8_t i);
Pokitto 31:f4b9b85c7b62 129 static void updatePattern();
Pokitto 31:f4b9b85c7b62 130 static void setPatternLooping(bool loop, uint8_t channel);
Pokitto 31:f4b9b85c7b62 131 static void stopPattern(uint8_t channel);
Pokitto 31:f4b9b85c7b62 132 static void stopPattern();
Pokitto 31:f4b9b85c7b62 133 static bool patternIsPlaying[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 134
Pokitto 31:f4b9b85c7b62 135 static void command(uint8_t cmd, uint8_t X, int8_t Y, uint8_t i);
Pokitto 31:f4b9b85c7b62 136 static void playNote(uint8_t pitch, uint8_t duration, uint8_t channel);
Pokitto 31:f4b9b85c7b62 137 static void updateNote();
Pokitto 31:f4b9b85c7b62 138 static void updateNote(uint8_t i);
Pokitto 31:f4b9b85c7b62 139 static void stopNote(uint8_t channel);
Pokitto 31:f4b9b85c7b62 140 static void stopNote();
Pokitto 31:f4b9b85c7b62 141
Pokitto 31:f4b9b85c7b62 142 static uint8_t outputPitch[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 143 static int8_t outputVolume[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 144
Pokitto 31:f4b9b85c7b62 145 static void setMasterVolume(uint8_t);
Pokitto 31:f4b9b85c7b62 146 static uint8_t GetMasterVolume();
Pokitto 31:f4b9b85c7b62 147 static void setVolume(int16_t volume);
Pokitto 31:f4b9b85c7b62 148 static uint16_t getVolume();
Pokitto 31:f4b9b85c7b62 149 static void setVolume(int8_t volume, uint8_t channel);
Pokitto 31:f4b9b85c7b62 150 static uint8_t getVolume(uint8_t channel);
Pokitto 31:f4b9b85c7b62 151
Pokitto 31:f4b9b85c7b62 152 static void playOK();
Pokitto 31:f4b9b85c7b62 153 static void playCancel();
Pokitto 31:f4b9b85c7b62 154 static void playTick();
Pokitto 31:f4b9b85c7b62 155
Pokitto 31:f4b9b85c7b62 156 static uint8_t prescaler;
Pokitto 31:f4b9b85c7b62 157
Pokitto 31:f4b9b85c7b62 158 static void setChannelHalfPeriod(uint8_t channel, uint8_t halfPeriod);
Pokitto 31:f4b9b85c7b62 159
Pokitto 31:f4b9b85c7b62 160 static void generateOutput(); //!\\ DO NOT USE
Pokitto 31:f4b9b85c7b62 161 static uint16_t globalVolume;
Pokitto 31:f4b9b85c7b62 162
Pokitto 31:f4b9b85c7b62 163
Pokitto 31:f4b9b85c7b62 164 #if (NUM_CHANNELS > 0)
Pokitto 31:f4b9b85c7b62 165 //tracks data
Pokitto 31:f4b9b85c7b62 166 static uint16_t *trackData[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 167 static uint8_t trackCursor[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 168 static uint16_t **patternSet[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 169 static int8_t patternPitch[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 170
Pokitto 31:f4b9b85c7b62 171 // pattern data
Pokitto 31:f4b9b85c7b62 172 static uint16_t *patternData[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 173 static uint16_t **instrumentSet[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 174 static bool patternLooping[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 175 static uint16_t patternCursor[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 176
Pokitto 31:f4b9b85c7b62 177 // note data
Pokitto 31:f4b9b85c7b62 178 static uint8_t notePitch[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 179 static uint8_t noteDuration[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 180 static int8_t noteVolume[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 181 static bool notePlaying[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 182
Pokitto 31:f4b9b85c7b62 183 // commands data
Pokitto 31:f4b9b85c7b62 184 static int8_t commandsCounter[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 185 static int8_t volumeSlideStepDuration[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 186 static int8_t volumeSlideStepSize[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 187 static uint8_t arpeggioStepDuration[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 188 static int8_t arpeggioStepSize[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 189 static uint8_t tremoloStepDuration[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 190 static int8_t tremoloStepSize[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 191
Pokitto 31:f4b9b85c7b62 192
Pokitto 31:f4b9b85c7b62 193 // instrument data
Pokitto 31:f4b9b85c7b62 194 static uint16_t *instrumentData[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 195 static uint8_t instrumentLength[NUM_CHANNELS]; //number of steps in the instrument
Pokitto 31:f4b9b85c7b62 196 static uint8_t instrumentLooping[NUM_CHANNELS]; //how many steps to loop on when the last step of the instrument is reached
Pokitto 31:f4b9b85c7b62 197 static uint16_t instrumentCursor[NUM_CHANNELS]; //which step is being played
Pokitto 31:f4b9b85c7b62 198 static uint8_t instrumentNextChange[NUM_CHANNELS]; //how many frames before the next step
Pokitto 31:f4b9b85c7b62 199
Pokitto 31:f4b9b85c7b62 200 //current step data
Pokitto 31:f4b9b85c7b62 201 static int8_t stepVolume[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 202 static uint8_t stepPitch[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 203
Pokitto 31:f4b9b85c7b62 204 static uint8_t chanVolumes[NUM_CHANNELS];
Pokitto 31:f4b9b85c7b62 205 #endif
Pokitto 31:f4b9b85c7b62 206 static void updateOutput();
Pokitto 31:f4b9b85c7b62 207 };
Pokitto 31:f4b9b85c7b62 208
Pokitto 31:f4b9b85c7b62 209 }
Pokitto 31:f4b9b85c7b62 210
Pokitto 31:f4b9b85c7b62 211 #endif // POKITTOSOUND_H
Pokitto 31:f4b9b85c7b62 212
Pokitto 31:f4b9b85c7b62 213
Pokitto 31:f4b9b85c7b62 214
Pokitto 31:f4b9b85c7b62 215
Pokitto 31:f4b9b85c7b62 216
Pokitto 31:f4b9b85c7b62 217