display.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. //Read a binary file from SD card to current framebuffer
  28. int readFrameToFrameBuffer(String filepath){
  29. File file = SD.open(filepath, FILE_READ);
  30. if (!file) {
  31. Serial.println("Failed to open file for reading");
  32. return 1;
  33. }
  34. // Read file byte by byte
  35. size_t bytesRead = 0;
  36. while (file.available() && bytesRead < FRAME_BUFFER_SIZE) {
  37. frame_buffer[bytesRead] = file.read();
  38. bytesRead++;
  39. }
  40. // Close the file
  41. file.close();
  42. return 0;
  43. }
  44. /*
  45. * renderFrame render the frame buffer to display
  46. *
  47. * The display is an upside down two split LED grid matrix display
  48. * the render sequence (when viewed from front) is as follows
  49. * and each matrix module is upside down (row 0 on bottom)
  50. * [8][7][6][5]
  51. * [4][3][2][1]
  52. *
  53. */
  54. void renderFrame() {
  55. //Top half of the display
  56. int fsize = sizeof(frame_buffer);
  57. for (int i = 0; i < fsize / 2; i += 4) {
  58. for (int d = 0; d <= 3; d++) {
  59. //For each of the driver, from 0 to 3
  60. byte rowData = frame_buffer[i + d];
  61. mx.setRow(d, d, 7 - int(i / 4), fByte(rowData));
  62. }
  63. }
  64. //Bottom half of the display
  65. for (int i = fsize / 2; i < fsize; i += 4) {
  66. for (int d = 4; d <= 7; d++) {
  67. //For each of the driver, from 4 to 7
  68. byte rowData = frame_buffer[i + (d - 4)];
  69. mx.setRow(d, d, 7 - (int(i / 4) - 8), fByte(rowData));
  70. }
  71. }
  72. }
  73. /* Utilities Functions */
  74. //Set display brightness, from 0x0(min) to 0xF (max)
  75. void setDisplayBrightness(byte brightness){
  76. for(int i =0; i<MAX_DEVICES; i++){
  77. mx.control(i,MD_MAX72XX::INTENSITY, brightness);
  78. }
  79. }
  80. //Helper function to reverse a byte in bits
  81. //e.g. 11011101 -> 10111011
  82. byte fByte(byte c) {
  83. char r = 0;
  84. for (byte i = 0; i < 8; i++) {
  85. r <<= 1;
  86. r |= c & 1;
  87. c >>= 1;
  88. } return r;
  89. }