Advertisement
Guest User

Untitled

a guest
Apr 21st, 2021
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. // Use the Parola library to scroll text on the display
  2. //
  3. // Demonstrates the use of the scrolling function to display text received
  4. // from the serial interface
  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. // Scrolling direction is controlled by a switch on DIRECTION_SET digital in.
  10. // Invert ON/OFF is set by a switch on INVERT_SET digital in.
  11. //
  12. // UISwitch library can be found at https://github.com/MajicDesigns/MD_UISwitch
  13. // MD_MAX72XX library can be found at https://github.com/MajicDesigns/MD_MAX72XX
  14. //
  15.  
  16. #include <MD_Parola.h>
  17. #include <MD_MAX72xx.h>
  18. #include <SPI.h>
  19.  
  20. // set to 1 if we are implementing the user interface pot, switch, etc
  21. #define USE_UI_CONTROL 0
  22.  
  23. #if USE_UI_CONTROL
  24. #include <MD_UISwitch.h>
  25. #endif
  26.  
  27. // Turn on debug statements to the serial output
  28. #define DEBUG 0
  29.  
  30. #if DEBUG
  31. #define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
  32. #define PRINTS(x) Serial.print(F(x))
  33. #define PRINTX(x) Serial.println(x, HEX)
  34. #else
  35. #define PRINT(s, x)
  36. #define PRINTS(x)
  37. #define PRINTX(x)
  38. #endif
  39.  
  40. // Define the number of devices we have in the chain and the hardware interface
  41. // NOTE: These pin numbers will probably not work with your hardware and may
  42. // need to be adapted
  43. #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
  44. #define MAX_DEVICES 4
  45. #define CS_PIN      D4  
  46. #define CLK_PIN     D5
  47. #define DATA_PIN    D7
  48.  
  49. // HARDWARE SPI
  50. //MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
  51. // SOFTWARE SPI
  52. MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
  53.  
  54. // Scrolling parameters
  55. #if USE_UI_CONTROL
  56. const uint8_t SPEED_IN = A5;
  57. const uint8_t DIRECTION_SET = 8;  // change the effect
  58. const uint8_t INVERT_SET = 9;     // change the invert
  59.  
  60. const uint8_t SPEED_DEADBAND = 5;
  61. #endif // USE_UI_CONTROL
  62.  
  63. uint8_t scrollSpeed = 25;    // default frame delay value
  64. textEffect_t scrollEffect = PA_SCROLL_LEFT;
  65. textPosition_t scrollAlign = PA_LEFT;
  66. uint16_t scrollPause = 2000; // in milliseconds
  67.  
  68. // Global message buffers shared by Serial and Scrolling functions
  69. #define BUF_SIZE    75
  70. char curMessage[BUF_SIZE] = { "" };
  71. char newMessage[BUF_SIZE] = { "Hello! Enter new message?" };
  72. bool newMessageAvailable = true;
  73.  
  74. #if USE_UI_CONTROL
  75.  
  76. MD_UISwitch_Digital uiDirection(DIRECTION_SET);
  77. MD_UISwitch_Digital uiInvert(INVERT_SET);
  78.  
  79. void doUI(void)
  80. {
  81.   // set the speed if it has changed
  82.   {
  83.     int16_t speed = map(analogRead(SPEED_IN), 0, 1023, 10, 150);
  84.  
  85.     if ((speed >= ((int16_t)P.getSpeed() + SPEED_DEADBAND)) ||
  86.       (speed <= ((int16_t)P.getSpeed() - SPEED_DEADBAND)))
  87.     {
  88.       P.setSpeed(speed);
  89.       scrollSpeed = speed;
  90.       PRINT("\nChanged speed to ", P.getSpeed());
  91.     }
  92.   }
  93.  
  94.   if (uiDirection.read() == MD_UISwitch::KEY_PRESS) // SCROLL DIRECTION
  95.   {
  96.     PRINTS("\nChanging scroll direction");
  97.     scrollEffect = (scrollEffect == PA_SCROLL_LEFT ? PA_SCROLL_RIGHT : PA_SCROLL_LEFT);
  98.     P.setTextEffect(scrollEffect, scrollEffect);
  99.     P.displayClear();
  100.     P.displayReset();
  101.   }
  102.  
  103.   if (uiInvert.read() == MD_UISwitch::KEY_PRESS)  // INVERT MODE
  104.   {
  105.     PRINTS("\nChanging invert mode");
  106.     P.setInvert(!P.getInvert());
  107.   }
  108. }
  109. #endif // USE_UI_CONTROL
  110.  
  111. void readSerial(void)
  112. {
  113.   static char *cp = newMessage;
  114.  
  115.   while (Serial.available())
  116.   {
  117.     *cp = (char)Serial.read();
  118.     if ((*cp == '\n') || (cp - newMessage >= BUF_SIZE-2)) // end of message character or full buffer
  119.     {
  120.       *cp = '\0'; // end the string
  121.       // restart the index for next filling spree and flag we have a message waiting
  122.       cp = newMessage;
  123.       newMessageAvailable = true;
  124.     }
  125.     else  // move char pointer to next position
  126.       cp++;
  127.   }
  128. }
  129.  
  130. void setup()
  131. {
  132.   Serial.begin(57600);
  133.   Serial.print("\n[Parola Scrolling Display]\nType a message for the scrolling display\nEnd message line with a newline");
  134.  
  135. #if USE_UI_CONTROL
  136.   uiDirection.begin();
  137.   uiInvert.begin();
  138.   pinMode(SPEED_IN, INPUT);
  139.  
  140.   doUI();
  141. #endif // USE_UI_CONTROL
  142.  
  143.   P.begin();
  144.   P.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);
  145. }
  146.  
  147. void loop()
  148. {
  149. #if USE_UI_CONTROL
  150.   doUI();
  151. #endif // USE_UI_CONTROL
  152.  
  153.   if (P.displayAnimate())
  154.   {
  155.     if (newMessageAvailable)
  156.     {
  157.       strcpy(curMessage, newMessage);
  158.       newMessageAvailable = false;
  159.     }
  160.     P.displayReset();
  161.   }
  162.   readSerial();
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement