Advertisement
Guest User

Untitled

a guest
Dec 24th, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.49 KB | None | 0 0
  1. // Use the MD_MAX72XX library to scroll text on the display
  2. //
  3. // Demonstrates the use of the callback function to control what
  4. // is scrolled on the display text.
  5. //
  6. // User can enter text on the serial monitor and this will display as a
  7. // scrolling message on the display.
  8. // Speed for the display is controlled by a pot on SPEED_IN analog in.
  9. //
  10. #include <MD_MAX72xx.h>
  11. #include <SPI.h>
  12.  
  13. #define IMMEDIATE_NEW   0     // if 1 will immediately display a new message
  14. #define USE_POT_CONTROL 1
  15. #define PRINT_CALLBACK  0
  16.  
  17. #define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }
  18.  
  19. // Define the number of devices we have in the chain and the hardware interface
  20. // NOTE: These pin numbers will probably not work with your hardware and may
  21. // need to be adapted
  22. #define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
  23. #define MAX_DEVICES 4
  24.  
  25. #define CLK_PIN   13  // or SCK
  26. #define DATA_PIN  11  // or MOSI
  27. #define CS_PIN    10  // or SS
  28.  
  29. // SPI hardware interface
  30. MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
  31. // Arbitrary pins
  32. //MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
  33.  
  34. // Scrolling parameters
  35. #if USE_POT_CONTROL
  36. #define SPEED_IN  A5
  37. #else
  38. #define SCROLL_DELAY  75  // in milliseconds
  39. #endif // USE_POT_CONTROL
  40.  
  41. #define CHAR_SPACING  1 // pixels between characters
  42.  
  43. // Global message buffers shared by Serial and Scrolling functions
  44. #define BUF_SIZE  75
  45. uint8_t curMessage[BUF_SIZE] = { "Hello!  " };
  46. uint8_t newMessage[BUF_SIZE];
  47. bool newMessageAvailable = false;
  48.  
  49. uint16_t  scrollDelay;  // in milliseconds
  50.  
  51. void readSerial(void)
  52. {
  53.   static uint8_t  putIndex = 0;
  54.  
  55.   while (Serial.available())
  56.   {
  57.     newMessage[putIndex] = (char)Serial.read();
  58.     if ((newMessage[putIndex] == '\n') || (putIndex >= BUF_SIZE-3)) // end of message character or full buffer
  59.     {
  60.       // put in a message separator and end the string
  61.       newMessage[putIndex++] = ' ';
  62.       newMessage[putIndex] = '\0';
  63.       // restart the index for next filling spree and flag we have a message waiting
  64.       putIndex = 0;
  65.       newMessageAvailable = true;
  66.     }
  67.     else if (newMessage[putIndex] != '\r')
  68.       // Just save the next char in next location
  69.       putIndex++;
  70.   }
  71. }
  72.  
  73. void scrollDataSink(uint8_t dev, MD_MAX72XX::transformType_t t, uint8_t col)
  74. // Callback function for data that is being scrolled off the display
  75. {
  76. #if PRINT_CALLBACK
  77.   Serial.print("\n cb ");
  78.   Serial.print(dev);
  79.   Serial.print(' ');
  80.   Serial.print(t);
  81.   Serial.print(' ');
  82.   Serial.println(col);
  83. #endif
  84. }
  85.  
  86. uint8_t scrollDataSource(uint8_t dev, MD_MAX72XX::transformType_t t)
  87. // Callback function for data that is required for scrolling into the display
  88. {
  89.   static uint8_t* p = curMessage;
  90.   static enum { NEW_MESSAGE, LOAD_CHAR, SHOW_CHAR, BETWEEN_CHAR } state = LOAD_CHAR;
  91.   static uint8_t  curLen, showLen;
  92.   static uint8_t  cBuf[15];
  93.   uint8_t colData = 0;    // blank column is the default
  94.  
  95. #if IMMEDIATE_NEW
  96.   if (newMessageAvailable)  // there is a new message waiting
  97.   {
  98.     state = NEW_MESSAGE;
  99.     mx.clear(); // clear the display
  100.   }
  101. #endif
  102.  
  103.   // finite state machine to control what we do on the callback
  104.   switch(state)
  105.   {
  106.     case NEW_MESSAGE:   // Load the new message
  107.       memcpy(curMessage, newMessage, BUF_SIZE); // copy it in
  108.       newMessageAvailable = false;    // used it!
  109.       p = curMessage;
  110.       state = LOAD_CHAR;
  111.       break;
  112.  
  113.     case LOAD_CHAR: // Load the next character from the font table
  114.       showLen = mx.getChar(*p++, sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
  115.       curLen = 0;
  116.       state = SHOW_CHAR;
  117.  
  118.       // if we reached end of message, opportunity to load the next
  119.       if (*p == '\0')
  120.       {
  121.         p = curMessage;     // reset the pointer to start of message
  122. #if !IMMEDIATE_NEW
  123.         if (newMessageAvailable)  // there is a new message waiting
  124.         {
  125.           state = NEW_MESSAGE;    // we will load it here
  126.           break;
  127.         }
  128. #endif
  129.       }
  130.       // !! deliberately fall through to next state to start displaying
  131.  
  132.     case SHOW_CHAR: // display the next part of the character
  133.       colData = cBuf[curLen++];
  134.       if (curLen == showLen)
  135.       {
  136.         showLen = CHAR_SPACING;
  137.         curLen = 0;
  138.         state = BETWEEN_CHAR;
  139.       }
  140.       break;
  141.  
  142.     case BETWEEN_CHAR: // display inter-character spacing (blank columns)
  143.       colData = 0;
  144.       curLen++;
  145.       if (curLen == showLen)
  146.         state = LOAD_CHAR;
  147.       break;
  148.  
  149.     default:
  150.       state = LOAD_CHAR;
  151.   }
  152.  
  153.   return(colData);
  154. }
  155.  
  156.  void scrollText(void)
  157. {
  158.   static uint32_t   prevTime = 0;
  159.  
  160.   // Is it time to scroll the text?
  161.   if (millis()-prevTime >= scrollDelay)
  162.   {
  163.     mx.transform(MD_MAX72XX::TSL);  // scroll along - the callback will load all the data
  164.     prevTime = millis();      // starting point for next time
  165.   }
  166. }
  167.  
  168. uint16_t getScrollDelay(void)
  169. {
  170. #if USE_POT_CONTROL
  171.   uint16_t  t;
  172.  
  173.   t = analogRead(SPEED_IN);
  174.   t = map(t, 0, 1023, 25, 250);
  175.  
  176.   return(t);
  177. #else
  178.   return(SCROLL_DELAY);
  179. #endif
  180. }
  181.  
  182. void setup()
  183. {
  184.   mx.begin();
  185.   mx.setShiftDataInCallback(scrollDataSource);
  186.   mx.setShiftDataOutCallback(scrollDataSink);
  187.  
  188. #if USE_POT_CONTROL
  189.   pinMode(SPEED_IN, INPUT);
  190. #else
  191.   scrollDelay = SCROLL_DELAY;
  192. #endif
  193.  
  194.   newMessage[0] = '\0';
  195.  
  196.   Serial.begin(57600);
  197.   Serial.print("\n[MD_MAX72XX Message Display]\nType a message for the scrolling display\nEnd message line with a newline");
  198. }
  199.  
  200. void loop()
  201. {
  202.   scrollDelay = getScrollDelay();
  203.   readSerial();
  204.   scrollText();
  205. }
  206.  
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement