Advertisement
monsterzack5

matrix

Mar 7th, 2020
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // #define DEBUG
  2. /*
  3.  * Pinout:
  4.  *  d2 - 4017 reset
  5.  *  d3 - 4017 clock
  6.  *  
  7.  *  d5 - shift clock
  8.  *  d6 - shift clear
  9.  *  d7 - shift ser in
  10.  *
  11.  *  d9 - blank
  12.  */
  13.  
  14. /* In Physical Pin Order */
  15. const int DISPL_RST = D7;
  16. // Ground Pin
  17. const int a4017_CLK = D6;
  18. const int a4017_RST = D5;
  19. const int SHIFT_CLK = D4;
  20. const int SHIFT_CLR = D3;
  21. const int SHIFT_SER = D1;
  22. // Not Connected
  23. // Vcc
  24. /* ----- */
  25. const int updateWait = 4;
  26.  
  27. // Buffers
  28. byte controlBytes[8];
  29. byte frameBuffer[8][3];
  30. byte loadBuffer[8][3];
  31.  
  32. // Temp
  33. // byte yeet[8][3] = {
  34. //     {0b00000100, 0b10000000, 0b01001000},
  35. //     {0b00100100, 0b10010010, 0b01001001},
  36. //     {0b00100100, 0b10010010, 0b01001001},
  37. //     {0b00100100, 0b10010010, 0b01001001},
  38. //     {0b00100100, 0b10010010, 0b01001001},
  39. //     {0b00000100, 0b10010010, 0b01001000},
  40. //     {0b00000000, 0b10010010, 0b01000000},
  41. //     {0b00000000, 0b00010010, 0b00000000}};
  42.  
  43. byte yeet[8][3] = {
  44.     {0b00000100, 0b10000000, 0b01001000},
  45.     {0b00000000, 0b00000010, 0b00000000},
  46.     {0b00000000, 0b00000010, 0b00000000},
  47.     {0b00000000, 0b00000010, 0b00000000},
  48.     {0b00000000, 0b00000010, 0b00000000},
  49.     {0b00000000, 0b00000010, 0b00000000},
  50.     {0b00000000, 0b00000010, 0b00000000},
  51.     {0b00000000, 0b00000010, 0b00000000}};
  52.  
  53.  
  54. // Messaging Bytes
  55. #define READY 0b01010010  // ASCII R
  56. #define STOP 0b01010011   // ASCII S
  57. #define WAIT 0b01010111   // ASCII W
  58.  
  59. // Timekeeping
  60. unsigned long curTime = 0;
  61. unsigned long lastTimeGen = 0;
  62. unsigned long lastTimeUpdate = 0;
  63.  
  64. void setup() {
  65.     pinMode(SHIFT_SER, OUTPUT);
  66.     pinMode(SHIFT_CLR, OUTPUT);
  67.     pinMode(SHIFT_CLK, OUTPUT);
  68.     pinMode(a4017_RST, OUTPUT);
  69.     pinMode(a4017_CLK, OUTPUT);
  70.     pinMode(DISPL_RST, OUTPUT);
  71.  
  72.     // Resets the 4017
  73.     digitalWrite(a4017_RST, HIGH);
  74.     delay(1);
  75.     digitalWrite(a4017_RST, LOW);
  76.  
  77.     // Initiates serial communication
  78.     Serial.begin(115200, SERIAL_8N1);
  79.  
  80.     // Set the shift register reset pin
  81.     digitalWrite(SHIFT_CLR, HIGH);
  82.  
  83.     // wait a lil bit
  84.     delay(100);
  85.     clearBuffers();
  86.     clearReg();
  87.     flushSerial();
  88.     updateFrame();
  89. }
  90.  
  91. void flushSerial() {
  92.     Serial.flush();
  93.     while (Serial.available()) {
  94.         Serial.read();
  95.     }
  96.     Serial.write(READY);
  97. }
  98.  
  99. void clearReg() {
  100.     // Sends a pulse to the clear pin on the shift registers
  101.     digitalWrite(SHIFT_CLR, LOW);
  102.     delayMicroseconds(1);
  103.     digitalWrite(SHIFT_CLR, HIGH);
  104.     delayMicroseconds(1);
  105. }
  106.  
  107. void clearBuffers() {
  108.     // Clears the loadBuffer and frameBuffer
  109.     for (int i = 0; i < 8; i++) {
  110.         for (int j = 0; j < 3; j++) {
  111.             loadBuffer[i][j] = 0b00000000;
  112.             frameBuffer[i][j] = 0b00000000;
  113.         }
  114.         controlBytes[i] = 0b00000000;
  115.     }
  116. }
  117.  
  118. void nextColumb() {
  119.     // increments 4017 clock pin
  120.     digitalWrite(a4017_CLK, HIGH);
  121.     delayMicroseconds(1);
  122.     digitalWrite(a4017_CLK, LOW);
  123.     delayMicroseconds(1);
  124. }
  125.  
  126. void flashDisplay(int delay = 10) {
  127.     digitalWrite(DISPL_RST, HIGH);
  128.     delayMicroseconds(delay);
  129.     digitalWrite(DISPL_RST, LOW);
  130. }
  131.  
  132. void updateFrameBuffer() {
  133. #ifdef DEBUG
  134.     Serial.println("Frame completed! Current Loadbuffer");
  135.     for (int col = 0; col < 8; col++) {
  136.         for (int row = 0; row < 3; row++) {
  137.             Serial.print(formatByte(loadBuffer[col][row]));
  138.         }
  139.         Serial.println();
  140.     }
  141. #endif
  142.     Serial.write(READY);
  143.     for (int col = 0; col < 8; col++) {
  144.         for (int row = 0; row < 3; row++) {
  145.             frameBuffer[col][row] = loadBuffer[col][row];
  146.         }
  147.     }
  148. }
  149.  
  150. void custom_shiftOut(int dataPin, int clockPin, byte byteValue) {
  151.       for (int i = 7; i >= 0; i--) {
  152.          digitalWrite(clockPin, LOW);
  153.          delayMicroseconds(1);
  154.          digitalWrite(dataPin, bitRead(byteValue, i));
  155.          delayMicroseconds(1);
  156.          digitalWrite(clockPin, HIGH);
  157.          delayMicroseconds(1);
  158.       }
  159.    
  160. }
  161.  
  162. void updateFrame() {
  163.     for (int col = 0; col < 8; col++) {
  164.         for (int row = 0; row < 3; row++) {
  165.             shiftOut(SHIFT_SER, SHIFT_CLK, MSBFIRST, ~yeet[col][row]);
  166.             // shiftOut(SHIFT_SER, SHIFT_CLK, MSBFIRST, ~frameBuffer[col][row]);
  167.             // custom_shiftOut(SHIFT_SER, SHIFT_CLK, ~yeet[col][row]);
  168.         }
  169.         flashDisplay();
  170.       //   clearReg();
  171.         nextColumb();
  172.     }
  173. }
  174.  
  175. #ifdef DEBUG
  176. String formatByte(byte myByte) {
  177.     String formatedByte = "";
  178.     for (byte mask = 0x80; mask; mask >>= 1) {
  179.         // 000100000
  180.         if (mask & myByte) {
  181.             formatedByte += "1";
  182.         } else {
  183.             formatedByte += "0";
  184.         }
  185.     }
  186.     return formatedByte;
  187. }
  188. #endif
  189.  
  190. int currentCol = 0;
  191. void readFromSerial() {
  192.     byte firstByte = Serial.read();
  193.     byte secondByte = Serial.read();
  194.     byte thirdByte = Serial.read();
  195.     byte fourthByte = Serial.read();
  196.  
  197. #ifdef DEBUG
  198.     Serial.print("4 Bytes received for currentCol: ");
  199.     Serial.println(currentCol);
  200.     Serial.print("First byte:  ");
  201.     Serial.println(formatByte(firstByte));
  202.     Serial.print("Second byte: ");
  203.     Serial.println(formatByte(secondByte));
  204.     Serial.print("third byte:  ");
  205.     Serial.println(formatByte(thirdByte));
  206.     Serial.print("Fourth byte: ");
  207.     Serial.println(formatByte(fourthByte));
  208. #endif
  209.  
  210.     // take 4 bytes, and convert to 3
  211.     // with another byte holding the control bits
  212.  
  213.     // input should look like:
  214.     // 01001001 11001101 01010100 00100100
  215.     // 01-001001 00-001101 00-010100 01-100100
  216.     // ^^--------^^--------^^--------^^------- control bits
  217.     // 00-001|001 00-001|101 00-010|100 00-100|100
  218.     // ---^^^-^^^----^^^-^^^----^^^-^^^----^^^-^^^-- LED Bits, 2 per byte
  219.     // output should look like:
  220.     // save in buffer: 00100100 11010101 00100100
  221.     // control byte: 01000001
  222.  
  223.     // Build the control byte first
  224.     bitWrite(controlBytes[currentCol], 7, bitRead(firstByte, 7));
  225.     bitWrite(controlBytes[currentCol], 6, bitRead(firstByte, 6));
  226.  
  227.     bitWrite(controlBytes[currentCol], 5, bitRead(secondByte, 7));
  228.     bitWrite(controlBytes[currentCol], 4, bitRead(secondByte, 6));
  229.  
  230.     bitWrite(controlBytes[currentCol], 3, bitRead(thirdByte, 7));
  231.     bitWrite(controlBytes[currentCol], 2, bitRead(thirdByte, 6));
  232.  
  233.     bitWrite(controlBytes[currentCol], 1, bitRead(fourthByte, 7));
  234.     bitWrite(controlBytes[currentCol], 0, bitRead(fourthByte, 6));
  235.  
  236.     // Next turn the 4 bytes into 3 by stripping the control bits
  237.     bitWrite(loadBuffer[currentCol][0], 7, bitRead(firstByte, 5));
  238.     bitWrite(loadBuffer[currentCol][0], 6, bitRead(firstByte, 4));
  239.     bitWrite(loadBuffer[currentCol][0], 5, bitRead(firstByte, 3));
  240.     bitWrite(loadBuffer[currentCol][0], 4, bitRead(firstByte, 2));
  241.     bitWrite(loadBuffer[currentCol][0], 3, bitRead(firstByte, 1));
  242.     bitWrite(loadBuffer[currentCol][0], 2, bitRead(firstByte, 0));
  243.     // exausted firstByte
  244.     bitWrite(loadBuffer[currentCol][0], 1, bitRead(secondByte, 5));
  245.     bitWrite(loadBuffer[currentCol][0], 0, bitRead(secondByte, 4));
  246.     bitWrite(loadBuffer[currentCol][1], 7, bitRead(secondByte, 3));
  247.     bitWrite(loadBuffer[currentCol][1], 6, bitRead(secondByte, 2));
  248.     bitWrite(loadBuffer[currentCol][1], 5, bitRead(secondByte, 1));
  249.     bitWrite(loadBuffer[currentCol][1], 4, bitRead(secondByte, 0));
  250.     // exausted secondByte
  251.     bitWrite(loadBuffer[currentCol][1], 3, bitRead(thirdByte, 5));
  252.     bitWrite(loadBuffer[currentCol][1], 2, bitRead(thirdByte, 4));
  253.     bitWrite(loadBuffer[currentCol][1], 1, bitRead(thirdByte, 3));
  254.     bitWrite(loadBuffer[currentCol][1], 0, bitRead(thirdByte, 2));
  255.     bitWrite(loadBuffer[currentCol][2], 7, bitRead(thirdByte, 1));
  256.     bitWrite(loadBuffer[currentCol][2], 6, bitRead(thirdByte, 0));
  257.     // exausted thirdByte
  258.     bitWrite(loadBuffer[currentCol][2], 5, bitRead(fourthByte, 5));
  259.     bitWrite(loadBuffer[currentCol][2], 4, bitRead(fourthByte, 4));
  260.     bitWrite(loadBuffer[currentCol][2], 3, bitRead(fourthByte, 3));
  261.     bitWrite(loadBuffer[currentCol][2], 2, bitRead(fourthByte, 2));
  262.     bitWrite(loadBuffer[currentCol][2], 1, bitRead(fourthByte, 1));
  263.     bitWrite(loadBuffer[currentCol][2], 0, bitRead(fourthByte, 0));
  264.     // exausted fourthByte
  265.  
  266.     currentCol += 1;
  267.     if (currentCol == 8) {
  268.         currentCol = 0;
  269.         updateFrameBuffer();
  270.     }
  271. }
  272.  
  273. // Loop:
  274. // Check if time > amount needed
  275. // - if new frame, update buffer, blink frame
  276. // - if not, blink old frame buffer
  277. // --
  278. // Check if data in Serial buffer
  279. // - if so, read it into local buffer
  280. // - if buffer fully updated, set new frame = true
  281. //
  282.  
  283. void loop() {
  284.     curTime = millis();
  285.     if ((curTime - lastTimeUpdate) >= updateWait) {
  286.         lastTimeUpdate = curTime;
  287.         updateFrame();
  288.     }
  289.  
  290.     if (Serial.available() >= 4) {
  291. #ifdef DEBUG
  292.         Serial.print("Amount of bytes in buffer: ");
  293.         Serial.println(Serial.available());
  294. #endif
  295.         readFromSerial();
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement