-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
128 lines (112 loc) · 4.56 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "core/common.h"
#include "core/display.h"
#include "core/battery.h"
#include "core/keyboard.h"
#include "core/LoRa/lora.h"
#include "screens/splashscreen.h"
#include "screens/menuscreen.h"
#include "screens/snakescreen.h"
#include "screens/gameboyscreen.h"
#include "screens/g2048screen.h"
#include "screens/tetrisscreen.h"
#include "screens/minescreen.h"
#include "screens/ticscreen.h"
#include "screens/pa2screen.h"
#include "screens/aboutscreen.h"
#include "screens/settingsscreen.h"
#define HIGHSCORESIZE (FLASH_PAGE_SIZE/4)
#define FLASH_TARGET_OFFSET (1536 * 1024)
Screen *screen;
Lora *lora;
uint32_t highscores[64];
bool shouldSwitchScreen;
uint8_t newScreenId, newOption;
void highScoreHandler(uint32_t highscore) {
highscores[0] = 64;highscores[1] = 128;
highscores[screen->screenId] = highscore;
uint32_t ints = save_and_disable_interrupts();
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, (uint8_t*)highscores, FLASH_PAGE_SIZE);
restore_interrupts (ints);
}
void readHighScoreData() {
const uint32_t* flash_target_contents = (const uint32_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
for (int i = 0; i < HIGHSCORESIZE; i++)
highscores[i] = flash_target_contents[i];
if(highscores[0] != 64 || highscores[1] != 128)
for (int i = 0; i < HIGHSCORESIZE; i++)
highscores[i] = 0;
}
void backHandler(int8_t menu, uint8_t option) {
newScreenId = menu;
newOption = option;
shouldSwitchScreen = true;
}
void checkScreenSwitch() {
if(!shouldSwitchScreen)
return;
if(screen->screenId == ScreenEnum::MENUSCREEN) {
delete screen;
if(newScreenId == ScreenEnum::SNAKESCREEN)
screen = new SnakeScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::GAMEBOYSCREEN)
screen = new GameBoyScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::G2048SCREEN)
screen = new G2048Screen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::TETRISSCREEN)
screen = new TetrisScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::MINESCREEN)
screen = new MineScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::TICSCREEN)
screen = new TicScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::PA2SCREEN)
screen = new PixelAdventureScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::SETTINGSSCREEN)
screen = new SettingsScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else if(newScreenId == ScreenEnum::ABOUTSCREEN)
screen = new AboutScreen(*backHandler, *highScoreHandler, highscores[newScreenId], newOption);
else
printf("[Main] Something failed badly\n");
} else if(screen->screenId == ScreenEnum::SPLASHSCREEN) {
delete screen;
screen = new MenuScreen(*backHandler, *highScoreHandler, newScreenId, newOption);
} else {
delete screen;
screen = new MenuScreen(*backHandler, *highScoreHandler, newScreenId-2, newOption);
}
shouldSwitchScreen = false;
}
int main(int argc, char *argv[]) {
sleep_ms(50);
vreg_set_voltage(VREG_VOLTAGE_1_20);
sleep_ms(1);
set_sys_clock_khz(266 * 1000, true);
sleep_ms(50);
stdio_init_all();
sleep_ms(1000);
printf("[Main] Starting\n");
srand((unsigned int)time(0));
readHighScoreData();
Display *display = new Display();
display->initDMAChannel();
display->initSequence();
display->clear(Color(255, 255, 255));
display->update();
Battery *battery = new Battery();
KeyBoard *keyboard = new KeyBoard();
screen = new SplashScreen(*backHandler, *highScoreHandler, 0, 1);
timetype lastUpdate = getTime();
bool close = false;
while (!close) {
uint16_t deltaTimeMS = getTimeDiffMS(lastUpdate);
lastUpdate = getTime();
screen->update(deltaTimeMS);
keyboard->checkKeyState(screen);
screen->draw(display);
battery->drawLevel(display);
// printf("[Main] FPS: %d\n", int(1000 / deltaTimeMS));
display->update();
checkScreenSwitch();
}
return EXIT_SUCCESS;
}