display.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. LED Matrix Display Driver
  3. Library doc
  4. https://majicdesigns.github.io/MD_MAX72XX/class_m_d___m_a_x72_x_x.html
  5. */
  6. #define FRAME_BUFFER_SIZE 64 //4(32 bits) x 16 bytes
  7. //frame buffer, 32x16px
  8. //each LED is 1 bit
  9. unsigned char frame_buffer[] = {
  10. 0x00, 0x00, 0x00, 0x00,
  11. 0x00, 0x00, 0x00, 0x00,
  12. 0x00, 0x00, 0x00, 0x00,
  13. 0x22, 0x00, 0x00, 0x44,
  14. 0x44, 0x00, 0x00, 0x22,
  15. 0x89, 0x80, 0x01, 0x91,
  16. 0x92, 0x40, 0x02, 0x49,
  17. 0x82, 0x40, 0x02, 0x41,
  18. 0x81, 0x80, 0x01, 0x81,
  19. 0x80, 0x04, 0x40, 0x01,
  20. 0x80, 0x09, 0x20, 0x01,
  21. 0x87, 0x89, 0x21, 0xe1,
  22. 0x40, 0x06, 0xc0, 0x02,
  23. 0x27, 0x80, 0x01, 0xe4,
  24. 0x00, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00
  26. };
  27. //A combined function for read frame to framebuffer and render to display
  28. //Example usage: loadFrameAndRender("/a.bin");
  29. void loadFrameAndRender(String filepath){
  30. readFrameToFrameBuffer(filepath);
  31. renderFrame();
  32. }
  33. //Read a binary file from SD card to current framebuffer
  34. int readFrameToFrameBuffer(String filepath){
  35. File file = SD.open(filepath, FILE_READ);
  36. if (!file) {
  37. Serial.println("Failed to open file for reading");
  38. return 1;
  39. }
  40. // Read file byte by byte
  41. size_t bytesRead = 0;
  42. while (file.available() && bytesRead < FRAME_BUFFER_SIZE) {
  43. frame_buffer[bytesRead] = file.read();
  44. bytesRead++;
  45. }
  46. // Close the file
  47. file.close();
  48. return 0;
  49. }
  50. /*
  51. * renderFrame render the frame buffer to display
  52. *
  53. * The display is an upside down two split LED grid matrix display
  54. * the render sequence (when viewed from front) is as follows
  55. * and each matrix module is upside down (row 0 on bottom)
  56. * [8][7][6][5]
  57. * [4][3][2][1]
  58. *
  59. */
  60. void renderFrame() {
  61. //Top half of the display
  62. int fsize = sizeof(frame_buffer);
  63. for (int i = 0; i < fsize / 2; i += 4) {
  64. for (int d = 0; d <= 3; d++) {
  65. //For each of the driver, from 0 to 3
  66. byte rowData = frame_buffer[i + d];
  67. mx.setRow(d, d, 7 - int(i / 4), fByte(rowData));
  68. }
  69. }
  70. //Bottom half of the display
  71. for (int i = fsize / 2; i < fsize; i += 4) {
  72. for (int d = 4; d <= 7; d++) {
  73. //For each of the driver, from 4 to 7
  74. byte rowData = frame_buffer[i + (d - 4)];
  75. mx.setRow(d, d, 7 - (int(i / 4) - 8), fByte(rowData));
  76. }
  77. }
  78. }
  79. /* Utilities Functions */
  80. //Set display brightness, from 0x0(min) to 0xF (max)
  81. void setDisplayBrightness(byte brightness){
  82. for(int i =0; i<MAX_DEVICES; i++){
  83. mx.control(i,MD_MAX72XX::INTENSITY, brightness);
  84. }
  85. }
  86. //Helper function to reverse a byte in bits
  87. //e.g. 11011101 -> 10111011
  88. byte fByte(byte c) {
  89. char r = 0;
  90. for (byte i = 0; i < 8; i++) {
  91. r <<= 1;
  92. r |= c & 1;
  93. c >>= 1;
  94. } return r;
  95. }