Advertisement
NetAgent

CM-5 Mode 7 Mini-Display v1 Final

Jun 12th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. /// LED Matrix: https://www.amazon.com/gp/product/B01EJ1AFW8
  2. /// Install the MD_MAX72xx v2.10.00 and TimerOne liberty for the Arduino Uno R3.
  3. /// Credits go to those who did all the hard work at http://www.housedillon.com/?p=1272
  4. //
  5.  
  6. //VIDEO here: https://youtu.be/OlV-Urn8THE
  7.  
  8. #include <TimerOne.h>
  9.  
  10. #define HEIGHT 8
  11. #define WIDTH 32 //64
  12.  
  13. #define HEIGHTR 32  //64
  14. #define WIDTHR 8   //16
  15.  
  16. #define ROWCHANGE 4   //Rows change direction every X rows //default is 4
  17. #define INVERT 0      //LEDs will be lit in reverse order resulting in more LED active at once //default is 0
  18. #define MOVEDELAY 200 //How much delay should there be before the rows move //default is 200 (131)
  19.  
  20. byte row = 0;
  21.  
  22. // Program to exercise the MD_MAX72XX library
  23. //
  24. // Uses most of the functions in the library
  25. #include <MD_MAX72xx.h> //You'll need to #define USE_FC16_HW as 1 and USE_PAROLA_HW as 0
  26. #include <SPI.h>
  27.  
  28. #define  MAX_DEVICES 4
  29.  
  30. #define CLK_PIN   13  // or SCK
  31. #define DATA_PIN  11  // or MOSI
  32. #define CS_PIN    10  // or SS
  33.  
  34. // SPI hardware interface
  35. MD_MAX72XX mx = MD_MAX72XX(CS_PIN, MAX_DEVICES);
  36. // Arbitrary pins
  37. // MD_MAX72XX mx = MD_MAX72XX(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
  38.  
  39. //=== S E T U P ===
  40.  
  41. void setup() {
  42.   //pinMode(led, OUTPUT);
  43.  
  44.   mx.begin();
  45.   mx.control(MD_MAX72XX::INTENSITY, 10); //How brite should the LEDs be 1 to 10
  46.  
  47.   //Turn all LEDs on then turn them off
  48.   mx.control(MD_MAX72XX::TEST, MD_MAX72XX::ON);
  49.   delay(500);
  50.   mx.control(MD_MAX72XX::TEST, MD_MAX72XX::OFF);
  51.  
  52.  
  53. }
  54.  
  55. //=== L O O P ===
  56.  
  57. void loop() {
  58.  
  59.   cm5();
  60.  
  61. }
  62.  
  63. void HardwarePlot(int x, int y, int z)
  64. {
  65.  
  66.   if (z == INVERT) {
  67.     mx.setPoint(y, x, true);
  68.   } else {
  69.     mx.setPoint(y, x, false);
  70.   }
  71. }
  72.  
  73. long long array[2][16];
  74.  
  75. byte getPixel(byte x, byte y, byte whichArray) {
  76.  
  77.   unsigned long long temp = (1ULL << y);
  78.   if (array[whichArray][x] & temp) {
  79.     return 1; //the yth bit was set, so return 1.
  80.   }
  81.   else {
  82.     return 0; //the yth bit was clear, so return 0;
  83.   }
  84. }
  85.  
  86. void setPixel(byte x, byte y, byte whichArray, byte pixel) {
  87.  
  88.   unsigned long long temp = (1ULL << y);
  89.   if (pixel) {
  90.     array[whichArray][x] |= temp;
  91.   }
  92.   else {
  93.     array[whichArray][x] &= ~temp;
  94.   }
  95. }
  96.  
  97.  
  98. void clearArray(byte no) {
  99.   byte i, j; for (j = 0; j < HEIGHT; j++) {
  100.     for (i = 0; i < WIDTH; i++ ) {
  101.       setPixel(i, j, no, 0);
  102.     }
  103.   }
  104. }
  105.  
  106. #define RNUM_SEED 0xBAD
  107. static uint16_t rnum = RNUM_SEED;
  108.  
  109. void paintCM5() {
  110.   int i, j; bool pixel;
  111.  
  112.   for (i = 0; i < WIDTHR; i++ ) {
  113.     for (j = 0; j < HEIGHTR; j++) {
  114.  
  115.       pixel = getPixel(i, j, 0);
  116.  
  117.       if (j & ROWCHANGE) {
  118.         if (pixel == 1 ) {
  119.           HardwarePlot(j, i, 0);
  120.         }
  121.         else {
  122.           HardwarePlot(j, i, 1);
  123.         }
  124.       }
  125.       else if (pixel == 1 ) {
  126.         HardwarePlot(j, (WIDTHR - 1) - i, 0);
  127.  
  128.       }
  129.       else {
  130.         HardwarePlot(j, (WIDTHR - 1) - i, 1);
  131.  
  132.       }
  133.  
  134.     }
  135.   }
  136. }
  137.  
  138. static uint16_t get_random_bit(void)
  139. {
  140. #define X rnum
  141.  
  142.   uint16_t lfsr_bit = ((X >> 0) ^ (X >> 1) ^ (X >> 3) ^ (X >> 12)) & 1;
  143.  
  144.   uint16_t rand_bit = (X | (X >> 2)) & 1;
  145.  
  146. #undef X
  147.  
  148.   rnum = (lfsr_bit << 15) | (rnum >> 1);
  149.  
  150.   return rand_bit;
  151.  
  152. }
  153.  
  154. void cm5() {
  155.  
  156.   int pixel, i, j;
  157.   int count = 0;
  158.   clearArray(0);
  159.   //cm5 mode 7
  160.   uint16_t gen_bit;
  161.  
  162.   uint16_t rnum = 1;
  163.  
  164.   while (count < 3080) {
  165.  
  166.     for (j = 63; j >= 0; j-- ) {
  167.  
  168.  
  169.       uint16_t bit = get_random_bit();
  170.  
  171.       if (bit == 0) {
  172.         setPixel(16, j, 0, 1);
  173.       }
  174.     }
  175.  
  176.     for (j = 0; j < 64; j++) {
  177.       for (i = 0; i < 16 + 1; i++) {
  178.         pixel = getPixel(i + 1, j, 0); if (pixel == 1) setPixel(i, j, 0, 1); else setPixel(i, j, 0, 0);
  179.       }
  180.     }
  181.  
  182.     count++;
  183.  
  184.  
  185.     if (count > 0) {
  186.       paintCM5();
  187.       delay(MOVEDELAY); //131
  188.     }
  189.   }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement