Guest User

Hakology Arduino Serial Code

a guest
Nov 23rd, 2016
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.67 KB | None | 0 0
  1. #include <MD_MAX72xx.h>
  2. #if USE_LIBRARY_SPI
  3. #include <SPI.h>
  4. #endif
  5.  
  6.  
  7. #define PRINT_CALLBACK  0
  8.  
  9. #define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }
  10.  
  11. #define MAX_DEVICES 8
  12.  
  13. #define CLK_PIN     A5  // or SCK
  14. #define DATA_PIN    A4  // or MOSI
  15. #define CS_PIN      13  // or SS
  16.  
  17. MD_MAX72XX mx = MD_MAX72XX(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
  18.  
  19. #define SCROLL_DELAY  25
  20. #define CHAR_SPACING    1   // pixels between characters
  21.  
  22. // Global message buffers shared by Serial and Scrolling functions
  23. #define BUF_SIZE    300
  24. char curMessage[BUF_SIZE];
  25. char newMessage[BUF_SIZE];
  26. bool newMessageAvailable = false;
  27.  
  28. uint16_t    scrollDelay;    // in milliseconds
  29.  
  30. void readSerial(void)
  31. {
  32.   static uint8_t    putIndex = 0;
  33.  
  34.   while (Serial.available())
  35.   {
  36.     newMessage[putIndex] = (char)Serial.read();
  37.     if ((newMessage[putIndex] == '\n') || (putIndex >= BUF_SIZE-3)) // end of message character or full buffer
  38.     {
  39.       newMessage[putIndex++] = ' ';
  40.       newMessage[putIndex] = '\0';
  41.       putIndex = 0;
  42.       newMessageAvailable = true;
  43.     }
  44.     else
  45.       newMessage[putIndex++];
  46.   }
  47. }
  48.  
  49. void scrollDataSink(uint8_t dev, MD_MAX72XX::transformType_t t, uint8_t col)
  50. // Callback function for data that is being scrolled off the display
  51. {
  52. #if PRINT_CALLBACK
  53.   Serial.print("\n cb ");
  54.   Serial.print(dev);
  55.   Serial.print(' ');
  56.   Serial.print(t);
  57.   Serial.print(' ');
  58.   Serial.println(col);
  59. #endif
  60. }
  61.  
  62. uint8_t scrollDataSource(uint8_t dev, MD_MAX72XX::transformType_t t)
  63. // Callback function for data that is required for scrolling into the display
  64. {
  65.   static char       *p = curMessage;
  66.   static uint8_t    state = 0;
  67.   static uint8_t    curLen, showLen;
  68.   static uint8_t    cBuf[8];
  69.   uint8_t colData;
  70.  
  71.   // finite state machine to control what we do on the callback
  72.   switch(state)
  73.   {
  74.     case 0: // Load the next character from the font table
  75.       showLen = mx.getChar(*p++, sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
  76.       curLen = 0;
  77.       state++;
  78.  
  79.       // if we reached end of message, reset the message pointer
  80.       if (*p == '\0')
  81.       {
  82.         p = curMessage;         // reset the pointer to start of message
  83.         if (newMessageAvailable)    // there is a new message waiting
  84.         {
  85.           strcpy(curMessage, newMessage);   // copy it in
  86.           newMessageAvailable = false;
  87.         }
  88.       }
  89.       // !! deliberately fall through to next state to start displaying
  90.  
  91.     case 1: // display the next part of the character
  92.       colData = cBuf[curLen++];
  93.       if (curLen == showLen)
  94.       {
  95.         showLen = CHAR_SPACING;
  96.         curLen = 0;
  97.         state = 2;
  98.       }
  99.       break;
  100.  
  101.     case 2: // display inter-character spacing (blank column)
  102.       colData = 0;
  103.       curLen++;
  104.       if (curLen == showLen)
  105.         state = 0;
  106.       break;
  107.  
  108.     default:
  109.       state = 0;
  110.   }
  111.  
  112.   return(colData);
  113. }
  114.  
  115.  void scrollText(void)
  116. {
  117.   static uint32_t   prevTime = 0;
  118.   // Is it time to scroll the text?
  119.   if (millis()-prevTime >= scrollDelay)
  120.   {
  121.     mx.transform(MD_MAX72XX::TSL);  // scroll along - the callback will load all the data
  122.     prevTime = millis();            // starting point for next time
  123.   }
  124. }
  125.  
  126. uint16_t getScrollDelay(void)
  127. {
  128.   scrollDelay = 10;
  129.   return scrollDelay;
  130. }
  131.  
  132. void setup()
  133. {
  134.   mx.begin();
  135.   mx.setShiftDataInCallback(scrollDataSource);
  136.   mx.setShiftDataOutCallback(scrollDataSink);
  137.   strcpy(curMessage, "Waiting for serial data! ");
  138.   newMessage[0] = '\0';
  139.   mx.control(MD_MAX72XX::INTENSITY, 0);
  140.   Serial.begin(57600);
  141.   Serial.print("\n[MD_MAX72XX Message Display]\nType a message for the scrolling display\nEnd message line with a newline");
  142. }
  143. void loop()
  144. {
  145.   scrollDelay = getScrollDelay();
  146.   readSerial();
  147.   scrollText();
  148. }
Add Comment
Please, Sign In to add comment