enhering

Better blinker class

Sep 11th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #ifndef BLINK_H_LABVANT
  2. #define BLINK_H_LABVANT
  3.  
  4.   #define NORMAL_ON_COUNT 5000
  5.   #define NORMAL_OFF_COUNT 5000
  6.  
  7.   #include "AVRBase.h"
  8.  
  9.   class Blink : public AVRBase {
  10.     public:
  11.       Blink();
  12.       ~Blink();
  13.  
  14.       void SetLEDGPIO(uint8_t nPort) { m_nLedPort = nPort; PrepareLEDPort(); }
  15.  
  16.       void SetLEDOnCount(uint32_t nCount)       { m_nChosenOnCount = nCount; }
  17.       void SetLEDOffCount(uint32_t nCount)      { m_nChosenOffCount = nCount; }
  18.  
  19.       void PrepareLEDPort();
  20.       void TurnLEDOn();
  21.       void TurnLEDOff();
  22.  
  23.       void ManageLED();
  24.      
  25.       void BlinkLED();
  26.       void StopLED();
  27.       void AttentionBlinkLED(uint8_t);
  28.  
  29.     private:
  30.       uint8_t m_nLedPort;
  31.  
  32.       volatile bool m_bBlinking;
  33.       volatile bool m_bAttention;
  34.       volatile bool m_bPeriodComplete;
  35.  
  36.       volatile uint8_t m_nNumBlinks;
  37.       volatile uint8_t m_nAttentionCounter;
  38.  
  39.       volatile long m_nCounter;
  40.       volatile bool m_bLEDisOn;
  41.       volatile long m_nLedOffCount;
  42.       volatile long m_nLedOnCount;
  43.       volatile long m_nBlinkCount;
  44.  
  45.       volatile long m_nChosenOnCount, m_nChosenOffCount, m_nChosenAttentionOnCount, m_nChosenAttentionOffCount;
  46.   };
  47.  
  48. #endif
  49.  
  50. #include "Blink.h"
  51.  
  52. Blink::Blink() {
  53.   m_nLedPort = 7;
  54.  
  55.   m_nChosenOnCount = NORMAL_ON_COUNT;
  56.   m_nChosenOffCount = NORMAL_OFF_COUNT;
  57.  
  58.   m_nLedOffCount = NORMAL_OFF_COUNT;
  59.   m_nLedOnCount  = NORMAL_ON_COUNT;
  60.  
  61.   m_bBlinking = false;
  62.   m_bAttention = false;
  63.  
  64.   m_nAttentionCounter = 0;
  65.   m_nCounter = 0;
  66. }
  67.  
  68. Blink::~Blink() {
  69.  
  70. }
  71.  
  72. void Blink::PrepareLEDPort() {
  73.   switch (m_nLedPort) {
  74.     case 1:  GPIO1_IS_OUTPUT; break;
  75.     case 2:  GPIO2_IS_OUTPUT; break;
  76.     case 3:  GPIO3_IS_OUTPUT; break;
  77.     case 4:  GPIO4_IS_OUTPUT; break;
  78.     case 5:  GPIO5_IS_OUTPUT; break;
  79.     case 6:  GPIO6_IS_OUTPUT; break;
  80.     case 7:  GPIO7_IS_OUTPUT; break;
  81.     case 8:  GPIO8_IS_OUTPUT; break;
  82.     case 9:  GPIO9_IS_OUTPUT; break;
  83.     case 10: GPIO10_IS_OUTPUT; break;
  84.     case 11: GPIO11_IS_OUTPUT; break;
  85.     default: GPIO7_IS_OUTPUT; break;
  86.   }
  87. }
  88.  
  89. void Blink::TurnLEDOn() {
  90.   switch (m_nLedPort) {
  91.     case 1:  GPIO1_HIGH; break;
  92.     case 2:  GPIO2_HIGH; break;
  93.     case 3:  GPIO3_HIGH; break;
  94.     case 4:  GPIO4_HIGH; break;
  95.     case 5:  GPIO5_HIGH; break;
  96.     case 6:  GPIO6_HIGH; break;
  97.     case 7:  GPIO7_HIGH; break;
  98.     case 8:  GPIO8_HIGH; break;
  99.     case 9:  GPIO9_HIGH; break;
  100.     case 10: GPIO10_HIGH; break;
  101.     case 11: GPIO11_HIGH; break;
  102.     default: GPIO7_HIGH; break;
  103.   }
  104.  
  105.   m_bLEDisOn = true;
  106. }
  107.  
  108. void Blink::TurnLEDOff() {
  109.   switch (m_nLedPort) {
  110.     case 1:  GPIO1_LOW; break;
  111.     case 2:  GPIO2_LOW; break;
  112.     case 3:  GPIO3_LOW; break;
  113.     case 4:  GPIO4_LOW; break;
  114.     case 5:  GPIO5_LOW; break;
  115.     case 6:  GPIO6_LOW; break;
  116.     case 7:  GPIO7_LOW; break;
  117.     case 8:  GPIO8_LOW; break;
  118.     case 9:  GPIO9_LOW; break;
  119.     case 10: GPIO10_LOW; break;
  120.     case 11: GPIO11_LOW; break;
  121.     default: GPIO7_LOW; break;
  122.   }
  123.  
  124.   m_bLEDisOn = false;
  125. }
  126.  
  127. void Blink::ManageLED() {
  128.   m_nCounter++;
  129.  
  130.   if (m_bBlinking) {
  131.  
  132.     if (m_bLEDisOn) {
  133.       if (m_nCounter > m_nLedOnCount) {
  134.         TurnLEDOff();
  135.         m_nCounter = 0;
  136.         m_bPeriodComplete = true;  
  137.       }
  138.     }
  139.     else {
  140.       if (m_nCounter > m_nLedOffCount) {
  141.         TurnLEDOn();
  142.         m_nCounter = 0;
  143.         m_bPeriodComplete = false;  
  144.       }
  145.     }
  146.  
  147.     if ( m_bAttention ) {
  148.  
  149.       if (m_bPeriodComplete ) {
  150.         m_bPeriodComplete = false;
  151.         m_nAttentionCounter--;
  152.       }
  153.  
  154.       if (m_nAttentionCounter == 0) {
  155.         m_bBlinking = false;
  156.         m_nCounter = 0;
  157.       }
  158.     }
  159.   }
  160.   else {
  161.     if (m_nCounter > (3 * (m_nLedOnCount + m_nLedOffCount))) {
  162.       m_bBlinking = true;
  163.       m_bAttention = true;
  164.       m_nAttentionCounter = m_nNumBlinks;
  165.       m_nCounter = 0;
  166.     }
  167.   }
  168. }
  169.  
  170. void Blink::BlinkLED() {
  171.   m_bBlinking = true;
  172.   m_bAttention = false;
  173.  
  174.   m_nLedOnCount = m_nChosenOnCount;
  175.   m_nLedOffCount = m_nChosenOffCount;
  176. }
  177.  
  178. void Blink::StopLED() {
  179.   m_bBlinking = false;
  180.   m_bAttention = false;
  181. }
  182.  
  183. void Blink::AttentionBlinkLED(uint8_t nTimes) {
  184.   m_bAttention = true;
  185.   m_nNumBlinks = nTimes;
  186.   m_nAttentionCounter = m_nNumBlinks;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment