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:
Sat Jul 21 11:39:21 2018 +0000
Revision:
53:ee5e28819ebb
Parent:
52:c04087025cab
Child:
54:7082c373d764
fixed countdown bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 52:c04087025cab 1 /**************************************************************************/
Pokitto 52:c04087025cab 2 /*!
Pokitto 52:c04087025cab 3 @file PokittoCore.cpp
Pokitto 52:c04087025cab 4 @author Jonne Valola
Pokitto 52:c04087025cab 5
Pokitto 52:c04087025cab 6 @section LICENSE
Pokitto 52:c04087025cab 7
Pokitto 52:c04087025cab 8 Software License Agreement (BSD License)
Pokitto 52:c04087025cab 9
Pokitto 52:c04087025cab 10 Copyright (c) 2016, Jonne Valola
Pokitto 52:c04087025cab 11 All rights reserved.
Pokitto 52:c04087025cab 12
Pokitto 52:c04087025cab 13 Redistribution and use in source and binary forms, with or without
Pokitto 52:c04087025cab 14 modification, are permitted provided that the following conditions are met:
Pokitto 52:c04087025cab 15 1. Redistributions of source code must retain the above copyright
Pokitto 52:c04087025cab 16 notice, this list of conditions and the following disclaimer.
Pokitto 52:c04087025cab 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 52:c04087025cab 18 notice, this list of conditions and the following disclaimer in the
Pokitto 52:c04087025cab 19 documentation and/or other materials provided with the distribution.
Pokitto 52:c04087025cab 20 3. Neither the name of the copyright holders nor the
Pokitto 52:c04087025cab 21 names of its contributors may be used to endorse or promote products
Pokitto 52:c04087025cab 22 derived from this software without specific prior written permission.
Pokitto 52:c04087025cab 23
Pokitto 52:c04087025cab 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 52:c04087025cab 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 52:c04087025cab 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 52:c04087025cab 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 52:c04087025cab 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 52:c04087025cab 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 52:c04087025cab 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 52:c04087025cab 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 52:c04087025cab 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 52:c04087025cab 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 52:c04087025cab 34 */
Pokitto 52:c04087025cab 35 /**************************************************************************/
Pokitto 52:c04087025cab 36
Pokitto 52:c04087025cab 37 #include "Pokitto_settings.h"
Pokitto 52:c04087025cab 38 #include "PokittoCore.h"
Pokitto 52:c04087025cab 39 #include "PokittoConsole.h"
Pokitto 52:c04087025cab 40 #include "PokittoFonts.h"
Pokitto 52:c04087025cab 41 #include "PokittoTimer.h"
Pokitto 52:c04087025cab 42 #include "PokittoLogos.h"
Pokitto 52:c04087025cab 43 #include <stdlib.h>
Pokitto 52:c04087025cab 44
Pokitto 52:c04087025cab 45 #ifndef DISABLEAVRMIN
Pokitto 52:c04087025cab 46 #define max(a,b) ((a)>(b)?(a):(b))
Pokitto 52:c04087025cab 47 #endif // DISABLEAVRMIN
Pokitto 52:c04087025cab 48
Pokitto 52:c04087025cab 49 char selectedfile[25];
Pokitto 52:c04087025cab 50
Pokitto 52:c04087025cab 51 //#define F
Pokitto 52:c04087025cab 52 #ifdef __ARMCC_VERSION
Pokitto 52:c04087025cab 53 typedef void (*func_t)(void);
Pokitto 52:c04087025cab 54 #endif
Pokitto 52:c04087025cab 55
Pokitto 52:c04087025cab 56 #ifndef POK_SIM
Pokitto 52:c04087025cab 57 #include "iap.h"
Pokitto 52:c04087025cab 58 /** start the user application
Pokitto 52:c04087025cab 59 * https://community.nxp.com/thread/417695
Pokitto 52:c04087025cab 60 *
Pokitto 52:c04087025cab 61 */
Pokitto 52:c04087025cab 62 void start_application(unsigned long app_link_location){
Pokitto 52:c04087025cab 63 //asm(" ldr sp, [r0,#0]");
Pokitto 52:c04087025cab 64 //asm(" ldr pc, [r0,#4]");
Pokitto 52:c04087025cab 65 //This code is not valid for the Cortex-m0+ instruction set.
Pokitto 52:c04087025cab 66 // The equivalent for this (as used by the KL26) is
Pokitto 52:c04087025cab 67 __disable_irq();// Start by disabling interrupts, before changing interrupt vectors
Pokitto 52:c04087025cab 68
Pokitto 52:c04087025cab 69 // delete buttons
Pokitto 52:c04087025cab 70 //pokDeleteButtons();
Pokitto 52:c04087025cab 71
Pokitto 52:c04087025cab 72 // completely kill button interrupts in preparation for reset
Pokitto 52:c04087025cab 73 LPC_PINT->IENR = 0;
Pokitto 52:c04087025cab 74 LPC_PINT->IENF = 0;
Pokitto 52:c04087025cab 75
Pokitto 52:c04087025cab 76 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk & ~(SysTick_CTRL_ENABLE_Msk); //disable systick
Pokitto 52:c04087025cab 77 LPC_SYSCON->PDRUNCFG |= (1 << 10); /* Power-down USB PHY */
Pokitto 52:c04087025cab 78 LPC_SYSCON->PDRUNCFG |= (1 << 8); /* Power-down USB PLL */
Pokitto 52:c04087025cab 79
Pokitto 52:c04087025cab 80 // reset clock source to IRC
Pokitto 52:c04087025cab 81 LPC_SYSCON->MAINCLKUEN = 0x01; /* Update MCLK Clock Source */
Pokitto 52:c04087025cab 82 LPC_SYSCON->MAINCLKUEN = 0x00; /* Toggle Update Register */
Pokitto 52:c04087025cab 83 while (LPC_SYSCON->MAINCLKUEN & 0x01); /* Wait Until Updated */
Pokitto 52:c04087025cab 84 // switch clock selection to IRC
Pokitto 52:c04087025cab 85 LPC_SYSCON->MAINCLKSEL = 0; /* Select Clock Source */
Pokitto 52:c04087025cab 86 LPC_SYSCON->MAINCLKUEN = 0x01; /* Update MCLK Clock Source */
Pokitto 52:c04087025cab 87 LPC_SYSCON->MAINCLKUEN = 0x00; /* Toggle Update Register */
Pokitto 52:c04087025cab 88 while (LPC_SYSCON->MAINCLKUEN & 0x01); /* Wait Until Updated */
Pokitto 52:c04087025cab 89 //disable PLL clock output
Pokitto 52:c04087025cab 90 LPC_SYSCON->SYSPLLCLKUEN = 0;
Pokitto 52:c04087025cab 91 while (LPC_SYSCON->SYSPLLCLKUEN & 0x00);
Pokitto 52:c04087025cab 92 LPC_SYSCON->SYSPLLCTRL = 0;
Pokitto 52:c04087025cab 93
Pokitto 52:c04087025cab 94 //kill peripherals
Pokitto 52:c04087025cab 95 LPC_SYSCON->MAINCLKSEL = 0;
Pokitto 52:c04087025cab 96 LPC_SYSCON->PRESETCTRL = 0; //disable all peripherals
Pokitto 52:c04087025cab 97
Pokitto 52:c04087025cab 98 //power down PLL
Pokitto 52:c04087025cab 99 volatile uint32_t tmp;
Pokitto 52:c04087025cab 100 tmp = (LPC_SYSCON->PDRUNCFG & 0x000025FFL);
Pokitto 52:c04087025cab 101 tmp |= ((1<<7) & 0x000025FFL);
Pokitto 52:c04087025cab 102 LPC_SYSCON->PDRUNCFG = (tmp | 0x0000C800L); /* Power-down SYSPLL */
Pokitto 52:c04087025cab 103
Pokitto 52:c04087025cab 104 //Chip_Clock_SetMainClockSource(SYSCTL_MAINCLKSRC_IRC); //switch to IRC
Pokitto 52:c04087025cab 105
Pokitto 52:c04087025cab 106 // clear all gpio states
Pokitto 52:c04087025cab 107 LPC_GPIO_PORT->PIN[0] = 0;
Pokitto 52:c04087025cab 108 LPC_GPIO_PORT->PIN[1] = 0;
Pokitto 52:c04087025cab 109 LPC_GPIO_PORT->PIN[2] = 0;
Pokitto 52:c04087025cab 110
Pokitto 52:c04087025cab 111 SCB->VTOR = app_link_location;//APPL_ADDRESS; /* Change vector table address */
Pokitto 52:c04087025cab 112 #ifndef __ARMCC_VERSION
Pokitto 52:c04087025cab 113 asm(" mov r0, %[address]"::[address] "r" (app_link_location));
Pokitto 52:c04087025cab 114 asm(" ldr r1, [r0,#0]"); // get the stack pointer value from the program's reset vector
Pokitto 52:c04087025cab 115 asm(" mov sp, r1"); // copy the value to the stack pointer
Pokitto 52:c04087025cab 116 asm(" ldr r0, [r0,#4]"); // get the program counter value from the program's reset vector
Pokitto 52:c04087025cab 117 asm(" blx r0"); // jump to the' start address
Pokitto 52:c04087025cab 118 #else
Pokitto 52:c04087025cab 119 uint32_t *app_loc = (uint32_t*)app_link_location;
Pokitto 52:c04087025cab 120 __set_MSP (app_loc[0]);
Pokitto 52:c04087025cab 121 ((func_t)(app_loc[1]))();
Pokitto 52:c04087025cab 122 #endif
Pokitto 52:c04087025cab 123 }
Pokitto 52:c04087025cab 124 #endif
Pokitto 52:c04087025cab 125
Pokitto 52:c04087025cab 126 // returns a random integar between 0 and maxVal
Pokitto 52:c04087025cab 127 int random(int maxVal)
Pokitto 52:c04087025cab 128 {
Pokitto 52:c04087025cab 129 return random( 0, maxVal);
Pokitto 52:c04087025cab 130 }
Pokitto 52:c04087025cab 131
Pokitto 52:c04087025cab 132 // returns a random integar between minVal and maxVal
Pokitto 52:c04087025cab 133 int random(int minVal, int maxVal)
Pokitto 52:c04087025cab 134 {
Pokitto 52:c04087025cab 135 // int rand(void); included by default from newlib
Pokitto 52:c04087025cab 136 #ifdef OLDINCLUSIVERANDOM
Pokitto 52:c04087025cab 137 return rand() % (maxVal-minVal+1) + minVal;
Pokitto 52:c04087025cab 138 #else
Pokitto 52:c04087025cab 139 return rand() % (maxVal-minVal) + minVal;
Pokitto 52:c04087025cab 140 #endif
Pokitto 52:c04087025cab 141 }
Pokitto 52:c04087025cab 142
Pokitto 52:c04087025cab 143 using namespace Pokitto;
Pokitto 52:c04087025cab 144
Pokitto 52:c04087025cab 145 bool Core::run_state; // this definition needed
Pokitto 52:c04087025cab 146
Pokitto 52:c04087025cab 147 /** Components */
Pokitto 52:c04087025cab 148 Backlight Core::backlight;
Pokitto 52:c04087025cab 149 Buttons Core::buttons;
Pokitto 52:c04087025cab 150 Battery Core::battery;
Pokitto 52:c04087025cab 151 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 152 Sound Core::sound;
Pokitto 52:c04087025cab 153 #endif
Pokitto 52:c04087025cab 154 Display Core::display;
Pokitto 52:c04087025cab 155
Pokitto 52:c04087025cab 156 //GB Related
Pokitto 52:c04087025cab 157 uint8_t Core::startMenuTimer;
Pokitto 52:c04087025cab 158 uint8_t Core::timePerFrame;
Pokitto 52:c04087025cab 159 uint32_t Core::nextFrameMillis;
Pokitto 52:c04087025cab 160 uint32_t Core::frameCount;
Pokitto 52:c04087025cab 161 const char* Core::popupText;
Pokitto 52:c04087025cab 162 uint8_t Core::popupTimeLeft;
Pokitto 52:c04087025cab 163 uint16_t Core::frameDurationMicros;
Pokitto 52:c04087025cab 164 uint32_t Core::frameStartMicros, Core::frameEndMicros;
Pokitto 52:c04087025cab 165 uint8_t Core::volbar_visible=0;
Pokitto 52:c04087025cab 166
Pokitto 52:c04087025cab 167 uint32_t Core::fps_counter;
Pokitto 52:c04087025cab 168 bool Core::fps_counter_updated;
Pokitto 52:c04087025cab 169 uint32_t Core::fps_refreshtime;
Pokitto 52:c04087025cab 170 uint32_t Core::fps_frameCount;
Pokitto 52:c04087025cab 171
Pokitto 52:c04087025cab 172 Core::Core() {
Pokitto 52:c04087025cab 173
Pokitto 52:c04087025cab 174 }
Pokitto 52:c04087025cab 175
Pokitto 52:c04087025cab 176
Pokitto 52:c04087025cab 177 int Core::updateLoader (uint32_t version, uint32_t jumpaddress) {
Pokitto 52:c04087025cab 178 #ifndef POK_SIM
Pokitto 52:c04087025cab 179 uint32_t counter=0;
Pokitto 52:c04087025cab 180 uint8_t data[256];
Pokitto 52:c04087025cab 181 /** prepare the flash writing **/
Pokitto 52:c04087025cab 182 float progress=0;
Pokitto 52:c04087025cab 183 int opg=-1;
Pokitto 52:c04087025cab 184 uint32_t fsize=0;
Pokitto 52:c04087025cab 185 fileEnd(); //
Pokitto 52:c04087025cab 186 fsize = fileGetPosition();
Pokitto 52:c04087025cab 187 if (fsize>0x40000-jumpaddress) fsize = 0x40000-jumpaddress; // shouldn't happen!!
Pokitto 52:c04087025cab 188 fileRewind();
Pokitto 52:c04087025cab 189 display.println("PLEASE WAIT");
Pokitto 52:c04087025cab 190 while (1) {
Pokitto 52:c04087025cab 191 //if (counter >= fsize-0x200) {
Pokitto 52:c04087025cab 192 // display.println("gotcha");
Pokitto 52:c04087025cab 193 //}
Pokitto 52:c04087025cab 194 if (counter >= fsize) {
Pokitto 52:c04087025cab 195 break;
Pokitto 52:c04087025cab 196 }
Pokitto 52:c04087025cab 197 opg=progress;
Pokitto 52:c04087025cab 198 if (fileReadBytes(&data[0],0x100)<0x100) {
Pokitto 52:c04087025cab 199 if (fsize-counter>0x100) {
Pokitto 52:c04087025cab 200 display.println("ERROR READING LOA.DER FILE");
Pokitto 52:c04087025cab 201 return 1; // 1 means error
Pokitto 52:c04087025cab 202 }
Pokitto 52:c04087025cab 203 }
Pokitto 52:c04087025cab 204 if (CopyPageToFlash(jumpaddress+counter,data)) {
Pokitto 52:c04087025cab 205 display.println("FLASH WRITE ERROR");
Pokitto 52:c04087025cab 206 return 1;
Pokitto 52:c04087025cab 207 } else {
Pokitto 52:c04087025cab 208 counter += 0x100;
Pokitto 52:c04087025cab 209 display.print(".");
Pokitto 52:c04087025cab 210 }
Pokitto 52:c04087025cab 211 }
Pokitto 52:c04087025cab 212 #endif // POK_SIM
Pokitto 52:c04087025cab 213 return 0; //success
Pokitto 52:c04087025cab 214 }
Pokitto 52:c04087025cab 215
Pokitto 52:c04087025cab 216 void Core::showWarning() {
Pokitto 52:c04087025cab 217 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 218 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 219 display.clearLCD();
Pokitto 52:c04087025cab 220 display.directcolor = COLOR_RED;
Pokitto 52:c04087025cab 221 display.setFont(fntC64UIGfx);
Pokitto 52:c04087025cab 222 display.adjustCharStep = 0;
Pokitto 52:c04087025cab 223 display.adjustLineStep = 0;
Pokitto 52:c04087025cab 224 display.setCursor(10*8,9);
Pokitto 52:c04087025cab 225 display.print("WARNING!");
Pokitto 52:c04087025cab 226 display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 227 display.setCursor(5*8,4*9);
Pokitto 52:c04087025cab 228 display.print("LOUD SOUND THROUGH");
Pokitto 52:c04087025cab 229 display.setCursor(2*8,5*9);
Pokitto 52:c04087025cab 230 display.print("HEADPHONES COULD DAMAGE");
Pokitto 52:c04087025cab 231 display.setCursor(7*8,6*9);
Pokitto 52:c04087025cab 232 display.print("YOUR HEARING.");
Pokitto 52:c04087025cab 233 display.setCursor(5*8,8*9);
Pokitto 52:c04087025cab 234 display.print("USE "); display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 235 display.print("0-100% VOLUME");
Pokitto 52:c04087025cab 236 display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 237 display.setCursor(5*8,9*9);
Pokitto 52:c04087025cab 238 display.print("FOR LISTENING WITH");
Pokitto 52:c04087025cab 239 display.setCursor(8*8,10*9);
Pokitto 52:c04087025cab 240 display.print("HEADPHONES");
Pokitto 52:c04087025cab 241 display.setCursor(5*8,12*9);
Pokitto 52:c04087025cab 242 display.print("USE "); display.directcolor = COLOR_RED;
Pokitto 52:c04087025cab 243 display.print("> 100% VOLUME"); display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 244 display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 245 display.setCursor(1*8,13*9);
Pokitto 52:c04087025cab 246 display.print("ONLY FOR LISTENING VIA THE");
Pokitto 52:c04087025cab 247 display.setCursor(5*8,14*9);
Pokitto 52:c04087025cab 248 display.print("BUILT-IN SPEAKER");
Pokitto 52:c04087025cab 249 display.setCursor(5*8,17*9);
Pokitto 52:c04087025cab 250 display.print("PRESS ");display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 251 display.print("C ");display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 252 display.print("TO ACCEPT");
Pokitto 52:c04087025cab 253 while (!buttons.cBtn()) {
Pokitto 52:c04087025cab 254 wait(100);
Pokitto 52:c04087025cab 255 }
Pokitto 52:c04087025cab 256 display.clearLCD();
Pokitto 52:c04087025cab 257 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 258 }
Pokitto 52:c04087025cab 259
Pokitto 52:c04087025cab 260 void Core::jumpToLoader() {
Pokitto 52:c04087025cab 261 //display.setFont(font5x7);
Pokitto 52:c04087025cab 262 //display.adjustCharStep=1;
Pokitto 52:c04087025cab 263 //display.adjustLineStep=2;
Pokitto 52:c04087025cab 264 display.fontSize=1;
Pokitto 52:c04087025cab 265 display.directbgcolor=COLOR_BLACK;
Pokitto 52:c04087025cab 266 display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 267 display.clearLCD();
Pokitto 52:c04087025cab 268 display.setCursor(0,0);
Pokitto 52:c04087025cab 269 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 270 #ifdef POK_SIM
Pokitto 52:c04087025cab 271 display.println("LOADER IS NOT AVAILABLE ON THE SIMULATOR. PRESS A TO RETURN.");
Pokitto 52:c04087025cab 272 #else
Pokitto 52:c04087025cab 273 uint32_t* bootinfo;
Pokitto 52:c04087025cab 274 uint32_t bootversion=0, sdversion=0, sdjump=0;
Pokitto 52:c04087025cab 275 bool flashloader=false, checkforboot=true;
Pokitto 52:c04087025cab 276 //check for loa.der on SD card
Pokitto 52:c04087025cab 277 #if POK_ENABLE_LOADER_UPDATES > 0
Pokitto 52:c04087025cab 278 pokInitSD();
Pokitto 52:c04087025cab 279 if (fileOpen("LOA.DER", FILE_MODE_BINARY)==0) {
Pokitto 52:c04087025cab 280 //LOA.DER found on SD
Pokitto 52:c04087025cab 281 fileEnd(); // go to end
Pokitto 52:c04087025cab 282 fileSeekRelative(-8); //rewind 8 bytes
Pokitto 52:c04087025cab 283 uint32_t* tptr = &sdversion;
Pokitto 52:c04087025cab 284 fileReadBytes((uint8_t*)tptr,4); //read version number of loader on SD card
Pokitto 52:c04087025cab 285 tptr = &sdjump;
Pokitto 52:c04087025cab 286 fileReadBytes((uint8_t*)tptr,4); //read jump address of loader on sd card
Pokitto 52:c04087025cab 287 fileRewind();
Pokitto 52:c04087025cab 288 }
Pokitto 52:c04087025cab 289 #endif
Pokitto 52:c04087025cab 290 //now start searching for bootkey
Pokitto 52:c04087025cab 291 while (checkforboot)
Pokitto 52:c04087025cab 292 {
Pokitto 52:c04087025cab 293 checkforboot=false; flashloader=false;
Pokitto 52:c04087025cab 294 bootinfo = (uint32_t*)0x3FFF4;
Pokitto 52:c04087025cab 295 if (*bootinfo != 0xB007AB1E) bootinfo = (uint32_t*)0x3FF04; //allow couple of alternative locations
Pokitto 52:c04087025cab 296 if (*bootinfo != 0xB007AB1E) bootinfo = (uint32_t*)0x3FE04; //allow couple of alternative locations
Pokitto 52:c04087025cab 297 if (*bootinfo != 0xB007AB1E) bootinfo = (uint32_t*)0x3F004; //for futureproofing
Pokitto 52:c04087025cab 298 if (*bootinfo != 0xB007AB1E) {
Pokitto 52:c04087025cab 299 // no bootkey found at all
Pokitto 52:c04087025cab 300 display.directcolor=COLOR_YELLOW;
Pokitto 52:c04087025cab 301 display.println("NO LOADER INSTALLED");
Pokitto 52:c04087025cab 302 if (sdversion==0 || sdjump < 0x38000) {
Pokitto 52:c04087025cab 303 //file open of loader failed
Pokitto 52:c04087025cab 304 display.println("NO VALID LOA.DER ON SD");
Pokitto 52:c04087025cab 305 display.println("");
Pokitto 52:c04087025cab 306 display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 307 display.println("PUT LOA.DER ON SD & REBOOT");
Pokitto 52:c04087025cab 308 } else flashloader=true;
Pokitto 52:c04087025cab 309 } else {
Pokitto 52:c04087025cab 310 //loader was found
Pokitto 52:c04087025cab 311 //check if we should update the loader
Pokitto 52:c04087025cab 312 display.directcolor=COLOR_CYAN;
Pokitto 52:c04087025cab 313 display.print("LOADER V.");
Pokitto 52:c04087025cab 314 display.directcolor=COLOR_WHITE;
Pokitto 52:c04087025cab 315 display.println(*(bootinfo+1));
Pokitto 52:c04087025cab 316 #if POK_ENABLE_LOADER_UPDATES
Pokitto 52:c04087025cab 317 if (sdversion>(*(bootinfo+1))) flashloader=true;
Pokitto 52:c04087025cab 318 else start_application(*(bootinfo+2)); //never returns
Pokitto 52:c04087025cab 319 #else
Pokitto 52:c04087025cab 320 start_application(*(bootinfo+2)); //never returns
Pokitto 52:c04087025cab 321 #endif
Pokitto 52:c04087025cab 322 }
Pokitto 52:c04087025cab 323 // update loader if it was requested
Pokitto 52:c04087025cab 324 if(flashloader) {
Pokitto 52:c04087025cab 325 display.directcolor=COLOR_MAGENTA;
Pokitto 52:c04087025cab 326 display.print("NEW LOADER ON SD V.");
Pokitto 52:c04087025cab 327 display.directcolor=COLOR_WHITE;
Pokitto 52:c04087025cab 328 display.println(sdversion);
Pokitto 52:c04087025cab 329 display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 330 display.println("UPDATE LOADER?\n(UP=YES, DOWN=CANCEL)");
Pokitto 52:c04087025cab 331 while(1) {
Pokitto 52:c04087025cab 332 if (buttons.upBtn()) {
Pokitto 52:c04087025cab 333 if (updateLoader(sdversion,sdjump)) {
Pokitto 52:c04087025cab 334 display.println("PUT LOA.DER ON SD AND RETRY");
Pokitto 52:c04087025cab 335 } else {
Pokitto 52:c04087025cab 336 display.println("SUCCESS!!");
Pokitto 52:c04087025cab 337 checkforboot=true; //recheck
Pokitto 52:c04087025cab 338 }
Pokitto 52:c04087025cab 339 break;
Pokitto 52:c04087025cab 340 }
Pokitto 52:c04087025cab 341 if (buttons.downBtn()) return;
Pokitto 52:c04087025cab 342 }
Pokitto 52:c04087025cab 343 } // if flashloader
Pokitto 52:c04087025cab 344 } // while checkforboot
Pokitto 52:c04087025cab 345 #endif // POK_SIM
Pokitto 52:c04087025cab 346 while (!buttons.aBtn()) {
Pokitto 52:c04087025cab 347 buttons.pollButtons();
Pokitto 52:c04087025cab 348 if (buttons.aBtn()) {
Pokitto 52:c04087025cab 349 while (buttons.aBtn()) {
Pokitto 52:c04087025cab 350 buttons.pollButtons();
Pokitto 52:c04087025cab 351 }
Pokitto 52:c04087025cab 352 return;
Pokitto 52:c04087025cab 353 }
Pokitto 52:c04087025cab 354 }
Pokitto 52:c04087025cab 355 }
Pokitto 52:c04087025cab 356
Pokitto 52:c04087025cab 357 void Core::askLoader() {
Pokitto 52:c04087025cab 358 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 359 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 360 display.clearLCD();
Pokitto 52:c04087025cab 361 display.directcolor = COLOR_RED;
Pokitto 52:c04087025cab 362 display.setFont(fntC64UIGfx);
Pokitto 52:c04087025cab 363 display.fontSize=1;
Pokitto 52:c04087025cab 364 display.adjustCharStep = 0;
Pokitto 52:c04087025cab 365 display.adjustLineStep = 0;
Pokitto 52:c04087025cab 366 display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 367 display.set_cursor(12*8,6*8);
Pokitto 52:c04087025cab 368 display.print("ijkl");
Pokitto 52:c04087025cab 369 display.set_cursor(12*8,7*8);
Pokitto 52:c04087025cab 370 display.print("mnop");
Pokitto 52:c04087025cab 371 display.set_cursor(12*8,8*8);
Pokitto 52:c04087025cab 372 display.print("qrst");
Pokitto 52:c04087025cab 373 display.set_cursor(12*8,9*8);
Pokitto 52:c04087025cab 374 display.print("uvwx");
Pokitto 52:c04087025cab 375 display.set_cursor(5*8,12*8);
Pokitto 52:c04087025cab 376 display.print("PRESS");
Pokitto 52:c04087025cab 377 display.directcolor=COLOR_WHITE;
Pokitto 52:c04087025cab 378 display.print(" C ");
Pokitto 52:c04087025cab 379 display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 380 display.print("FOR LOADER");
Pokitto 52:c04087025cab 381 display.directcolor=COLOR_WHITE;
Pokitto 52:c04087025cab 382 display.fontSize=2;
Pokitto 52:c04087025cab 383 int countd = 0;
Pokitto 52:c04087025cab 384 #ifndef POK_SIM
Pokitto 52:c04087025cab 385 //read countdown time from settings
Pokitto 52:c04087025cab 386 countd = eeprom_read_byte((uint16_t*)EESETTINGS_LOADERWAIT);
Pokitto 52:c04087025cab 387 #endif
Pokitto 52:c04087025cab 388 if (countd<=0 || countd > 5) countd=3;
Pokitto 52:c04087025cab 389 uint16_t c2 = getTime();
Pokitto 52:c04087025cab 390 while (countd) {
Pokitto 52:c04087025cab 391 buttons.pollButtons();
Pokitto 52:c04087025cab 392 display.set_cursor(13*8,15*8);
Pokitto 52:c04087025cab 393 display.print(countd);
Pokitto 52:c04087025cab 394 if (getTime()>c2+1000) {
Pokitto 52:c04087025cab 395 c2=getTime();
Pokitto 52:c04087025cab 396 countd--;
Pokitto 52:c04087025cab 397 }
Pokitto 52:c04087025cab 398 if (cBtn()) {while (cBtn()) buttons.pollButtons();jumpToLoader();countd=0;}
Pokitto 52:c04087025cab 399 if (aBtn()) {while (aBtn()) buttons.pollButtons();countd=0;}
Pokitto 52:c04087025cab 400 if (bBtn()) {while (bBtn()) buttons.pollButtons();countd=0;}
Pokitto 52:c04087025cab 401 }
Pokitto 52:c04087025cab 402 display.fontSize=1;
Pokitto 52:c04087025cab 403 display.clearLCD();
Pokitto 52:c04087025cab 404 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 405 }
Pokitto 52:c04087025cab 406
Pokitto 52:c04087025cab 407
Pokitto 52:c04087025cab 408 void Core::drawvolbar(int x, int y, int level, bool text) {
Pokitto 52:c04087025cab 409 uint16_t oldcol = display.directcolor;
Pokitto 52:c04087025cab 410 level = level >> 5;
Pokitto 52:c04087025cab 411 if (text) display.directRectangle(0,0,50,50,COLOR_BLACK);
Pokitto 52:c04087025cab 412 if (level<4) display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 413 if (level==4) display.directcolor = COLOR_YELLOW;
Pokitto 52:c04087025cab 414 if (level>4) display.directcolor = COLOR_RED;
Pokitto 52:c04087025cab 415
Pokitto 52:c04087025cab 416 if (text) {
Pokitto 52:c04087025cab 417 bool temp = display.isDirectPrintingEnabled();
Pokitto 52:c04087025cab 418 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 419 display.print(x-1,y-20,(int)sound.getVolume());
Pokitto 52:c04087025cab 420 display.print(" ");
Pokitto 52:c04087025cab 421 display.enableDirectPrinting(temp);
Pokitto 52:c04087025cab 422 }
Pokitto 52:c04087025cab 423 if (level<1) display.directcolor = COLOR_GRAY_80;
Pokitto 52:c04087025cab 424 display.directBitmap(x,y,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 425 if (level<2) display.directcolor = COLOR_GRAY_80;
Pokitto 52:c04087025cab 426 display.directBitmap(x+8,y,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 427 display.directBitmap(x+8,y-4,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 428 if (level<3) display.directcolor = COLOR_GRAY_80;
Pokitto 52:c04087025cab 429 display.directBitmap(x+16,y,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 430 display.directBitmap(x+16,y-4,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 431 display.directBitmap(x+16,y-8,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 432 if (level<4) {
Pokitto 52:c04087025cab 433 display.directcolor = COLOR_GRAY_80;
Pokitto 52:c04087025cab 434 }
Pokitto 52:c04087025cab 435 display.directBitmap(x+24,y,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 436 display.directBitmap(x+24,y-4,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 437 display.directBitmap(x+24,y-8,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 438 display.directBitmap(x+24,y-12,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 439
Pokitto 52:c04087025cab 440 if (level<5) {
Pokitto 52:c04087025cab 441 display.directcolor = COLOR_GRAY_80;
Pokitto 52:c04087025cab 442 }
Pokitto 52:c04087025cab 443 display.directBitmap(x+32,y,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 444 display.directBitmap(x+32,y-4,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 445 display.directBitmap(x+32,y-8,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 446 display.directBitmap(x+32,y-12,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 447 display.directBitmap(x+32,y-16,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 448
Pokitto 52:c04087025cab 449 if (level<6) {
Pokitto 52:c04087025cab 450 display.directcolor = COLOR_GRAY_80;
Pokitto 52:c04087025cab 451 }
Pokitto 52:c04087025cab 452 display.directBitmap(x+40,y,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 453 display.directBitmap(x+40,y-4,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 454 display.directBitmap(x+40,y-8,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 455 display.directBitmap(x+40,y-12,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 456 display.directBitmap(x+40,y-16,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 457 display.directBitmap(x+40,y-20,Pokitto_volumebar,1,1);
Pokitto 52:c04087025cab 458 display.directcolor = oldcol;
Pokitto 52:c04087025cab 459 }
Pokitto 52:c04087025cab 460
Pokitto 52:c04087025cab 461
Pokitto 52:c04087025cab 462 #ifdef POK_SIM
Pokitto 52:c04087025cab 463 #define VINCMULT 0.9f
Pokitto 52:c04087025cab 464 #else
Pokitto 52:c04087025cab 465 #define VINCMULT 50
Pokitto 52:c04087025cab 466 #endif //POK_SIM
Pokitto 52:c04087025cab 467 void Core::setVolLimit() {
Pokitto 52:c04087025cab 468 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 469 display.adjustCharStep = 0;
Pokitto 52:c04087025cab 470 //sound.setMaxVol(VOLUME_HEADPHONE_MAX);
Pokitto 52:c04087025cab 471 int dstate=1;
Pokitto 52:c04087025cab 472 bool wipe = true;
Pokitto 52:c04087025cab 473 float vol = sound.getVolume(); float tvol;
Pokitto 52:c04087025cab 474 #ifndef POK_SIM
Pokitto 52:c04087025cab 475 vol=eeprom_read_byte((uint16_t*)EESETTINGS_VOL);
Pokitto 53:ee5e28819ebb 476 if (vol<0) vol=0;
Pokitto 52:c04087025cab 477 Pokitto::Sound::globalVolume=vol;
Pokitto 52:c04087025cab 478 #endif
Pokitto 52:c04087025cab 479 if (vol>VOLUME_HEADPHONE_MAX) sound.setMaxVol(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 480 else sound.setMaxVol(VOLUME_HEADPHONE_MAX);
Pokitto 52:c04087025cab 481 #ifdef PRODUCTIONTESTING
Pokitto 52:c04087025cab 482 vol=170;
Pokitto 52:c04087025cab 483 sound.setMaxVol(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 484 #endif
Pokitto 52:c04087025cab 485 for (uint8_t t=0;t<=vol;t++) {
Pokitto 52:c04087025cab 486 sound.setVolume(t);
Pokitto 52:c04087025cab 487 }
Pokitto 52:c04087025cab 488 volbar_visible=0;
Pokitto 52:c04087025cab 489 int countd=0;
Pokitto 52:c04087025cab 490 #ifndef POK_SIM
Pokitto 52:c04087025cab 491 //read countdown time from settings
Pokitto 52:c04087025cab 492 countd = eeprom_read_byte((uint16_t*)EESETTINGS_VOLWAIT);
Pokitto 52:c04087025cab 493 #endif
Pokitto 53:ee5e28819ebb 494 if (countd<=0 || countd > 10) countd=0xFFFF;
Pokitto 52:c04087025cab 495 #ifdef PRODUCTIONTESTING
Pokitto 52:c04087025cab 496 countd=2;
Pokitto 52:c04087025cab 497 #endif
Pokitto 52:c04087025cab 498 uint16_t c2 = getTime();
Pokitto 52:c04087025cab 499 while (core.isRunning() && dstate && countd){
Pokitto 52:c04087025cab 500 if (getTime()>c2+1000) {
Pokitto 52:c04087025cab 501 c2=getTime();
Pokitto 52:c04087025cab 502 if (countd<0xFFFF) countd--;
Pokitto 52:c04087025cab 503 }
Pokitto 52:c04087025cab 504 switch (dstate) {
Pokitto 52:c04087025cab 505 case 1:
Pokitto 52:c04087025cab 506 //redraw
Pokitto 52:c04087025cab 507 if (wipe) {
Pokitto 52:c04087025cab 508 display.clearLCD();
Pokitto 52:c04087025cab 509 display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 510 display.setCursor(4*8,2*8);
Pokitto 52:c04087025cab 511 display.print("SELECT VOLUME LIMIT");
Pokitto 52:c04087025cab 512 display.setCursor(5*8,17*9);
Pokitto 52:c04087025cab 513 display.print("PRESS ");
Pokitto 52:c04087025cab 514 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 515 display.print("A");
Pokitto 52:c04087025cab 516 display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 517 display.print(" TO ACCEPT");
Pokitto 52:c04087025cab 518 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 519 // draw frame below first
Pokitto 52:c04087025cab 520 display.setCursor(0,11*8);
Pokitto 52:c04087025cab 521 display.println(" abbbbbbbbbbbbbbbbbbbbbbbc");
Pokitto 52:c04087025cab 522 display.println(" | |");
Pokitto 52:c04087025cab 523 display.println(" | |");
Pokitto 52:c04087025cab 524 display.println(" | |");
Pokitto 52:c04087025cab 525 display.println(" | |");
Pokitto 52:c04087025cab 526 display.println(" dbbbbbbbbbbbbbbbbbbbbbbbe");
Pokitto 52:c04087025cab 527 } // wipe = true
Pokitto 52:c04087025cab 528 display.setCursor(6*8,17*9);
Pokitto 52:c04087025cab 529 if (discrete_vol<4) display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 530 else display.directcolor = COLOR_RED;
Pokitto 52:c04087025cab 531 display.directBitmap(21*8-4,6*8,Pokitto_headphones,1,2);
Pokitto 52:c04087025cab 532 display.setCursor(3*8,6*8+6);
Pokitto 52:c04087025cab 533 display.print("HEADPHONES");
Pokitto 52:c04087025cab 534 display.setCursor(3*8,8*8+2);
Pokitto 52:c04087025cab 535 if (discrete_vol>3) display.print("TOO LOUD!");
Pokitto 52:c04087025cab 536 else display.print("OK ");
Pokitto 52:c04087025cab 537 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 538 display.directBitmap(21*8-4,12*8,Pokitto_speaker,1,2);
Pokitto 52:c04087025cab 539 display.setCursor(3*8,12*8+6);
Pokitto 52:c04087025cab 540 display.print("VOLUME MAX");
Pokitto 52:c04087025cab 541 display.setCursor(3*8,14*8+2);
Pokitto 52:c04087025cab 542 //tvol = (vol/float(VOLUME_HEADPHONE_MAX))*100;
Pokitto 52:c04087025cab 543 //if (tvol > 100 && tvol < 120) tvol=100;
Pokitto 52:c04087025cab 544 //if (sound.getVolume()-5>VOLUME_HEADPHONE_MAX) { display.directcolor=COLOR_RED;}
Pokitto 52:c04087025cab 545 if ((sound.getVolume()>>5)>3) { display.directcolor=COLOR_RED;}
Pokitto 52:c04087025cab 546 else display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 547 if (discrete_vol==4) display.directcolor=COLOR_YELLOW;
Pokitto 52:c04087025cab 548 if (discrete_vol==7) discrete_vol=6;
Pokitto 52:c04087025cab 549 display.print((int)discrete_vol);
Pokitto 52:c04087025cab 550 //display.print(int(tvol));
Pokitto 52:c04087025cab 551 display.print(" ");
Pokitto 52:c04087025cab 552 display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 553 drawvolbar(14*8,14*8+4+3,sound.getVolume(),false);
Pokitto 52:c04087025cab 554 //display.setCursor(1,10);
Pokitto 52:c04087025cab 555 //display.print(vol);
Pokitto 52:c04087025cab 556 dstate=2; break;
Pokitto 52:c04087025cab 557 case 2:
Pokitto 52:c04087025cab 558 #ifndef POK_SIM
Pokitto 52:c04087025cab 559 buttons.pollButtons();
Pokitto 52:c04087025cab 560 #endif // POK_SIM
Pokitto 52:c04087025cab 561 buttons.update();
Pokitto 52:c04087025cab 562 if (aBtn()) {dstate=0;break;}
Pokitto 52:c04087025cab 563 if (buttons.pressed(BTN_RIGHT)) {
Pokitto 52:c04087025cab 564 countd=0xFFFF; //disable countdown
Pokitto 52:c04087025cab 565 /*if (vol >= VOLUME_HEADPHONE_MAX && vol < VOLUME_HEADPHONE_MAX+1 ) vol += 0.00025f*VINCMULT;
Pokitto 52:c04087025cab 566 else if (vol >= VOLUME_HEADPHONE_MAX) vol += 0.25f*VINCMULT;
Pokitto 52:c04087025cab 567 else vol += 0.05f*VINCMULT;
Pokitto 52:c04087025cab 568 if (vol > VOLUME_HEADPHONE_MAX + 20) {
Pokitto 52:c04087025cab 569 sound.setMaxVol(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 570 }
Pokitto 52:c04087025cab 571 if (vol > VOLUME_SPEAKER_MAX) vol=VOLUME_SPEAKER_MAX;
Pokitto 52:c04087025cab 572 sound.setVolume(vol);*/
Pokitto 52:c04087025cab 573 sound.volumeUp();
Pokitto 52:c04087025cab 574 dstate=1; wipe=false;
Pokitto 52:c04087025cab 575 break;
Pokitto 52:c04087025cab 576 }
Pokitto 52:c04087025cab 577 if (buttons.pressed(BTN_LEFT)) {
Pokitto 52:c04087025cab 578 countd=0xFFFF; //disable countdown
Pokitto 52:c04087025cab 579 /*if (vol >= VOLUME_HEADPHONE_MAX) vol -= 0.25f*VINCMULT;
Pokitto 52:c04087025cab 580 else vol -= 0.05f*VINCMULT;
Pokitto 52:c04087025cab 581 if (vol <= VOLUME_HEADPHONE_MAX) sound.setMaxVol(VOLUME_HEADPHONE_MAX);
Pokitto 52:c04087025cab 582 if (vol < 0) vol=0;
Pokitto 52:c04087025cab 583 sound.setVolume(vol);*/
Pokitto 52:c04087025cab 584 sound.volumeDown();
Pokitto 52:c04087025cab 585 dstate=1; wipe=false;
Pokitto 52:c04087025cab 586 break;
Pokitto 52:c04087025cab 587 }
Pokitto 52:c04087025cab 588 break;
Pokitto 52:c04087025cab 589 }
Pokitto 52:c04087025cab 590 }
Pokitto 52:c04087025cab 591 vol = sound.getVolume();
Pokitto 52:c04087025cab 592 #ifndef POK_SIM
Pokitto 52:c04087025cab 593 if (vol != eeprom_read_byte((uint16_t*)EESETTINGS_VOL)) eeprom_write_byte((uint16_t*)EESETTINGS_VOL,(uint8_t)vol);
Pokitto 52:c04087025cab 594 #endif
Pokitto 52:c04087025cab 595 sound.setVolume(vol);
Pokitto 52:c04087025cab 596 //sound.volumeUp();
Pokitto 52:c04087025cab 597 display.setCursor(0,0);
Pokitto 52:c04087025cab 598 }
Pokitto 52:c04087025cab 599
Pokitto 52:c04087025cab 600 void Core::begin() {
Pokitto 52:c04087025cab 601
Pokitto 52:c04087025cab 602 init(); // original functions
Pokitto 52:c04087025cab 603 timePerFrame = POK_FRAMEDURATION;
Pokitto 52:c04087025cab 604 //nextFrameMillis = 0;
Pokitto 52:c04087025cab 605 //frameCount = 0;
Pokitto 52:c04087025cab 606 frameEndMicros = 1;
Pokitto 52:c04087025cab 607 startMenuTimer = 255;
Pokitto 52:c04087025cab 608 //read default settings from flash memory (set using settings.hex)
Pokitto 52:c04087025cab 609 readSettings();
Pokitto 52:c04087025cab 610 //init everything
Pokitto 52:c04087025cab 611 backlight.begin();
Pokitto 52:c04087025cab 612 backlight.set(BACKLIGHT_MAX);
Pokitto 52:c04087025cab 613 buttons.begin();
Pokitto 52:c04087025cab 614 buttons.update();
Pokitto 52:c04087025cab 615 battery.begin();
Pokitto 52:c04087025cab 616 display.begin();
Pokitto 52:c04087025cab 617 #if POK_DISPLAYLOGO
Pokitto 52:c04087025cab 618 #if PROJ_DEVELOPER_MODE != 1
Pokitto 52:c04087025cab 619 showLogo();
Pokitto 52:c04087025cab 620 #endif // PROJ_DEVELOPER_MODE
Pokitto 52:c04087025cab 621 #endif // POK_DISPLAYLOGO
Pokitto 52:c04087025cab 622
Pokitto 52:c04087025cab 623 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 624 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 625 display.clearLCD();
Pokitto 52:c04087025cab 626 display.setFont(fntC64UIGfx);
Pokitto 52:c04087025cab 627
Pokitto 52:c04087025cab 628 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 629 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 630 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 631 display.clearLCD();
Pokitto 52:c04087025cab 632 display.setFont(fntC64UIGfx);
Pokitto 52:c04087025cab 633 display.adjustCharStep=0;
Pokitto 52:c04087025cab 634 display.adjustLineStep=1;
Pokitto 52:c04087025cab 635 #ifdef JUSTLOAD
Pokitto 52:c04087025cab 636 jumpToLoader();
Pokitto 52:c04087025cab 637 #endif
Pokitto 52:c04087025cab 638
Pokitto 52:c04087025cab 639 #ifndef DISABLE_LOADER
Pokitto 52:c04087025cab 640 #if PROJ_DEVELOPER_MODE != 1
Pokitto 52:c04087025cab 641 askLoader();
Pokitto 52:c04087025cab 642 #endif // PROJ_DEVELOPER_MODE
Pokitto 52:c04087025cab 643 #endif
Pokitto 52:c04087025cab 644
Pokitto 52:c04087025cab 645 #if PROJ_DEVELOPER_MODE==1
Pokitto 52:c04087025cab 646 sound.setMaxVol(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 647 sound.setVolume(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 648 #else
Pokitto 52:c04087025cab 649 //showWarning();
Pokitto 52:c04087025cab 650 setVolLimit();
Pokitto 52:c04087025cab 651 display.clear();
Pokitto 52:c04087025cab 652 display.update();
Pokitto 52:c04087025cab 653
Pokitto 52:c04087025cab 654 while(buttons.states[BTN_A])
Pokitto 52:c04087025cab 655 {
Pokitto 52:c04087025cab 656 buttons.update();
Pokitto 52:c04087025cab 657 }
Pokitto 52:c04087025cab 658
Pokitto 52:c04087025cab 659 //sound.setVolume(sound.getVolume());//make sure we're at set volume before continue
Pokitto 52:c04087025cab 660 #endif
Pokitto 52:c04087025cab 661 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 662 display.adjustCharStep=1;
Pokitto 52:c04087025cab 663 display.adjustLineStep=1;
Pokitto 52:c04087025cab 664 display.fontSize=1;
Pokitto 52:c04087025cab 665 display.textWrap=true;
Pokitto 52:c04087025cab 666 #if POK_GAMEBUINO_SUPPORT > 0
Pokitto 52:c04087025cab 667 display.setFont(font5x7);
Pokitto 52:c04087025cab 668 #else
Pokitto 52:c04087025cab 669 display.setFont(fontC64);
Pokitto 52:c04087025cab 670 #endif
Pokitto 52:c04087025cab 671 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 672 sound.begin();
Pokitto 52:c04087025cab 673
Pokitto 52:c04087025cab 674 //mute when B is held during start up or if battery is low
Pokitto 52:c04087025cab 675 battery.update();
Pokitto 52:c04087025cab 676 if(buttons.pressed(BTN_B)){
Pokitto 52:c04087025cab 677 sound.setVolume(0);
Pokitto 52:c04087025cab 678 }
Pokitto 52:c04087025cab 679 else{ //play the startup sound on each channel for it to be louder
Pokitto 52:c04087025cab 680 #if POK_GBSOUND > 0
Pokitto 52:c04087025cab 681 #if(NUM_CHANNELS > 0)
Pokitto 52:c04087025cab 682 sound.playPattern(startupSound, 0);
Pokitto 52:c04087025cab 683 #endif
Pokitto 52:c04087025cab 684 #if(NUM_CHANNELS > 1)
Pokitto 52:c04087025cab 685 sound.playPattern(startupSound, 1);
Pokitto 52:c04087025cab 686 #endif
Pokitto 52:c04087025cab 687 #if(NUM_CHANNELS > 2)
Pokitto 52:c04087025cab 688 sound.playPattern(startupSound, 2);
Pokitto 52:c04087025cab 689 #endif
Pokitto 52:c04087025cab 690 #if(NUM_CHANNELS > 3)
Pokitto 52:c04087025cab 691 sound.playPattern(startupSound, 3);
Pokitto 52:c04087025cab 692 #endif
Pokitto 52:c04087025cab 693 #endif // POK_GBSOUND
Pokitto 52:c04087025cab 694 }
Pokitto 52:c04087025cab 695 #endif // POK ENABLE_SOUND
Pokitto 52:c04087025cab 696 }
Pokitto 52:c04087025cab 697
Pokitto 52:c04087025cab 698 void Core::init() {
Pokitto 52:c04087025cab 699 run_state = true;
Pokitto 52:c04087025cab 700 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 701 display.setFont(DEFAULT_FONT);
Pokitto 52:c04087025cab 702 initClock();
Pokitto 52:c04087025cab 703 initGPIO();
Pokitto 52:c04087025cab 704 initButtons();
Pokitto 52:c04087025cab 705 initRandom();
Pokitto 52:c04087025cab 706 //initAudio();
Pokitto 52:c04087025cab 707 //initDisplay();
Pokitto 52:c04087025cab 708 }
Pokitto 52:c04087025cab 709
Pokitto 52:c04087025cab 710 void Core::init(uint8_t switches) {
Pokitto 52:c04087025cab 711 run_state = true;
Pokitto 52:c04087025cab 712 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 713 display.setFont(DEFAULT_FONT);
Pokitto 52:c04087025cab 714 initClock();
Pokitto 52:c04087025cab 715 initGPIO();
Pokitto 52:c04087025cab 716 initButtons();
Pokitto 52:c04087025cab 717 initRandom();
Pokitto 52:c04087025cab 718 //initAudio();
Pokitto 52:c04087025cab 719 //initDisplay();
Pokitto 52:c04087025cab 720 }
Pokitto 52:c04087025cab 721
Pokitto 52:c04087025cab 722 void Core::initButtons() {
Pokitto 52:c04087025cab 723 #ifndef POK_SIM
Pokitto 52:c04087025cab 724 Pokitto::initButtons();
Pokitto 52:c04087025cab 725 #endif
Pokitto 52:c04087025cab 726 }
Pokitto 52:c04087025cab 727
Pokitto 52:c04087025cab 728 bool Core::isRunning() {
Pokitto 52:c04087025cab 729 #ifdef POK_SIM
Pokitto 52:c04087025cab 730 run_state = simulator.isRunning();
Pokitto 52:c04087025cab 731 #endif // POK_SIM
Pokitto 52:c04087025cab 732 return run_state;
Pokitto 52:c04087025cab 733 }
Pokitto 52:c04087025cab 734
Pokitto 52:c04087025cab 735 void Core::initDisplay() {
Pokitto 52:c04087025cab 736 #if POK_DISPLAYLOGO > 0
Pokitto 52:c04087025cab 737 showLogo();
Pokitto 52:c04087025cab 738 #endif
Pokitto 52:c04087025cab 739 #if POK_USE_CONSOLE > 0
Pokitto 52:c04087025cab 740 console.AddMessage(MSOURCE_LCD,MSG_INIT_OK);
Pokitto 52:c04087025cab 741 #endif
Pokitto 52:c04087025cab 742 }
Pokitto 52:c04087025cab 743
Pokitto 52:c04087025cab 744 void Core::showLogo() {
Pokitto 52:c04087025cab 745 uint32_t now;
Pokitto 52:c04087025cab 746 uint8_t state=0; //jump directly to logo, bypass teeth
Pokitto 52:c04087025cab 747 uint16_t counter=0, i=0;
Pokitto 52:c04087025cab 748 uint16_t sc;
Pokitto 52:c04087025cab 749 while (state < 255/6) {
Pokitto 52:c04087025cab 750 now=getTime();
Pokitto 52:c04087025cab 751 if (now>refreshtime) {
Pokitto 52:c04087025cab 752 refreshtime=now+30;
Pokitto 52:c04087025cab 753 switch (state) {
Pokitto 52:c04087025cab 754 case 0:
Pokitto 52:c04087025cab 755 /** POKITTO CLEAN **/
Pokitto 52:c04087025cab 756 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 757 display.fillLCD(display.directbgcolor);
Pokitto 52:c04087025cab 758 sc = COLOR_BLACK;
Pokitto 52:c04087025cab 759 state++;
Pokitto 52:c04087025cab 760 break;
Pokitto 52:c04087025cab 761 case 1:
Pokitto 52:c04087025cab 762 /** POKITTO FADE IN **/
Pokitto 52:c04087025cab 763 display.directcolor = display.interpolateColor(sc, COLOR_GREEN, i);
Pokitto 52:c04087025cab 764 display.directBitmap(POK_LCD_W/2 - (*Pokitto_logo/2),POK_LCD_H/2-(*(Pokitto_logo+1)/2),Pokitto_logo,1,1);
Pokitto 52:c04087025cab 765 i += 28;
Pokitto 52:c04087025cab 766 if (i>=0xFF) { state++; i=0;}
Pokitto 52:c04087025cab 767 break;
Pokitto 52:c04087025cab 768 case 2:
Pokitto 52:c04087025cab 769 /** POKITTO WAIT **/
Pokitto 52:c04087025cab 770 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 771 display.directBitmap(POK_LCD_W/2 - (*Pokitto_logo/2),POK_LCD_H/2-(*(Pokitto_logo+1)/2),Pokitto_logo,1,1);
Pokitto 52:c04087025cab 772 i+= 0x3F;
Pokitto 52:c04087025cab 773 if (i>0x3FF) state = 255;
Pokitto 52:c04087025cab 774 break;
Pokitto 52:c04087025cab 775 }
Pokitto 52:c04087025cab 776 if(buttons.aBtn()) state=255;
Pokitto 52:c04087025cab 777 }
Pokitto 52:c04087025cab 778 }
Pokitto 52:c04087025cab 779 }
Pokitto 52:c04087025cab 780
Pokitto 52:c04087025cab 781 void Core::readSettings() {
Pokitto 52:c04087025cab 782 // ToDo
Pokitto 52:c04087025cab 783 /*display.contrast = SCR_CONTRAST;
Pokitto 52:c04087025cab 784 backlight.backlightMin = BACKLIGHT_MIN;
Pokitto 52:c04087025cab 785 backlight.backlightMax = BACKLIGHT_MAX;
Pokitto 52:c04087025cab 786 backlight.ambientLightMin = AMBIENTLIGHT_MIN;
Pokitto 52:c04087025cab 787 backlight.ambientLightMax = AMBIENTLIGHT_MAX;
Pokitto 52:c04087025cab 788 */
Pokitto 52:c04087025cab 789 sound.setMaxVol(VOLUME_HEADPHONE_MAX);
Pokitto 52:c04087025cab 790 sound.globalVolume = VOLUME_STARTUP;
Pokitto 52:c04087025cab 791
Pokitto 52:c04087025cab 792 startMenuTimer = START_MENU_TIMER;
Pokitto 52:c04087025cab 793 /*
Pokitto 52:c04087025cab 794 battery.thresolds[0] = BAT_LVL_CRITIC;
Pokitto 52:c04087025cab 795 battery.thresolds[1] = BAT_LVL_LOW;
Pokitto 52:c04087025cab 796 battery.thresolds[2] = BAT_LVL_MED;
Pokitto 52:c04087025cab 797 battery.thresolds[3] = BAT_LVL_FULL;*/
Pokitto 52:c04087025cab 798 }
Pokitto 52:c04087025cab 799
Pokitto 52:c04087025cab 800 void Core::titleScreen(const char* name){
Pokitto 52:c04087025cab 801 titleScreen(name, 0);
Pokitto 52:c04087025cab 802 }
Pokitto 52:c04087025cab 803
Pokitto 52:c04087025cab 804 void Core::titleScreen(const uint8_t* logo){
Pokitto 52:c04087025cab 805 titleScreen((""), logo);
Pokitto 52:c04087025cab 806 }
Pokitto 52:c04087025cab 807
Pokitto 52:c04087025cab 808 void Core::titleScreen(){
Pokitto 52:c04087025cab 809 titleScreen((""));
Pokitto 52:c04087025cab 810 }
Pokitto 52:c04087025cab 811
Pokitto 52:c04087025cab 812 void Core::titleScreen(const char* name, const uint8_t *logo){
Pokitto 52:c04087025cab 813 display.setFont(font5x7);
Pokitto 52:c04087025cab 814 while(buttons.aBtn()) wait(10); //don't accidentally skip menu
Pokitto 52:c04087025cab 815 if(startMenuTimer){
Pokitto 52:c04087025cab 816 display.fontSize = 1;
Pokitto 52:c04087025cab 817 display.textWrap = false;
Pokitto 52:c04087025cab 818 display.persistence = false;
Pokitto 52:c04087025cab 819 battery.show = false;
Pokitto 52:c04087025cab 820 display.setColor(BLACK);
Pokitto 52:c04087025cab 821 while(isRunning()){
Pokitto 52:c04087025cab 822 if(update()){
Pokitto 52:c04087025cab 823 uint8_t logoOffset = name[0]?display.fontHeight:0; //add an offset the logo when there is a name to display
Pokitto 52:c04087025cab 824 //draw graphics
Pokitto 52:c04087025cab 825 display.setColorDepth(1);
Pokitto 52:c04087025cab 826 display.setColor(3);
Pokitto 52:c04087025cab 827 //display.drawBitmap(0,0, gamebuinoLogo);
Pokitto 52:c04087025cab 828 display.setColor(1);
Pokitto 52:c04087025cab 829 if(logo){
Pokitto 52:c04087025cab 830 display.drawBitmap(0, 12+logoOffset, logo);
Pokitto 52:c04087025cab 831 }
Pokitto 52:c04087025cab 832 display.cursorX = 0;
Pokitto 52:c04087025cab 833 display.cursorY = 12;
Pokitto 52:c04087025cab 834 display.print(name);
Pokitto 52:c04087025cab 835
Pokitto 52:c04087025cab 836 //A button
Pokitto 52:c04087025cab 837 display.cursorX = LCDWIDTH - display.fontWidth*3 -1;
Pokitto 52:c04087025cab 838 display.cursorY = LCDHEIGHT - display.fontHeight*3 - 3;
Pokitto 52:c04087025cab 839 if((frameCount/16)%2)
Pokitto 52:c04087025cab 840 display.println(("\25 \20"));
Pokitto 52:c04087025cab 841 else
Pokitto 52:c04087025cab 842 display.println(("\25\20 "));
Pokitto 52:c04087025cab 843 //B button
Pokitto 52:c04087025cab 844 display.cursorX = LCDWIDTH - display.fontWidth*3 - 1;
Pokitto 52:c04087025cab 845 display.cursorY++;
Pokitto 52:c04087025cab 846 if(sound.globalVolume)
Pokitto 52:c04087025cab 847 display.println(("\26\23\24"));
Pokitto 52:c04087025cab 848 else
Pokitto 52:c04087025cab 849 display.println(("\26\23x"));
Pokitto 52:c04087025cab 850 //C button
Pokitto 52:c04087025cab 851 display.cursorX = LCDWIDTH - display.fontWidth*3 - 1;
Pokitto 52:c04087025cab 852 display.cursorY++;
Pokitto 52:c04087025cab 853 //display.println(F("\27SD"));
Pokitto 52:c04087025cab 854
Pokitto 52:c04087025cab 855 //toggle volume when B is pressed
Pokitto 52:c04087025cab 856 if(buttons.pressed(BTN_B)){
Pokitto 52:c04087025cab 857 sound.setVolume(sound.getVolume() + 1);
Pokitto 52:c04087025cab 858 sound.playTick();
Pokitto 52:c04087025cab 859 }
Pokitto 52:c04087025cab 860 //leave the menu
Pokitto 52:c04087025cab 861 if(buttons.pressed(BTN_A) || ((frameCount>=startMenuTimer)&&(startMenuTimer != 255))){
Pokitto 52:c04087025cab 862 startMenuTimer = 255; //don't automatically skip the title screen next time it's displayed
Pokitto 52:c04087025cab 863 sound.stopPattern(0);
Pokitto 52:c04087025cab 864 sound.playOK();
Pokitto 52:c04087025cab 865 break;
Pokitto 52:c04087025cab 866 }
Pokitto 52:c04087025cab 867 //flash the loader
Pokitto 52:c04087025cab 868 //if(buttons.pressed(BTN_C))
Pokitto 52:c04087025cab 869 // ToDo changeGame();
Pokitto 52:c04087025cab 870 }
Pokitto 52:c04087025cab 871 }
Pokitto 52:c04087025cab 872 battery.show = true;
Pokitto 52:c04087025cab 873 }
Pokitto 52:c04087025cab 874 }
Pokitto 52:c04087025cab 875
Pokitto 52:c04087025cab 876 /**
Pokitto 52:c04087025cab 877 * Update all the subsystems, like graphics, audio, events, etc.
Pokitto 52:c04087025cab 878 * Note: the update rect is used for drawing only part of the screen buffer to LCD. Because of speed optimizations,
Pokitto 52:c04087025cab 879 * the x, y, and width of the update rect must be dividable by 4 pixels, and the height must be dividable by 8 pixels.
Pokitto 52:c04087025cab 880 * The update rect is currently used for 220x176, 4 colors, screen mode only.
Pokitto 52:c04087025cab 881 * @param useDirectMode True, if only direct screen drawing is used. False, if the screen buffer is drawn. Note: If sprites are enabled, they are drawn in both modes.
Pokitto 52:c04087025cab 882 * @param updRectX The update rect.
Pokitto 52:c04087025cab 883 * @param updRectY The update rect.
Pokitto 52:c04087025cab 884 * @param updRectW The update rect.
Pokitto 52:c04087025cab 885 * @param updRectH The update rect.
Pokitto 52:c04087025cab 886 */
Pokitto 52:c04087025cab 887 bool Core::update(bool useDirectMode, uint8_t updRectX, uint8_t updRectY, uint8_t updRectW, uint8_t updRectH) {
Pokitto 52:c04087025cab 888
Pokitto 52:c04087025cab 889 #if POK_STREAMING_MUSIC
Pokitto 52:c04087025cab 890 sound.updateStream();
Pokitto 52:c04087025cab 891 #endif
Pokitto 52:c04087025cab 892
Pokitto 52:c04087025cab 893 uint32_t now = getTime();
Pokitto 52:c04087025cab 894 if ((((nextFrameMillis - now)) > timePerFrame) && frameEndMicros) { //if time to render a new frame is reached and the frame end has ran once
Pokitto 52:c04087025cab 895 nextFrameMillis = now + timePerFrame;
Pokitto 52:c04087025cab 896 frameCount++;
Pokitto 52:c04087025cab 897
Pokitto 52:c04087025cab 898 frameEndMicros = 0;
Pokitto 52:c04087025cab 899 backlight.update();
Pokitto 52:c04087025cab 900 buttons.update();
Pokitto 52:c04087025cab 901 battery.update();
Pokitto 52:c04087025cab 902
Pokitto 52:c04087025cab 903 // FPS counter
Pokitto 52:c04087025cab 904 #if defined(PROJ_USE_FPS_COUNTER) || defined(PROJ_SHOW_FPS_COUNTER)
Pokitto 52:c04087025cab 905 const uint32_t fpsInterval_ms = 1000*3;
Pokitto 52:c04087025cab 906
Pokitto 52:c04087025cab 907 fps_frameCount++;
Pokitto 52:c04087025cab 908 if (now > fps_refreshtime) {
Pokitto 52:c04087025cab 909 fps_counter = (1000*fps_frameCount) / (now - fps_refreshtime + fpsInterval_ms);
Pokitto 52:c04087025cab 910 fps_refreshtime = now + fpsInterval_ms;
Pokitto 52:c04087025cab 911 fps_frameCount = 0;
Pokitto 52:c04087025cab 912 fps_counter_updated = true;
Pokitto 52:c04087025cab 913 }
Pokitto 52:c04087025cab 914 #endif
Pokitto 52:c04087025cab 915
Pokitto 52:c04087025cab 916 return true;
Pokitto 52:c04087025cab 917
Pokitto 52:c04087025cab 918 } else {
Pokitto 52:c04087025cab 919 if (!frameEndMicros) { //runs once at the end of the frame
Pokitto 52:c04087025cab 920 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 921 sound.updateTrack();
Pokitto 52:c04087025cab 922 sound.updatePattern();
Pokitto 52:c04087025cab 923 sound.updateNote();
Pokitto 52:c04087025cab 924 #endif
Pokitto 52:c04087025cab 925 updatePopup();
Pokitto 52:c04087025cab 926 displayBattery();
Pokitto 52:c04087025cab 927
Pokitto 52:c04087025cab 928 display.update(useDirectMode, updRectX, updRectY, updRectW, updRectH); //send the buffer to the screen
Pokitto 52:c04087025cab 929
Pokitto 52:c04087025cab 930 frameEndMicros = 1; //jonne
Pokitto 52:c04087025cab 931
Pokitto 52:c04087025cab 932 }
Pokitto 52:c04087025cab 933 return false;
Pokitto 52:c04087025cab 934 }
Pokitto 52:c04087025cab 935 }
Pokitto 52:c04087025cab 936
Pokitto 52:c04087025cab 937 void Core::displayBattery(){
Pokitto 52:c04087025cab 938 #if (ENABLE_BATTERY > 0)
Pokitto 52:c04087025cab 939 //display.setColor(BLACK, WHITE);
Pokitto 52:c04087025cab 940 uint8_t ox,oy;
Pokitto 52:c04087025cab 941 ox=display.cursorX;
Pokitto 52:c04087025cab 942 oy=display.cursorY;
Pokitto 52:c04087025cab 943 display.cursorX = LCDWIDTH-display.fontWidth+1;
Pokitto 52:c04087025cab 944 display.cursorY = 0;
Pokitto 52:c04087025cab 945 switch(battery.level){
Pokitto 52:c04087025cab 946 case 0://battery critic, power down
Pokitto 52:c04087025cab 947 sound.stopPattern();
Pokitto 52:c04087025cab 948 backlight.set(0);
Pokitto 52:c04087025cab 949 display.clear();
Pokitto 52:c04087025cab 950 display.fontSize = 1;
Pokitto 52:c04087025cab 951 display.print(("LOW BATTERY\n"));
Pokitto 52:c04087025cab 952 display.print(battery.voltage);
Pokitto 52:c04087025cab 953 display.print(("mV\n\nPLEASE\nTURN OFF"));
Pokitto 52:c04087025cab 954 display.update();
Pokitto 52:c04087025cab 955 break;
Pokitto 52:c04087025cab 956 case 1: //empty battery
Pokitto 52:c04087025cab 957 if((frameCount % 16) < 8) display.print('\7'); //blinking battery
Pokitto 52:c04087025cab 958 else display.print('x');
Pokitto 52:c04087025cab 959 break;
Pokitto 52:c04087025cab 960 case 2://low battery
Pokitto 52:c04087025cab 961 case 3://full battery
Pokitto 52:c04087025cab 962 case 4://full battery
Pokitto 52:c04087025cab 963 if(battery.show){
Pokitto 52:c04087025cab 964 display.print(char(5+battery.level));
Pokitto 52:c04087025cab 965 }
Pokitto 52:c04087025cab 966 break;
Pokitto 52:c04087025cab 967 default:
Pokitto 52:c04087025cab 968 if(battery.show){
Pokitto 52:c04087025cab 969 display.print('/');
Pokitto 52:c04087025cab 970 }
Pokitto 52:c04087025cab 971 break;
Pokitto 52:c04087025cab 972 }
Pokitto 52:c04087025cab 973 display.cursorX = ox;
Pokitto 52:c04087025cab 974 display.cursorY = oy;
Pokitto 52:c04087025cab 975 #endif
Pokitto 52:c04087025cab 976 }
Pokitto 52:c04087025cab 977
Pokitto 52:c04087025cab 978 char* Core::filemenu(char *ext) {
Pokitto 52:c04087025cab 979 uint8_t oldPersistence = display.persistence;
Pokitto 52:c04087025cab 980 display.persistence = false;
Pokitto 52:c04087025cab 981 uint16_t oldpal0=display.palette[0];
Pokitto 52:c04087025cab 982 uint16_t oldpal1=display.palette[1];
Pokitto 52:c04087025cab 983 uint16_t oldpal2=display.palette[2];
Pokitto 52:c04087025cab 984 display.palette[2]=COLOR_GREEN;
Pokitto 52:c04087025cab 985 display.palette[1]=COLOR_WHITE;
Pokitto 52:c04087025cab 986 display.palette[0]=COLOR_BLACK;
Pokitto 52:c04087025cab 987 uint8_t oldbg=display.bgcolor;
Pokitto 52:c04087025cab 988 uint8_t oldfg=display.color;
Pokitto 52:c04087025cab 989 display.color=1;
Pokitto 52:c04087025cab 990 display.bgcolor=0;
Pokitto 52:c04087025cab 991
Pokitto 52:c04087025cab 992 int16_t rowh = display.fontHeight + 2;
Pokitto 52:c04087025cab 993 int8_t activeItem = 0;
Pokitto 52:c04087025cab 994 int16_t currentTopItem = 0;
Pokitto 52:c04087025cab 995 int16_t itemsPerScreen = (display.height+1) / rowh;
Pokitto 52:c04087025cab 996 int16_t numOfItemsFound = 0;
Pokitto 52:c04087025cab 997 boolean exit = false;
Pokitto 52:c04087025cab 998
Pokitto 52:c04087025cab 999 char* txt;
Pokitto 52:c04087025cab 1000
Pokitto 52:c04087025cab 1001 pokInitSD();
Pokitto 52:c04087025cab 1002
Pokitto 52:c04087025cab 1003 bool updated = true;
Pokitto 52:c04087025cab 1004 while (isRunning()) {
Pokitto 52:c04087025cab 1005 if (update(true)) {
Pokitto 52:c04087025cab 1006 if (buttons.pressed(BTN_A) || buttons.pressed(BTN_B) || buttons.pressed(BTN_C)) {
Pokitto 52:c04087025cab 1007 exit = true; //time to exit menu !
Pokitto 52:c04087025cab 1008 if (buttons.pressed(BTN_A)) {
Pokitto 52:c04087025cab 1009 sound.playOK();
Pokitto 52:c04087025cab 1010 } else {
Pokitto 52:c04087025cab 1011 *selectedfile = 0;
Pokitto 52:c04087025cab 1012 sound.playCancel();
Pokitto 52:c04087025cab 1013 }
Pokitto 52:c04087025cab 1014 updated = true; // update screen
Pokitto 52:c04087025cab 1015 }
Pokitto 52:c04087025cab 1016 if (exit == false) {
Pokitto 52:c04087025cab 1017 if (buttons.repeat(BTN_DOWN,4)) {
Pokitto 52:c04087025cab 1018 if( ++activeItem >= numOfItemsFound ) activeItem = numOfItemsFound - 1;
Pokitto 52:c04087025cab 1019 if( activeItem >= currentTopItem + itemsPerScreen) currentTopItem += itemsPerScreen; // next page
Pokitto 52:c04087025cab 1020 sound.playTick();
Pokitto 52:c04087025cab 1021 updated = true; // update screen
Pokitto 52:c04087025cab 1022 }
Pokitto 52:c04087025cab 1023 if (buttons.repeat(BTN_UP,4)) {
Pokitto 52:c04087025cab 1024 if( --activeItem < 0 ) activeItem = 0;
Pokitto 52:c04087025cab 1025 if( activeItem < currentTopItem) currentTopItem -= itemsPerScreen; // previous page
Pokitto 52:c04087025cab 1026 sound.playTick();
Pokitto 52:c04087025cab 1027 updated = true; // update screen
Pokitto 52:c04087025cab 1028 }
Pokitto 52:c04087025cab 1029
Pokitto 52:c04087025cab 1030 } else { //exit :
Pokitto 52:c04087025cab 1031 display.bgcolor=oldbg;
Pokitto 52:c04087025cab 1032 display.color=oldfg;
Pokitto 52:c04087025cab 1033 display.palette[0] = oldpal0;
Pokitto 52:c04087025cab 1034 display.palette[1] = oldpal1;
Pokitto 52:c04087025cab 1035 display.palette[2] = oldpal2;
Pokitto 52:c04087025cab 1036 display.persistence = oldPersistence;
Pokitto 52:c04087025cab 1037 return selectedfile;
Pokitto 52:c04087025cab 1038 }
Pokitto 52:c04087025cab 1039
Pokitto 52:c04087025cab 1040 //draw a fancy menu
Pokitto 52:c04087025cab 1041 display.textWrap = false;
Pokitto 52:c04087025cab 1042 uint16_t fc,bc;
Pokitto 52:c04087025cab 1043 fc = display.color;
Pokitto 52:c04087025cab 1044 bc = display.bgcolor;
Pokitto 52:c04087025cab 1045 if( updated ) { // update screen?
Pokitto 52:c04087025cab 1046 #if POK_SIM
Pokitto 52:c04087025cab 1047 getFirstFile(ext);
Pokitto 52:c04087025cab 1048 #else
Pokitto 52:c04087025cab 1049 bool isFirstFile = true;
Pokitto 52:c04087025cab 1050 #endif
Pokitto 52:c04087025cab 1051 for (int i = 0; ; i++) {
Pokitto 52:c04087025cab 1052 display.cursorX = 0;
Pokitto 52:c04087025cab 1053 display.invisiblecolor=255;
Pokitto 52:c04087025cab 1054 display.cursorY = (i - currentTopItem ) * rowh;
Pokitto 52:c04087025cab 1055
Pokitto 52:c04087025cab 1056 // read the file name from SD
Pokitto 52:c04087025cab 1057 #ifndef POK_SIM
Pokitto 52:c04087025cab 1058 if(isFirstFile) {
Pokitto 52:c04087025cab 1059 txt = getFirstFile(ext);
Pokitto 52:c04087025cab 1060 isFirstFile = false;
Pokitto 52:c04087025cab 1061 }
Pokitto 52:c04087025cab 1062 else
Pokitto 52:c04087025cab 1063 #endif
Pokitto 52:c04087025cab 1064
Pokitto 52:c04087025cab 1065 txt = getNextFile(ext);
Pokitto 52:c04087025cab 1066
Pokitto 52:c04087025cab 1067 if (txt) {
Pokitto 52:c04087025cab 1068
Pokitto 52:c04087025cab 1069 numOfItemsFound = i+1;
Pokitto 52:c04087025cab 1070
Pokitto 52:c04087025cab 1071 // Draw active line with diffrent backgtound and char color
Pokitto 52:c04087025cab 1072 if (i == activeItem){
Pokitto 52:c04087025cab 1073 display.cursorX = 3;
Pokitto 52:c04087025cab 1074 display.color=2;
Pokitto 52:c04087025cab 1075 display.fillRect(0, display.cursorY - 2, LCDWIDTH, rowh);
Pokitto 52:c04087025cab 1076 display.setColor(0,2);
Pokitto 52:c04087025cab 1077 } else display.setColor(1,0);
Pokitto 52:c04087025cab 1078
Pokitto 52:c04087025cab 1079 // break loop if going out of screen
Pokitto 52:c04087025cab 1080 if(i >= currentTopItem + itemsPerScreen ) {
Pokitto 52:c04087025cab 1081 break;
Pokitto 52:c04087025cab 1082 }
Pokitto 52:c04087025cab 1083
Pokitto 52:c04087025cab 1084 // Display only if the file is on the current page
Pokitto 52:c04087025cab 1085 if( i >= currentTopItem) {
Pokitto 52:c04087025cab 1086 display.print(display.cursorX, display.cursorY, txt);
Pokitto 52:c04087025cab 1087 if (i == activeItem)
Pokitto 52:c04087025cab 1088 strcpy(selectedfile,txt);
Pokitto 52:c04087025cab 1089 }
Pokitto 52:c04087025cab 1090 } else
Pokitto 52:c04087025cab 1091 break; // break loop as no more files found
Pokitto 52:c04087025cab 1092
Pokitto 52:c04087025cab 1093 display.setColor(1,0);
Pokitto 52:c04087025cab 1094 } // end for
Pokitto 52:c04087025cab 1095
Pokitto 52:c04087025cab 1096 display.update();
Pokitto 52:c04087025cab 1097 }
Pokitto 52:c04087025cab 1098 updated = false;
Pokitto 52:c04087025cab 1099 } // update
Pokitto 52:c04087025cab 1100
Pokitto 52:c04087025cab 1101 display.setColor(1,0);
Pokitto 52:c04087025cab 1102 }
Pokitto 52:c04087025cab 1103 return 0;
Pokitto 52:c04087025cab 1104 }
Pokitto 52:c04087025cab 1105
Pokitto 52:c04087025cab 1106 char* Core::filemenu() {
Pokitto 52:c04087025cab 1107 return filemenu("");
Pokitto 52:c04087025cab 1108 }
Pokitto 52:c04087025cab 1109
Pokitto 52:c04087025cab 1110 int8_t Core::menu(const char* const* items, uint8_t length) {
Pokitto 52:c04087025cab 1111 if (display.color>3) display.color=1;
Pokitto 52:c04087025cab 1112 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1113 display.persistence = false;
Pokitto 52:c04087025cab 1114 int8_t activeItem = 0;
Pokitto 52:c04087025cab 1115 int16_t currentY = display.height;
Pokitto 52:c04087025cab 1116 int16_t targetY = 0, rowh = display.fontHeight + 2;
Pokitto 52:c04087025cab 1117 boolean exit = false;
Pokitto 52:c04087025cab 1118 int8_t answer = -1;
Pokitto 52:c04087025cab 1119 while (isRunning()) {
Pokitto 52:c04087025cab 1120 if (update()) {
Pokitto 52:c04087025cab 1121 if (buttons.pressed(BTN_A) || buttons.pressed(BTN_B) || buttons.pressed(BTN_C)) {
Pokitto 52:c04087025cab 1122 exit = true; //time to exit menu !
Pokitto 52:c04087025cab 1123 targetY = - display.fontHeight * length - 2; //send the menu out of the screen
Pokitto 52:c04087025cab 1124 if (buttons.pressed(BTN_A)) {
Pokitto 52:c04087025cab 1125 answer = activeItem;
Pokitto 52:c04087025cab 1126 sound.playOK();
Pokitto 52:c04087025cab 1127 } else {
Pokitto 52:c04087025cab 1128 sound.playCancel();
Pokitto 52:c04087025cab 1129 }
Pokitto 52:c04087025cab 1130 }
Pokitto 52:c04087025cab 1131 if (exit == false) {
Pokitto 52:c04087025cab 1132 if (buttons.repeat(BTN_DOWN,4)) {
Pokitto 52:c04087025cab 1133 activeItem++;
Pokitto 52:c04087025cab 1134 sound.playTick();
Pokitto 52:c04087025cab 1135 }
Pokitto 52:c04087025cab 1136 if (buttons.repeat(BTN_UP,4)) {
Pokitto 52:c04087025cab 1137 activeItem--;
Pokitto 52:c04087025cab 1138 sound.playTick();
Pokitto 52:c04087025cab 1139 }
Pokitto 52:c04087025cab 1140 //don't go out of the menu
Pokitto 52:c04087025cab 1141 if (activeItem == length) activeItem = 0;
Pokitto 52:c04087025cab 1142 if (activeItem < 0) activeItem = length - 1;
Pokitto 52:c04087025cab 1143
Pokitto 52:c04087025cab 1144 targetY = -rowh * activeItem + (rowh+4); //center the menu on the active item
Pokitto 52:c04087025cab 1145 } else { //exit :
Pokitto 52:c04087025cab 1146 if ((currentY - targetY) <= 1)
Pokitto 52:c04087025cab 1147 return (answer);
Pokitto 52:c04087025cab 1148 }
Pokitto 52:c04087025cab 1149 //draw a fancy menu
Pokitto 52:c04087025cab 1150 currentY = (currentY + targetY) / 2;
Pokitto 52:c04087025cab 1151 display.cursorX = 0;
Pokitto 52:c04087025cab 1152 display.cursorY = currentY;
Pokitto 52:c04087025cab 1153 display.textWrap = false;
Pokitto 52:c04087025cab 1154 uint16_t fc,bc;
Pokitto 52:c04087025cab 1155 fc = display.color;
Pokitto 52:c04087025cab 1156 bc = display.bgcolor;
Pokitto 52:c04087025cab 1157 for (byte i = 0; i < length; i++) {
Pokitto 52:c04087025cab 1158 display.cursorY = currentY + rowh * i;
Pokitto 52:c04087025cab 1159 if (i == activeItem){
Pokitto 52:c04087025cab 1160 display.cursorX = 3;
Pokitto 52:c04087025cab 1161
Pokitto 52:c04087025cab 1162 //display.fillRoundRect(0, currentY + display.fontHeight * activeItem - 2, LCDWIDTH, (display.fontHeight+3), 3);
Pokitto 52:c04087025cab 1163 display.fillRect(0, currentY + rowh * activeItem - 2, LCDWIDTH, (rowh));
Pokitto 52:c04087025cab 1164 display.setColor(bc,fc);
Pokitto 52:c04087025cab 1165 } else display.setColor(fc,bc);
Pokitto 52:c04087025cab 1166
Pokitto 52:c04087025cab 1167 display.println((char*)*(const unsigned int*)(items+i));
Pokitto 52:c04087025cab 1168 display.setColor(fc,bc);
Pokitto 52:c04087025cab 1169 }
Pokitto 52:c04087025cab 1170
Pokitto 52:c04087025cab 1171 }
Pokitto 52:c04087025cab 1172 }
Pokitto 52:c04087025cab 1173 #else
Pokitto 52:c04087025cab 1174 return 0;
Pokitto 52:c04087025cab 1175 #endif
Pokitto 52:c04087025cab 1176 return 0;
Pokitto 52:c04087025cab 1177 }
Pokitto 52:c04087025cab 1178
Pokitto 52:c04087025cab 1179 void Core::keyboard(char* text, uint8_t length) {
Pokitto 52:c04087025cab 1180 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1181 display.persistence = false;
Pokitto 52:c04087025cab 1182 //memset(text, 0, length); //clear the text
Pokitto 52:c04087025cab 1183 text[length-1] = '\0';
Pokitto 52:c04087025cab 1184 //active character in the typing area
Pokitto 52:c04087025cab 1185 int8_t activeChar = 0;
Pokitto 52:c04087025cab 1186 //selected char on the keyboard
Pokitto 52:c04087025cab 1187 int8_t activeX = 0;
Pokitto 52:c04087025cab 1188 int8_t activeY = 2;
Pokitto 52:c04087025cab 1189 //position of the keyboard on the screen
Pokitto 52:c04087025cab 1190 int8_t currentX = LCDWIDTH;
Pokitto 52:c04087025cab 1191 int8_t currentY = LCDHEIGHT;
Pokitto 52:c04087025cab 1192 int8_t targetX = 0;
Pokitto 52:c04087025cab 1193 int8_t targetY = 0;
Pokitto 52:c04087025cab 1194
Pokitto 52:c04087025cab 1195 while (1) {
Pokitto 52:c04087025cab 1196 if (update()) {
Pokitto 52:c04087025cab 1197 //move the character selector
Pokitto 52:c04087025cab 1198 if (buttons.repeat(BTN_DOWN, 4)) {
Pokitto 52:c04087025cab 1199 activeY++;
Pokitto 52:c04087025cab 1200 sound.playTick();
Pokitto 52:c04087025cab 1201 }
Pokitto 52:c04087025cab 1202 if (buttons.repeat(BTN_UP, 4)) {
Pokitto 52:c04087025cab 1203 activeY--;
Pokitto 52:c04087025cab 1204 sound.playTick();
Pokitto 52:c04087025cab 1205 }
Pokitto 52:c04087025cab 1206 if (buttons.repeat(BTN_RIGHT, 4)) {
Pokitto 52:c04087025cab 1207 activeX++;
Pokitto 52:c04087025cab 1208 sound.playTick();
Pokitto 52:c04087025cab 1209 }
Pokitto 52:c04087025cab 1210 if (buttons.repeat(BTN_LEFT, 4)) {
Pokitto 52:c04087025cab 1211 activeX--;
Pokitto 52:c04087025cab 1212 sound.playTick();
Pokitto 52:c04087025cab 1213 }
Pokitto 52:c04087025cab 1214 //don't go out of the keyboard
Pokitto 52:c04087025cab 1215 if (activeX == KEYBOARD_W) activeX = 0;
Pokitto 52:c04087025cab 1216 if (activeX < 0) activeX = KEYBOARD_W - 1;
Pokitto 52:c04087025cab 1217 if (activeY == KEYBOARD_H) activeY = 0;
Pokitto 52:c04087025cab 1218 if (activeY < 0) activeY = KEYBOARD_H - 1;
Pokitto 52:c04087025cab 1219 //set the keyboard position on screen
Pokitto 52:c04087025cab 1220 targetX = -(display.fontWidth+1) * activeX + LCDWIDTH / 2 - 3;
Pokitto 52:c04087025cab 1221 targetY = -(display.fontHeight+1) * activeY + LCDHEIGHT / 2 - 4 - display.fontHeight;
Pokitto 52:c04087025cab 1222 //smooth the keyboard displacement
Pokitto 52:c04087025cab 1223 currentX = (targetX + currentX) / 2;
Pokitto 52:c04087025cab 1224 currentY = (targetY + currentY) / 2;
Pokitto 52:c04087025cab 1225 //type character
Pokitto 52:c04087025cab 1226 if (buttons.pressed(BTN_A)) {
Pokitto 52:c04087025cab 1227 if (activeChar < (length-1)) {
Pokitto 52:c04087025cab 1228 byte thisChar = activeX + KEYBOARD_W * activeY;
Pokitto 52:c04087025cab 1229 if((thisChar == 0)||(thisChar == 10)||(thisChar == 13)) //avoid line feed and carriage return
Pokitto 52:c04087025cab 1230 continue;
Pokitto 52:c04087025cab 1231 text[activeChar] = thisChar;
Pokitto 52:c04087025cab 1232 text[activeChar+1] = '\0';
Pokitto 52:c04087025cab 1233 }
Pokitto 52:c04087025cab 1234 activeChar++;
Pokitto 52:c04087025cab 1235 sound.playOK();
Pokitto 52:c04087025cab 1236 if (activeChar > length)
Pokitto 52:c04087025cab 1237 activeChar = length;
Pokitto 52:c04087025cab 1238 }
Pokitto 52:c04087025cab 1239 //erase character
Pokitto 52:c04087025cab 1240 if (buttons.pressed(BTN_B)) {
Pokitto 52:c04087025cab 1241 activeChar--;
Pokitto 52:c04087025cab 1242 sound.playCancel();
Pokitto 52:c04087025cab 1243 if (activeChar >= 0)
Pokitto 52:c04087025cab 1244 text[activeChar] = 0;
Pokitto 52:c04087025cab 1245 else
Pokitto 52:c04087025cab 1246 activeChar = 0;
Pokitto 52:c04087025cab 1247 }
Pokitto 52:c04087025cab 1248 //leave menu
Pokitto 52:c04087025cab 1249 if (buttons.pressed(BTN_C)) {
Pokitto 52:c04087025cab 1250 sound.playOK();
Pokitto 52:c04087025cab 1251 while (1) {
Pokitto 52:c04087025cab 1252 if (update()) {
Pokitto 52:c04087025cab 1253 //display.setCursor(0,0);
Pokitto 52:c04087025cab 1254 display.println(("You entered\n"));
Pokitto 52:c04087025cab 1255 display.print(text);
Pokitto 52:c04087025cab 1256 display.println(("\n\n\n\x15:okay \x16:edit"));
Pokitto 52:c04087025cab 1257 if(buttons.pressed(BTN_A)){
Pokitto 52:c04087025cab 1258 sound.playOK();
Pokitto 52:c04087025cab 1259 return;
Pokitto 52:c04087025cab 1260 }
Pokitto 52:c04087025cab 1261 if(buttons.pressed(BTN_B)){
Pokitto 52:c04087025cab 1262 sound.playCancel();
Pokitto 52:c04087025cab 1263 break;
Pokitto 52:c04087025cab 1264 }
Pokitto 52:c04087025cab 1265 }
Pokitto 52:c04087025cab 1266 }
Pokitto 52:c04087025cab 1267 }
Pokitto 52:c04087025cab 1268 //draw the keyboard
Pokitto 52:c04087025cab 1269 for (int8_t y = 0; y < KEYBOARD_H; y++) {
Pokitto 52:c04087025cab 1270 for (int8_t x = 0; x < KEYBOARD_W; x++) {
Pokitto 52:c04087025cab 1271 display.drawChar(currentX + x * (display.fontWidth+1), currentY + y * (display.fontHeight+1), x + y * KEYBOARD_W, 1);
Pokitto 52:c04087025cab 1272 }
Pokitto 52:c04087025cab 1273 }
Pokitto 52:c04087025cab 1274 //draw instruction
Pokitto 52:c04087025cab 1275 display.cursorX = currentX-display.fontWidth*6-2;
Pokitto 52:c04087025cab 1276 display.cursorY = currentY+1*(display.fontHeight+1);
Pokitto 52:c04087025cab 1277 display.print(("\25type"));
Pokitto 52:c04087025cab 1278
Pokitto 52:c04087025cab 1279 display.cursorX = currentX-display.fontWidth*6-2;
Pokitto 52:c04087025cab 1280 display.cursorY = currentY+2*(display.fontHeight+1);
Pokitto 52:c04087025cab 1281 display.print(("\26back"));
Pokitto 52:c04087025cab 1282
Pokitto 52:c04087025cab 1283 display.cursorX = currentX-display.fontWidth*6-2;
Pokitto 52:c04087025cab 1284 display.cursorY = currentY+3*(display.fontHeight+1);
Pokitto 52:c04087025cab 1285 display.print(("\27save"));
Pokitto 52:c04087025cab 1286
Pokitto 52:c04087025cab 1287 //erase some pixels around the selected character
Pokitto 52:c04087025cab 1288 display.setColor(WHITE);
Pokitto 52:c04087025cab 1289 display.drawFastHLine(currentX + activeX * (display.fontWidth+1) - 1, currentY + activeY * (display.fontHeight+1) - 2, 7);
Pokitto 52:c04087025cab 1290 //draw the selection rectangle
Pokitto 52:c04087025cab 1291 display.setColor(BLACK);
Pokitto 52:c04087025cab 1292 display.drawRoundRect(currentX + activeX * (display.fontWidth+1) - 2, currentY + activeY * (display.fontHeight+1) - 3, (display.fontWidth+2)+(display.fontWidth-1)%2, (display.fontHeight+5), 3);
Pokitto 52:c04087025cab 1293 //draw keyboard outline
Pokitto 52:c04087025cab 1294 //display.drawRoundRect(currentX - 6, currentY - 6, KEYBOARD_W * (display.fontWidth+1) + 12, KEYBOARD_H * (display.fontHeight+1) + 12, 8, BLACK);
Pokitto 52:c04087025cab 1295 //text field
Pokitto 52:c04087025cab 1296 display.drawFastHLine(0, LCDHEIGHT-display.fontHeight-2, LCDWIDTH);
Pokitto 52:c04087025cab 1297 display.setColor(WHITE);
Pokitto 52:c04087025cab 1298 display.fillRect(0, LCDHEIGHT-display.fontHeight-1, LCDWIDTH, display.fontHeight+1);
Pokitto 52:c04087025cab 1299 //typed text
Pokitto 52:c04087025cab 1300 display.cursorX = 0;
Pokitto 52:c04087025cab 1301 display.cursorY = LCDHEIGHT-display.fontHeight;
Pokitto 52:c04087025cab 1302 display.setColor(BLACK);
Pokitto 52:c04087025cab 1303 display.print(text);
Pokitto 52:c04087025cab 1304 //blinking cursor
Pokitto 52:c04087025cab 1305 if (((frameCount % 8) < 4) && (activeChar < (length-1)))
Pokitto 52:c04087025cab 1306 display.drawChar(display.fontWidth * activeChar, LCDHEIGHT-display.fontHeight, '_',1);
Pokitto 52:c04087025cab 1307 }
Pokitto 52:c04087025cab 1308 }
Pokitto 52:c04087025cab 1309 #endif
Pokitto 52:c04087025cab 1310 }
Pokitto 52:c04087025cab 1311
Pokitto 52:c04087025cab 1312 void Core::popup(const char* text, uint8_t duration){
Pokitto 52:c04087025cab 1313 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1314 popupText = text;
Pokitto 52:c04087025cab 1315 popupTimeLeft = duration+12;
Pokitto 52:c04087025cab 1316 #endif
Pokitto 52:c04087025cab 1317 }
Pokitto 52:c04087025cab 1318
Pokitto 52:c04087025cab 1319 void Core::updatePopup(){
Pokitto 52:c04087025cab 1320 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1321 if (popupTimeLeft){
Pokitto 52:c04087025cab 1322 uint8_t yOffset = 0;
Pokitto 52:c04087025cab 1323 if(popupTimeLeft<12){
Pokitto 52:c04087025cab 1324 yOffset = 12-popupTimeLeft;
Pokitto 52:c04087025cab 1325 }
Pokitto 52:c04087025cab 1326 display.fontSize = 1;
Pokitto 52:c04087025cab 1327 display.setColor(WHITE);
Pokitto 52:c04087025cab 1328 display.fillRoundRect(0,LCDHEIGHT-display.fontHeight+yOffset-3,84,display.fontHeight+3,3);
Pokitto 52:c04087025cab 1329 display.setColor(BLACK);
Pokitto 52:c04087025cab 1330 display.drawRoundRect(0,LCDHEIGHT-display.fontHeight+yOffset-3,84,display.fontHeight+3,3);
Pokitto 52:c04087025cab 1331 display.cursorX = 4;
Pokitto 52:c04087025cab 1332 display.cursorY = LCDHEIGHT-display.fontHeight+yOffset-1;
Pokitto 52:c04087025cab 1333 display.print(popupText);
Pokitto 52:c04087025cab 1334 popupTimeLeft--;
Pokitto 52:c04087025cab 1335 }
Pokitto 52:c04087025cab 1336 #endif
Pokitto 52:c04087025cab 1337 }
Pokitto 52:c04087025cab 1338
Pokitto 52:c04087025cab 1339 void Core::setFrameRate(uint8_t fps) {
Pokitto 52:c04087025cab 1340 timePerFrame = 1000 / fps;
Pokitto 52:c04087025cab 1341 sound.prescaler = fps / 20;
Pokitto 52:c04087025cab 1342 sound.prescaler = __avrmax(1, sound.prescaler);
Pokitto 52:c04087025cab 1343 }
Pokitto 52:c04087025cab 1344
Pokitto 52:c04087025cab 1345 void Core::pickRandomSeed(){
Pokitto 52:c04087025cab 1346 initRandom();
Pokitto 52:c04087025cab 1347 }
Pokitto 52:c04087025cab 1348
Pokitto 52:c04087025cab 1349 bool Core::collidePointRect(int16_t x1, int16_t y1 ,int16_t x2 ,int16_t y2, int16_t w, int16_t h){
Pokitto 52:c04087025cab 1350 if((x1>=x2)&&(x1<x2+w))
Pokitto 52:c04087025cab 1351 if((y1>=y2)&&(y1<y2+h))
Pokitto 52:c04087025cab 1352 return true;
Pokitto 52:c04087025cab 1353 return false;
Pokitto 52:c04087025cab 1354 }
Pokitto 52:c04087025cab 1355
Pokitto 52:c04087025cab 1356 bool Core::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 52:c04087025cab 1357 return !( x2 >= x1+w1 ||
Pokitto 52:c04087025cab 1358 x2+w2 <= x1 ||
Pokitto 52:c04087025cab 1359 y2 >= y1+h1 ||
Pokitto 52:c04087025cab 1360 y2+h2 <= y1 );
Pokitto 52:c04087025cab 1361 }
Pokitto 52:c04087025cab 1362
Pokitto 52:c04087025cab 1363 bool Core::collideBitmapBitmap(int16_t x1, int16_t y1, const uint8_t* b1, int16_t x2, int16_t y2, const uint8_t* b2){
Pokitto 52:c04087025cab 1364 int16_t w1 = pgm_read_byte(b1);
Pokitto 52:c04087025cab 1365 int16_t h1 = pgm_read_byte(b1 + 1);
Pokitto 52:c04087025cab 1366 int16_t w2 = pgm_read_byte(b2);
Pokitto 52:c04087025cab 1367 int16_t h2 = pgm_read_byte(b2 + 1);
Pokitto 52:c04087025cab 1368
Pokitto 52:c04087025cab 1369 if(collideRectRect(x1, y1, w1, h1, x2, y2, w2, h2) == false){
Pokitto 52:c04087025cab 1370 return false;
Pokitto 52:c04087025cab 1371 }
Pokitto 52:c04087025cab 1372
Pokitto 52:c04087025cab 1373 int16_t xmin = (x1>=x2)? 0 : x2-x1;
Pokitto 52:c04087025cab 1374 int16_t ymin = (y1>=y2)? 0 : y2-y1;
Pokitto 52:c04087025cab 1375 int16_t xmax = (x1+w1>=x2+w2)? x2+w2-x1 : w1;
Pokitto 52:c04087025cab 1376 int16_t ymax = (y1+h1>=y2+h2)? y2+h2-y1 : h1;
Pokitto 52:c04087025cab 1377 for(uint8_t y = ymin; y < ymax; y++){
Pokitto 52:c04087025cab 1378 for(uint8_t x = xmin; x < xmax; x++){
Pokitto 52:c04087025cab 1379 if(display.getBitmapPixel(b1, x, y) && display.getBitmapPixel(b2, x1+x-x2, y1+y-y2)){
Pokitto 52:c04087025cab 1380 return true;
Pokitto 52:c04087025cab 1381 }
Pokitto 52:c04087025cab 1382 }
Pokitto 52:c04087025cab 1383 }
Pokitto 52:c04087025cab 1384 return false;
Pokitto 52:c04087025cab 1385 }
Pokitto 52:c04087025cab 1386
Pokitto 52:c04087025cab 1387
Pokitto 52:c04087025cab 1388 //** EOF **//
Pokitto 52:c04087025cab 1389
Pokitto 52:c04087025cab 1390
Pokitto 52:c04087025cab 1391
Pokitto 52:c04087025cab 1392
Pokitto 52:c04087025cab 1393
Pokitto 52:c04087025cab 1394
Pokitto 52:c04087025cab 1395
Pokitto 52:c04087025cab 1396
Pokitto 52:c04087025cab 1397