display_test.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Use the MD_MAX72XX library to Print some text on the display
  2. //
  3. // Demonstrates the use of the library to print text.
  4. //
  5. // User can enter text on the serial monitor and this will display as a
  6. // message on the display.
  7. // Library doc https://majicdesigns.github.io/MD_MAX72XX/class_m_d___m_a_x72_x_x.html
  8. #include <MD_MAX72xx.h>
  9. #include <SPI.h>
  10. #define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }
  11. // Define the number of devices we have in the chain and the hardware interface
  12. // NOTE: These pin numbers will probably not work with your hardware and may
  13. // need to be adapted
  14. #define HARDWARE_TYPE MD_MAX72XX::DR1CR0RR0_HW
  15. #define MAX_DEVICES 8
  16. #define CLK_PIN D5 // or SCK
  17. #define DATA_PIN D7 // or MOSI
  18. #define CS_PIN D4 // or SS
  19. // SPI hardware interface
  20. MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
  21. // Arbitrary pins
  22. //MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
  23. // Text parameters
  24. #define CHAR_SPACING 1 // pixels between characters
  25. // Global message buffers shared by Serial and Scrolling functions
  26. #define BUF_SIZE 75
  27. char message[BUF_SIZE] = "FuwawaMococo";
  28. bool newMessageAvailable = true;
  29. void readSerial(void)
  30. {
  31. static uint8_t putIndex = 0;
  32. while (Serial.available())
  33. {
  34. message[putIndex] = (char)Serial.read();
  35. if ((message[putIndex] == '\n') || (putIndex >= BUF_SIZE-3)) // end of message character or full buffer
  36. {
  37. // put in a message separator and end the string
  38. message[putIndex] = '\0';
  39. // restart the index for next filling spree and flag we have a message waiting
  40. putIndex = 0;
  41. newMessageAvailable = true;
  42. }
  43. else
  44. // Just save the next char in next location
  45. message[putIndex++];
  46. }
  47. }
  48. void printText(uint8_t modStart, uint8_t modEnd, char *pMsg)
  49. // Print the text string to the LED matrix modules specified.
  50. // Message area is padded with blank columns after printing.
  51. {
  52. uint8_t state = 0;
  53. uint8_t curLen;
  54. uint16_t showLen;
  55. uint8_t cBuf[8];
  56. int16_t col = ((modEnd + 1) * COL_SIZE) - 1;
  57. mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
  58. do // finite state machine to print the characters in the space available
  59. {
  60. switch(state)
  61. {
  62. case 0: // Load the next character from the font table
  63. // if we reached end of message, reset the message pointer
  64. if (*pMsg == '\0')
  65. {
  66. showLen = col - (modEnd * COL_SIZE); // padding characters
  67. state = 2;
  68. break;
  69. }
  70. // retrieve the next character form the font file
  71. showLen = mx.getChar(*pMsg++, sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
  72. curLen = 0;
  73. state++;
  74. // !! deliberately fall through to next state to start displaying
  75. case 1: // display the next part of the character
  76. mx.setColumn(col--, cBuf[curLen++]);
  77. // done with font character, now display the space between chars
  78. if (curLen == showLen)
  79. {
  80. showLen = CHAR_SPACING;
  81. state = 2;
  82. }
  83. break;
  84. case 2: // initialize state for displaying empty columns
  85. curLen = 0;
  86. state++;
  87. // fall through
  88. case 3: // display inter-character spacing or end of message padding (blank columns)
  89. mx.setColumn(col--, 0);
  90. curLen++;
  91. if (curLen == showLen)
  92. state = 0;
  93. break;
  94. default:
  95. col = -1; // this definitely ends the do loop
  96. }
  97. } while (col >= (modStart * COL_SIZE));
  98. mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
  99. }
  100. void setup()
  101. {
  102. mx.begin();
  103. Serial.begin(57600);
  104. Serial.print("\n[MD_MAX72XX Message Display]\nType a message for the display\nEnd message line with a newline");
  105. }
  106. void loop()
  107. {
  108. readSerial();
  109. if (newMessageAvailable)
  110. {
  111. PRINT("\nProcessing new message: ", message);
  112. printText(0, MAX_DEVICES-1, message);
  113. newMessageAvailable = false;
  114. }
  115. }