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

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
59:61d308a0b113
added fix for directrectangle()

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 59:61d308a0b113 182 //float progress=0;
Pokitto 59:61d308a0b113 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 59:61d308a0b113 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 59:61d308a0b113 274 uint32_t 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 54:7082c373d764 468
Pokitto 52:c04087025cab 469 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 470 display.adjustCharStep = 0;
Pokitto 52:c04087025cab 471 //sound.setMaxVol(VOLUME_HEADPHONE_MAX);
Pokitto 52:c04087025cab 472 int dstate=1;
Pokitto 52:c04087025cab 473 bool wipe = true;
Pokitto 59:61d308a0b113 474 float vol = sound.getVolume();
Pokitto 52:c04087025cab 475 #ifndef POK_SIM
Pokitto 52:c04087025cab 476 vol=eeprom_read_byte((uint16_t*)EESETTINGS_VOL);
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 54:7082c373d764 485 //sound.setVolume(0);
Pokitto 54:7082c373d764 486 //if (vol) {
Pokitto 54:7082c373d764 487 //for (uint8_t t=0;t<=vol;t++) sound.setVolume(t);
Pokitto 54:7082c373d764 488 //}
Pokitto 54:7082c373d764 489 discrete_vol = ((int)vol>>5); //get rid of 0 vol number
Pokitto 54:7082c373d764 490 sound.setVolume(vol);
Pokitto 52:c04087025cab 491 volbar_visible=0;
Pokitto 52:c04087025cab 492 int countd=0;
Pokitto 52:c04087025cab 493 #ifndef POK_SIM
Pokitto 52:c04087025cab 494 //read countdown time from settings
Pokitto 54:7082c373d764 495 //countd = eeprom_read_byte((uint16_t*)EESETTINGS_VOLWAIT);
Pokitto 52:c04087025cab 496 #endif
Pokitto 53:ee5e28819ebb 497 if (countd<=0 || countd > 10) countd=0xFFFF;
Pokitto 52:c04087025cab 498 #ifdef PRODUCTIONTESTING
Pokitto 52:c04087025cab 499 countd=2;
Pokitto 52:c04087025cab 500 #endif
Pokitto 52:c04087025cab 501 uint16_t c2 = getTime();
Pokitto 52:c04087025cab 502 while (core.isRunning() && dstate && countd){
Pokitto 52:c04087025cab 503 if (getTime()>c2+1000) {
Pokitto 52:c04087025cab 504 c2=getTime();
Pokitto 52:c04087025cab 505 if (countd<0xFFFF) countd--;
Pokitto 52:c04087025cab 506 }
Pokitto 52:c04087025cab 507 switch (dstate) {
Pokitto 52:c04087025cab 508 case 1:
Pokitto 52:c04087025cab 509 //redraw
Pokitto 52:c04087025cab 510 if (wipe) {
Pokitto 52:c04087025cab 511 display.clearLCD();
Pokitto 52:c04087025cab 512 display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 513 display.setCursor(4*8,2*8);
Pokitto 52:c04087025cab 514 display.print("SELECT VOLUME LIMIT");
Pokitto 52:c04087025cab 515 display.setCursor(5*8,17*9);
Pokitto 52:c04087025cab 516 display.print("PRESS ");
Pokitto 52:c04087025cab 517 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 518 display.print("A");
Pokitto 52:c04087025cab 519 display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 520 display.print(" TO ACCEPT");
Pokitto 52:c04087025cab 521 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 522 // draw frame below first
Pokitto 52:c04087025cab 523 display.setCursor(0,11*8);
Pokitto 52:c04087025cab 524 display.println(" abbbbbbbbbbbbbbbbbbbbbbbc");
Pokitto 52:c04087025cab 525 display.println(" | |");
Pokitto 52:c04087025cab 526 display.println(" | |");
Pokitto 52:c04087025cab 527 display.println(" | |");
Pokitto 52:c04087025cab 528 display.println(" | |");
Pokitto 52:c04087025cab 529 display.println(" dbbbbbbbbbbbbbbbbbbbbbbbe");
Pokitto 52:c04087025cab 530 } // wipe = true
Pokitto 52:c04087025cab 531 display.setCursor(6*8,17*9);
Pokitto 52:c04087025cab 532 if (discrete_vol<4) display.directcolor = COLOR_WHITE;
Pokitto 52:c04087025cab 533 else display.directcolor = COLOR_RED;
Pokitto 52:c04087025cab 534 display.directBitmap(21*8-4,6*8,Pokitto_headphones,1,2);
Pokitto 52:c04087025cab 535 display.setCursor(3*8,6*8+6);
Pokitto 52:c04087025cab 536 display.print("HEADPHONES");
Pokitto 52:c04087025cab 537 display.setCursor(3*8,8*8+2);
Pokitto 52:c04087025cab 538 if (discrete_vol>3) display.print("TOO LOUD!");
Pokitto 52:c04087025cab 539 else display.print("OK ");
Pokitto 52:c04087025cab 540 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 541 display.directBitmap(21*8-4,12*8,Pokitto_speaker,1,2);
Pokitto 52:c04087025cab 542 display.setCursor(3*8,12*8+6);
Pokitto 52:c04087025cab 543 display.print("VOLUME MAX");
Pokitto 52:c04087025cab 544 display.setCursor(3*8,14*8+2);
Pokitto 52:c04087025cab 545 //tvol = (vol/float(VOLUME_HEADPHONE_MAX))*100;
Pokitto 52:c04087025cab 546 //if (tvol > 100 && tvol < 120) tvol=100;
Pokitto 52:c04087025cab 547 //if (sound.getVolume()-5>VOLUME_HEADPHONE_MAX) { display.directcolor=COLOR_RED;}
Pokitto 52:c04087025cab 548 if ((sound.getVolume()>>5)>3) { display.directcolor=COLOR_RED;}
Pokitto 52:c04087025cab 549 else display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 550 if (discrete_vol==4) display.directcolor=COLOR_YELLOW;
Pokitto 52:c04087025cab 551 if (discrete_vol==7) discrete_vol=6;
Pokitto 52:c04087025cab 552 display.print((int)discrete_vol);
Pokitto 52:c04087025cab 553 //display.print(int(tvol));
Pokitto 52:c04087025cab 554 display.print(" ");
Pokitto 52:c04087025cab 555 display.directcolor=COLOR_GREEN;
Pokitto 52:c04087025cab 556 drawvolbar(14*8,14*8+4+3,sound.getVolume(),false);
Pokitto 52:c04087025cab 557 //display.setCursor(1,10);
Pokitto 52:c04087025cab 558 //display.print(vol);
Pokitto 52:c04087025cab 559 dstate=2; break;
Pokitto 52:c04087025cab 560 case 2:
Pokitto 52:c04087025cab 561 #ifndef POK_SIM
Pokitto 52:c04087025cab 562 buttons.pollButtons();
Pokitto 52:c04087025cab 563 #endif // POK_SIM
Pokitto 52:c04087025cab 564 buttons.update();
Pokitto 52:c04087025cab 565 if (aBtn()) {dstate=0;break;}
Pokitto 52:c04087025cab 566 if (buttons.pressed(BTN_RIGHT)) {
Pokitto 52:c04087025cab 567 countd=0xFFFF; //disable countdown
Pokitto 52:c04087025cab 568 /*if (vol >= VOLUME_HEADPHONE_MAX && vol < VOLUME_HEADPHONE_MAX+1 ) vol += 0.00025f*VINCMULT;
Pokitto 52:c04087025cab 569 else if (vol >= VOLUME_HEADPHONE_MAX) vol += 0.25f*VINCMULT;
Pokitto 52:c04087025cab 570 else vol += 0.05f*VINCMULT;
Pokitto 52:c04087025cab 571 if (vol > VOLUME_HEADPHONE_MAX + 20) {
Pokitto 52:c04087025cab 572 sound.setMaxVol(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 573 }
Pokitto 52:c04087025cab 574 if (vol > VOLUME_SPEAKER_MAX) vol=VOLUME_SPEAKER_MAX;
Pokitto 52:c04087025cab 575 sound.setVolume(vol);*/
Pokitto 52:c04087025cab 576 sound.volumeUp();
Pokitto 52:c04087025cab 577 dstate=1; wipe=false;
Pokitto 52:c04087025cab 578 break;
Pokitto 52:c04087025cab 579 }
Pokitto 52:c04087025cab 580 if (buttons.pressed(BTN_LEFT)) {
Pokitto 52:c04087025cab 581 countd=0xFFFF; //disable countdown
Pokitto 52:c04087025cab 582 /*if (vol >= VOLUME_HEADPHONE_MAX) vol -= 0.25f*VINCMULT;
Pokitto 52:c04087025cab 583 else vol -= 0.05f*VINCMULT;
Pokitto 52:c04087025cab 584 if (vol <= VOLUME_HEADPHONE_MAX) sound.setMaxVol(VOLUME_HEADPHONE_MAX);
Pokitto 52:c04087025cab 585 if (vol < 0) vol=0;
Pokitto 52:c04087025cab 586 sound.setVolume(vol);*/
Pokitto 52:c04087025cab 587 sound.volumeDown();
Pokitto 52:c04087025cab 588 dstate=1; wipe=false;
Pokitto 52:c04087025cab 589 break;
Pokitto 52:c04087025cab 590 }
Pokitto 52:c04087025cab 591 break;
Pokitto 52:c04087025cab 592 }
Pokitto 52:c04087025cab 593 }
Pokitto 52:c04087025cab 594 vol = sound.getVolume();
Pokitto 52:c04087025cab 595 #ifndef POK_SIM
Pokitto 52:c04087025cab 596 if (vol != eeprom_read_byte((uint16_t*)EESETTINGS_VOL)) eeprom_write_byte((uint16_t*)EESETTINGS_VOL,(uint8_t)vol);
Pokitto 52:c04087025cab 597 #endif
Pokitto 52:c04087025cab 598 sound.setVolume(vol);
Pokitto 52:c04087025cab 599 //sound.volumeUp();
Pokitto 52:c04087025cab 600 display.setCursor(0,0);
Pokitto 52:c04087025cab 601 }
Pokitto 52:c04087025cab 602
Pokitto 52:c04087025cab 603 void Core::begin() {
Pokitto 52:c04087025cab 604
Pokitto 52:c04087025cab 605 init(); // original functions
Pokitto 52:c04087025cab 606 timePerFrame = POK_FRAMEDURATION;
Pokitto 52:c04087025cab 607 //nextFrameMillis = 0;
Pokitto 52:c04087025cab 608 //frameCount = 0;
Pokitto 52:c04087025cab 609 frameEndMicros = 1;
Pokitto 52:c04087025cab 610 startMenuTimer = 255;
Pokitto 52:c04087025cab 611 //read default settings from flash memory (set using settings.hex)
Pokitto 52:c04087025cab 612 readSettings();
Pokitto 52:c04087025cab 613 //init everything
Pokitto 52:c04087025cab 614 backlight.begin();
Pokitto 52:c04087025cab 615 backlight.set(BACKLIGHT_MAX);
Pokitto 52:c04087025cab 616 buttons.begin();
Pokitto 52:c04087025cab 617 buttons.update();
Pokitto 52:c04087025cab 618 battery.begin();
Pokitto 52:c04087025cab 619 display.begin();
Pokitto 52:c04087025cab 620 #if POK_DISPLAYLOGO
Pokitto 52:c04087025cab 621 #if PROJ_DEVELOPER_MODE != 1
Pokitto 52:c04087025cab 622 showLogo();
Pokitto 52:c04087025cab 623 #endif // PROJ_DEVELOPER_MODE
Pokitto 52:c04087025cab 624 #endif // POK_DISPLAYLOGO
Pokitto 52:c04087025cab 625
Pokitto 52:c04087025cab 626 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 627 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 628 display.clearLCD();
Pokitto 52:c04087025cab 629 display.setFont(fntC64UIGfx);
Pokitto 52:c04087025cab 630
Pokitto 52:c04087025cab 631 display.enableDirectPrinting(true);
Pokitto 52:c04087025cab 632 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 633 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 634 display.clearLCD();
Pokitto 52:c04087025cab 635 display.setFont(fntC64UIGfx);
Pokitto 52:c04087025cab 636 display.adjustCharStep=0;
Pokitto 52:c04087025cab 637 display.adjustLineStep=1;
Pokitto 52:c04087025cab 638 #ifdef JUSTLOAD
Pokitto 52:c04087025cab 639 jumpToLoader();
Pokitto 52:c04087025cab 640 #endif
Pokitto 52:c04087025cab 641
Pokitto 52:c04087025cab 642 #ifndef DISABLE_LOADER
Pokitto 52:c04087025cab 643 #if PROJ_DEVELOPER_MODE != 1
Pokitto 52:c04087025cab 644 askLoader();
Pokitto 52:c04087025cab 645 #endif // PROJ_DEVELOPER_MODE
Pokitto 52:c04087025cab 646 #endif
Pokitto 52:c04087025cab 647
Pokitto 52:c04087025cab 648 #if PROJ_DEVELOPER_MODE==1
Pokitto 52:c04087025cab 649 sound.setMaxVol(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 650 sound.setVolume(VOLUME_SPEAKER_MAX);
Pokitto 52:c04087025cab 651 #else
Pokitto 52:c04087025cab 652 //showWarning();
Pokitto 52:c04087025cab 653 setVolLimit();
Pokitto 52:c04087025cab 654 display.clear();
Pokitto 52:c04087025cab 655 display.update();
Pokitto 52:c04087025cab 656
Pokitto 52:c04087025cab 657 while(buttons.states[BTN_A])
Pokitto 52:c04087025cab 658 {
Pokitto 52:c04087025cab 659 buttons.update();
Pokitto 52:c04087025cab 660 }
Pokitto 52:c04087025cab 661
Pokitto 52:c04087025cab 662 //sound.setVolume(sound.getVolume());//make sure we're at set volume before continue
Pokitto 52:c04087025cab 663 #endif
Pokitto 52:c04087025cab 664 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 665 display.adjustCharStep=1;
Pokitto 52:c04087025cab 666 display.adjustLineStep=1;
Pokitto 52:c04087025cab 667 display.fontSize=1;
Pokitto 52:c04087025cab 668 display.textWrap=true;
Pokitto 52:c04087025cab 669 #if POK_GAMEBUINO_SUPPORT > 0
Pokitto 52:c04087025cab 670 display.setFont(font5x7);
Pokitto 52:c04087025cab 671 #else
Pokitto 52:c04087025cab 672 display.setFont(fontC64);
Pokitto 52:c04087025cab 673 #endif
Pokitto 52:c04087025cab 674 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 675 sound.begin();
Pokitto 52:c04087025cab 676
Pokitto 52:c04087025cab 677 //mute when B is held during start up or if battery is low
Pokitto 52:c04087025cab 678 battery.update();
Pokitto 52:c04087025cab 679 if(buttons.pressed(BTN_B)){
Pokitto 52:c04087025cab 680 sound.setVolume(0);
Pokitto 52:c04087025cab 681 }
Pokitto 52:c04087025cab 682 else{ //play the startup sound on each channel for it to be louder
Pokitto 52:c04087025cab 683 #if POK_GBSOUND > 0
Pokitto 52:c04087025cab 684 #if(NUM_CHANNELS > 0)
Pokitto 52:c04087025cab 685 sound.playPattern(startupSound, 0);
Pokitto 52:c04087025cab 686 #endif
Pokitto 52:c04087025cab 687 #if(NUM_CHANNELS > 1)
Pokitto 52:c04087025cab 688 sound.playPattern(startupSound, 1);
Pokitto 52:c04087025cab 689 #endif
Pokitto 52:c04087025cab 690 #if(NUM_CHANNELS > 2)
Pokitto 52:c04087025cab 691 sound.playPattern(startupSound, 2);
Pokitto 52:c04087025cab 692 #endif
Pokitto 52:c04087025cab 693 #if(NUM_CHANNELS > 3)
Pokitto 52:c04087025cab 694 sound.playPattern(startupSound, 3);
Pokitto 52:c04087025cab 695 #endif
Pokitto 52:c04087025cab 696 #endif // POK_GBSOUND
Pokitto 52:c04087025cab 697 }
Pokitto 52:c04087025cab 698 #endif // POK ENABLE_SOUND
Pokitto 52:c04087025cab 699 }
Pokitto 52:c04087025cab 700
Pokitto 52:c04087025cab 701 void Core::init() {
Pokitto 52:c04087025cab 702 run_state = true;
Pokitto 52:c04087025cab 703 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 704 display.setFont(DEFAULT_FONT);
Pokitto 52:c04087025cab 705 initClock();
Pokitto 52:c04087025cab 706 initGPIO();
Pokitto 52:c04087025cab 707 initButtons();
Pokitto 52:c04087025cab 708 initRandom();
Pokitto 52:c04087025cab 709 //initAudio();
Pokitto 52:c04087025cab 710 //initDisplay();
Pokitto 52:c04087025cab 711 }
Pokitto 52:c04087025cab 712
Pokitto 52:c04087025cab 713 void Core::init(uint8_t switches) {
Pokitto 52:c04087025cab 714 run_state = true;
Pokitto 52:c04087025cab 715 display.enableDirectPrinting(false);
Pokitto 52:c04087025cab 716 display.setFont(DEFAULT_FONT);
Pokitto 52:c04087025cab 717 initClock();
Pokitto 52:c04087025cab 718 initGPIO();
Pokitto 52:c04087025cab 719 initButtons();
Pokitto 52:c04087025cab 720 initRandom();
Pokitto 52:c04087025cab 721 //initAudio();
Pokitto 52:c04087025cab 722 //initDisplay();
Pokitto 52:c04087025cab 723 }
Pokitto 52:c04087025cab 724
Pokitto 52:c04087025cab 725 void Core::initButtons() {
Pokitto 52:c04087025cab 726 #ifndef POK_SIM
Pokitto 52:c04087025cab 727 Pokitto::initButtons();
Pokitto 52:c04087025cab 728 #endif
Pokitto 52:c04087025cab 729 }
Pokitto 52:c04087025cab 730
Pokitto 52:c04087025cab 731 bool Core::isRunning() {
Pokitto 52:c04087025cab 732 #ifdef POK_SIM
Pokitto 52:c04087025cab 733 run_state = simulator.isRunning();
Pokitto 52:c04087025cab 734 #endif // POK_SIM
Pokitto 52:c04087025cab 735 return run_state;
Pokitto 52:c04087025cab 736 }
Pokitto 52:c04087025cab 737
Pokitto 52:c04087025cab 738 void Core::initDisplay() {
Pokitto 52:c04087025cab 739 #if POK_DISPLAYLOGO > 0
Pokitto 52:c04087025cab 740 showLogo();
Pokitto 52:c04087025cab 741 #endif
Pokitto 52:c04087025cab 742 #if POK_USE_CONSOLE > 0
Pokitto 52:c04087025cab 743 console.AddMessage(MSOURCE_LCD,MSG_INIT_OK);
Pokitto 52:c04087025cab 744 #endif
Pokitto 52:c04087025cab 745 }
Pokitto 52:c04087025cab 746
Pokitto 52:c04087025cab 747 void Core::showLogo() {
Pokitto 52:c04087025cab 748 uint32_t now;
Pokitto 52:c04087025cab 749 uint8_t state=0; //jump directly to logo, bypass teeth
Pokitto 59:61d308a0b113 750 uint16_t i=0;
Pokitto 52:c04087025cab 751 uint16_t sc;
Pokitto 52:c04087025cab 752 while (state < 255/6) {
Pokitto 52:c04087025cab 753 now=getTime();
Pokitto 52:c04087025cab 754 if (now>refreshtime) {
Pokitto 52:c04087025cab 755 refreshtime=now+30;
Pokitto 52:c04087025cab 756 switch (state) {
Pokitto 52:c04087025cab 757 case 0:
Pokitto 52:c04087025cab 758 /** POKITTO CLEAN **/
Pokitto 52:c04087025cab 759 display.directbgcolor = COLOR_BLACK;
Pokitto 52:c04087025cab 760 display.fillLCD(display.directbgcolor);
Pokitto 52:c04087025cab 761 sc = COLOR_BLACK;
Pokitto 52:c04087025cab 762 state++;
Pokitto 52:c04087025cab 763 break;
Pokitto 52:c04087025cab 764 case 1:
Pokitto 52:c04087025cab 765 /** POKITTO FADE IN **/
Pokitto 52:c04087025cab 766 display.directcolor = display.interpolateColor(sc, COLOR_GREEN, i);
Pokitto 52:c04087025cab 767 display.directBitmap(POK_LCD_W/2 - (*Pokitto_logo/2),POK_LCD_H/2-(*(Pokitto_logo+1)/2),Pokitto_logo,1,1);
Pokitto 52:c04087025cab 768 i += 28;
Pokitto 52:c04087025cab 769 if (i>=0xFF) { state++; i=0;}
Pokitto 52:c04087025cab 770 break;
Pokitto 52:c04087025cab 771 case 2:
Pokitto 52:c04087025cab 772 /** POKITTO WAIT **/
Pokitto 52:c04087025cab 773 display.directcolor = COLOR_GREEN;
Pokitto 52:c04087025cab 774 display.directBitmap(POK_LCD_W/2 - (*Pokitto_logo/2),POK_LCD_H/2-(*(Pokitto_logo+1)/2),Pokitto_logo,1,1);
Pokitto 52:c04087025cab 775 i+= 0x3F;
Pokitto 52:c04087025cab 776 if (i>0x3FF) state = 255;
Pokitto 52:c04087025cab 777 break;
Pokitto 52:c04087025cab 778 }
Pokitto 52:c04087025cab 779 if(buttons.aBtn()) state=255;
Pokitto 52:c04087025cab 780 }
Pokitto 52:c04087025cab 781 }
Pokitto 52:c04087025cab 782 }
Pokitto 52:c04087025cab 783
Pokitto 52:c04087025cab 784 void Core::readSettings() {
Pokitto 52:c04087025cab 785 // ToDo
Pokitto 52:c04087025cab 786 /*display.contrast = SCR_CONTRAST;
Pokitto 52:c04087025cab 787 backlight.backlightMin = BACKLIGHT_MIN;
Pokitto 52:c04087025cab 788 backlight.backlightMax = BACKLIGHT_MAX;
Pokitto 52:c04087025cab 789 backlight.ambientLightMin = AMBIENTLIGHT_MIN;
Pokitto 52:c04087025cab 790 backlight.ambientLightMax = AMBIENTLIGHT_MAX;
Pokitto 52:c04087025cab 791 */
Pokitto 52:c04087025cab 792 sound.setMaxVol(VOLUME_HEADPHONE_MAX);
Pokitto 52:c04087025cab 793 sound.globalVolume = VOLUME_STARTUP;
Pokitto 52:c04087025cab 794
Pokitto 52:c04087025cab 795 startMenuTimer = START_MENU_TIMER;
Pokitto 52:c04087025cab 796 /*
Pokitto 52:c04087025cab 797 battery.thresolds[0] = BAT_LVL_CRITIC;
Pokitto 52:c04087025cab 798 battery.thresolds[1] = BAT_LVL_LOW;
Pokitto 52:c04087025cab 799 battery.thresolds[2] = BAT_LVL_MED;
Pokitto 52:c04087025cab 800 battery.thresolds[3] = BAT_LVL_FULL;*/
Pokitto 52:c04087025cab 801 }
Pokitto 52:c04087025cab 802
Pokitto 52:c04087025cab 803 void Core::titleScreen(const char* name){
Pokitto 52:c04087025cab 804 titleScreen(name, 0);
Pokitto 52:c04087025cab 805 }
Pokitto 52:c04087025cab 806
Pokitto 52:c04087025cab 807 void Core::titleScreen(const uint8_t* logo){
Pokitto 52:c04087025cab 808 titleScreen((""), logo);
Pokitto 52:c04087025cab 809 }
Pokitto 52:c04087025cab 810
Pokitto 52:c04087025cab 811 void Core::titleScreen(){
Pokitto 52:c04087025cab 812 titleScreen((""));
Pokitto 52:c04087025cab 813 }
Pokitto 52:c04087025cab 814
Pokitto 52:c04087025cab 815 void Core::titleScreen(const char* name, const uint8_t *logo){
Pokitto 52:c04087025cab 816 display.setFont(font5x7);
Pokitto 52:c04087025cab 817 while(buttons.aBtn()) wait(10); //don't accidentally skip menu
Pokitto 52:c04087025cab 818 if(startMenuTimer){
Pokitto 52:c04087025cab 819 display.fontSize = 1;
Pokitto 52:c04087025cab 820 display.textWrap = false;
Pokitto 52:c04087025cab 821 display.persistence = false;
Pokitto 52:c04087025cab 822 battery.show = false;
Pokitto 52:c04087025cab 823 display.setColor(BLACK);
Pokitto 52:c04087025cab 824 while(isRunning()){
Pokitto 52:c04087025cab 825 if(update()){
Pokitto 52:c04087025cab 826 uint8_t logoOffset = name[0]?display.fontHeight:0; //add an offset the logo when there is a name to display
Pokitto 52:c04087025cab 827 //draw graphics
Pokitto 52:c04087025cab 828 display.setColorDepth(1);
Pokitto 52:c04087025cab 829 display.setColor(3);
Pokitto 52:c04087025cab 830 //display.drawBitmap(0,0, gamebuinoLogo);
Pokitto 52:c04087025cab 831 display.setColor(1);
Pokitto 52:c04087025cab 832 if(logo){
Pokitto 52:c04087025cab 833 display.drawBitmap(0, 12+logoOffset, logo);
Pokitto 52:c04087025cab 834 }
Pokitto 52:c04087025cab 835 display.cursorX = 0;
Pokitto 52:c04087025cab 836 display.cursorY = 12;
Pokitto 52:c04087025cab 837 display.print(name);
Pokitto 52:c04087025cab 838
Pokitto 52:c04087025cab 839 //A button
Pokitto 52:c04087025cab 840 display.cursorX = LCDWIDTH - display.fontWidth*3 -1;
Pokitto 52:c04087025cab 841 display.cursorY = LCDHEIGHT - display.fontHeight*3 - 3;
Pokitto 52:c04087025cab 842 if((frameCount/16)%2)
Pokitto 52:c04087025cab 843 display.println(("\25 \20"));
Pokitto 52:c04087025cab 844 else
Pokitto 52:c04087025cab 845 display.println(("\25\20 "));
Pokitto 52:c04087025cab 846 //B button
Pokitto 52:c04087025cab 847 display.cursorX = LCDWIDTH - display.fontWidth*3 - 1;
Pokitto 52:c04087025cab 848 display.cursorY++;
Pokitto 52:c04087025cab 849 if(sound.globalVolume)
Pokitto 52:c04087025cab 850 display.println(("\26\23\24"));
Pokitto 52:c04087025cab 851 else
Pokitto 52:c04087025cab 852 display.println(("\26\23x"));
Pokitto 52:c04087025cab 853 //C button
Pokitto 52:c04087025cab 854 display.cursorX = LCDWIDTH - display.fontWidth*3 - 1;
Pokitto 52:c04087025cab 855 display.cursorY++;
Pokitto 52:c04087025cab 856 //display.println(F("\27SD"));
Pokitto 52:c04087025cab 857
Pokitto 52:c04087025cab 858 //toggle volume when B is pressed
Pokitto 52:c04087025cab 859 if(buttons.pressed(BTN_B)){
Pokitto 52:c04087025cab 860 sound.setVolume(sound.getVolume() + 1);
Pokitto 52:c04087025cab 861 sound.playTick();
Pokitto 52:c04087025cab 862 }
Pokitto 52:c04087025cab 863 //leave the menu
Pokitto 52:c04087025cab 864 if(buttons.pressed(BTN_A) || ((frameCount>=startMenuTimer)&&(startMenuTimer != 255))){
Pokitto 52:c04087025cab 865 startMenuTimer = 255; //don't automatically skip the title screen next time it's displayed
Pokitto 52:c04087025cab 866 sound.stopPattern(0);
Pokitto 52:c04087025cab 867 sound.playOK();
Pokitto 52:c04087025cab 868 break;
Pokitto 52:c04087025cab 869 }
Pokitto 52:c04087025cab 870 //flash the loader
Pokitto 52:c04087025cab 871 //if(buttons.pressed(BTN_C))
Pokitto 52:c04087025cab 872 // ToDo changeGame();
Pokitto 52:c04087025cab 873 }
Pokitto 52:c04087025cab 874 }
Pokitto 52:c04087025cab 875 battery.show = true;
Pokitto 52:c04087025cab 876 }
Pokitto 52:c04087025cab 877 }
Pokitto 52:c04087025cab 878
Pokitto 52:c04087025cab 879 /**
Pokitto 52:c04087025cab 880 * Update all the subsystems, like graphics, audio, events, etc.
Pokitto 52:c04087025cab 881 * Note: the update rect is used for drawing only part of the screen buffer to LCD. Because of speed optimizations,
Pokitto 52:c04087025cab 882 * 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 883 * The update rect is currently used for 220x176, 4 colors, screen mode only.
Pokitto 52:c04087025cab 884 * @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 885 * @param updRectX The update rect.
Pokitto 52:c04087025cab 886 * @param updRectY The update rect.
Pokitto 52:c04087025cab 887 * @param updRectW The update rect.
Pokitto 52:c04087025cab 888 * @param updRectH The update rect.
Pokitto 52:c04087025cab 889 */
Pokitto 52:c04087025cab 890 bool Core::update(bool useDirectMode, uint8_t updRectX, uint8_t updRectY, uint8_t updRectW, uint8_t updRectH) {
Pokitto 52:c04087025cab 891
Pokitto 52:c04087025cab 892 #if POK_STREAMING_MUSIC
Pokitto 52:c04087025cab 893 sound.updateStream();
Pokitto 52:c04087025cab 894 #endif
Pokitto 52:c04087025cab 895
Pokitto 52:c04087025cab 896 uint32_t now = getTime();
Pokitto 52:c04087025cab 897 if ((((nextFrameMillis - now)) > timePerFrame) && frameEndMicros) { //if time to render a new frame is reached and the frame end has ran once
Pokitto 52:c04087025cab 898 nextFrameMillis = now + timePerFrame;
Pokitto 52:c04087025cab 899 frameCount++;
Pokitto 52:c04087025cab 900
Pokitto 52:c04087025cab 901 frameEndMicros = 0;
Pokitto 52:c04087025cab 902 backlight.update();
Pokitto 52:c04087025cab 903 buttons.update();
Pokitto 52:c04087025cab 904 battery.update();
Pokitto 52:c04087025cab 905
Pokitto 52:c04087025cab 906 // FPS counter
Pokitto 52:c04087025cab 907 #if defined(PROJ_USE_FPS_COUNTER) || defined(PROJ_SHOW_FPS_COUNTER)
Pokitto 52:c04087025cab 908 const uint32_t fpsInterval_ms = 1000*3;
Pokitto 52:c04087025cab 909
Pokitto 52:c04087025cab 910 fps_frameCount++;
Pokitto 52:c04087025cab 911 if (now > fps_refreshtime) {
Pokitto 52:c04087025cab 912 fps_counter = (1000*fps_frameCount) / (now - fps_refreshtime + fpsInterval_ms);
Pokitto 52:c04087025cab 913 fps_refreshtime = now + fpsInterval_ms;
Pokitto 52:c04087025cab 914 fps_frameCount = 0;
Pokitto 52:c04087025cab 915 fps_counter_updated = true;
Pokitto 52:c04087025cab 916 }
Pokitto 52:c04087025cab 917 #endif
Pokitto 52:c04087025cab 918
Pokitto 52:c04087025cab 919 return true;
Pokitto 52:c04087025cab 920
Pokitto 52:c04087025cab 921 } else {
Pokitto 52:c04087025cab 922 if (!frameEndMicros) { //runs once at the end of the frame
Pokitto 52:c04087025cab 923 #if POK_ENABLE_SOUND > 0
Pokitto 52:c04087025cab 924 sound.updateTrack();
Pokitto 52:c04087025cab 925 sound.updatePattern();
Pokitto 52:c04087025cab 926 sound.updateNote();
Pokitto 52:c04087025cab 927 #endif
Pokitto 52:c04087025cab 928 updatePopup();
Pokitto 52:c04087025cab 929 displayBattery();
Pokitto 52:c04087025cab 930
Pokitto 52:c04087025cab 931 display.update(useDirectMode, updRectX, updRectY, updRectW, updRectH); //send the buffer to the screen
Pokitto 52:c04087025cab 932
Pokitto 52:c04087025cab 933 frameEndMicros = 1; //jonne
Pokitto 52:c04087025cab 934
Pokitto 52:c04087025cab 935 }
Pokitto 52:c04087025cab 936 return false;
Pokitto 52:c04087025cab 937 }
Pokitto 52:c04087025cab 938 }
Pokitto 52:c04087025cab 939
Pokitto 52:c04087025cab 940 void Core::displayBattery(){
Pokitto 52:c04087025cab 941 #if (ENABLE_BATTERY > 0)
Pokitto 52:c04087025cab 942 //display.setColor(BLACK, WHITE);
Pokitto 52:c04087025cab 943 uint8_t ox,oy;
Pokitto 52:c04087025cab 944 ox=display.cursorX;
Pokitto 52:c04087025cab 945 oy=display.cursorY;
Pokitto 52:c04087025cab 946 display.cursorX = LCDWIDTH-display.fontWidth+1;
Pokitto 52:c04087025cab 947 display.cursorY = 0;
Pokitto 52:c04087025cab 948 switch(battery.level){
Pokitto 52:c04087025cab 949 case 0://battery critic, power down
Pokitto 52:c04087025cab 950 sound.stopPattern();
Pokitto 52:c04087025cab 951 backlight.set(0);
Pokitto 52:c04087025cab 952 display.clear();
Pokitto 52:c04087025cab 953 display.fontSize = 1;
Pokitto 52:c04087025cab 954 display.print(("LOW BATTERY\n"));
Pokitto 52:c04087025cab 955 display.print(battery.voltage);
Pokitto 52:c04087025cab 956 display.print(("mV\n\nPLEASE\nTURN OFF"));
Pokitto 52:c04087025cab 957 display.update();
Pokitto 52:c04087025cab 958 break;
Pokitto 52:c04087025cab 959 case 1: //empty battery
Pokitto 52:c04087025cab 960 if((frameCount % 16) < 8) display.print('\7'); //blinking battery
Pokitto 52:c04087025cab 961 else display.print('x');
Pokitto 52:c04087025cab 962 break;
Pokitto 52:c04087025cab 963 case 2://low battery
Pokitto 52:c04087025cab 964 case 3://full battery
Pokitto 52:c04087025cab 965 case 4://full battery
Pokitto 52:c04087025cab 966 if(battery.show){
Pokitto 52:c04087025cab 967 display.print(char(5+battery.level));
Pokitto 52:c04087025cab 968 }
Pokitto 52:c04087025cab 969 break;
Pokitto 52:c04087025cab 970 default:
Pokitto 52:c04087025cab 971 if(battery.show){
Pokitto 52:c04087025cab 972 display.print('/');
Pokitto 52:c04087025cab 973 }
Pokitto 52:c04087025cab 974 break;
Pokitto 52:c04087025cab 975 }
Pokitto 52:c04087025cab 976 display.cursorX = ox;
Pokitto 52:c04087025cab 977 display.cursorY = oy;
Pokitto 52:c04087025cab 978 #endif
Pokitto 52:c04087025cab 979 }
Pokitto 52:c04087025cab 980
Pokitto 52:c04087025cab 981 char* Core::filemenu(char *ext) {
Pokitto 52:c04087025cab 982 uint8_t oldPersistence = display.persistence;
Pokitto 52:c04087025cab 983 display.persistence = false;
Pokitto 52:c04087025cab 984 uint16_t oldpal0=display.palette[0];
Pokitto 52:c04087025cab 985 uint16_t oldpal1=display.palette[1];
Pokitto 52:c04087025cab 986 uint16_t oldpal2=display.palette[2];
Pokitto 52:c04087025cab 987 display.palette[2]=COLOR_GREEN;
Pokitto 52:c04087025cab 988 display.palette[1]=COLOR_WHITE;
Pokitto 52:c04087025cab 989 display.palette[0]=COLOR_BLACK;
Pokitto 52:c04087025cab 990 uint8_t oldbg=display.bgcolor;
Pokitto 52:c04087025cab 991 uint8_t oldfg=display.color;
Pokitto 52:c04087025cab 992 display.color=1;
Pokitto 52:c04087025cab 993 display.bgcolor=0;
Pokitto 52:c04087025cab 994
Pokitto 52:c04087025cab 995 int16_t rowh = display.fontHeight + 2;
Pokitto 52:c04087025cab 996 int8_t activeItem = 0;
Pokitto 52:c04087025cab 997 int16_t currentTopItem = 0;
Pokitto 52:c04087025cab 998 int16_t itemsPerScreen = (display.height+1) / rowh;
Pokitto 52:c04087025cab 999 int16_t numOfItemsFound = 0;
Pokitto 52:c04087025cab 1000 boolean exit = false;
Pokitto 52:c04087025cab 1001
Pokitto 52:c04087025cab 1002 char* txt;
Pokitto 52:c04087025cab 1003
Pokitto 52:c04087025cab 1004 pokInitSD();
Pokitto 52:c04087025cab 1005
Pokitto 52:c04087025cab 1006 bool updated = true;
Pokitto 52:c04087025cab 1007 while (isRunning()) {
Pokitto 52:c04087025cab 1008 if (update(true)) {
Pokitto 52:c04087025cab 1009 if (buttons.pressed(BTN_A) || buttons.pressed(BTN_B) || buttons.pressed(BTN_C)) {
Pokitto 52:c04087025cab 1010 exit = true; //time to exit menu !
Pokitto 52:c04087025cab 1011 if (buttons.pressed(BTN_A)) {
Pokitto 52:c04087025cab 1012 sound.playOK();
Pokitto 52:c04087025cab 1013 } else {
Pokitto 52:c04087025cab 1014 *selectedfile = 0;
Pokitto 52:c04087025cab 1015 sound.playCancel();
Pokitto 52:c04087025cab 1016 }
Pokitto 52:c04087025cab 1017 updated = true; // update screen
Pokitto 52:c04087025cab 1018 }
Pokitto 52:c04087025cab 1019 if (exit == false) {
Pokitto 52:c04087025cab 1020 if (buttons.repeat(BTN_DOWN,4)) {
Pokitto 52:c04087025cab 1021 if( ++activeItem >= numOfItemsFound ) activeItem = numOfItemsFound - 1;
Pokitto 52:c04087025cab 1022 if( activeItem >= currentTopItem + itemsPerScreen) currentTopItem += itemsPerScreen; // next page
Pokitto 52:c04087025cab 1023 sound.playTick();
Pokitto 52:c04087025cab 1024 updated = true; // update screen
Pokitto 52:c04087025cab 1025 }
Pokitto 52:c04087025cab 1026 if (buttons.repeat(BTN_UP,4)) {
Pokitto 52:c04087025cab 1027 if( --activeItem < 0 ) activeItem = 0;
Pokitto 52:c04087025cab 1028 if( activeItem < currentTopItem) currentTopItem -= itemsPerScreen; // previous page
Pokitto 52:c04087025cab 1029 sound.playTick();
Pokitto 52:c04087025cab 1030 updated = true; // update screen
Pokitto 52:c04087025cab 1031 }
Pokitto 52:c04087025cab 1032
Pokitto 52:c04087025cab 1033 } else { //exit :
Pokitto 52:c04087025cab 1034 display.bgcolor=oldbg;
Pokitto 52:c04087025cab 1035 display.color=oldfg;
Pokitto 52:c04087025cab 1036 display.palette[0] = oldpal0;
Pokitto 52:c04087025cab 1037 display.palette[1] = oldpal1;
Pokitto 52:c04087025cab 1038 display.palette[2] = oldpal2;
Pokitto 52:c04087025cab 1039 display.persistence = oldPersistence;
Pokitto 52:c04087025cab 1040 return selectedfile;
Pokitto 52:c04087025cab 1041 }
Pokitto 52:c04087025cab 1042
Pokitto 52:c04087025cab 1043 //draw a fancy menu
Pokitto 52:c04087025cab 1044 display.textWrap = false;
Pokitto 59:61d308a0b113 1045 //uint16_t fc,bc;
Pokitto 59:61d308a0b113 1046 //fc = display.color;
Pokitto 59:61d308a0b113 1047 //bc = display.bgcolor;
Pokitto 52:c04087025cab 1048 if( updated ) { // update screen?
Pokitto 52:c04087025cab 1049 #if POK_SIM
Pokitto 52:c04087025cab 1050 getFirstFile(ext);
Pokitto 52:c04087025cab 1051 #else
Pokitto 52:c04087025cab 1052 bool isFirstFile = true;
Pokitto 52:c04087025cab 1053 #endif
Pokitto 52:c04087025cab 1054 for (int i = 0; ; i++) {
Pokitto 52:c04087025cab 1055 display.cursorX = 0;
Pokitto 52:c04087025cab 1056 display.invisiblecolor=255;
Pokitto 52:c04087025cab 1057 display.cursorY = (i - currentTopItem ) * rowh;
Pokitto 52:c04087025cab 1058
Pokitto 52:c04087025cab 1059 // read the file name from SD
Pokitto 52:c04087025cab 1060 #ifndef POK_SIM
Pokitto 52:c04087025cab 1061 if(isFirstFile) {
Pokitto 52:c04087025cab 1062 txt = getFirstFile(ext);
Pokitto 52:c04087025cab 1063 isFirstFile = false;
Pokitto 52:c04087025cab 1064 }
Pokitto 52:c04087025cab 1065 else
Pokitto 52:c04087025cab 1066 #endif
Pokitto 52:c04087025cab 1067
Pokitto 52:c04087025cab 1068 txt = getNextFile(ext);
Pokitto 52:c04087025cab 1069
Pokitto 52:c04087025cab 1070 if (txt) {
Pokitto 52:c04087025cab 1071
Pokitto 52:c04087025cab 1072 numOfItemsFound = i+1;
Pokitto 52:c04087025cab 1073
Pokitto 52:c04087025cab 1074 // Draw active line with diffrent backgtound and char color
Pokitto 52:c04087025cab 1075 if (i == activeItem){
Pokitto 52:c04087025cab 1076 display.cursorX = 3;
Pokitto 52:c04087025cab 1077 display.color=2;
Pokitto 52:c04087025cab 1078 display.fillRect(0, display.cursorY - 2, LCDWIDTH, rowh);
Pokitto 52:c04087025cab 1079 display.setColor(0,2);
Pokitto 52:c04087025cab 1080 } else display.setColor(1,0);
Pokitto 52:c04087025cab 1081
Pokitto 52:c04087025cab 1082 // break loop if going out of screen
Pokitto 52:c04087025cab 1083 if(i >= currentTopItem + itemsPerScreen ) {
Pokitto 52:c04087025cab 1084 break;
Pokitto 52:c04087025cab 1085 }
Pokitto 52:c04087025cab 1086
Pokitto 52:c04087025cab 1087 // Display only if the file is on the current page
Pokitto 52:c04087025cab 1088 if( i >= currentTopItem) {
Pokitto 52:c04087025cab 1089 display.print(display.cursorX, display.cursorY, txt);
Pokitto 52:c04087025cab 1090 if (i == activeItem)
Pokitto 52:c04087025cab 1091 strcpy(selectedfile,txt);
Pokitto 52:c04087025cab 1092 }
Pokitto 52:c04087025cab 1093 } else
Pokitto 52:c04087025cab 1094 break; // break loop as no more files found
Pokitto 52:c04087025cab 1095
Pokitto 52:c04087025cab 1096 display.setColor(1,0);
Pokitto 52:c04087025cab 1097 } // end for
Pokitto 52:c04087025cab 1098
Pokitto 52:c04087025cab 1099 display.update();
Pokitto 52:c04087025cab 1100 }
Pokitto 52:c04087025cab 1101 updated = false;
Pokitto 52:c04087025cab 1102 } // update
Pokitto 52:c04087025cab 1103
Pokitto 52:c04087025cab 1104 display.setColor(1,0);
Pokitto 52:c04087025cab 1105 }
Pokitto 52:c04087025cab 1106 return 0;
Pokitto 52:c04087025cab 1107 }
Pokitto 52:c04087025cab 1108
Pokitto 52:c04087025cab 1109 char* Core::filemenu() {
Pokitto 52:c04087025cab 1110 return filemenu("");
Pokitto 52:c04087025cab 1111 }
Pokitto 52:c04087025cab 1112
Pokitto 52:c04087025cab 1113 int8_t Core::menu(const char* const* items, uint8_t length) {
Pokitto 52:c04087025cab 1114 if (display.color>3) display.color=1;
Pokitto 52:c04087025cab 1115 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1116 display.persistence = false;
Pokitto 52:c04087025cab 1117 int8_t activeItem = 0;
Pokitto 52:c04087025cab 1118 int16_t currentY = display.height;
Pokitto 52:c04087025cab 1119 int16_t targetY = 0, rowh = display.fontHeight + 2;
Pokitto 52:c04087025cab 1120 boolean exit = false;
Pokitto 52:c04087025cab 1121 int8_t answer = -1;
Pokitto 52:c04087025cab 1122 while (isRunning()) {
Pokitto 52:c04087025cab 1123 if (update()) {
Pokitto 52:c04087025cab 1124 if (buttons.pressed(BTN_A) || buttons.pressed(BTN_B) || buttons.pressed(BTN_C)) {
Pokitto 52:c04087025cab 1125 exit = true; //time to exit menu !
Pokitto 52:c04087025cab 1126 targetY = - display.fontHeight * length - 2; //send the menu out of the screen
Pokitto 52:c04087025cab 1127 if (buttons.pressed(BTN_A)) {
Pokitto 52:c04087025cab 1128 answer = activeItem;
Pokitto 52:c04087025cab 1129 sound.playOK();
Pokitto 52:c04087025cab 1130 } else {
Pokitto 52:c04087025cab 1131 sound.playCancel();
Pokitto 52:c04087025cab 1132 }
Pokitto 52:c04087025cab 1133 }
Pokitto 52:c04087025cab 1134 if (exit == false) {
Pokitto 52:c04087025cab 1135 if (buttons.repeat(BTN_DOWN,4)) {
Pokitto 52:c04087025cab 1136 activeItem++;
Pokitto 52:c04087025cab 1137 sound.playTick();
Pokitto 52:c04087025cab 1138 }
Pokitto 52:c04087025cab 1139 if (buttons.repeat(BTN_UP,4)) {
Pokitto 52:c04087025cab 1140 activeItem--;
Pokitto 52:c04087025cab 1141 sound.playTick();
Pokitto 52:c04087025cab 1142 }
Pokitto 52:c04087025cab 1143 //don't go out of the menu
Pokitto 52:c04087025cab 1144 if (activeItem == length) activeItem = 0;
Pokitto 52:c04087025cab 1145 if (activeItem < 0) activeItem = length - 1;
Pokitto 52:c04087025cab 1146
Pokitto 52:c04087025cab 1147 targetY = -rowh * activeItem + (rowh+4); //center the menu on the active item
Pokitto 52:c04087025cab 1148 } else { //exit :
Pokitto 52:c04087025cab 1149 if ((currentY - targetY) <= 1)
Pokitto 52:c04087025cab 1150 return (answer);
Pokitto 52:c04087025cab 1151 }
Pokitto 52:c04087025cab 1152 //draw a fancy menu
Pokitto 52:c04087025cab 1153 currentY = (currentY + targetY) / 2;
Pokitto 52:c04087025cab 1154 display.cursorX = 0;
Pokitto 52:c04087025cab 1155 display.cursorY = currentY;
Pokitto 52:c04087025cab 1156 display.textWrap = false;
Pokitto 52:c04087025cab 1157 uint16_t fc,bc;
Pokitto 52:c04087025cab 1158 fc = display.color;
Pokitto 52:c04087025cab 1159 bc = display.bgcolor;
Pokitto 52:c04087025cab 1160 for (byte i = 0; i < length; i++) {
Pokitto 52:c04087025cab 1161 display.cursorY = currentY + rowh * i;
Pokitto 52:c04087025cab 1162 if (i == activeItem){
Pokitto 52:c04087025cab 1163 display.cursorX = 3;
Pokitto 52:c04087025cab 1164
Pokitto 52:c04087025cab 1165 //display.fillRoundRect(0, currentY + display.fontHeight * activeItem - 2, LCDWIDTH, (display.fontHeight+3), 3);
Pokitto 52:c04087025cab 1166 display.fillRect(0, currentY + rowh * activeItem - 2, LCDWIDTH, (rowh));
Pokitto 52:c04087025cab 1167 display.setColor(bc,fc);
Pokitto 52:c04087025cab 1168 } else display.setColor(fc,bc);
Pokitto 52:c04087025cab 1169
Pokitto 52:c04087025cab 1170 display.println((char*)*(const unsigned int*)(items+i));
Pokitto 52:c04087025cab 1171 display.setColor(fc,bc);
Pokitto 52:c04087025cab 1172 }
Pokitto 52:c04087025cab 1173
Pokitto 52:c04087025cab 1174 }
Pokitto 52:c04087025cab 1175 }
Pokitto 52:c04087025cab 1176 #else
Pokitto 52:c04087025cab 1177 return 0;
Pokitto 52:c04087025cab 1178 #endif
Pokitto 52:c04087025cab 1179 return 0;
Pokitto 52:c04087025cab 1180 }
Pokitto 52:c04087025cab 1181
Pokitto 52:c04087025cab 1182 void Core::keyboard(char* text, uint8_t length) {
Pokitto 52:c04087025cab 1183 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1184 display.persistence = false;
Pokitto 52:c04087025cab 1185 //memset(text, 0, length); //clear the text
Pokitto 52:c04087025cab 1186 text[length-1] = '\0';
Pokitto 52:c04087025cab 1187 //active character in the typing area
Pokitto 52:c04087025cab 1188 int8_t activeChar = 0;
Pokitto 52:c04087025cab 1189 //selected char on the keyboard
Pokitto 52:c04087025cab 1190 int8_t activeX = 0;
Pokitto 52:c04087025cab 1191 int8_t activeY = 2;
Pokitto 52:c04087025cab 1192 //position of the keyboard on the screen
Pokitto 52:c04087025cab 1193 int8_t currentX = LCDWIDTH;
Pokitto 52:c04087025cab 1194 int8_t currentY = LCDHEIGHT;
Pokitto 52:c04087025cab 1195 int8_t targetX = 0;
Pokitto 52:c04087025cab 1196 int8_t targetY = 0;
Pokitto 52:c04087025cab 1197
Pokitto 52:c04087025cab 1198 while (1) {
Pokitto 52:c04087025cab 1199 if (update()) {
Pokitto 52:c04087025cab 1200 //move the character selector
Pokitto 52:c04087025cab 1201 if (buttons.repeat(BTN_DOWN, 4)) {
Pokitto 52:c04087025cab 1202 activeY++;
Pokitto 52:c04087025cab 1203 sound.playTick();
Pokitto 52:c04087025cab 1204 }
Pokitto 52:c04087025cab 1205 if (buttons.repeat(BTN_UP, 4)) {
Pokitto 52:c04087025cab 1206 activeY--;
Pokitto 52:c04087025cab 1207 sound.playTick();
Pokitto 52:c04087025cab 1208 }
Pokitto 52:c04087025cab 1209 if (buttons.repeat(BTN_RIGHT, 4)) {
Pokitto 52:c04087025cab 1210 activeX++;
Pokitto 52:c04087025cab 1211 sound.playTick();
Pokitto 52:c04087025cab 1212 }
Pokitto 52:c04087025cab 1213 if (buttons.repeat(BTN_LEFT, 4)) {
Pokitto 52:c04087025cab 1214 activeX--;
Pokitto 52:c04087025cab 1215 sound.playTick();
Pokitto 52:c04087025cab 1216 }
Pokitto 52:c04087025cab 1217 //don't go out of the keyboard
Pokitto 52:c04087025cab 1218 if (activeX == KEYBOARD_W) activeX = 0;
Pokitto 52:c04087025cab 1219 if (activeX < 0) activeX = KEYBOARD_W - 1;
Pokitto 52:c04087025cab 1220 if (activeY == KEYBOARD_H) activeY = 0;
Pokitto 52:c04087025cab 1221 if (activeY < 0) activeY = KEYBOARD_H - 1;
Pokitto 52:c04087025cab 1222 //set the keyboard position on screen
Pokitto 52:c04087025cab 1223 targetX = -(display.fontWidth+1) * activeX + LCDWIDTH / 2 - 3;
Pokitto 52:c04087025cab 1224 targetY = -(display.fontHeight+1) * activeY + LCDHEIGHT / 2 - 4 - display.fontHeight;
Pokitto 52:c04087025cab 1225 //smooth the keyboard displacement
Pokitto 52:c04087025cab 1226 currentX = (targetX + currentX) / 2;
Pokitto 52:c04087025cab 1227 currentY = (targetY + currentY) / 2;
Pokitto 52:c04087025cab 1228 //type character
Pokitto 52:c04087025cab 1229 if (buttons.pressed(BTN_A)) {
Pokitto 52:c04087025cab 1230 if (activeChar < (length-1)) {
Pokitto 52:c04087025cab 1231 byte thisChar = activeX + KEYBOARD_W * activeY;
Pokitto 52:c04087025cab 1232 if((thisChar == 0)||(thisChar == 10)||(thisChar == 13)) //avoid line feed and carriage return
Pokitto 52:c04087025cab 1233 continue;
Pokitto 52:c04087025cab 1234 text[activeChar] = thisChar;
Pokitto 52:c04087025cab 1235 text[activeChar+1] = '\0';
Pokitto 52:c04087025cab 1236 }
Pokitto 52:c04087025cab 1237 activeChar++;
Pokitto 52:c04087025cab 1238 sound.playOK();
Pokitto 52:c04087025cab 1239 if (activeChar > length)
Pokitto 52:c04087025cab 1240 activeChar = length;
Pokitto 52:c04087025cab 1241 }
Pokitto 52:c04087025cab 1242 //erase character
Pokitto 52:c04087025cab 1243 if (buttons.pressed(BTN_B)) {
Pokitto 52:c04087025cab 1244 activeChar--;
Pokitto 52:c04087025cab 1245 sound.playCancel();
Pokitto 52:c04087025cab 1246 if (activeChar >= 0)
Pokitto 52:c04087025cab 1247 text[activeChar] = 0;
Pokitto 52:c04087025cab 1248 else
Pokitto 52:c04087025cab 1249 activeChar = 0;
Pokitto 52:c04087025cab 1250 }
Pokitto 52:c04087025cab 1251 //leave menu
Pokitto 52:c04087025cab 1252 if (buttons.pressed(BTN_C)) {
Pokitto 52:c04087025cab 1253 sound.playOK();
Pokitto 52:c04087025cab 1254 while (1) {
Pokitto 52:c04087025cab 1255 if (update()) {
Pokitto 52:c04087025cab 1256 //display.setCursor(0,0);
Pokitto 52:c04087025cab 1257 display.println(("You entered\n"));
Pokitto 52:c04087025cab 1258 display.print(text);
Pokitto 52:c04087025cab 1259 display.println(("\n\n\n\x15:okay \x16:edit"));
Pokitto 52:c04087025cab 1260 if(buttons.pressed(BTN_A)){
Pokitto 52:c04087025cab 1261 sound.playOK();
Pokitto 52:c04087025cab 1262 return;
Pokitto 52:c04087025cab 1263 }
Pokitto 52:c04087025cab 1264 if(buttons.pressed(BTN_B)){
Pokitto 52:c04087025cab 1265 sound.playCancel();
Pokitto 52:c04087025cab 1266 break;
Pokitto 52:c04087025cab 1267 }
Pokitto 52:c04087025cab 1268 }
Pokitto 52:c04087025cab 1269 }
Pokitto 52:c04087025cab 1270 }
Pokitto 52:c04087025cab 1271 //draw the keyboard
Pokitto 52:c04087025cab 1272 for (int8_t y = 0; y < KEYBOARD_H; y++) {
Pokitto 52:c04087025cab 1273 for (int8_t x = 0; x < KEYBOARD_W; x++) {
Pokitto 52:c04087025cab 1274 display.drawChar(currentX + x * (display.fontWidth+1), currentY + y * (display.fontHeight+1), x + y * KEYBOARD_W, 1);
Pokitto 52:c04087025cab 1275 }
Pokitto 52:c04087025cab 1276 }
Pokitto 52:c04087025cab 1277 //draw instruction
Pokitto 52:c04087025cab 1278 display.cursorX = currentX-display.fontWidth*6-2;
Pokitto 52:c04087025cab 1279 display.cursorY = currentY+1*(display.fontHeight+1);
Pokitto 52:c04087025cab 1280 display.print(("\25type"));
Pokitto 52:c04087025cab 1281
Pokitto 52:c04087025cab 1282 display.cursorX = currentX-display.fontWidth*6-2;
Pokitto 52:c04087025cab 1283 display.cursorY = currentY+2*(display.fontHeight+1);
Pokitto 52:c04087025cab 1284 display.print(("\26back"));
Pokitto 52:c04087025cab 1285
Pokitto 52:c04087025cab 1286 display.cursorX = currentX-display.fontWidth*6-2;
Pokitto 52:c04087025cab 1287 display.cursorY = currentY+3*(display.fontHeight+1);
Pokitto 52:c04087025cab 1288 display.print(("\27save"));
Pokitto 52:c04087025cab 1289
Pokitto 52:c04087025cab 1290 //erase some pixels around the selected character
Pokitto 52:c04087025cab 1291 display.setColor(WHITE);
Pokitto 52:c04087025cab 1292 display.drawFastHLine(currentX + activeX * (display.fontWidth+1) - 1, currentY + activeY * (display.fontHeight+1) - 2, 7);
Pokitto 52:c04087025cab 1293 //draw the selection rectangle
Pokitto 52:c04087025cab 1294 display.setColor(BLACK);
Pokitto 52:c04087025cab 1295 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 1296 //draw keyboard outline
Pokitto 52:c04087025cab 1297 //display.drawRoundRect(currentX - 6, currentY - 6, KEYBOARD_W * (display.fontWidth+1) + 12, KEYBOARD_H * (display.fontHeight+1) + 12, 8, BLACK);
Pokitto 52:c04087025cab 1298 //text field
Pokitto 52:c04087025cab 1299 display.drawFastHLine(0, LCDHEIGHT-display.fontHeight-2, LCDWIDTH);
Pokitto 52:c04087025cab 1300 display.setColor(WHITE);
Pokitto 52:c04087025cab 1301 display.fillRect(0, LCDHEIGHT-display.fontHeight-1, LCDWIDTH, display.fontHeight+1);
Pokitto 52:c04087025cab 1302 //typed text
Pokitto 52:c04087025cab 1303 display.cursorX = 0;
Pokitto 52:c04087025cab 1304 display.cursorY = LCDHEIGHT-display.fontHeight;
Pokitto 52:c04087025cab 1305 display.setColor(BLACK);
Pokitto 52:c04087025cab 1306 display.print(text);
Pokitto 52:c04087025cab 1307 //blinking cursor
Pokitto 52:c04087025cab 1308 if (((frameCount % 8) < 4) && (activeChar < (length-1)))
Pokitto 52:c04087025cab 1309 display.drawChar(display.fontWidth * activeChar, LCDHEIGHT-display.fontHeight, '_',1);
Pokitto 52:c04087025cab 1310 }
Pokitto 52:c04087025cab 1311 }
Pokitto 52:c04087025cab 1312 #endif
Pokitto 52:c04087025cab 1313 }
Pokitto 52:c04087025cab 1314
Pokitto 52:c04087025cab 1315 void Core::popup(const char* text, uint8_t duration){
Pokitto 52:c04087025cab 1316 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1317 popupText = text;
Pokitto 52:c04087025cab 1318 popupTimeLeft = duration+12;
Pokitto 52:c04087025cab 1319 #endif
Pokitto 52:c04087025cab 1320 }
Pokitto 52:c04087025cab 1321
Pokitto 52:c04087025cab 1322 void Core::updatePopup(){
Pokitto 52:c04087025cab 1323 #if (ENABLE_GUI > 0)
Pokitto 52:c04087025cab 1324 if (popupTimeLeft){
Pokitto 52:c04087025cab 1325 uint8_t yOffset = 0;
Pokitto 52:c04087025cab 1326 if(popupTimeLeft<12){
Pokitto 52:c04087025cab 1327 yOffset = 12-popupTimeLeft;
Pokitto 52:c04087025cab 1328 }
Pokitto 52:c04087025cab 1329 display.fontSize = 1;
Pokitto 52:c04087025cab 1330 display.setColor(WHITE);
Pokitto 52:c04087025cab 1331 display.fillRoundRect(0,LCDHEIGHT-display.fontHeight+yOffset-3,84,display.fontHeight+3,3);
Pokitto 52:c04087025cab 1332 display.setColor(BLACK);
Pokitto 52:c04087025cab 1333 display.drawRoundRect(0,LCDHEIGHT-display.fontHeight+yOffset-3,84,display.fontHeight+3,3);
Pokitto 52:c04087025cab 1334 display.cursorX = 4;
Pokitto 52:c04087025cab 1335 display.cursorY = LCDHEIGHT-display.fontHeight+yOffset-1;
Pokitto 52:c04087025cab 1336 display.print(popupText);
Pokitto 52:c04087025cab 1337 popupTimeLeft--;
Pokitto 52:c04087025cab 1338 }
Pokitto 52:c04087025cab 1339 #endif
Pokitto 52:c04087025cab 1340 }
Pokitto 52:c04087025cab 1341
Pokitto 52:c04087025cab 1342 void Core::setFrameRate(uint8_t fps) {
Pokitto 52:c04087025cab 1343 timePerFrame = 1000 / fps;
Pokitto 52:c04087025cab 1344 sound.prescaler = fps / 20;
Pokitto 52:c04087025cab 1345 sound.prescaler = __avrmax(1, sound.prescaler);
Pokitto 52:c04087025cab 1346 }
Pokitto 52:c04087025cab 1347
Pokitto 52:c04087025cab 1348 void Core::pickRandomSeed(){
Pokitto 52:c04087025cab 1349 initRandom();
Pokitto 52:c04087025cab 1350 }
Pokitto 52:c04087025cab 1351
Pokitto 52:c04087025cab 1352 bool Core::collidePointRect(int16_t x1, int16_t y1 ,int16_t x2 ,int16_t y2, int16_t w, int16_t h){
Pokitto 52:c04087025cab 1353 if((x1>=x2)&&(x1<x2+w))
Pokitto 52:c04087025cab 1354 if((y1>=y2)&&(y1<y2+h))
Pokitto 52:c04087025cab 1355 return true;
Pokitto 52:c04087025cab 1356 return false;
Pokitto 52:c04087025cab 1357 }
Pokitto 52:c04087025cab 1358
Pokitto 52:c04087025cab 1359 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 1360 return !( x2 >= x1+w1 ||
Pokitto 52:c04087025cab 1361 x2+w2 <= x1 ||
Pokitto 52:c04087025cab 1362 y2 >= y1+h1 ||
Pokitto 52:c04087025cab 1363 y2+h2 <= y1 );
Pokitto 52:c04087025cab 1364 }
Pokitto 52:c04087025cab 1365
Pokitto 52:c04087025cab 1366 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 1367 int16_t w1 = pgm_read_byte(b1);
Pokitto 52:c04087025cab 1368 int16_t h1 = pgm_read_byte(b1 + 1);
Pokitto 52:c04087025cab 1369 int16_t w2 = pgm_read_byte(b2);
Pokitto 52:c04087025cab 1370 int16_t h2 = pgm_read_byte(b2 + 1);
Pokitto 52:c04087025cab 1371
Pokitto 52:c04087025cab 1372 if(collideRectRect(x1, y1, w1, h1, x2, y2, w2, h2) == false){
Pokitto 52:c04087025cab 1373 return false;
Pokitto 52:c04087025cab 1374 }
Pokitto 52:c04087025cab 1375
Pokitto 52:c04087025cab 1376 int16_t xmin = (x1>=x2)? 0 : x2-x1;
Pokitto 52:c04087025cab 1377 int16_t ymin = (y1>=y2)? 0 : y2-y1;
Pokitto 52:c04087025cab 1378 int16_t xmax = (x1+w1>=x2+w2)? x2+w2-x1 : w1;
Pokitto 52:c04087025cab 1379 int16_t ymax = (y1+h1>=y2+h2)? y2+h2-y1 : h1;
Pokitto 52:c04087025cab 1380 for(uint8_t y = ymin; y < ymax; y++){
Pokitto 52:c04087025cab 1381 for(uint8_t x = xmin; x < xmax; x++){
Pokitto 52:c04087025cab 1382 if(display.getBitmapPixel(b1, x, y) && display.getBitmapPixel(b2, x1+x-x2, y1+y-y2)){
Pokitto 52:c04087025cab 1383 return true;
Pokitto 52:c04087025cab 1384 }
Pokitto 52:c04087025cab 1385 }
Pokitto 52:c04087025cab 1386 }
Pokitto 52:c04087025cab 1387 return false;
Pokitto 52:c04087025cab 1388 }
Pokitto 52:c04087025cab 1389
Pokitto 52:c04087025cab 1390
Pokitto 52:c04087025cab 1391 //** EOF **//
Pokitto 52:c04087025cab 1392
Pokitto 52:c04087025cab 1393
Pokitto 52:c04087025cab 1394
Pokitto 52:c04087025cab 1395
Pokitto 52:c04087025cab 1396
Pokitto 52:c04087025cab 1397
Pokitto 52:c04087025cab 1398
Pokitto 52:c04087025cab 1399
Pokitto 52:c04087025cab 1400