forked from adafruit/Mini-LED-Gamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mini_LED_Gamer.ino
178 lines (161 loc) · 5.26 KB
/
Mini_LED_Gamer.ino
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
#include "HT16K33.h"
#include "Tetris.h"
#include "Snake.h"
#include "Paint.h"
// ADC clock configurations
#define ADPS_16 _BV(ADPS2)
#define ADPS_32 _BV(ADPS2)|_BV(ADPS0)
#define ADPS_128 _BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0)
// Buttons: bits 0-6 in the first byte of the key-scan matrix
#define LEFT 6
#define SELECT 1
#define DOWN 2
#define UP 5
#define RIGHT 0
#define BRIGHTNESS_UP 4
#define BRIGHTNESS_DOWN 3
// Objects
HT16K33 ht(0x70); // I2C address of the HT16K33 IC
Tetris tetris;
Snake snake;
Paint paint(3,3); // initilize cursor position to (3,3)
// Mode: 0-Undecided; 1-Tetris; 2-Snake; 3-Paint
uint8_t mode=0;
// Are we paused?
uint8_t paused=0;
// Interrupt Variables
uint8_t interruptCounter2Hz = 0;
ISR(TIMER1_OVF_vect){
// things that need to be done at 50Hz
ht.readButtons();
processButtons();
switch(mode) {
case 0:
ht.storeToBuffer(getMenu());
break;
case 1:
ht.storeToBuffer(tetris.getActiveBoard());
break;
case 2:
ht.storeToBuffer(snake.getActiveBoard());
break;
case 3:
ht.storeToBuffer(paint.getActiveCanvas());
}
ht.refreshDisplay();
// things that happen less frequently
interruptCounter2Hz++;
if (interruptCounter2Hz==25) {
interruptCounter2Hz=0;
paint.flashCursor();
}
}
void processButtons() {
// press and hold brightness down control and press brightness up control to toggle pause
if (ht.getButtonHoldTime(BRIGHTNESS_DOWN)>10 && ht.getButtonFirstPress(BRIGHTNESS_UP)) {
paused=paused^1;
}
// These two buttons are fixed
if (ht.allowToMove(BRIGHTNESS_DOWN,25,6) && ht.getButtonHoldTime(SELECT)==0) ht.decreaseBrightness();
if (ht.allowToMove(BRIGHTNESS_UP,25,6) && ht.getButtonHoldTime(SELECT)==0) ht.increaseBrightness();
// change to the previous/next program by pressing holding select and press the brightness control buttons
if (mode!=0 && ht.getButtonHoldTime(SELECT)>10) {
if (ht.getButtonFirstPress(BRIGHTNESS_UP)) {
changeOption(-1);
changeMode();
}
else if (ht.getButtonFirstPress(BRIGHTNESS_DOWN)){
changeOption(1);
changeMode();
}
}
// control for different programs
switch(mode) {
case 0:
if (ht.getButtonFirstPress(LEFT)) changeOption(-1);
if (ht.getButtonFirstPress(RIGHT)) changeOption(1);
if (ht.getButtonFirstPress(SELECT)) changeMode();
break;
case 1:
if (tetris.gameRunning) {
if (ht.allowToMove(UP,100,4)) tetris.rotatePiece();
if (ht.allowToMove(DOWN,15,2)) tetris.movePiece(0,1);
if (ht.allowToMove(LEFT,15,2)) tetris.movePiece(-1,0);
if (ht.allowToMove(RIGHT,15,2)) tetris.movePiece(1,0);
if (ht.getButtonFirstPress(SELECT)) tetris.dropPiece();
}
else {
// when the game is over, use right to switch games
if (ht.getButtonFirstPress(RIGHT)) {
changeOption(1);
resetMode();
}
if (ht.getButtonFirstPress(SELECT)) tetris.init();
}
break;
case 2:
if (snake.gameRunning) {
if (ht.getButtonFirstPress(UP)) snake.changeDirection(0,-1);
if (ht.getButtonFirstPress(DOWN)) snake.changeDirection(0,1);
if (ht.getButtonFirstPress(LEFT)) snake.changeDirection(-1,0);
if (ht.getButtonFirstPress(RIGHT)) snake.changeDirection(1,0);
}
else {
// when the game is over, use right/left to switch games
if (ht.getButtonFirstPress(RIGHT)) {
changeOption(1);
resetMode();
}
if (ht.getButtonFirstPress(LEFT)) {
changeOption(-1);
resetMode();
}
if (ht.getButtonFirstPress(SELECT)) snake.init();
}
break;
case 3:
if (ht.allowToMove(UP,25,2)) paint.moveCursor(0,-1);
if (ht.allowToMove(DOWN,25,2)) paint.moveCursor(0,1);
if (ht.allowToMove(LEFT,25,2)) paint.moveCursor(-1,0);
if (ht.allowToMove(RIGHT,25,2)) paint.moveCursor(1,0);
if (ht.getButtonFirstPress(SELECT)) paint.draw();
if (ht.getButtonHoldTime(SELECT)==150) paint.clearCanvas();
}
}
void setup(){
// Set randomSeed; the first few random() are not used because they never change
randomSeed(analogRead(A0));
for (uint8_t i=0;i<4;i++) random();
// Initiate objects
Serial.begin(115200);
ht.init();
tetris.init();
snake.init();
//ADC: 16MHz/16=1MHz
ADCSRA &= ~ADPS_128; // clean out ADPS
ADCSRA |= ADPS_32; // set Division Factor
// Timer1 overflow interrupt setup
TCNT1 = 0; // reset the counter
ICR1 = 19999; // top value of the counter (16*10^6Hz/2000000*20000us/8-1) -> 1/20000us = 50Hz
TCCR1A = 0; // clear TCCR1A (not used)
TCCR1B = _BV(WGM13) | _BV(CS11); // mode 8(PWM), set prescalar=8;
TIMSK1 = _BV(TOIE1); // Enable Overflow Interrupt
}
void loop(){
// For both the tetris and snake, they have a function called "run" as the main game loop.
// For the paint program, all the actions are handled in the timer interrupt, so there is nothing to do in the main loop
switch(mode) {
case 0:
break;
case 1:
if (paused == 0) {
tetris.run(); break;
}
case 2:
if (paused == 0) {
snake.run(); break;
}
case 3:
;
}
}