-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathKeebcard.ino
More file actions
191 lines (161 loc) · 4.49 KB
/
Keebcard.ino
File metadata and controls
191 lines (161 loc) · 4.49 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <avr/wdt.h>
#include <EEPROM.h>
// #include <Entropy.h>
#include "settings.h"
// PROGRAM DEFINES =============================================================
// EEPROM address for storing random seed
#define RANDOM_SEED_ADDR 0
// uncomment whichever one you want
// #define BUSINESS_CARD
#define TETRIS
// #define CONWAY
// #define PONG
// #define SNAKE
// cut down on features since digispark has a big bootloader
// #define DIGISPARK
#ifdef TETRIS
#include "Tetris.h"
#elif defined(CONWAY)
#include "Conway.h"
#elif defined(PONG)
#include "Pong.h"
#elif defined(BUSINESS_CARD)
#include "BusinessCard.h"
#elif defined(SNAKE)
#include "Snake.h"
#endif
void universal_setup() {
// flip screen horizontally
// oled.setSegmentRemap(0xA0);
// flip screen vertically
// oled.setComOutputDirection(0xC0);
oled.begin();
oled.setFont(FONT8X16);
// Switch the half of RAM that we are writing to, to be the half that is non currently displayed
oled.switchRenderFrame();
// clear both buffers of any dead squirrels
oled.clear();
oled.switchFrame();
oled.clear();
// Turn on the display
oled.on();
// seed random number with value from EEPROM
uint32_t seed = 0;
EEPROM.get(RANDOM_SEED_ADDR, seed);
srand(seed);
// srand(analogRead(MIDDLE_BUTTON) + analogRead(2) + analogRead(3));
// here's the good stuff, Entropy based off clock jitter
// Entropy.initialize();
pinMode(LEFT_BUTTON, INPUT);
pinMode(RIGHT_BUTTON, INPUT);
pinMode(MIDDLE_BUTTON, INPUT);
#ifdef SNAKE
oled.setMemoryAddressingMode(0); // TODO prolly don't need this
// no double buffering for now. very little to update so it runs fast anyways
oled.switchRenderFrame();
#endif
}
void wdt_check_reset() {
MCUSR &= ~(1 << WDRF); // Clear watchdog reset flag
wdt_disable(); // Disable watchdog on startup
}
void setup() {
wdt_check_reset(); // Disable watchdog timer on startup
universal_setup();
setup_game();
}
void setup_game() {
#ifdef TETRIS
GIMSK = 0b00100000; // turns on pin change interrupts
PCMSK = 0b00011010; // turn on interrupts on pins PB0, PB1, &amp; PB4
sei(); // enables interrupts
Tetris tetris(&oled);
// allow for random start piece by continuously polling for random numbers if any button is pressed
while (digitalRead(LEFT_BUTTON) == LOW || digitalRead(RIGHT_BUTTON) == LOW || digitalRead(MIDDLE_BUTTON) == LOW) {
rand();
}
oled.setCursor(8, 1);
oled.print(F("This end up ->"));
oled.switchFrame();
delay(800);
oled.switchFrame();
oled.clear();
gameOver(tetris.run());
oled.switchFrame();
#elif defined(CONWAY)
oled.setMemoryAddressingMode(0); // TODO prolly don't need this
Conway conway(&oled);
conway.run();
#elif defined(BUSINESS_CARD)
businessCard(&oled);
#elif defined(PONG)
oled.setCursor(8, 1);
oled.print(F("PLAYER 1 START"));
oled.switchFrame();
delay(800);
oled.switchFrame();
oled.clear();
oled.setMemoryAddressingMode(1);
Pong pong(&oled);
pong.run();
#elif defined(SNAKE)
oled.setCursor(8, 1);
oled.print(F("NOKIA OS"));
// oled.switchFrame();
delay(800);
// oled.switchFrame();
oled.clear();
Snake snake(&oled);
gameOver(snake.run());
#endif
}
void loop() {
if (digitalRead(MIDDLE_BUTTON) == LOW) {
// oled.clear();
// oled.switchFrame();
// oled.clear();
// setup_game();
}
}
void gameOver(uint32_t score) {
// Update EEPROM with a new random seed for next boot
uint32_t newSeed = rand();
EEPROM.put(RANDOM_SEED_ADDR, newSeed);
oled.clear();
oled.setCursor(0, 0);
oled.print(F("Game Over!"));
oled.setCursor(0, 2);
oled.print(F("Score: "));
oled.print(score);
oled.switchFrame();
oled.clear();
oled.setCursor(0, 0);
oled.print(F("Left to restart"));
oled.setCursor(0, 2);
oled.print(F("Score: "));
oled.print(score);
delay(100);
// Wait for middle button press to restart
while (true) {
for (uint8_t i = 0; i < 100; i++) {
if (digitalRead(LEFT_BUTTON) == LOW) {
// Wait for button release to avoid immediate re-trigger
while (digitalRead(LEFT_BUTTON) == LOW) {
delay(10);
}
oled.clear();
oled.setCursor(0,0);
oled.print(F("Restarting..."));
oled.switchFrame();
delay(100);
// Enable watchdog timer and reset system
wdt_enable(WDTO_250MS);
while(1); // Wait for watchdog reset
}
delay(10); // Small delay to prevent excessive CPU usage
}
oled.switchFrame();
}
}