aboutsummaryrefslogtreecommitdiff
path: root/Jetris.ino
blob: 6c9990bad02609098c9c9ee74251ebba70d7f9c4 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// THings that miss
//	Functions that return something
//  Make more safety

#include "sprites.h"
#include "MaxCommands.h"

#define clk		 5
#define cs       6
#define dataIn 	 3
#define setDisplay(d) (curDisplay = d)
#define leftPin     2
#define rightPin    4
#define dropPin  11
#define rotatePin 10
#define clickTime 200

//Use the drawDot function to draw
uint8_t buttonLayer[16];
uint8_t topLayer[16];

int curDisplay = 0;

void setup() {
	//Init Serial
	Serial.begin(9600);

	Serial.println("Starting up \n\n\n");

	randomSeed(analogRead(0));

	//Init pins
	pinMode(cs,  OUTPUT);
	pinMode(clk, OUTPUT);
	pinMode(dataIn, OUTPUT);
	pinMode(leftPin, INPUT_PULLUP);
	pinMode(rightPin, INPUT_PULLUP);
	pinMode(dropPin, INPUT_PULLUP);
	pinMode(rotatePin, INPUT_PULLUP);

	//ButtonInterrupt
	//attachInterrupt(digitalPinToInterrupt(left), buttonHandle, FALLING);

	//Setup displays
	setDisplay(0);
	initDisplay();

	setDisplay(1);
	initDisplay();

	initSprites();
	
	initGame();	

}

/////////////////////////////
//       GAME LOGIC        //
/////////////////////////////

Sprite *block;

int xPos;
int yPos;


long loopTime;

void initGame() {
	initBlock();
}

void loop() {
	
	handleButtons();

	if(millis() - loopTime < 500) 
		return;
	
	//Move
	if(checkCollision(0, 1)) {
		drawSprite(buttonLayer, xPos, yPos, block);	
		handleFullRows(0);
		renderAll();
		initBlock();
	}else{
		moveBlock(0, 1);
	}

	loopTime = millis();

}

void initBlock(){
	xPos = 0;
	yPos = 0;

	block = blocks[random(7)];
}

void moveBlock(int xMove, int yMove) {
	xPos += xMove;
	yPos += yMove;

	//Clear screen
	drawRegion(topLayer, 0xFF, 0xFFFF, false);

	//Draw ball
	drawSprite(topLayer, xPos, yPos, block);

	//Render
	renderAll();

}

//Checks if input and button layer have bits in common, or if a block hits button. Returns true if collision
int checkCollision(int xMove, int yMove) {
	//Newolute position on map
	int yNew = yPos + yMove;
	int xNew = xPos + xMove;

	for(int i = 0; i < block->height; i++) {
		if(( block->buff[i] >> xNew) & buttonLayer[i + yNew])
			return 1;
	}

	//Check if out of bounds
	if(xNew + block->width > 8 || xNew < 0 || yNew + block->height > 16) 
		return 1;

	return 0;
}

void rotateBlock() {
	Serial.println("Rotate");

	//If next block exist, switch to that
	if( block->rotateNext ) {
		Serial.println((uint16_t)block);
		Serial.println((uint16_t)block->rotateNext);

		block = block->rotateNext;
		Serial.println((uint16_t)block);
	}

	//And render
	moveBlock(0, 0);
}

void dropBlock() {
	//Move down until it hits something
	int count = 0;
	while( !checkCollision(0, 1) ) {
		moveBlock(0, 1);
		delay(10);
	}

	drawSprite(buttonLayer, xPos, yPos, block);
}

void renderAll(){
	setDisplay(0);
	render(0);
	setDisplay(1);
	render(8);
}

void handleFullRows(int start) {
	for(int i = start; i < 16; i++ ) {
		if(buttonLayer[15 - i] == 0xFF) {
			for(int j = i; j < 16; j++) {
				buttonLayer[15 - j] = buttonLayer[14 - j];
			}
			handleFullRows(i);
			return;
		}
	}
}

