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@43:6183b12dd99c, 2018-05-01 (annotated)
- Committer:
- Pokitto
- Date:
- Tue May 01 18:42:56 2018 +0000
- Revision:
- 43:6183b12dd99c
- Child:
- 60:8b6a110feeea
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 PokittoCore.h |
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 | #ifndef POKITTOCORE_H |
Pokitto | 43:6183b12dd99c | 38 | #define POKITTOCORE_H |
Pokitto | 43:6183b12dd99c | 39 | |
Pokitto | 43:6183b12dd99c | 40 | #include <stdint.h> |
Pokitto | 43:6183b12dd99c | 41 | #include <math.h> |
Pokitto | 43:6183b12dd99c | 42 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 43 | #include "pwmout_api.h" |
Pokitto | 43:6183b12dd99c | 44 | #include "HWButtons.h" |
Pokitto | 43:6183b12dd99c | 45 | #else |
Pokitto | 43:6183b12dd99c | 46 | #include "PokittoSimulator.h" |
Pokitto | 43:6183b12dd99c | 47 | #endif |
Pokitto | 43:6183b12dd99c | 48 | #if POK_USE_CONSOLE > 0 |
Pokitto | 43:6183b12dd99c | 49 | #include "PokittoConsole.h" |
Pokitto | 43:6183b12dd99c | 50 | #endif // POK_USE_CONSOLE |
Pokitto | 43:6183b12dd99c | 51 | #if POK_ENABLE_SD > 0 |
Pokitto | 43:6183b12dd99c | 52 | #include "PokittoDisk.h" |
Pokitto | 43:6183b12dd99c | 53 | #endif |
Pokitto | 43:6183b12dd99c | 54 | |
Pokitto | 43:6183b12dd99c | 55 | #include "PokittoFonts.h" |
Pokitto | 43:6183b12dd99c | 56 | #include "PokittoPalettes.h" |
Pokitto | 43:6183b12dd99c | 57 | #include "PokittoDisplay.h" |
Pokitto | 43:6183b12dd99c | 58 | #include "PokittoButtons.h" |
Pokitto | 43:6183b12dd99c | 59 | #include "PokittoBattery.h" |
Pokitto | 43:6183b12dd99c | 60 | #include "PokittoBacklight.h" |
Pokitto | 43:6183b12dd99c | 61 | #include "PokittoSound.h" |
Pokitto | 43:6183b12dd99c | 62 | #include "PokittoFakeavr.h" |
Pokitto | 43:6183b12dd99c | 63 | |
Pokitto | 43:6183b12dd99c | 64 | #define PALETTE_SIZE 256 |
Pokitto | 43:6183b12dd99c | 65 | #define PI 3.141592741f |
Pokitto | 43:6183b12dd99c | 66 | |
Pokitto | 43:6183b12dd99c | 67 | // For GB compatibility |
Pokitto | 43:6183b12dd99c | 68 | #if PROJ_GAMEBUINO > 0 |
Pokitto | 43:6183b12dd99c | 69 | extern void setup(); |
Pokitto | 43:6183b12dd99c | 70 | extern void loop(); |
Pokitto | 43:6183b12dd99c | 71 | #endif // PROJ_GAMEBUINO |
Pokitto | 43:6183b12dd99c | 72 | |
Pokitto | 43:6183b12dd99c | 73 | extern uint32_t* ptimer; // re-directed tick counter |
Pokitto | 43:6183b12dd99c | 74 | |
Pokitto | 43:6183b12dd99c | 75 | namespace Pokitto { |
Pokitto | 43:6183b12dd99c | 76 | |
Pokitto | 43:6183b12dd99c | 77 | /** Core class. |
Pokitto | 43:6183b12dd99c | 78 | * The Core class is a class consisting of static data and methods. |
Pokitto | 43:6183b12dd99c | 79 | * It handles the lowlevel hardware functions of the Pokitto. |
Pokitto | 43:6183b12dd99c | 80 | * It is declared as static to prevent several instances running at same time. |
Pokitto | 43:6183b12dd99c | 81 | * Example: |
Pokitto | 43:6183b12dd99c | 82 | * @code |
Pokitto | 43:6183b12dd99c | 83 | * // A simple "Hello World!" program with Pokitto |
Pokitto | 43:6183b12dd99c | 84 | * |
Pokitto | 43:6183b12dd99c | 85 | * #include "Pokitto.h" |
Pokitto | 43:6183b12dd99c | 86 | * |
Pokitto | 43:6183b12dd99c | 87 | * Pokitto::Core myApp; |
Pokitto | 43:6183b12dd99c | 88 | * |
Pokitto | 43:6183b12dd99c | 89 | * int main() { |
Pokitto | 43:6183b12dd99c | 90 | * myApp.begin(); // This starts up the console (the display, buttons etc.) |
Pokitto | 43:6183b12dd99c | 91 | * while(myApp.isRunning()) { |
Pokitto | 43:6183b12dd99c | 92 | * if(myApp.Update()) { |
Pokitto | 43:6183b12dd99c | 93 | * myApp.display.print("Hello World!"); |
Pokitto | 43:6183b12dd99c | 94 | * } |
Pokitto | 43:6183b12dd99c | 95 | * } |
Pokitto | 43:6183b12dd99c | 96 | * } |
Pokitto | 43:6183b12dd99c | 97 | * @endcode |
Pokitto | 43:6183b12dd99c | 98 | */ |
Pokitto | 43:6183b12dd99c | 99 | |
Pokitto | 43:6183b12dd99c | 100 | class Core |
Pokitto | 43:6183b12dd99c | 101 | { |
Pokitto | 43:6183b12dd99c | 102 | public: |
Pokitto | 43:6183b12dd99c | 103 | /** Create a Core runtime instance |
Pokitto | 43:6183b12dd99c | 104 | */ |
Pokitto | 43:6183b12dd99c | 105 | Core(); |
Pokitto | 43:6183b12dd99c | 106 | |
Pokitto | 43:6183b12dd99c | 107 | /** Backlight component of the Core runtime */ |
Pokitto | 43:6183b12dd99c | 108 | static Backlight backlight; |
Pokitto | 43:6183b12dd99c | 109 | /** Buttons component of the Core runtime */ |
Pokitto | 43:6183b12dd99c | 110 | static Buttons buttons; |
Pokitto | 43:6183b12dd99c | 111 | /** Battery component of the Core runtime */ |
Pokitto | 43:6183b12dd99c | 112 | static Battery battery; |
Pokitto | 43:6183b12dd99c | 113 | /** Sound component of the Core runtime */ |
Pokitto | 43:6183b12dd99c | 114 | static Sound sound; |
Pokitto | 43:6183b12dd99c | 115 | /** Display component of the Core runtime */ |
Pokitto | 43:6183b12dd99c | 116 | static Display display; |
Pokitto | 43:6183b12dd99c | 117 | |
Pokitto | 43:6183b12dd99c | 118 | // EXCECUTION CONTROL |
Pokitto | 43:6183b12dd99c | 119 | public: |
Pokitto | 43:6183b12dd99c | 120 | /** Initialize runtime (use this one) */ |
Pokitto | 43:6183b12dd99c | 121 | static void begin(); |
Pokitto | 43:6183b12dd99c | 122 | /** Initialize runtime (deprecated, avoid) */ |
Pokitto | 43:6183b12dd99c | 123 | static void init(); |
Pokitto | 43:6183b12dd99c | 124 | /** Initialize runtime with options (deprecated, avoid) */ |
Pokitto | 43:6183b12dd99c | 125 | static void init(uint8_t); |
Pokitto | 43:6183b12dd99c | 126 | /** Return run state (1 = running, 0 = shutting down) */ |
Pokitto | 43:6183b12dd99c | 127 | static bool isRunning(); |
Pokitto | 43:6183b12dd99c | 128 | /** Stop running */ |
Pokitto | 43:6183b12dd99c | 129 | static void quit(); |
Pokitto | 43:6183b12dd99c | 130 | //private: |
Pokitto | 43:6183b12dd99c | 131 | /** run_state is true as long as program is running */ |
Pokitto | 43:6183b12dd99c | 132 | static bool run_state; |
Pokitto | 43:6183b12dd99c | 133 | |
Pokitto | 43:6183b12dd99c | 134 | public: |
Pokitto | 43:6183b12dd99c | 135 | // INITIALIZATION |
Pokitto | 43:6183b12dd99c | 136 | /** Initialize display */ |
Pokitto | 43:6183b12dd99c | 137 | static void initDisplay(); |
Pokitto | 43:6183b12dd99c | 138 | /** Initialize random generator */ |
Pokitto | 43:6183b12dd99c | 139 | static void initRandom(); |
Pokitto | 43:6183b12dd99c | 140 | /** Initialize GPIO */ |
Pokitto | 43:6183b12dd99c | 141 | static void initGPIO(); |
Pokitto | 43:6183b12dd99c | 142 | /** Initialize LCD */ |
Pokitto | 43:6183b12dd99c | 143 | static void initLCD(); |
Pokitto | 43:6183b12dd99c | 144 | /** Initialize Audio */ |
Pokitto | 43:6183b12dd99c | 145 | static void initAudio(); |
Pokitto | 43:6183b12dd99c | 146 | |
Pokitto | 43:6183b12dd99c | 147 | |
Pokitto | 43:6183b12dd99c | 148 | // DISPLAY |
Pokitto | 43:6183b12dd99c | 149 | public: |
Pokitto | 43:6183b12dd99c | 150 | /** Initialize backlight */ |
Pokitto | 43:6183b12dd99c | 151 | static void initBacklight(); |
Pokitto | 43:6183b12dd99c | 152 | |
Pokitto | 43:6183b12dd99c | 153 | private: |
Pokitto | 43:6183b12dd99c | 154 | /** Backlight PWM pointer */ |
Pokitto | 43:6183b12dd99c | 155 | #ifndef POK_SIM |
Pokitto | 43:6183b12dd99c | 156 | static pwmout_t backlightpwm; |
Pokitto | 43:6183b12dd99c | 157 | #endif |
Pokitto | 43:6183b12dd99c | 158 | |
Pokitto | 43:6183b12dd99c | 159 | // TIMEKEEPING |
Pokitto | 43:6183b12dd99c | 160 | public: |
Pokitto | 43:6183b12dd99c | 161 | /** Initialize runtime clock */ |
Pokitto | 43:6183b12dd99c | 162 | static void initClock(); |
Pokitto | 43:6183b12dd99c | 163 | /** Get value of time elapsed during program in milliseconds */ |
Pokitto | 43:6183b12dd99c | 164 | static uint32_t getTime(); |
Pokitto | 43:6183b12dd99c | 165 | /** Wait for n milliseconds */ |
Pokitto | 43:6183b12dd99c | 166 | static void wait(uint16_t); |
Pokitto | 43:6183b12dd99c | 167 | /** FPS */ |
Pokitto | 43:6183b12dd99c | 168 | static uint32_t fps_counter; |
Pokitto | 43:6183b12dd99c | 169 | static bool fps_counter_updated; |
Pokitto | 43:6183b12dd99c | 170 | private: |
Pokitto | 43:6183b12dd99c | 171 | /** Time of next refresh */ |
Pokitto | 43:6183b12dd99c | 172 | static uint32_t refreshtime; |
Pokitto | 43:6183b12dd99c | 173 | |
Pokitto | 43:6183b12dd99c | 174 | // DIRECT TO SCREEN |
Pokitto | 43:6183b12dd99c | 175 | public: |
Pokitto | 43:6183b12dd99c | 176 | /** Display Pokitto logo */ |
Pokitto | 43:6183b12dd99c | 177 | static void showLogo(); |
Pokitto | 43:6183b12dd99c | 178 | static void showWarning(); |
Pokitto | 43:6183b12dd99c | 179 | static void setVolLimit(); |
Pokitto | 43:6183b12dd99c | 180 | |
Pokitto | 43:6183b12dd99c | 181 | // BUTTON INPUT HANDLING |
Pokitto | 43:6183b12dd99c | 182 | public: |
Pokitto | 43:6183b12dd99c | 183 | static void initButtons(); |
Pokitto | 43:6183b12dd99c | 184 | static void pollButtons(); |
Pokitto | 43:6183b12dd99c | 185 | static uint8_t leftBtn(); |
Pokitto | 43:6183b12dd99c | 186 | static uint8_t rightBtn(); |
Pokitto | 43:6183b12dd99c | 187 | static uint8_t upBtn(); |
Pokitto | 43:6183b12dd99c | 188 | static uint8_t downBtn(); |
Pokitto | 43:6183b12dd99c | 189 | static uint8_t aBtn(); |
Pokitto | 43:6183b12dd99c | 190 | static uint8_t bBtn(); |
Pokitto | 43:6183b12dd99c | 191 | static uint8_t cBtn(); |
Pokitto | 43:6183b12dd99c | 192 | static uint8_t leftHeld(); |
Pokitto | 43:6183b12dd99c | 193 | static uint8_t rightHeld(); |
Pokitto | 43:6183b12dd99c | 194 | static uint8_t upHeld(); |
Pokitto | 43:6183b12dd99c | 195 | static uint8_t downHeld(); |
Pokitto | 43:6183b12dd99c | 196 | static uint8_t aHeld(); |
Pokitto | 43:6183b12dd99c | 197 | static uint8_t bHeld(); |
Pokitto | 43:6183b12dd99c | 198 | static uint8_t cHeld(); |
Pokitto | 43:6183b12dd99c | 199 | |
Pokitto | 43:6183b12dd99c | 200 | static uint8_t leftReleased(); |
Pokitto | 43:6183b12dd99c | 201 | static uint8_t rightReleased(); |
Pokitto | 43:6183b12dd99c | 202 | static uint8_t upReleased(); |
Pokitto | 43:6183b12dd99c | 203 | static uint8_t downReleased(); |
Pokitto | 43:6183b12dd99c | 204 | static uint8_t aReleased(); |
Pokitto | 43:6183b12dd99c | 205 | static uint8_t bReleased(); |
Pokitto | 43:6183b12dd99c | 206 | static uint8_t cReleased(); |
Pokitto | 43:6183b12dd99c | 207 | |
Pokitto | 43:6183b12dd99c | 208 | // AUDIO RELATED |
Pokitto | 43:6183b12dd99c | 209 | static uint8_t ampIsOn(); |
Pokitto | 43:6183b12dd99c | 210 | static void ampEnable(uint8_t); |
Pokitto | 43:6183b12dd99c | 211 | static uint8_t soundbyte; |
Pokitto | 43:6183b12dd99c | 212 | |
Pokitto | 43:6183b12dd99c | 213 | // GB RELATED |
Pokitto | 43:6183b12dd99c | 214 | public: |
Pokitto | 43:6183b12dd99c | 215 | static void readSettings(); |
Pokitto | 43:6183b12dd99c | 216 | static void titleScreen(const char* name, const uint8_t *logo); |
Pokitto | 43:6183b12dd99c | 217 | static void titleScreen(const char* name); |
Pokitto | 43:6183b12dd99c | 218 | static void titleScreen(const uint8_t* logo); |
Pokitto | 43:6183b12dd99c | 219 | static void titleScreen(); |
Pokitto | 43:6183b12dd99c | 220 | static bool update(bool useDirectMode=false, uint8_t updRectX=0, uint8_t updRectY=0, uint8_t updRectW=LCDWIDTH, uint8_t updRectH=LCDHEIGHT); |
Pokitto | 43:6183b12dd99c | 221 | static uint32_t frameCount; |
Pokitto | 43:6183b12dd99c | 222 | static int8_t menu(const char* const* items, uint8_t length); |
Pokitto | 43:6183b12dd99c | 223 | static char* filemenu(char*); |
Pokitto | 43:6183b12dd99c | 224 | static char* filemenu(); |
Pokitto | 43:6183b12dd99c | 225 | static void keyboard(char* text, uint8_t length); |
Pokitto | 43:6183b12dd99c | 226 | static void popup(const char* text, uint8_t duration); |
Pokitto | 43:6183b12dd99c | 227 | static void setFrameRate(uint8_t fps); |
Pokitto | 43:6183b12dd99c | 228 | static void pickRandomSeed(); |
Pokitto | 43:6183b12dd99c | 229 | |
Pokitto | 43:6183b12dd99c | 230 | static uint8_t getCpuLoad(); |
Pokitto | 43:6183b12dd99c | 231 | static uint16_t getFreeRam(); |
Pokitto | 43:6183b12dd99c | 232 | |
Pokitto | 43:6183b12dd99c | 233 | static bool collidePointRect(int16_t x1, int16_t y1 ,int16_t x2 ,int16_t y2, int16_t w, int16_t h); |
Pokitto | 43:6183b12dd99c | 234 | 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 | 43:6183b12dd99c | 235 | static bool collideBitmapBitmap(int16_t x1, int16_t y1, const uint8_t* b1, int16_t x2, int16_t y2, const uint8_t* b2); |
Pokitto | 43:6183b12dd99c | 236 | |
Pokitto | 43:6183b12dd99c | 237 | private: |
Pokitto | 43:6183b12dd99c | 238 | static uint8_t timePerFrame; |
Pokitto | 43:6183b12dd99c | 239 | static uint32_t nextFrameMillis; |
Pokitto | 43:6183b12dd99c | 240 | static void updatePopup(); |
Pokitto | 43:6183b12dd99c | 241 | static const char* popupText; |
Pokitto | 43:6183b12dd99c | 242 | static uint8_t popupTimeLeft; |
Pokitto | 43:6183b12dd99c | 243 | static void displayBattery(); |
Pokitto | 43:6183b12dd99c | 244 | static uint16_t frameDurationMicros; |
Pokitto | 43:6183b12dd99c | 245 | static uint32_t frameStartMicros, frameEndMicros; |
Pokitto | 43:6183b12dd99c | 246 | static uint8_t startMenuTimer; |
Pokitto | 43:6183b12dd99c | 247 | static int updateLoader(uint32_t,uint32_t); |
Pokitto | 43:6183b12dd99c | 248 | static uint32_t fps_refreshtime; |
Pokitto | 43:6183b12dd99c | 249 | static uint32_t fps_frameCount; |
Pokitto | 43:6183b12dd99c | 250 | |
Pokitto | 43:6183b12dd99c | 251 | public: |
Pokitto | 43:6183b12dd99c | 252 | static uint8_t volbar_visible; |
Pokitto | 43:6183b12dd99c | 253 | static void drawvolbar(int,int,int, bool); |
Pokitto | 43:6183b12dd99c | 254 | static void askLoader(); |
Pokitto | 43:6183b12dd99c | 255 | static void jumpToLoader(); |
Pokitto | 43:6183b12dd99c | 256 | }; |
Pokitto | 43:6183b12dd99c | 257 | |
Pokitto | 43:6183b12dd99c | 258 | // this is the instance used by the system |
Pokitto | 43:6183b12dd99c | 259 | extern Core core; |
Pokitto | 43:6183b12dd99c | 260 | |
Pokitto | 43:6183b12dd99c | 261 | |
Pokitto | 43:6183b12dd99c | 262 | } |
Pokitto | 43:6183b12dd99c | 263 | |
Pokitto | 43:6183b12dd99c | 264 | #endif // POKITTOCORE_H |
Pokitto | 43:6183b12dd99c | 265 | |
Pokitto | 43:6183b12dd99c | 266 | |
Pokitto | 43:6183b12dd99c | 267 |