aboutsummaryrefslogtreecommitdiff
path: root/Jetris.ino
blob: 5b33508ac377e7869285dca8976bdfa5f32c85be (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
// THings that miss
//	Functions that return something

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

#define clk		 5
#define cs       6
#define dataIn 	 3
#define setDisplay(d) (curDisplay = d)

//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");

	//Init pins
	pinMode(cs,  OUTPUT);
	pinMode(clk, OUTPUT);
	pinMode(dataIn, OUTPUT);

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

	setDisplay(1);
	initDisplay();
	
	

}

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



void loop() {
	delay(100);
	
	//Clear screen
	drawRegion(topLayer, 0xFF, 0xFFFF, false);


	//Draw ball
	drawSprite(topLayer, 0, 5, &checker);
	
	renderAll();
	//renderToSerial();


}

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

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();
	}
}

/////////////////////////////
// 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, 0x01);
	
	//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;
	
	//Clear out drawing area
	uint8_t xMask = (uint8_t)(~( (1 << ( 8 - sprite->width ) ) -1 )) >> x;
	uint16_t yMask =  ( (1 <<     sprite->height    ) -1 ) << y;

	drawRegion(layer, xMask, yMask, false);


	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;
	}
}

void render(int where) {

	for(int i = where; i < (where + 8); i++ ) {
		writeCommand(maxDIGIT_0 + i - where, buttonLayer[i]);
	}
	
	//Shift everything throught
	for( int i = 0; i < curDisplay; i++) {
		writeCommand(0, 0);
	}

}

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);
	}
}