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