#define CLK 13
#define DIN 11
#define CS 10
#define X_SEGMENTS 4
#define Y_SEGMENTS 2
#define NUM_SEGMENTS (X_SEGMENTS * Y_SEGMENTS)
// a framebuffer to hold the state of the entire matrix of LEDs
// laid out in raster order, with (0, 0) at the top-left
byte fb[8 * NUM_SEGMENTS];
int headx=0;
int heady=0;
int length=1;
bool up=false;
bool down=false;
bool right=false;
bool left=false;
int movesfromswitch=0;
int score=0;
int lastmove=-1;
int foodx=random(32);
int foody=random(16);
void shiftAll2(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 shiftAll(byte address,byte data)
{
digitalWrite(CS, LOW);
for (int i=7; i>=0; i--)
{
digitalWrite(CLK, LOW); // incepe o scriere sincronizata
digitalWrite(DIN, address & (0x01 << i)); // cand i=7 scriu cel mai din stg bit din data...
digitalWrite(CLK, HIGH); // a terminat sincronismul
}
for (int i=7; i>=0; i--)
{
digitalWrite(CLK, LOW); // incepe o scriere sincronizata
digitalWrite(DIN, data & (0x01 << i)); // cand i=7 scriu cel mai din stg bit din data...
digitalWrite(CLK, HIGH); // a terminat sincronismul
}
digitalWrite(CS, HIGH);
}
void setup() {
Serial.begin(115200);
pinMode(CLK, OUTPUT);
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
// 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
}
void show() {
for (byte row = 0; row < 8; row++) {
digitalWrite(CS, LOW);
byte segment = NUM_SEGMENTS;
while (segment--) {
byte x = segment % X_SEGMENTS;
byte y = (segment / X_SEGMENTS) * 8;
byte addr = (row + y) * X_SEGMENTS;
if (segment >= X_SEGMENTS) { // odd rows of segments
shiftOut(DIN, CLK, MSBFIRST, 8 - row);
shiftOut(DIN, CLK, LSBFIRST, fb[addr + x]);
}else { // even rows of segments
shiftOut(DIN, CLK, MSBFIRST, 1 + row);
shiftOut(DIN, CLK, MSBFIRST, fb[addr - x + X_SEGMENTS - 1]);
}
}
digitalWrite(CS, HIGH);
}
}
int moduloval(int headx){
if(headx<0){
headx+=32;
}
if(headx>31){
headx=0;
}
return headx;
}
// functie care aprinde pixelul (x,y) unde (0,0) este in partea dreapta-jos a matricei de leduri
void setpixel(int x,int y){
x=moduloval(x);
y=moduloval(y);
int pos=y*32+x;
int pos2=pos%8;
fb[pos/8]=(1<<(pos2));
}
void loop(){
byte rightbtn = digitalRead(4);
byte downbtn = digitalRead(5);
byte leftbtn = digitalRead(6);
byte upbtn = digitalRead(7);
for(int i=0; i<64; i++) fb[i]=0;
setpixel(foodx,foody);
if((rightbtn==1 || leftbtn==1) && (!right && !left)){
if(up==true){
lastmove=7;
}
else if(down==true){
lastmove=5;
}
if(rightbtn==1){
right = true;
}
if(leftbtn==1)
{
left=true;
}
up=false;
down=false;
movesfromswitch=0;
}
if((upbtn==1 || downbtn==1) && (!up && !down)){
if(right==true){
lastmove=4;
}
else if(left==true){
lastmove=6;
}
if(upbtn==1){
up=true;
}
if(downbtn==1)
{
down=true;
}
right=false;
left=false;
movesfromswitch=0;
}
if(up){
heady+=1;
}
if(down)
{
heady-=1;
}
if(right){
headx-=1;
}
if(left)
{
headx+=1;
}
// modulo dimensiunea matricei de leduri
if(headx<0){
headx+=32;
}
if(heady<0){
heady+=16;
}
if(headx>31){
headx=0;
}
if(heady>15){
heady=0;
}
/* incercare esuata de a desena coada
for(int i=0;i<length;i++){
if(i==0){
setpixel(headx,heady);
}
if(i<movesfromswitch){
if(right){
setpixel(headx+i,heady);
}
else if(left){
setpixel(headx-i,heady);
}
else if(up){
setpixel(headx,heady-i);
}
else if(down){
setpixel(headx,heady+i);
}
}
else {
if(lastmove==4){
setpixel(headx+i,heady);
}
else if(lastmove==6){
setpixel(headx-i,heady);
}
else if(lastmove==7){
setpixel(headx,heady-i);
}
else if(lastmove==5){
setpixel(headx,heady+i);
}
}
}*/
// daca capul sarpelui este pe aceeasi pozitie ca mancarea, creste scor-ul si lungimea, si se generaza un nou food
if(headx==foodx && heady==foody){
score+=1;
length+=1;
foodx=random(32);
foody=random(16);
}
setpixel(headx,heady);
show();
}