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:
Mon Apr 02 22:37:22 2018 +0000
Revision:
36:771321e70814
Synced with Github repo

Who changed what in which revision?

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