thaufas

Untitled

Apr 4th, 2022
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Circuit Playground Express - Rotating Rainbow                             //
  3. //                                                                           //
  4. // Author: thaufas                                                           //
  5. //                                                                           //
  6. // Novel Concept: Only uses a delay() during setup, which permits more       //
  7. //                efficient processor usage. With this approach              //
  8. //                other processes can run without being blocked.             //
  9. ///////////////////////////////////////////////////////////////////////////////
  10.  
  11.  
  12. #include <Adafruit_CircuitPlayground.h>
  13.  
  14. const uint16_t BAUDRATE = 57600;                                                 // Set the serial communication BAUD rate
  15. const long SERIAL_CONFIG = SERIAL_8N1;                                           // Set the serial configuration - default is SERIAL_8N1: 8 data bits, no parity, 1 stop bit
  16. const uint16_t LED_BRIGHTNESS = 128;                                             // RANGE: 0-255, Higher is brighter
  17. const uint16_t NUM_PIXELS = 10;                                                  // RANGE: 0-9
  18. const uint16_t DISPLAYOFFTIME = 0;                                               // milliseconds: Amount of time to turn LED off
  19. const uint16_t DISPLAYONTIME = 50;                                               // milliseconds: Amount of time to turn LED on
  20. const uint16_t INTERPIXEL_UPDATE_DELAY = 0;                                      // milliseconds: Time between initial lighting of pixels
  21.  
  22. ///////////////////////////////////////////////////////////////////////////////
  23. //       Define the class that will control the pixels and eliminate the     //
  24. //       the need for delays by monitoring and updating pixel state.         //
  25. //       This approach enables higher utilization of the microprocessor.     //
  26. //       Although the system is not truly multi-threaded, so long as         //
  27. //       no single task monopolizes the CPU, to the end user, the system     //
  28. //       will appear to be multi-threaded.                                   //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. class Pixel {
  31.   private:
  32.     uint8_t  _pixel_id;
  33.     uint8_t  _LEDState;
  34.     uint32_t _lastUpdateTime;
  35.     uint32_t _timeIntervalOn;
  36.     uint32_t _timeIntervalOff;
  37.     uint32_t _nextTimeScheduledOff;
  38.     uint32_t _nextTimeScheduledOn;
  39.     uint32_t _colorValue;
  40.  
  41.   public:
  42.     Pixel(uint8_t pixel_id = 0, uint32_t colorValue = 0x00FF00, uint32_t timeIntervalOn = 750, uint32_t timeIntervalOff = 500, uint8_t LEDState = LOW) :
  43.       _pixel_id(pixel_id), _colorValue(colorValue), _timeIntervalOn(timeIntervalOn), _timeIntervalOff(timeIntervalOff), _LEDState(LEDState) {
  44.       _lastUpdateTime = millis();
  45.  
  46.       if (_LEDState == HIGH ) {
  47.         _nextTimeScheduledOff = _lastUpdateTime + _timeIntervalOn;
  48.         _nextTimeScheduledOn = _nextTimeScheduledOff + _timeIntervalOff;
  49.       } else {
  50.         _nextTimeScheduledOn = _lastUpdateTime + _timeIntervalOff;
  51.         _nextTimeScheduledOff = _nextTimeScheduledOn + _timeIntervalOn;
  52.       }
  53.  
  54.       //Update();
  55.     }
  56.  
  57.     bool Update() {
  58.       if (_LEDState == HIGH && millis() > _nextTimeScheduledOff) {
  59.         CircuitPlayground.setPixelColor(_pixel_id, 0x000000);
  60.         _LEDState = LOW;
  61.         _lastUpdateTime = millis();
  62.         _nextTimeScheduledOn = _lastUpdateTime + _timeIntervalOff;
  63.         return (true);                                                           // Return 'true' if updates were required.
  64.       } else if (_LEDState == LOW && millis() > _nextTimeScheduledOff) {
  65.         CircuitPlayground.setPixelColor(_pixel_id, _colorValue);
  66.         _LEDState = HIGH;
  67.         _lastUpdateTime = millis();
  68.         _nextTimeScheduledOff = _lastUpdateTime + _timeIntervalOn;
  69.         return (true);                                                           // Return 'true' if updates were required.
  70.       } else {
  71.         return (false);                                                          // Return 'false' if no updates were required.
  72.       }
  73.     }
  74.  
  75.     uint8_t SetPixelID(uint8_t pixel_id) {
  76.       return (_pixel_id = pixel_id);
  77.     }
  78.  
  79.     uint8_t GetPixelID() {
  80.       return (_pixel_id);
  81.     }
  82.  
  83.     uint32_t GetLastUpdateTime() {
  84.       return (_lastUpdateTime);
  85.     }
  86.  
  87.     uint32_t SetColorValue(uint32_t colorValue) {
  88.       return (_colorValue = colorValue);
  89.     }
  90.  
  91.     uint32_t GetColorValue() {
  92.       return (_colorValue);
  93.     }
  94.  
  95.     uint32_t SetTimeIntervalOn(uint32_t timeIntervalOn) {
  96.       return (_timeIntervalOn = timeIntervalOn);
  97.     }
  98.  
  99.     uint32_t GetTimeIntervalOn() {
  100.       return (_timeIntervalOn);
  101.     }
  102.  
  103.     uint32_t SetTimeIntervalOff(uint32_t timeIntervalOff) {
  104.       return (_timeIntervalOff = timeIntervalOff);
  105.     }
  106.  
  107.     uint32_t GetTimeIntervalOff() {
  108.       return (_timeIntervalOff);
  109.     }
  110.  
  111.     uint8_t SetLEDState(uint8_t LEDState) {
  112.       return (_LEDState = LEDState);
  113.     }
  114.  
  115.     uint8_t GetLEDState() {
  116.       return (_LEDState);
  117.     }
  118.  
  119.     uint32_t SetNextScheduledTimeOff(uint32_t nextTimeScheduledOff) {
  120.       _nextTimeScheduledOff = nextTimeScheduledOff;
  121.  
  122.       return (_nextTimeScheduledOff);
  123.     }
  124.  
  125.     uint32_t GetNextScheduledTimeOff() {
  126.       return (_nextTimeScheduledOff);
  127.     }
  128.  
  129.     uint32_t SetNextScheduledTimeOn(uint32_t nextTimeScheduledOn) {
  130.       return (_nextTimeScheduledOn = nextTimeScheduledOn);
  131.     }
  132.  
  133.     uint32_t GetNextScheduledTimeOn() {
  134.       return (_nextTimeScheduledOn);
  135.     }
  136. };                                                                               // End class Pixel
  137.  
  138. void PrintDiagInfoToSerial(Pixel &P) {
  139.   Serial.print("[");
  140.   Serial.print(P.GetPixelID());
  141.   Serial.print("]");
  142.   Serial.print("\t");
  143.   Serial.print("_LEDState==");
  144.   Serial.print(P.GetLEDState());
  145.   Serial.print("\t");
  146.   Serial.print("millis()==");
  147.   Serial.print(millis());
  148.   Serial.print("\t");
  149.   Serial.print("_timeIntervalOn==");
  150.   Serial.print(P.GetTimeIntervalOn());
  151.   Serial.print("\t");
  152.   Serial.print("_timeIntervalOff==");
  153.   Serial.print(P.GetTimeIntervalOff());
  154.   Serial.print("\t");
  155.   Serial.print("P.GetNextScheduledTimeOn()==");
  156.   Serial.print(P.GetNextScheduledTimeOn());
  157.   Serial.print("\t");
  158.   Serial.print("P.GetNextScheduledTimeOff()==");
  159.   Serial.print(P.GetNextScheduledTimeOff());
  160.   Serial.print("\t");
  161.   Serial.print("Color==");
  162.   Serial.println(P.GetColorValue());
  163. }
  164.  
  165. static uint16_t colorIndexCtr;
  166. static uint32_t colorValueArr[] = {
  167.   11815680, 6919680, 2023680, 53805, 34680,
  168.   15555, 983280, 5898405, 10813530, 15728655
  169. };
  170.  
  171. Pixel Pixels[NUM_PIXELS];                                                        // Declare the pixel array
  172.  
  173. void setup() {
  174.   Serial.begin(BAUDRATE, SERIAL_CONFIG);
  175.   CircuitPlayground.begin(LED_BRIGHTNESS);
  176.   CircuitPlayground.clearPixels();
  177.  
  178.   for (int i = 0; i < NUM_PIXELS; ++i) {
  179.     Pixels[i].SetPixelID(i);
  180.     Pixels[i].SetColorValue(colorValueArr[i]);
  181.     Pixels[i].SetTimeIntervalOn(DISPLAYONTIME);
  182.     Pixels[i].SetTimeIntervalOff(DISPLAYOFFTIME);
  183.     Pixels[i].Update();
  184.     delay(INTERPIXEL_UPDATE_DELAY);
  185.   }
  186.  
  187. }
  188.  
  189. void loop()
  190. {
  191.   static bool LEDWhiteState = false;
  192.   static uint32_t lastUpdate = millis(), currTime;
  193.  
  194.   currTime = millis();
  195.  
  196.   if (currTime > lastUpdate + DISPLAYONTIME) {
  197.     lastUpdate = currTime;
  198.     ++colorIndexCtr;
  199.     for (int i = 0; i < NUM_PIXELS; ++i) {
  200.       Pixels[i].SetColorValue(colorValueArr[(i + colorIndexCtr) % NUM_PIXELS]);
  201.     }
  202.   }
  203.  
  204.   for (int i = 0; i < NUM_PIXELS; ++i)
  205.   {
  206.     Pixels[i].Update();
  207.  
  208.     if (CircuitPlayground.rightButton())
  209.     {
  210.       CircuitPlayground.setPixelColor(i, 0xFFFFFF);
  211.     }
  212.  
  213.     if (CircuitPlayground.leftButton())
  214.     {
  215.       CircuitPlayground.setPixelColor(i, 0x0000FF);
  216.     }
  217.  
  218.     if (CircuitPlayground.slideSwitch())
  219.     {
  220.       PrintDiagInfoToSerial(Pixels[i]);
  221.     } else
  222.     {
  223.       PrintDiagInfoToSerial(CircuitPlayground);
  224.     }
  225.   }
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment