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

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