Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #define DEBUG
- /*
- * Pinout:
- * d2 - 4017 reset
- * d3 - 4017 clock
- *
- * d5 - shift clock
- * d6 - shift clear
- * d7 - shift ser in
- *
- * d9 - blank
- */
- /* In Physical Pin Order */
- const int DISPL_RST = D7;
- // Ground Pin
- const int a4017_CLK = D6;
- const int a4017_RST = D5;
- const int SHIFT_CLK = D4;
- const int SHIFT_CLR = D3;
- const int SHIFT_SER = D1;
- // Not Connected
- // Vcc
- /* ----- */
- const int updateWait = 4;
- // Buffers
- byte controlBytes[8];
- byte frameBuffer[8][3];
- byte loadBuffer[8][3];
- // Temp
- // byte yeet[8][3] = {
- // {0b00000100, 0b10000000, 0b01001000},
- // {0b00100100, 0b10010010, 0b01001001},
- // {0b00100100, 0b10010010, 0b01001001},
- // {0b00100100, 0b10010010, 0b01001001},
- // {0b00100100, 0b10010010, 0b01001001},
- // {0b00000100, 0b10010010, 0b01001000},
- // {0b00000000, 0b10010010, 0b01000000},
- // {0b00000000, 0b00010010, 0b00000000}};
- byte yeet[8][3] = {
- {0b00000100, 0b10000000, 0b01001000},
- {0b00000000, 0b00000010, 0b00000000},
- {0b00000000, 0b00000010, 0b00000000},
- {0b00000000, 0b00000010, 0b00000000},
- {0b00000000, 0b00000010, 0b00000000},
- {0b00000000, 0b00000010, 0b00000000},
- {0b00000000, 0b00000010, 0b00000000},
- {0b00000000, 0b00000010, 0b00000000}};
- // Messaging Bytes
- #define READY 0b01010010 // ASCII R
- #define STOP 0b01010011 // ASCII S
- #define WAIT 0b01010111 // ASCII W
- // Timekeeping
- unsigned long curTime = 0;
- unsigned long lastTimeGen = 0;
- unsigned long lastTimeUpdate = 0;
- void setup() {
- pinMode(SHIFT_SER, OUTPUT);
- pinMode(SHIFT_CLR, OUTPUT);
- pinMode(SHIFT_CLK, OUTPUT);
- pinMode(a4017_RST, OUTPUT);
- pinMode(a4017_CLK, OUTPUT);
- pinMode(DISPL_RST, OUTPUT);
- // Resets the 4017
- digitalWrite(a4017_RST, HIGH);
- delay(1);
- digitalWrite(a4017_RST, LOW);
- // Initiates serial communication
- Serial.begin(115200, SERIAL_8N1);
- // Set the shift register reset pin
- digitalWrite(SHIFT_CLR, HIGH);
- // wait a lil bit
- delay(100);
- clearBuffers();
- clearReg();
- flushSerial();
- updateFrame();
- }
- void flushSerial() {
- Serial.flush();
- while (Serial.available()) {
- Serial.read();
- }
- Serial.write(READY);
- }
- void clearReg() {
- // Sends a pulse to the clear pin on the shift registers
- digitalWrite(SHIFT_CLR, LOW);
- delayMicroseconds(1);
- digitalWrite(SHIFT_CLR, HIGH);
- delayMicroseconds(1);
- }
- void clearBuffers() {
- // Clears the loadBuffer and frameBuffer
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 3; j++) {
- loadBuffer[i][j] = 0b00000000;
- frameBuffer[i][j] = 0b00000000;
- }
- controlBytes[i] = 0b00000000;
- }
- }
- void nextColumb() {
- // increments 4017 clock pin
- digitalWrite(a4017_CLK, HIGH);
- delayMicroseconds(1);
- digitalWrite(a4017_CLK, LOW);
- delayMicroseconds(1);
- }
- void flashDisplay(int delay = 10) {
- digitalWrite(DISPL_RST, HIGH);
- delayMicroseconds(delay);
- digitalWrite(DISPL_RST, LOW);
- }
- void updateFrameBuffer() {
- #ifdef DEBUG
- Serial.println("Frame completed! Current Loadbuffer");
- for (int col = 0; col < 8; col++) {
- for (int row = 0; row < 3; row++) {
- Serial.print(formatByte(loadBuffer[col][row]));
- }
- Serial.println();
- }
- #endif
- Serial.write(READY);
- for (int col = 0; col < 8; col++) {
- for (int row = 0; row < 3; row++) {
- frameBuffer[col][row] = loadBuffer[col][row];
- }
- }
- }
- void custom_shiftOut(int dataPin, int clockPin, byte byteValue) {
- for (int i = 7; i >= 0; i--) {
- digitalWrite(clockPin, LOW);
- delayMicroseconds(1);
- digitalWrite(dataPin, bitRead(byteValue, i));
- delayMicroseconds(1);
- digitalWrite(clockPin, HIGH);
- delayMicroseconds(1);
- }
- }
- void updateFrame() {
- for (int col = 0; col < 8; col++) {
- for (int row = 0; row < 3; row++) {
- shiftOut(SHIFT_SER, SHIFT_CLK, MSBFIRST, ~yeet[col][row]);
- // shiftOut(SHIFT_SER, SHIFT_CLK, MSBFIRST, ~frameBuffer[col][row]);
- // custom_shiftOut(SHIFT_SER, SHIFT_CLK, ~yeet[col][row]);
- }
- flashDisplay();
- // clearReg();
- nextColumb();
- }
- }
- #ifdef DEBUG
- String formatByte(byte myByte) {
- String formatedByte = "";
- for (byte mask = 0x80; mask; mask >>= 1) {
- // 000100000
- if (mask & myByte) {
- formatedByte += "1";
- } else {
- formatedByte += "0";
- }
- }
- return formatedByte;
- }
- #endif
- int currentCol = 0;
- void readFromSerial() {
- byte firstByte = Serial.read();
- byte secondByte = Serial.read();
- byte thirdByte = Serial.read();
- byte fourthByte = Serial.read();
- #ifdef DEBUG
- Serial.print("4 Bytes received for currentCol: ");
- Serial.println(currentCol);
- Serial.print("First byte: ");
- Serial.println(formatByte(firstByte));
- Serial.print("Second byte: ");
- Serial.println(formatByte(secondByte));
- Serial.print("third byte: ");
- Serial.println(formatByte(thirdByte));
- Serial.print("Fourth byte: ");
- Serial.println(formatByte(fourthByte));
- #endif
- // take 4 bytes, and convert to 3
- // with another byte holding the control bits
- // input should look like:
- // 01001001 11001101 01010100 00100100
- // 01-001001 00-001101 00-010100 01-100100
- // ^^--------^^--------^^--------^^------- control bits
- // 00-001|001 00-001|101 00-010|100 00-100|100
- // ---^^^-^^^----^^^-^^^----^^^-^^^----^^^-^^^-- LED Bits, 2 per byte
- // output should look like:
- // save in buffer: 00100100 11010101 00100100
- // control byte: 01000001
- // Build the control byte first
- bitWrite(controlBytes[currentCol], 7, bitRead(firstByte, 7));
- bitWrite(controlBytes[currentCol], 6, bitRead(firstByte, 6));
- bitWrite(controlBytes[currentCol], 5, bitRead(secondByte, 7));
- bitWrite(controlBytes[currentCol], 4, bitRead(secondByte, 6));
- bitWrite(controlBytes[currentCol], 3, bitRead(thirdByte, 7));
- bitWrite(controlBytes[currentCol], 2, bitRead(thirdByte, 6));
- bitWrite(controlBytes[currentCol], 1, bitRead(fourthByte, 7));
- bitWrite(controlBytes[currentCol], 0, bitRead(fourthByte, 6));
- // Next turn the 4 bytes into 3 by stripping the control bits
- bitWrite(loadBuffer[currentCol][0], 7, bitRead(firstByte, 5));
- bitWrite(loadBuffer[currentCol][0], 6, bitRead(firstByte, 4));
- bitWrite(loadBuffer[currentCol][0], 5, bitRead(firstByte, 3));
- bitWrite(loadBuffer[currentCol][0], 4, bitRead(firstByte, 2));
- bitWrite(loadBuffer[currentCol][0], 3, bitRead(firstByte, 1));
- bitWrite(loadBuffer[currentCol][0], 2, bitRead(firstByte, 0));
- // exausted firstByte
- bitWrite(loadBuffer[currentCol][0], 1, bitRead(secondByte, 5));
- bitWrite(loadBuffer[currentCol][0], 0, bitRead(secondByte, 4));
- bitWrite(loadBuffer[currentCol][1], 7, bitRead(secondByte, 3));
- bitWrite(loadBuffer[currentCol][1], 6, bitRead(secondByte, 2));
- bitWrite(loadBuffer[currentCol][1], 5, bitRead(secondByte, 1));
- bitWrite(loadBuffer[currentCol][1], 4, bitRead(secondByte, 0));
- // exausted secondByte
- bitWrite(loadBuffer[currentCol][1], 3, bitRead(thirdByte, 5));
- bitWrite(loadBuffer[currentCol][1], 2, bitRead(thirdByte, 4));
- bitWrite(loadBuffer[currentCol][1], 1, bitRead(thirdByte, 3));
- bitWrite(loadBuffer[currentCol][1], 0, bitRead(thirdByte, 2));
- bitWrite(loadBuffer[currentCol][2], 7, bitRead(thirdByte, 1));
- bitWrite(loadBuffer[currentCol][2], 6, bitRead(thirdByte, 0));
- // exausted thirdByte
- bitWrite(loadBuffer[currentCol][2], 5, bitRead(fourthByte, 5));
- bitWrite(loadBuffer[currentCol][2], 4, bitRead(fourthByte, 4));
- bitWrite(loadBuffer[currentCol][2], 3, bitRead(fourthByte, 3));
- bitWrite(loadBuffer[currentCol][2], 2, bitRead(fourthByte, 2));
- bitWrite(loadBuffer[currentCol][2], 1, bitRead(fourthByte, 1));
- bitWrite(loadBuffer[currentCol][2], 0, bitRead(fourthByte, 0));
- // exausted fourthByte
- currentCol += 1;
- if (currentCol == 8) {
- currentCol = 0;
- updateFrameBuffer();
- }
- }
- // Loop:
- // Check if time > amount needed
- // - if new frame, update buffer, blink frame
- // - if not, blink old frame buffer
- // --
- // Check if data in Serial buffer
- // - if so, read it into local buffer
- // - if buffer fully updated, set new frame = true
- //
- void loop() {
- curTime = millis();
- if ((curTime - lastTimeUpdate) >= updateWait) {
- lastTimeUpdate = curTime;
- updateFrame();
- }
- if (Serial.available() >= 4) {
- #ifdef DEBUG
- Serial.print("Amount of bytes in buffer: ");
- Serial.println(Serial.available());
- #endif
- readFromSerial();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement