display_test.ino 3.9 KB

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