int SRCLK = 13;
int RCLK = 12;
int moduleSerIn[8] = {11,10,9,8,7,6,5,4};
//pin port
//0 PORTD0
//1 PORTD1
//2 PORTD2
//3 PORTD3
//4 PORTD4
//5 PORTD5
//6 PORTD6
//7 PORTD7
//8 PORTB0
//9 PORTB1
//10 PORTB2
//11 PORTB3
//12 PORTB4
//13 PORTB5
static inline void SRCLKPinH() {bitSet(PORTB,5);}
static inline void SRCLKPinL() {bitClear(PORTB,5);}
static inline void RCLKPinH() {bitSet(PORTB,4);}
static inline void RCLKPinL() {bitClear(PORTB,4);}
static inline void ModSerInPinH0() {bitSet(PORTB,3);}
static inline void ModSerInPinL0() {bitClear(PORTB,3);}
static inline void ModSerInPinH1() {bitSet(PORTB,2);}
static inline void ModSerInPinL1() {bitClear(PORTB,2);}
static inline void ModSerInPinH2() {bitSet(PORTB,1);}
static inline void ModSerInPinL2() {bitClear(PORTB,1);}
static inline void ModSerInPinH3() {bitSet(PORTB,0);}
static inline void ModSerInPinL3() {bitClear(PORTB,0);}
static inline void ModSerInPinH4() {bitSet(PORTD,7);}
static inline void ModSerInPinL4() {bitClear(PORTD,7);}
static inline void ModSerInPinH5() {bitSet(PORTD,6);}
static inline void ModSerInPinL5() {bitClear(PORTD,6);}
static inline void ModSerInPinH6() {bitSet(PORTD,5);}
static inline void ModSerInPinL6() {bitClear(PORTD,5);}
static inline void ModSerInPinH7() {bitSet(PORTD,4);}
static inline void ModSerInPinL7() {bitClear(PORTD,4);}
/*
static void printall() { //debugger
//serial.print(bitRead(PORTB,5));
//serial.print(bitRead(PORTB,4));
//serial.print(bitRead(PORTB,3));
//serial.print(bitRead(PORTB,2));
//serial.print(bitRead(PORTB,1));
//serial.print(bitRead(PORTB,0));
//serial.print(bitRead(PORTB,7));
//serial.print(bitRead(PORTB,6));
//serial.print(bitRead(PORTB,5));
//serial.println(bitRead(PORTB,4));
}*/
static inline void ModSerInPinLall() {
bitClear(PORTB,3);
bitClear(PORTB,2);
bitClear(PORTB,1);
bitClear(PORTB,0);
bitClear(PORTD,7);
bitClear(PORTD,6);
bitClear(PORTD,5);
bitClear(PORTD,4);
}
static inline void ModSerInPinHall() {
bitSet(PORTB,3);
bitSet(PORTB,2);
bitSet(PORTB,1);
bitSet(PORTB,0);
bitSet(PORTD,7);
bitSet(PORTD,6);
bitSet(PORTD,5);
bitSet(PORTD,4);
}
static inline void ModSerInPinH(int i) {
if(i<4) {
bitSet(PORTB,3-i);
} else {
bitSet(PORTD,11-i);
}
}
static inline void ModSerInPinL(int i) {
if(i<4) {
bitClear(PORTB,3-i);
} else {
bitClear(PORTD,11-i);
}
}
//idk how to format storage
byte matrix[8][8] = {
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
{0B11100000,
0B01100000,
0B00100000,
0B00010000,
0B00001000,
0B00000100,
0B00000010,
0B00000001},
};
static void printMatrix() {
//serial.println();
//serial.println();
for(int i=0;i<8;i++) {//for each row
for(int j=0;j<8;j++) {//for each matrix
for (int k=7;k>=0;k--) {
Serial.write(bitRead(matrix[j][i],k) ? '1' : '0');
}
//serial.print(" ");
}
//serial.println();
}
}
const int pixels[8][8];
void setup() {
// put your setup code here, to run once:
pinMode(SRCLK, OUTPUT);
pinMode(RCLK, OUTPUT);
for(int i=0;i<8;i++) {
pinMode(moduleSerIn[i], OUTPUT);
}
Serial.begin(9600);
}
//declare animation vars (put font here?)
int scans = 0;//actual matrix scans
int frame = 0;//frames at decreased frame rate
//animation methods
void shiftLeft(int start, int endd, int* right) {//shifts everything left in the range and in from the right col
int jump[8] = {0,0,0,0,0,0,0,0};
for(int i=0;i<8;i++) {
jump[i] = right[i];
}
int tmp = 0;
for(int i=endd;i>=start;i--) { //for each matrix
for(int j=0;j<8;j++) {//for each row
tmp = ((0B10000000 & matrix[i][j])>>7);//save far left bit(temp)
matrix[i][j] = (matrix[i][j]<<1) | (0B00000001>>(-(jump[j]-1)));//shift and replace right with jump storage
jump[j] = tmp;//replage jump storage with temp bit
}
}
}
void shiftLeftLoop(int start, int endd) {//shifts everything left in the range and loops
int jump[8] = {0,0,0,0,0,0,0,0};
for(int i=0;i<8;i++) {
jump[i] = ((0B10000000 & matrix[start][i])>>7);
}
int tmp = 0;
for(int i=endd;i>=start;i--) { //for each matrix
for(int j=0;j<8;j++) {//for each row
tmp = ((0B10000000 & matrix[i][j])>>7);//save far left bit(temp)
matrix[i][j] = (matrix[i][j]<<1) | (0B00000001>>(-(jump[j]-1)));//shift and replace right with jump storage
jump[j] = tmp;//replage jump storage with temp bit
}
}
}
void loop() {
// put your main code here, to run repeatedly:
//do whatever LED matrix changing stuff you want here
scans++;
if (scans==200) {
scans = 0;
frame++;
if (frame==16) {
frame = 0;
}
//just shift everything left
int ones[8] = {1,1,1,1,1,1,1,1};
shiftLeft(5,7,ones);
shiftLeftLoop(2,4);
//little animation on matrix 1
for(int i=0;i<8;i++){
matrix[1][i] = (0B11111111<<8-frame+i);
}
}
//
//displays a full frame
SRCLKPinL();
RCLKPinL();
ModSerInPinLall();
for(int j=0;j<8;j++){//for each row (byte)
//first shift in which row is in use
for (int i=0; i<8; i++){
if (i==(7-j)) {
ModSerInPinHall();
} else {
ModSerInPinLall();
}
SRCLKPinH(); //Shift in a bit
SRCLKPinL();
}
//then shift in the row
for (int i=0; i<8; i++){ //for each col (bit)
//do an if for each module
for(int k=0;k<8;k++) {
//matrix[matrix][byte/row] & ... (>> bit/col)
if (matrix[k][j] & (B00000001<<i)) {
ModSerInPinH(k); //ready a bit to shift in
} else {
ModSerInPinL(k);
}
}
SRCLKPinH(); //shift in a bit
SRCLKPinL();
}
//shift everything from shift to storage
RCLKPinH();
RCLKPinL();
}
}