#define CLK 13
#define DIN 11
#define CS 10
#define X_SEGMENTS 4
#define Y_SEGMENTS 2
#define NUM_SEGMENTS (X_SEGMENTS * Y_SEGMENTS)
#define rb 5
#define gb 4
#define yb 6
#define bb 7
byte fb[8 * NUM_SEGMENTS];
//coord matrix for lit up leds
int matrix[16][32];
// array of snake coordinates (fir line for x, second line for y)
int snake[2][16*32];
int len = 1;
bool gameOver = false;
int dir = 0; // 0 - left, 1 - up, 2 - right, 3 - down
unsigned long start = millis();
void shiftAll(byte send_to_address, byte send_this_data){
digitalWrite(CS, LOW);
for (int i = 0; i < NUM_SEGMENTS; i++) {
shiftOut(DIN, CLK, MSBFIRST, send_to_address);
shiftOut(DIN, CLK, MSBFIRST, send_this_data);
}
digitalWrite(CS, HIGH);
}
void matrixToDisplay(){
int v;
int fb_ind = 63;
for(int line_ind = 0; line_ind < 16; ++line_ind){
for(int col_ind = 0; col_ind < 32; col_ind += 8){
v = 0;
for(int i=0; i<8; ++i){
v |= ((matrix[line_ind][col_ind + 7 - i] > 0) << i); //sau la shiftare 7-i
fb[fb_ind] = v;
}
--fb_ind;
}
}
}
void show() {
matrixToDisplay();
for (byte row = 0; row < 8; row++) {
digitalWrite(CS, LOW);
byte segment = NUM_SEGMENTS;
while (segment--) {
//compute row and column
byte x = segment % X_SEGMENTS;
byte y = segment / X_SEGMENTS * 8;
//compute address given the above
byte addr = (row + y) * X_SEGMENTS;
// odd rows of segments
if (segment & X_SEGMENTS) {
shiftOut(DIN, CLK, MSBFIRST, 8 - row); //?????
shiftOut(DIN, CLK, LSBFIRST, fb[addr + x]);
} else {
shiftOut(DIN, CLK, MSBFIRST, 1 + row);
shiftOut(DIN, CLK, MSBFIRST, fb[addr - x + X_SEGMENTS - 1]);
}
}
digitalWrite(CS, HIGH);
}
}
int start_line = 8;
int start_col = 0;
int top = 0;
int bottom = 0;
void generateCaveColumn(int column){
srand(analogRead(0));
int prob = 50; //50% by default
for(int line = 0; line < 8; ++line){
if(line > 0 && column > 0){ //inner walls
//the previous wall level exists
//and there are still 2 spaces left between the walls
//and the previous wall doesnt connect with the current wall
if(matrix[line-1][column] == 1 && matrix[line+2][column] != 1
&& matrix[bottom][column-1] != 1){
bool generated = rand()%100 > prob;
if(generated){
matrix[line][column] = 1;
top = line;
}
}else if(matrix[15-line+1][column] == 1 && matrix[15-line-1][column] != 1
&& matrix[top][column-1] != 1){
bool generated = rand()%100 > prob;
if(generated){
matrix[15 - line][column] = (rand()%100 > prob);
bottom = 15 - line;
}
}
}else{ //outer walls
matrix[line][column] = (rand()%100 > prob);
}
}
}
void shifCave(){
for(int line = 0; line < 16; ++line){
for(int col = 0; col < 31 ; --col){ //moving all walls tow the left by 1 unit
matrix[line][col] = matrix[line][col+1];
}
matrix[line][31] = 0; //clearing the last line
}
}
void setup() {
Serial.begin(115200);
pinMode(CLK, OUTPUT);
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
// Setup each MAX7219
shiftAll(0x0f, 0x00); //display test register - test mode off
shiftAll(0x0b, 0x07); //scan limit register - display digits 0 thru 7
shiftAll(0x0c, 0x01); //shutdown register - normal operation
shiftAll(0x0a, 0x0f); //intensity register - max brightness
shiftAll(0x09, 0x00); //decode mode register - No decode
for(int col = 0; col < 32; ++col){
generateCaveColumn(col);
show();
}
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
}
void loop() {
}