Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Part of https://bitbucket.org/enhering/yauvc
- // Project: https://hackaday.io/project/11724-yauvec-yet-another-unmanned-vehicle-controller
- int FreeRAM () {
- extern int __heap_start, *__brkval;
- int v;
- return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
- }
- #ifndef BLINK_H
- #define BLINK_H
- #define NORMAL_ON_COUNT 5000
- #define NORMAL_OFF_COUNT 5000
- #define ATTENTION_ON_COUNT 2 * NORMAL_ON_COUNT
- #define ATTENTION_OFF_COUNT 2 * NORMAL_OFF_COUNT
- class Blink {
- public:
- Blink();
- ~Blink();
- void SetLEDGPIO(uint8_t nPort) { m_nLedPort = nPort; PrepareLEDPort(); }
- void SetLEDOnCount(uint32_t nCount) { m_nChosenOnCount = nCount; }
- void SetLEDOffCount(uint32_t nCount) { m_nChosenOffCount = nCount; }
- void SetAttentionLEDOnCount(uint32_t nCount) { m_nChosenAttentionOnCount = nCount; }
- void SetAttentionLEDOffCount(uint32_t nCount) { m_nChosenAttentionOffCount = nCount; }
- void PrepareLEDPort();
- void TurnLEDOn();
- void TurnLEDOff();
- void ManageLED();
- void BlinkLED();
- void StopLED();
- void AttentionBlinkLED(uint8_t);
- void RepetitiveAttentionBlinkLED(uint8_t);
- void StopRepetitiveAttention();
- private:
- uint8_t m_nLedPort;
- volatile bool m_bBlinking;
- volatile bool m_bAttention;
- volatile bool m_bAttentionRepeat;
- volatile bool m_bAttentionDisable;
- volatile bool m_bPeriodComplete;
- volatile uint8_t m_nNumBlinks;
- volatile uint8_t m_nAttentionCounter;
- volatile uint8_t m_nAttentionDisableCounter;
- volatile long m_nCounter;
- volatile bool m_bLEDisOn;
- volatile long m_nLedOffCount;
- volatile long m_nLedOnCount;
- volatile long m_nBlinkCount;
- volatile long m_nChosenOnCount, m_nChosenOffCount, m_nChosenAttentionOnCount, m_nChosenAttentionOffCount;
- };
- #endif
- Blink::Blink() {
- m_nLedPort = 7;
- m_nChosenOnCount = NORMAL_ON_COUNT;
- m_nChosenOffCount = NORMAL_OFF_COUNT;
- m_nChosenAttentionOnCount = ATTENTION_ON_COUNT;
- m_nChosenAttentionOffCount = ATTENTION_OFF_COUNT;
- m_nLedOffCount = NORMAL_OFF_COUNT;
- m_nLedOnCount = NORMAL_ON_COUNT;
- m_bBlinking = false;
- m_bAttention = false;
- m_bAttentionRepeat = false;
- m_bAttentionDisable = false;
- m_nAttentionDisableCounter = 0;
- m_nAttentionCounter = 0;
- m_nCounter = 0;
- }
- Blink::~Blink() {
- }
- void Blink::PrepareLEDPort() {
- switch (m_nLedPort) {
- case 1: GPIO1_IS_OUTPUT; break;
- case 2: GPIO2_IS_OUTPUT; break;
- case 3: GPIO3_IS_OUTPUT; break;
- case 4: GPIO4_IS_OUTPUT; break;
- case 5: GPIO5_IS_OUTPUT; break;
- case 6: GPIO6_IS_OUTPUT; break;
- case 7: GPIO7_IS_OUTPUT; break;
- case 8: GPIO8_IS_OUTPUT; break;
- case 9: GPIO9_IS_OUTPUT; break;
- case 10: GPIO10_IS_OUTPUT; break;
- case 11: GPIO11_IS_OUTPUT; break;
- default: GPIO7_IS_OUTPUT; break;
- }
- }
- void Blink::TurnLEDOn() {
- switch (m_nLedPort) {
- case 1: GPIO1_HIGH; break;
- case 2: GPIO2_HIGH; break;
- case 3: GPIO3_HIGH; break;
- case 4: GPIO4_HIGH; break;
- case 5: GPIO5_HIGH; break;
- case 6: GPIO6_HIGH; break;
- case 7: GPIO7_HIGH; break;
- case 8: GPIO8_HIGH; break;
- case 9: GPIO9_HIGH; break;
- case 10: GPIO10_HIGH; break;
- case 11: GPIO11_HIGH; break;
- default: GPIO7_HIGH; break;
- }
- m_bLEDisOn = true;
- }
- void Blink::TurnLEDOff() {
- switch (m_nLedPort) {
- case 1: GPIO1_LOW; break;
- case 2: GPIO2_LOW; break;
- case 3: GPIO3_LOW; break;
- case 4: GPIO4_LOW; break;
- case 5: GPIO5_LOW; break;
- case 6: GPIO6_LOW; break;
- case 7: GPIO7_LOW; break;
- case 8: GPIO8_LOW; break;
- case 9: GPIO9_LOW; break;
- case 10: GPIO10_LOW; break;
- case 11: GPIO11_LOW; break;
- default: GPIO7_LOW; break;
- }
- m_bLEDisOn = false;
- }
- void Blink::ManageLED() {
- m_nCounter++;
- if (m_bBlinking) {
- if (m_bLEDisOn) {
- if (m_nCounter > m_nLedOnCount) {
- TurnLEDOff();
- m_nCounter = 0;
- m_bPeriodComplete = false;
- }
- }
- else {
- if (m_nCounter > m_nLedOffCount) {
- TurnLEDOn();
- m_nCounter = 0;
- m_bPeriodComplete = true;
- }
- }
- if (m_bAttention) {
- if (m_bPeriodComplete) {
- m_bPeriodComplete = false;
- m_nAttentionCounter--;
- }
- if (m_nAttentionCounter == 0) {
- m_bAttention = false;
- m_bAttentionDisable = true;
- m_nAttentionDisableCounter = m_nNumBlinks;
- m_nLedOnCount = m_nChosenOnCount;
- m_nLedOffCount = m_nChosenOffCount;
- }
- }
- if (m_bAttentionDisable) {
- if (m_bPeriodComplete) {
- m_bPeriodComplete = false;
- m_nAttentionDisableCounter--;
- }
- if (m_nAttentionDisableCounter == 0) {
- m_bAttentionDisable = false;
- if (m_bAttentionRepeat) {
- m_bAttention = true;
- m_nAttentionCounter = m_nNumBlinks;
- m_nLedOnCount = m_nChosenAttentionOnCount;
- m_nLedOffCount = m_nChosenAttentionOffCount;
- }
- else {
- m_bAttention = false;
- m_nLedOnCount = m_nChosenOnCount;
- m_nLedOffCount = m_nChosenOffCount;
- }
- }
- }
- }
- }
- void Blink::BlinkLED() {
- m_bBlinking = true;
- m_bAttention = false;
- m_bAttentionRepeat = false;
- m_nLedOnCount = m_nChosenOnCount;
- m_nLedOffCount = m_nChosenOffCount;
- }
- void Blink::StopLED() {
- m_bBlinking = false;
- m_bAttention = false;
- m_bAttentionRepeat = false;
- }
- void Blink::AttentionBlinkLED(uint8_t nTimes) {
- m_bAttention = true;
- m_bAttentionRepeat = false;
- m_nNumBlinks = nTimes + 1;
- m_nAttentionCounter = m_nNumBlinks;
- m_nLedOnCount = m_nChosenAttentionOnCount;
- m_nLedOffCount = m_nChosenAttentionOffCount;
- }
- void Blink::RepetitiveAttentionBlinkLED(uint8_t nTimes) {
- m_bAttention = true;
- m_bAttentionRepeat = true;
- m_nNumBlinks = nTimes;
- m_nAttentionCounter = m_nNumBlinks;
- m_nLedOnCount = m_nChosenAttentionOnCount;
- m_nLedOffCount = m_nChosenAttentionOffCount;
- }
- void Blink::StopRepetitiveAttention() {
- m_bAttentionRepeat = false;
- }
- EXAMPLE USAGE:
- Blink m_cBlink;
- int main(void) {
- long nCount = 0;
- m_cBlink.SetLEDGPIO(9);
- m_cBlink.SetLEDOnCount(1000);
- m_cBlink.SetLEDOffCount(5000);
- m_cBlink.SetAttentionLEDOnCount(10000);
- m_cBlink.SetAttentionLEDOffCount(20000);
- m_cBlink.BlinkLED();
- while (1) {
- nCount++;
- int nRAM = FreeRAM();
- if (nRAM < 2000) {
- m_cBlink.RepetitiveAttentionBlinkLED(3);
- }
- m_cBlink.ManageLED();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment