Advertisement
Guest User

printTextMD_MAX7219

a guest
Apr 15th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | Software | 0 0
  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.  
  8. #include <MD_MAX72xx.h>
  9. #include <SPI.h>
  10.  
  11. #define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }
  12.  
  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::PAROLA_HW
  17. #define MAX_DEVICES 4
  18.  
  19. #define CLK_PIN   13  // or SCK
  20. #define DATA_PIN  11  // or MOSI
  21. #define CS_PIN    10  // or SS
  22.  
  23. // SPI hardware interface
  24. MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
  25. // Arbitrary pins
  26. //MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
  27.  
  28. // Text parameters
  29. #define CHAR_SPACING  1 // pixels between characters
  30.  
  31. // Global message buffers shared by Serial and Scrolling functions
  32. #define BUF_SIZE  75
  33. char message[BUF_SIZE] = "Hello!";
  34. bool newMessageAvailable = true;
  35.  
  36. void readSerial(void)
  37. {
  38.   static uint8_t    putIndex = 0;
  39.  
  40.   while (Serial.available())
  41.   {
  42.     message[putIndex] = (char)Serial.read();
  43.     if ((message[putIndex] == '\n') || (putIndex >= BUF_SIZE-3))  // end of message character or full buffer
  44.     {
  45.       // put in a message separator and end the string
  46.       message[putIndex] = '\0';
  47.       // restart the index for next filling spree and flag we have a message waiting
  48.       putIndex = 0;
  49.       newMessageAvailable = true;
  50.     }
  51.     else
  52.       // Just save the next char in next location
  53.       message[putIndex++];
  54.   }
  55. }
  56.  
  57. void printText(uint8_t modStart, uint8_t modEnd, char *pMsg)
  58. // Print the text string to the LED matrix modules specified.
  59. // Message area is padded with blank columns after printing.
  60. {
  61.   uint8_t   state = 0;
  62.   uint8_t   curLen;
  63.   uint16_t  showLen;
  64.   uint8_t   cBuf[8];
  65.   int16_t   col = ((modEnd + 1) * COL_SIZE) - 1;
  66.  
  67.   mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
  68.  
  69.   do     // finite state machine to print the characters in the space available
  70.   {
  71.     switch(state)
  72.     {
  73.       case 0: // Load the next character from the font table
  74.         // if we reached end of message, reset the message pointer
  75.         if (*pMsg == '\0')
  76.         {
  77.           showLen = col - (modEnd * COL_SIZE);  // padding characters
  78.           state = 2;
  79.           break;
  80.         }
  81.  
  82.         // retrieve the next character form the font file
  83.         showLen = mx.getChar(*pMsg++, sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
  84.         curLen = 0;
  85.         state++;
  86.         // !! deliberately fall through to next state to start displaying
  87.  
  88.       case 1: // display the next part of the character
  89.         mx.setColumn(col--, cBuf[curLen++]);
  90.  
  91.         // done with font character, now display the space between chars
  92.         if (curLen == showLen)
  93.         {
  94.           showLen = CHAR_SPACING;
  95.           state = 2;
  96.         }
  97.         break;
  98.  
  99.       case 2: // initialize state for displaying empty columns
  100.         curLen = 0;
  101.         state++;
  102.         // fall through
  103.  
  104.       case 3:   // display inter-character spacing or end of message padding (blank columns)
  105.         mx.setColumn(col--, 0);
  106.         curLen++;
  107.         if (curLen == showLen)
  108.           state = 0;
  109.         break;
  110.  
  111.       default:
  112.         col = -1;   // this definitely ends the do loop
  113.     }
  114.   } while (col >= (modStart * COL_SIZE));
  115.  
  116.   mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
  117. }
  118.  
  119. void setup()
  120. {
  121.   mx.begin();
  122.  
  123.   Serial.begin(57600);
  124.   Serial.print("\n[MD_MAX72XX Message Display]\nType a message for the display\nEnd message line with a newline");
  125. }
  126.  
  127. void loop()
  128. {
  129.   readSerial();
  130.   if (newMessageAvailable)
  131.   {
  132.     PRINT("\nProcessing new message: ", message);
  133.     printText(0, MAX_DEVICES-1, message);
  134.     newMessageAvailable = false;
  135.   }
  136. }
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement