display_test.ino 4.1 KB

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