void renderToSerial() {
	Serial.write(0x33); Serial.println("[2J");
	for(int i = 0; i < 16; i++) {
		for(int b = 0; b < 8; b++) {
			Serial.print( (  buttonLayer[i] & ( 1 << 7 - b ) ) > 0 );
		}
		Serial.println();
	}
}

/////////////////////////////
//   Control Handling      //
/////////////////////////////

unsigned long leftLast, rightLast, dropLast, rotateLast;

void handleButtons() {
	//Read from switch
	int leftState = !digitalRead(leftPin);
	int rightState = !digitalRead(rightPin);
	int dropState = !digitalRead(dropPin);
	int rotateState = !digitalRead(rotatePin);
	
	//Check if last click was over 100 ms ago
	if(millis() - leftLast > clickTime && leftState) {
		leftLast = millis();

		//Move 
		if( !checkCollision(1, 0) ) {
			moveBlock(1, 0);
		}
	}

	if(millis() - rightLast > clickTime && rightState) {
		rightLast = millis();
		//Move 
		if( !checkCollision(-1, 0) ) {
			moveBlock(-1, 0);
		}
	}

	if(millis() - dropLast > clickTime && dropState) {
		dropLast = millis();

		dropBlock();
	}

	if(millis() - rotateLast > clickTime+100 && rotateState) {
		rotateLast = millis();

		rotateBlock();
	}
}

/////////////////////////////
// Screendrawing routines  //
/////////////////////////////

void initDisplay() {

	//clear all registers
	for ( int i = 0; i < 0x0F; i++) {
		writeCommand(i, 0);
	}
	
	//Turn it on
	writeCommand(maxSHUTDOWN_INV, 1);

	//Darker please
	writeCommand(maxINTENSITY, 0x00);
	
	//Activate all lines
	writeCommand(maxSCAN_LIMIT, 0x07);

}

//Draw a region, where mask specifies where to draw.
//	xMask = 0b00111100, yMask = 0b01111110 will draw a small 4x6 box
void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, Sprite* sprite) {
	//Check if in range
	if (x + sprite->width - 1 > 7 || y + sprite->height - 1> 15 )
		return;
	
	for ( int i = y; i < sprite->height + y; i++) {
		//Calculate bits
		uint8_t row = layer[i];
		row |= sprite->buff[i - y] >> x;
		
		layer[i] = row;

	}
	
}

void drawRegion(uint8_t layer[], uint8_t xMask, uint16_t yMask, bool state) {
	for (int i = 0; i < 16; i++ ) {
		//If y index not in mask, go to next
		if (! ( yMask & 1 << i ) )
			continue;

		//Flip the bits
		uint8_t row = layer[i];
		if ( state ) {
			//Write 1 where on the 1's places in xMask
			row |= xMask;
		} else {
			//Write 0. ~ means bitwise NOT
			row &= ~( xMask );
		}
		layer[i] = row;
	}
}

unsigned long reRenderLast;

void render(int where) {

	static uint8_t onScreen[16];

	bool reRender = (millis() - reRenderLast ) > 1000;

	for(int i = where; i < (where + 8); i++ ) {
		uint8_t toWrite = buttonLayer[i] | topLayer[i];
		if(onScreen[i] == toWrite && !reRender) 
			continue;

		writeCommand(maxDIGIT_0 + i - where, buttonLayer[i] | topLayer[i]);

		onScreen[i] = toWrite;
	}

	if(reRender) {
		reRenderLast = millis();
	}

}

void writeCommand(uint8_t addr, uint8_t data) {
	uint16_t uint8_tToWrite = addr << 8 | data;

	//Set Chip select low
	digitalWrite(cs, LOW);

	//MSBFIRST is a arduino standard, which says that data goes from left to right

	shiftOut(dataIn, clk, MSBFIRST, addr);
	shiftOut(dataIn, clk, MSBFIRST, data);

	//To get to other displays padding is added to step through them
	serialPad(curDisplay * 2);

	digitalWrite(cs, HIGH);
	
	//To Clear out other displays more padding is added
	serialPad(2 * 2);
}

void serialPad(int count) {

	for(int i = 0; i < count; i++) {
		//Pad with no-ops
		shiftOut(dataIn, clk, MSBFIRST, 0);
	}
}