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_CORE/PokittoCore.h@28:958b71c4b92a, 2018-01-05 (annotated)
- Committer:
- Pokitto
- Date:
- Fri Jan 05 02:19:51 2018 +0000
- Revision:
- 28:958b71c4b92a
- Parent:
- 24:9561281d0378
Sound level stored in EEPROM, sound output improved
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 24:9561281d0378 | 1 | /**************************************************************************/ |
Pokitto | 24:9561281d0378 | 2 | /*! |
Pokitto | 24:9561281d0378 | 3 | @file PokittoCore.h |
Pokitto | 24:9561281d0378 | 4 | @author Jonne Valola |
Pokitto | 24:9561281d0378 | 5 | |
Pokitto | 24:9561281d0378 | 6 | @section LICENSE |
Pokitto | 24:9561281d0378 | 7 | |
Pokitto | 24:9561281d0378 | 8 | Software License Agreement (BSD License) |
Pokitto | 24:9561281d0378 | 9 | |
Pokitto | 24:9561281d0378 | 10 | Copyright (c) 2016, Jonne Valola |
Pokitto | 24:9561281d0378 | 11 | All rights reserved. |
Pokitto | 24:9561281d0378 | 12 | |
Pokitto | 24:9561281d0378 | 13 | Redistribution and use in source and binary forms, with or without |
Pokitto | 24:9561281d0378 | 14 | modification, are permitted provided that the following conditions are met: |
Pokitto | 24:9561281d0378 | 15 | 1. Redistributions of source code must retain the above copyright |
Pokitto | 24:9561281d0378 | 16 | notice, this list of conditions and the following disclaimer. |
Pokitto | 24:9561281d0378 | 17 | 2. Redistributions in binary form must reproduce the above copyright |
Pokitto | 24:9561281d0378 | 18 | notice, this list of conditions and the following disclaimer in the |
Pokitto | 24:9561281d0378 | 19 | documentation and/or other materials provided with the distribution. |
Pokitto | 24:9561281d0378 | 20 | 3. Neither the name of the copyright holders nor the |
Pokitto | 24:9561281d0378 | 21 | names of its contributors may be used to endorse or promote products |
Pokitto | 24:9561281d0378 | 22 | derived from this software without specific prior written permission. |
Pokitto | 24:9561281d0378 | 23 | |
Pokitto | 24:9561281d0378 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
Pokitto | 24:9561281d0378 | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Pokitto | 24:9561281d0378 | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Pokitto | 24:9561281d0378 | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
Pokitto | 24:9561281d0378 | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Pokitto | 24:9561281d0378 | 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Pokitto | 24:9561281d0378 | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
Pokitto | 24:9561281d0378 | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Pokitto | 24:9561281d0378 | 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Pokitto | 24:9561281d0378 | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Pokitto | 24:9561281d0378 | 34 | */ |
Pokitto | 24:9561281d0378 | 35 | /**************************************************************************/ |
Pokitto | 24:9561281d0378 | 36 | |
Pokitto | 24:9561281d0378 | 37 | #ifndef POKITTOCORE_H |
Pokitto | 24:9561281d0378 | 38 | #define POKITTOCORE_H |
Pokitto | 24:9561281d0378 | 39 | |
Pokitto | 24:9561281d0378 | 40 | #include <stdint.h> |
Pokitto | 24:9561281d0378 | 41 | #include <math.h> |
Pokitto | 24:9561281d0378 | 42 | #ifndef POK_SIM |
Pokitto | 24:9561281d0378 | 43 | #include "pwmout_api.h" |
Pokitto | 24:9561281d0378 | 44 | #include "HWButtons.h" |
Pokitto | 24:9561281d0378 | 45 | #else |
Pokitto | 24:9561281d0378 | 46 | #include "PokittoSimulator.h" |
Pokitto | 24:9561281d0378 | 47 | #endif |
Pokitto | 24:9561281d0378 | 48 | #if POK_USE_CONSOLE > 0 |
Pokitto | 24:9561281d0378 | 49 | #include "PokittoConsole.h" |
Pokitto | 24:9561281d0378 | 50 | #endif // POK_USE_CONSOLE |
Pokitto | 24:9561281d0378 | 51 | #if POK_ENABLE_SD > 0 |
Pokitto | 24:9561281d0378 | 52 | #include "PokittoDisk.h" |
Pokitto | 24:9561281d0378 | 53 | #endif |
Pokitto | 24:9561281d0378 | 54 | |
Pokitto | 24:9561281d0378 | 55 | #include "PokittoFonts.h" |
Pokitto | 24:9561281d0378 | 56 | #include "PokittoPalettes.h" |
Pokitto | 24:9561281d0378 | 57 | #include "PokittoDisplay.h" |
Pokitto | 24:9561281d0378 | 58 | #include "PokittoButtons.h" |
Pokitto | 24:9561281d0378 | 59 | #include "PokittoBattery.h" |
Pokitto | 24:9561281d0378 | 60 | #include "PokittoBacklight.h" |
Pokitto | 24:9561281d0378 | 61 | #include "PokittoSound.h" |
Pokitto | 24:9561281d0378 | 62 | #include "PokittoFakeavr.h" |
Pokitto | 24:9561281d0378 | 63 | |
Pokitto | 24:9561281d0378 | 64 | #define PALETTE_SIZE 256 |
Pokitto | 24:9561281d0378 | 65 | #define PI 3.141592741f |
Pokitto | 24:9561281d0378 | 66 | |
Pokitto | 24:9561281d0378 | 67 | // For GB compatibility |
Pokitto | 24:9561281d0378 | 68 | #if PROJ_GAMEBUINO > 0 |
Pokitto | 24:9561281d0378 | 69 | extern void setup(); |
Pokitto | 24:9561281d0378 | 70 | extern void loop(); |
Pokitto | 24:9561281d0378 | 71 | #endif // PROJ_GAMEBUINO |
Pokitto | 24:9561281d0378 | 72 | |
Pokitto | 24:9561281d0378 | 73 | extern uint32_t* ptimer; // re-directed tick counter |
Pokitto | 24:9561281d0378 | 74 | |
Pokitto | 24:9561281d0378 | 75 | namespace Pokitto { |
Pokitto | 24:9561281d0378 | 76 | |
Pokitto | 24:9561281d0378 | 77 | /** Core class. |
Pokitto | 24:9561281d0378 | 78 | * The Core class is a class consisting of static data and methods. |
Pokitto | 24:9561281d0378 | 79 | * It handles the lowlevel hardware functions of the Pokitto. |
Pokitto | 24:9561281d0378 | 80 | * It is declared as static to prevent several instances running at same time. |
Pokitto | 24:9561281d0378 | 81 | * Example: |
Pokitto | 24:9561281d0378 | 82 | * @code |
Pokitto | 24:9561281d0378 | 83 | * // A simple "Hello World!" program with Pokitto |
Pokitto | 24:9561281d0378 | 84 | * |
Pokitto | 24:9561281d0378 | 85 | * #include "Pokitto.h" |
Pokitto | 24:9561281d0378 | 86 | * |
Pokitto | 24:9561281d0378 | 87 | * Pokitto::Core myApp; |
Pokitto | 24:9561281d0378 | 88 | * |
Pokitto | 24:9561281d0378 | 89 | * int main() { |
Pokitto | 24:9561281d0378 | 90 | * myApp.begin(); // This starts up the console (the display, buttons etc.) |
Pokitto | 24:9561281d0378 | 91 | * while(myApp.isRunning()) { |
Pokitto | 24:9561281d0378 | 92 | * if(myApp.Update()) { |
Pokitto | 24:9561281d0378 | 93 | * myApp.display.print("Hello World!"); |
Pokitto | 24:9561281d0378 | 94 | * } |
Pokitto | 24:9561281d0378 | 95 | * } |
Pokitto | 24:9561281d0378 | 96 | * } |
Pokitto | 24:9561281d0378 | 97 | * @endcode |
Pokitto | 24:9561281d0378 | 98 | */ |
Pokitto | 24:9561281d0378 | 99 | |
Pokitto | 24:9561281d0378 | 100 | class Core |
Pokitto | 24:9561281d0378 | 101 | { |
Pokitto | 24:9561281d0378 | 102 | public: |
Pokitto | 24:9561281d0378 | 103 | /** Create a Core runtime instance |
Pokitto | 24:9561281d0378 | 104 | */ |
Pokitto | 24:9561281d0378 | 105 | Core(); |
Pokitto | 24:9561281d0378 | 106 | |
Pokitto | 24:9561281d0378 | 107 | /** Backlight component of the Core runtime */ |
Pokitto | 24:9561281d0378 | 108 | static Backlight backlight; |
Pokitto | 24:9561281d0378 | 109 | /** Buttons component of the Core runtime */ |
Pokitto | 24:9561281d0378 | 110 | static Buttons buttons; |
Pokitto | 24:9561281d0378 | 111 | /** Battery component of the Core runtime */ |
Pokitto | 24:9561281d0378 | 112 | static Battery battery; |
Pokitto | 24:9561281d0378 | 113 | /** Sound component of the Core runtime */ |
Pokitto | 24:9561281d0378 | 114 | static Sound sound; |
Pokitto | 24:9561281d0378 | 115 | /** Display component of the Core runtime */ |
Pokitto | 24:9561281d0378 | 116 | static Display display; |
Pokitto | 24:9561281d0378 | 117 | |
Pokitto | 24:9561281d0378 | 118 | // EXCECUTION CONTROL |
Pokitto | 24:9561281d0378 | 119 | public: |
Pokitto | 24:9561281d0378 | 120 | /** Initialize runtime (use this one) */ |
Pokitto | 24:9561281d0378 | 121 | static void begin(); |
Pokitto | 24:9561281d0378 | 122 | /** Initialize runtime (deprecated, avoid) */ |
Pokitto | 24:9561281d0378 | 123 | static void init(); |
Pokitto | 24:9561281d0378 | 124 | /** Initialize runtime with options (deprecated, avoid) */ |
Pokitto | 24:9561281d0378 | 125 | static void init(uint8_t); |
Pokitto | 24:9561281d0378 | 126 | /** Return run state (1 = running, 0 = shutting down) */ |
Pokitto | 24:9561281d0378 | 127 | static bool isRunning(); |
Pokitto | 24:9561281d0378 | 128 | /** Stop running */ |
Pokitto | 24:9561281d0378 | 129 | static void quit(); |
Pokitto | 24:9561281d0378 | 130 | private: |
Pokitto | 24:9561281d0378 | 131 | /** run_state is true as long as program is running */ |
Pokitto | 24:9561281d0378 | 132 | static bool run_state; |
Pokitto | 24:9561281d0378 | 133 | |
Pokitto | 24:9561281d0378 | 134 | public: |
Pokitto | 24:9561281d0378 | 135 | // INITIALIZATION |
Pokitto | 24:9561281d0378 | 136 | /** Initialize display */ |
Pokitto | 24:9561281d0378 | 137 | static void initDisplay(); |
Pokitto | 24:9561281d0378 | 138 | /** Initialize random generator */ |
Pokitto | 24:9561281d0378 | 139 | static void initRandom(); |
Pokitto | 24:9561281d0378 | 140 | /** Initialize GPIO */ |
Pokitto | 24:9561281d0378 | 141 | static void initGPIO(); |
Pokitto | 24:9561281d0378 | 142 | /** Initialize LCD */ |
Pokitto | 24:9561281d0378 | 143 | static void initLCD(); |
Pokitto | 24:9561281d0378 | 144 | /** Initialize Audio */ |
Pokitto | 24:9561281d0378 | 145 | static void initAudio(); |
Pokitto | 24:9561281d0378 | 146 | |
Pokitto | 24:9561281d0378 | 147 | |
Pokitto | 24:9561281d0378 | 148 | // DISPLAY |
Pokitto | 24:9561281d0378 | 149 | public: |
Pokitto | 24:9561281d0378 | 150 | /** Initialize backlight */ |
Pokitto | 24:9561281d0378 | 151 | static void initBacklight(); |
Pokitto | 24:9561281d0378 | 152 | |
Pokitto | 24:9561281d0378 | 153 | private: |
Pokitto | 24:9561281d0378 | 154 | /** Backlight PWM pointer */ |
Pokitto | 24:9561281d0378 | 155 | #ifndef POK_SIM |
Pokitto | 24:9561281d0378 | 156 | static pwmout_t backlightpwm; |
Pokitto | 24:9561281d0378 | 157 | #endif |
Pokitto | 24:9561281d0378 | 158 | |
Pokitto | 24:9561281d0378 | 159 | // TIMEKEEPING |
Pokitto | 24:9561281d0378 | 160 | public: |
Pokitto | 24:9561281d0378 | 161 | /** Initialize runtime clock */ |
Pokitto | 24:9561281d0378 | 162 | static void initClock(); |
Pokitto | 24:9561281d0378 | 163 | /** Get value of time elapsed during program in milliseconds */ |
Pokitto | 24:9561281d0378 | 164 | static uint32_t getTime(); |
Pokitto | 24:9561281d0378 | 165 | /** Wait for n milliseconds */ |
Pokitto | 24:9561281d0378 | 166 | static void wait(uint16_t); |
Pokitto | 24:9561281d0378 | 167 | /** FPS */ |
Pokitto | 24:9561281d0378 | 168 | static uint32_t fps_counter; |
Pokitto | 24:9561281d0378 | 169 | private: |
Pokitto | 24:9561281d0378 | 170 | /** Time of next refresh */ |
Pokitto | 24:9561281d0378 | 171 | static uint32_t refreshtime; |
Pokitto | 24:9561281d0378 | 172 | |
Pokitto | 24:9561281d0378 | 173 | // DIRECT TO SCREEN |
Pokitto | 24:9561281d0378 | 174 | public: |
Pokitto | 24:9561281d0378 | 175 | /** Display Pokitto logo */ |
Pokitto | 24:9561281d0378 | 176 | static void showLogo(); |
Pokitto | 24:9561281d0378 | 177 | static void showWarning(); |
Pokitto | 24:9561281d0378 | 178 | static void setVolLimit(); |
Pokitto | 24:9561281d0378 | 179 | |
Pokitto | 24:9561281d0378 | 180 | // BUTTON INPUT HANDLING |
Pokitto | 24:9561281d0378 | 181 | private: |
Pokitto | 24:9561281d0378 | 182 | static uint8_t heldStates[]; |
Pokitto | 24:9561281d0378 | 183 | public: |
Pokitto | 24:9561281d0378 | 184 | static void initButtons(); |
Pokitto | 24:9561281d0378 | 185 | static void pollButtons(); |
Pokitto | 24:9561281d0378 | 186 | static uint8_t leftBtn(); |
Pokitto | 24:9561281d0378 | 187 | static uint8_t rightBtn(); |
Pokitto | 24:9561281d0378 | 188 | static uint8_t upBtn(); |
Pokitto | 24:9561281d0378 | 189 | static uint8_t downBtn(); |
Pokitto | 24:9561281d0378 | 190 | static uint8_t aBtn(); |
Pokitto | 24:9561281d0378 | 191 | static uint8_t bBtn(); |
Pokitto | 24:9561281d0378 | 192 | static uint8_t cBtn(); |
Pokitto | 24:9561281d0378 | 193 | static uint8_t leftHeld(); |
Pokitto | 24:9561281d0378 | 194 | static uint8_t rightHeld(); |
Pokitto | 24:9561281d0378 | 195 | static uint8_t upHeld(); |
Pokitto | 24:9561281d0378 | 196 | static uint8_t downHeld(); |
Pokitto | 24:9561281d0378 | 197 | static uint8_t aHeld(); |
Pokitto | 24:9561281d0378 | 198 | static uint8_t bHeld(); |
Pokitto | 24:9561281d0378 | 199 | static uint8_t cHeld(); |
Pokitto | 24:9561281d0378 | 200 | |
Pokitto | 24:9561281d0378 | 201 | static uint8_t leftReleased(); |
Pokitto | 24:9561281d0378 | 202 | static uint8_t rightReleased(); |
Pokitto | 24:9561281d0378 | 203 | static uint8_t upReleased(); |
Pokitto | 24:9561281d0378 | 204 | static uint8_t downReleased(); |
Pokitto | 24:9561281d0378 | 205 | static uint8_t aReleased(); |
Pokitto | 24:9561281d0378 | 206 | static uint8_t bReleased(); |
Pokitto | 24:9561281d0378 | 207 | static uint8_t cReleased(); |
Pokitto | 24:9561281d0378 | 208 | |
Pokitto | 24:9561281d0378 | 209 | // AUDIO RELATED |
Pokitto | 24:9561281d0378 | 210 | static uint8_t ampIsOn(); |
Pokitto | 24:9561281d0378 | 211 | static void ampEnable(uint8_t); |
Pokitto | 24:9561281d0378 | 212 | static uint8_t soundbyte; |
Pokitto | 24:9561281d0378 | 213 | |
Pokitto | 24:9561281d0378 | 214 | // GB RELATED |
Pokitto | 24:9561281d0378 | 215 | public: |
Pokitto | 24:9561281d0378 | 216 | static void readSettings(); |
Pokitto | 24:9561281d0378 | 217 | static void titleScreen(const char* name, const uint8_t *logo); |
Pokitto | 24:9561281d0378 | 218 | static void titleScreen(const char* name); |
Pokitto | 24:9561281d0378 | 219 | static void titleScreen(const uint8_t* logo); |
Pokitto | 24:9561281d0378 | 220 | static void titleScreen(); |
Pokitto | 24:9561281d0378 | 221 | static bool update(bool useDirectMode=false); |
Pokitto | 24:9561281d0378 | 222 | static uint32_t frameCount; |
Pokitto | 24:9561281d0378 | 223 | static int8_t menu(const char* const* items, uint8_t length); |
Pokitto | 24:9561281d0378 | 224 | static char* filemenu(char*); |
Pokitto | 24:9561281d0378 | 225 | static char* filemenu(); |
Pokitto | 24:9561281d0378 | 226 | static void keyboard(char* text, uint8_t length); |
Pokitto | 24:9561281d0378 | 227 | static void popup(const char* text, uint8_t duration); |
Pokitto | 24:9561281d0378 | 228 | static void setFrameRate(uint8_t fps); |
Pokitto | 24:9561281d0378 | 229 | static void pickRandomSeed(); |
Pokitto | 24:9561281d0378 | 230 | |
Pokitto | 24:9561281d0378 | 231 | static uint8_t getCpuLoad(); |
Pokitto | 24:9561281d0378 | 232 | static uint16_t getFreeRam(); |
Pokitto | 24:9561281d0378 | 233 | |
Pokitto | 24:9561281d0378 | 234 | static bool collidePointRect(int16_t x1, int16_t y1 ,int16_t x2 ,int16_t y2, int16_t w, int16_t h); |
Pokitto | 24:9561281d0378 | 235 | static bool collideRectRect(int16_t x1, int16_t y1, int16_t w1, int16_t h1 ,int16_t x2 ,int16_t y2, int16_t w2, int16_t h2); |
Pokitto | 24:9561281d0378 | 236 | static bool collideBitmapBitmap(int16_t x1, int16_t y1, const uint8_t* b1, int16_t x2, int16_t y2, const uint8_t* b2); |
Pokitto | 24:9561281d0378 | 237 | |
Pokitto | 24:9561281d0378 | 238 | private: |
Pokitto | 24:9561281d0378 | 239 | static uint8_t timePerFrame; |
Pokitto | 24:9561281d0378 | 240 | static uint32_t nextFrameMillis; |
Pokitto | 24:9561281d0378 | 241 | static void updatePopup(); |
Pokitto | 24:9561281d0378 | 242 | static const char* popupText; |
Pokitto | 24:9561281d0378 | 243 | static uint8_t popupTimeLeft; |
Pokitto | 24:9561281d0378 | 244 | static void displayBattery(); |
Pokitto | 24:9561281d0378 | 245 | static uint16_t frameDurationMicros; |
Pokitto | 24:9561281d0378 | 246 | static uint32_t frameStartMicros, frameEndMicros; |
Pokitto | 24:9561281d0378 | 247 | static uint8_t startMenuTimer; |
Pokitto | 24:9561281d0378 | 248 | static int updateLoader(uint32_t,uint32_t); |
Pokitto | 24:9561281d0378 | 249 | static uint32_t fps_refreshtime; |
Pokitto | 24:9561281d0378 | 250 | static uint32_t fps_frameCount; |
Pokitto | 24:9561281d0378 | 251 | |
Pokitto | 24:9561281d0378 | 252 | public: |
Pokitto | 24:9561281d0378 | 253 | static uint8_t volbar_visible; |
Pokitto | 24:9561281d0378 | 254 | static void drawvolbar(int,int,int, bool); |
Pokitto | 24:9561281d0378 | 255 | static void askLoader(); |
Pokitto | 24:9561281d0378 | 256 | static void jumpToLoader(); |
Pokitto | 24:9561281d0378 | 257 | }; |
Pokitto | 24:9561281d0378 | 258 | |
Pokitto | 24:9561281d0378 | 259 | // this is the instance used by the system |
Pokitto | 24:9561281d0378 | 260 | extern Core core; |
Pokitto | 24:9561281d0378 | 261 | |
Pokitto | 24:9561281d0378 | 262 | |
Pokitto | 24:9561281d0378 | 263 | } |
Pokitto | 24:9561281d0378 | 264 | |
Pokitto | 24:9561281d0378 | 265 | #endif // POKITTOCORE_H |
Pokitto | 24:9561281d0378 | 266 | |
Pokitto | 24:9561281d0378 | 267 | |
Pokitto | 24:9561281d0378 | 268 | |
Pokitto | 24:9561281d0378 | 269 |