PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

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