enhering

Blinker

Sep 4th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.70 KB | None | 0 0
  1. // Part of https://bitbucket.org/enhering/yauvc
  2. // Project: https://hackaday.io/project/11724-yauvec-yet-another-unmanned-vehicle-controller
  3.  
  4.  
  5. int FreeRAM () {
  6.   extern int __heap_start, *__brkval;
  7.   int v;
  8.   return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  9. }
  10.  
  11. #ifndef BLINK_H
  12. #define BLINK_H
  13.  
  14.   #define NORMAL_ON_COUNT 5000
  15.   #define NORMAL_OFF_COUNT 5000
  16.   #define ATTENTION_ON_COUNT  2 * NORMAL_ON_COUNT
  17.   #define ATTENTION_OFF_COUNT 2 * NORMAL_OFF_COUNT
  18.  
  19.   class Blink {
  20.     public:
  21.       Blink();
  22.       ~Blink();
  23.  
  24.       void SetLEDGPIO(uint8_t nPort) { m_nLedPort = nPort; PrepareLEDPort(); }
  25.  
  26.       void SetLEDOnCount(uint32_t nCount)       { m_nChosenOnCount = nCount; }
  27.       void SetLEDOffCount(uint32_t nCount)      { m_nChosenOffCount = nCount; }
  28.       void SetAttentionLEDOnCount(uint32_t nCount)  { m_nChosenAttentionOnCount = nCount; }
  29.       void SetAttentionLEDOffCount(uint32_t nCount) { m_nChosenAttentionOffCount = nCount; }
  30.  
  31.       void PrepareLEDPort();
  32.       void TurnLEDOn();
  33.       void TurnLEDOff();
  34.  
  35.       void ManageLED();
  36.      
  37.       void BlinkLED();
  38.       void StopLED();
  39.       void AttentionBlinkLED(uint8_t);
  40.       void RepetitiveAttentionBlinkLED(uint8_t);
  41.       void StopRepetitiveAttention();
  42.  
  43.     private:
  44.       uint8_t m_nLedPort;
  45.  
  46.       volatile bool m_bBlinking;
  47.       volatile bool m_bAttention;
  48.       volatile bool m_bAttentionRepeat;
  49.       volatile bool m_bAttentionDisable;
  50.       volatile bool m_bPeriodComplete;
  51.  
  52.       volatile uint8_t m_nNumBlinks;
  53.       volatile uint8_t m_nAttentionCounter;
  54.       volatile uint8_t m_nAttentionDisableCounter;
  55.  
  56.       volatile long m_nCounter;
  57.       volatile bool m_bLEDisOn;
  58.       volatile long m_nLedOffCount;
  59.       volatile long m_nLedOnCount;
  60.       volatile long m_nBlinkCount;
  61.  
  62.       volatile long m_nChosenOnCount, m_nChosenOffCount, m_nChosenAttentionOnCount, m_nChosenAttentionOffCount;
  63.   };
  64.  
  65. #endif
  66.  
  67. Blink::Blink() {
  68.   m_nLedPort = 7;
  69.  
  70.   m_nChosenOnCount = NORMAL_ON_COUNT;
  71.   m_nChosenOffCount = NORMAL_OFF_COUNT;
  72.   m_nChosenAttentionOnCount = ATTENTION_ON_COUNT;
  73.   m_nChosenAttentionOffCount = ATTENTION_OFF_COUNT;
  74.  
  75.   m_nLedOffCount = NORMAL_OFF_COUNT;
  76.   m_nLedOnCount  = NORMAL_ON_COUNT;
  77.  
  78.   m_bBlinking = false;
  79.   m_bAttention = false;
  80.   m_bAttentionRepeat = false;
  81.   m_bAttentionDisable = false;
  82.  
  83.   m_nAttentionDisableCounter = 0;
  84.   m_nAttentionCounter = 0;
  85.   m_nCounter = 0;
  86. }
  87.  
  88. Blink::~Blink() {
  89.  
  90. }
  91.  
  92. void Blink::PrepareLEDPort() {
  93.   switch (m_nLedPort) {
  94.     case 1:  GPIO1_IS_OUTPUT; break;
  95.     case 2:  GPIO2_IS_OUTPUT; break;
  96.     case 3:  GPIO3_IS_OUTPUT; break;
  97.     case 4:  GPIO4_IS_OUTPUT; break;
  98.     case 5:  GPIO5_IS_OUTPUT; break;
  99.     case 6:  GPIO6_IS_OUTPUT; break;
  100.     case 7:  GPIO7_IS_OUTPUT; break;
  101.     case 8:  GPIO8_IS_OUTPUT; break;
  102.     case 9:  GPIO9_IS_OUTPUT; break;
  103.     case 10: GPIO10_IS_OUTPUT; break;
  104.     case 11: GPIO11_IS_OUTPUT; break;
  105.     default: GPIO7_IS_OUTPUT; break;
  106.   }
  107. }
  108.  
  109. void Blink::TurnLEDOn() {
  110.   switch (m_nLedPort) {
  111.     case 1:  GPIO1_HIGH; break;
  112.     case 2:  GPIO2_HIGH; break;
  113.     case 3:  GPIO3_HIGH; break;
  114.     case 4:  GPIO4_HIGH; break;
  115.     case 5:  GPIO5_HIGH; break;
  116.     case 6:  GPIO6_HIGH; break;
  117.     case 7:  GPIO7_HIGH; break;
  118.     case 8:  GPIO8_HIGH; break;
  119.     case 9:  GPIO9_HIGH; break;
  120.     case 10: GPIO10_HIGH; break;
  121.     case 11: GPIO11_HIGH; break;
  122.     default: GPIO7_HIGH; break;
  123.   }
  124.  
  125.   m_bLEDisOn = true;
  126. }
  127.  
  128. void Blink::TurnLEDOff() {
  129.   switch (m_nLedPort) {
  130.     case 1:  GPIO1_LOW; break;
  131.     case 2:  GPIO2_LOW; break;
  132.     case 3:  GPIO3_LOW; break;
  133.     case 4:  GPIO4_LOW; break;
  134.     case 5:  GPIO5_LOW; break;
  135.     case 6:  GPIO6_LOW; break;
  136.     case 7:  GPIO7_LOW; break;
  137.     case 8:  GPIO8_LOW; break;
  138.     case 9:  GPIO9_LOW; break;
  139.     case 10: GPIO10_LOW; break;
  140.     case 11: GPIO11_LOW; break;
  141.     default: GPIO7_LOW; break;
  142.   }
  143.  
  144.   m_bLEDisOn = false;
  145. }
  146.  
  147. void Blink::ManageLED() {
  148.   m_nCounter++;
  149.  
  150.   if (m_bBlinking) {
  151.  
  152.     if (m_bLEDisOn) {
  153.       if (m_nCounter > m_nLedOnCount) {
  154.         TurnLEDOff();
  155.         m_nCounter = 0;
  156.         m_bPeriodComplete = false;
  157.       }
  158.     }
  159.     else {
  160.       if (m_nCounter > m_nLedOffCount) {
  161.         TurnLEDOn();
  162.         m_nCounter = 0;
  163.         m_bPeriodComplete = true;
  164.       }
  165.     }
  166.  
  167.  
  168.     if (m_bAttention) {
  169.       if (m_bPeriodComplete) {
  170.         m_bPeriodComplete = false;
  171.         m_nAttentionCounter--;
  172.       }
  173.  
  174.       if (m_nAttentionCounter == 0) {
  175.         m_bAttention = false;
  176.         m_bAttentionDisable = true;
  177.         m_nAttentionDisableCounter = m_nNumBlinks;
  178.  
  179.         m_nLedOnCount = m_nChosenOnCount;
  180.         m_nLedOffCount = m_nChosenOffCount;
  181.       }
  182.     }
  183.  
  184.     if (m_bAttentionDisable) {
  185.       if (m_bPeriodComplete) {
  186.         m_bPeriodComplete = false;
  187.         m_nAttentionDisableCounter--;
  188.       }
  189.  
  190.       if (m_nAttentionDisableCounter == 0) {
  191.         m_bAttentionDisable = false;
  192.  
  193.         if (m_bAttentionRepeat) {
  194.           m_bAttention = true;
  195.           m_nAttentionCounter = m_nNumBlinks;
  196.           m_nLedOnCount = m_nChosenAttentionOnCount;
  197.           m_nLedOffCount = m_nChosenAttentionOffCount;
  198.         }
  199.         else {
  200.           m_bAttention = false;
  201.           m_nLedOnCount = m_nChosenOnCount;
  202.           m_nLedOffCount = m_nChosenOffCount;
  203.         }
  204.  
  205.       }
  206.     }
  207.   }
  208. }
  209.  
  210. void Blink::BlinkLED() {
  211.   m_bBlinking = true;
  212.   m_bAttention = false;
  213.   m_bAttentionRepeat = false;
  214.   m_nLedOnCount  = m_nChosenOnCount;
  215.   m_nLedOffCount = m_nChosenOffCount;
  216. }
  217.  
  218. void Blink::StopLED() {
  219.   m_bBlinking = false;
  220.   m_bAttention = false;
  221.   m_bAttentionRepeat = false;
  222. }
  223.  
  224. void Blink::AttentionBlinkLED(uint8_t nTimes) {
  225.   m_bAttention = true;
  226.   m_bAttentionRepeat = false;
  227.   m_nNumBlinks = nTimes + 1;
  228.   m_nAttentionCounter = m_nNumBlinks;
  229.   m_nLedOnCount  = m_nChosenAttentionOnCount;
  230.   m_nLedOffCount = m_nChosenAttentionOffCount;
  231. }
  232.  
  233. void Blink::RepetitiveAttentionBlinkLED(uint8_t nTimes) {
  234.   m_bAttention = true;
  235.   m_bAttentionRepeat = true;
  236.   m_nNumBlinks = nTimes;
  237.   m_nAttentionCounter = m_nNumBlinks;
  238.   m_nLedOnCount  = m_nChosenAttentionOnCount;
  239.   m_nLedOffCount = m_nChosenAttentionOffCount;
  240. }
  241.  
  242. void Blink::StopRepetitiveAttention() {
  243.   m_bAttentionRepeat = false;
  244. }
  245.  
  246. EXAMPLE USAGE:
  247.  
  248. Blink m_cBlink;
  249.  
  250. int main(void) {
  251.  
  252.   long nCount = 0;
  253.     m_cBlink.SetLEDGPIO(9);
  254.     m_cBlink.SetLEDOnCount(1000);
  255.     m_cBlink.SetLEDOffCount(5000);
  256.     m_cBlink.SetAttentionLEDOnCount(10000);
  257.     m_cBlink.SetAttentionLEDOffCount(20000);
  258.  
  259.     m_cBlink.BlinkLED();
  260.  
  261.     while (1) {
  262.       nCount++;
  263.  
  264.       int nRAM = FreeRAM();
  265.  
  266.       if (nRAM < 2000) {
  267.         m_cBlink.RepetitiveAttentionBlinkLED(3);  
  268.       }
  269.      
  270.       m_cBlink.ManageLED();
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment