Guest User

Untitled

a guest
Mar 29th, 2017
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Simple 3bit color test for Hackspace LED Panel
  2. // written by Robin Baumgarten
  3.  
  4. // draw 1 white pixel on screen that moves from first to last spot, measuring fps along the way.
  5. // Tested on Teensy 3.6. (I get 788 fps at 240mhz!)
  6.  
  7. // with the panel the correct way up (you can read "digiled" on the black plastic) these are the pins:
  8. // see also: https://github.com/krux702/led_panel_wall/blob/master/photos/pinout.jpg
  9.  
  10. // all 8 top pins VCC (5v). Careful: You might need to connect at least two pins, since the halfs of the board use separate vccs
  11.  
  12. // D1 LAT A1 NC
  13. // D2 OE  A0 CLK
  14.  
  15. // all 8 bottom pins GND.
  16. // NC = not connected
  17.  
  18. // populate with your own arduino pins!
  19. #define PIN_CLK   13
  20. #define PIN_ADD0  26
  21. #define PIN_ADD1  25
  22. #define PIN_OE    7
  23. #define PIN_LAT   8
  24. #define PIN_D1  11
  25. #define PIN_D2  12
  26.  
  27. // pixel storage
  28. bool r[1024];
  29. bool g[1024];
  30. bool b[1024];
  31.  
  32. int fpsCounter = 0;
  33. long fpsMillisStart = 0;
  34.  
  35. void initPixels()
  36. {
  37.     for(int i =0; i < 1024; i++)
  38.     {
  39.         r[i] = 0; //(i % 5) == 0;
  40.         g[i] = 0; //(i % 6) == 0;
  41.         b[i] = 0; //(i % 7) == 0;
  42.     }
  43. }
  44.  
  45. void setup() {
  46.  
  47.   pinMode (PIN_CLK, OUTPUT);
  48.   pinMode (PIN_ADD0, OUTPUT);
  49.   pinMode (PIN_ADD1, OUTPUT);
  50.   pinMode (PIN_OE, OUTPUT);
  51.   pinMode (PIN_LAT, OUTPUT);
  52.   pinMode (PIN_D1, OUTPUT);
  53.   pinMode (PIN_D2, OUTPUT);
  54.  
  55.   delay(10);
  56.   digitalWrite(PIN_OE, LOW);
  57.  
  58.   initPixels();
  59.  
  60.   // A0 & A1 control bank to push vals to.
  61.   // 3
  62.   // 2
  63.   // 1
  64.   // 0
  65.   // etc
  66.   // each bank consists of 128 leds
  67.   // d1 is data for bottom half of display
  68.   // d2 is data for top half
  69.  
  70.   // shifting data:
  71.   // each bank is further split into chunks
  72.   // 0 pixel is bottom right, proceeds to left until hitting 8
  73.   // row then changes to row+4 and continues from column 0
  74.   // colour then changes to next in cycle
  75.   // data is BBBBBBBB BBBBBBBB GGGGGGGG GGGGGGGG RRRRRRRR RRRRRRRR
  76.   //         | row 0 | row 4  | row 0  | row 4  | row 0  | row 4
  77. }
  78.  
  79. // set a pixel to a certain 3-bit color on the screen. x: 0 - 63, y: 0 - 16
  80. void setPixel(int x, int y, bool cr, bool cg, bool cb)
  81. {
  82.     r[y * 64 + x] = cr;
  83.     g[y * 64 + x] = cg;
  84.     b[y * 64 + x] = cb;
  85. }
  86.  
  87. bool getBit(int ledIndex, int bank)
  88. {
  89.     // is this the higher row?
  90.     bool isRow4 = (ledIndex / 8) % 2 == 1;
  91.  
  92.     bool isBlue = (ledIndex % 48) / 16 == 0;
  93.     bool isGreen = (ledIndex % 48) / 16 == 1;
  94.     //bool isRed = (ledIndex % 48) / 16 == 2;
  95.  
  96.     int x = (ledIndex / 48) * 8 + (ledIndex % 8);
  97.  
  98.     if (isBlue)
  99.     {
  100.         return b[((isRow4 ? 4 : 0) + bank) * 64 + x];
  101.     }
  102.     else if (isGreen)
  103.     {
  104.         return g[((isRow4 ? 4 : 0) + bank) * 64 + x];
  105.     }  
  106.     else //if (isRed)
  107.     {
  108.         return r[((isRow4 ? 4 : 0) + bank) * 64 + x];
  109.     }
  110. }
  111.  
  112. bool a0Order[] = {true, false, true, false};
  113. bool a1Order[] = {true, false, false, true};
  114.  
  115. void drawPixels()
  116. {
  117.     for (int bank = 0; bank < 4; bank++)
  118.     {
  119.         // enable correct bank
  120.         digitalWrite(PIN_ADD0, a0Order[bank]);
  121.         digitalWrite(PIN_ADD1, a1Order[bank]);
  122.  
  123.         //set latch low to send data
  124.         digitalWrite(PIN_LAT, LOW);
  125.  
  126.         for (int i = 0; i < 384; i++)
  127.         {  // led index
  128.             digitalWrite(PIN_D1, getBit(i, bank));     // write to bottom half
  129.             digitalWrite(PIN_D2, getBit(i, bank + 8)); // write to top half
  130.  
  131.             // toggle clock
  132.             digitalWrite(PIN_CLK, LOW);
  133.             digitalWrite(PIN_CLK, HIGH);
  134.         }
  135.        
  136.         digitalWrite(PIN_LAT, HIGH);
  137.     }
  138. }
  139.  
  140. int c = 1;
  141. void loop()
  142. {
  143.     // set one of the pixels to white
  144.     setPixel(c % 64, c / 64, true, true, true);
  145.     drawPixels();
  146.  
  147.     // clear pixel
  148.     setPixel(c % 64, c / 64, false, false, false);
  149.  
  150.     c++;
  151.     if (c > 1023) c = 0;
  152.  
  153.     // print out fps
  154.     fpsCounter++;
  155.     if (millis() - fpsMillisStart > 1000)
  156.     {
  157.         fpsMillisStart = millis();
  158.         Serial.print("FPS: ");
  159.         Serial.println(fpsCounter);
  160.         fpsCounter = 0;
  161.     }
  162. }
RAW Paste Data