From 45fc80911d32f6a840d0f38394829a667a2fb24b Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 15 Feb 2016 13:53:21 +0100 Subject: [PATCH] millis, extend switch behaviour --- src/engine.cpp | 12 +++++ src/engine.h | 2 +- src/hmi.cpp | 131 +++++++++++++++++++++++++++++-------------------- 3 files changed, 90 insertions(+), 55 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 8de95ec..f480c0b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -21,6 +21,9 @@ const uint32_t STEP_100ms = 1000; volatile uint32_t onTime = STEP_100ms * 1; // 1000 = 100ms volatile uint32_t offTime = 0; +volatile uint32_t millis = 0; +const uint32_t DIVIDER_MILLIS = 20; + interrupt (TIMER0_A0_VECTOR) timer0a0_isr(void) { static uint8_t state = 0; static uint32_t timestamp = 0; @@ -34,6 +37,12 @@ interrupt (TIMER0_A0_VECTOR) timer0a0_isr(void) { P1OUT |= BIT7; } + static uint16_t divCntMillis = 0; + divCntMillis++; + if (divCntMillis >= DIVIDER_MILLIS) { + divCntMillis = 0; + millis++; + } switch (state) { case 0: @@ -102,4 +111,7 @@ void engineDecOnTime() { } } +uint32_t getMillis() { + return millis; +} diff --git a/src/engine.h b/src/engine.h index 3208b87..723ef90 100644 --- a/src/engine.h +++ b/src/engine.h @@ -17,7 +17,7 @@ void engineIncOffTime(); void engineIncOnTime(); void engineDecOffTime(); void engineDecOnTime(); - +uint32_t getMillis(); diff --git a/src/hmi.cpp b/src/hmi.cpp index 599e0cc..75391da 100644 --- a/src/hmi.cpp +++ b/src/hmi.cpp @@ -6,83 +6,94 @@ #include "debug.h" +const uint32_t LOWER_SHORT_TIME = 100; +const uint32_t UPPER_SHORT_TIME = 1000; + + class SwitchPort { public: - SwitchPort(uint16_t bit, uint16_t inRegister) : m_bit(bit), m_state(0), m_cnt(0), m_inRegister(inRegister) { + enum e_switchResult { SWITCH_IDLE, SWITCH_SHORT, SWITCH_LONG }; + SwitchPort() : m_state(0), m_cnt(0) { }; - bool get() { - bool result = false; - bool inValue = false; - switch (m_inRegister) { - case 1: - inValue = P1IN & m_bit; - break; - case 2: - inValue = P2IN & m_bit; - break; - } + virtual bool getSwitchState() = 0; + e_switchResult get() { + e_switchResult result = SWITCH_IDLE; + bool inValue = getSwitchState(); switch (m_state) { - case 0: - - if (inValue) { - m_cnt = 0; - } else { - m_cnt++; - } - if (m_cnt > 100) { - m_state = 1; - m_cnt = 0; - } - break; - case 1: - result = true; - m_state = 2; - break; - case 2: - if (inValue) { - m_cnt++; - } else { - m_cnt = 0; - } - if (m_cnt > 20) { - m_state = 0; - m_cnt = 0; - } - break; - default: - m_state = 0; - break; + STATE_IDLE: + if (inValue == false) { + m_state = STATE_BUTTON_PRESSED; + m_timestamp = getMillis(); + } + break; + STATE_BUTTON_PRESSED: + if (inValue == true) { + m_state = STATE_BUTTON_RELEASED; + } + break; + STATE_BUTTON_RELEASED: + uint32_t duration = m_timestamp - getMillis(); + if ((duration => LOWER_SHORT_TIME) && (duration <= UPPER_SHORT_TIME)) { + m_state = STATE_SHORT; + } else if (duration > UPPER_SHORT_TIME) { + m_state = STATE_LONG; + } else { + m_state = STATE_IDLE; + } + break; + STATE_SHORT: + result = SWITCH_SHORT; + m_state = STATE_IDLE; + break; + STATE_LONG: + result = SWITCH_LONG; + m_state = STATE_IDLE; + break; + default: + m_state = STATE_IDLE; + break; } + return result; }; private: - uint16_t m_bit; - uint8_t m_state; - uint16_t m_cnt; - uint8_t m_inRegister; + enum e_state { STATE_IDLE, STATE_BUTTON_PRESSED, STATE_BUTTON_RELEASED, STATE_SHORT, STATE_LONG }; + e_state m_state; + uint32_t m_timestamp; }; class SwitchPort2 : public SwitchPort { public: - SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) { + SwitchPort2(uint16_t bit) : SwitchPort() { P2REN |= bit; P2DIR &= ~bit; P2SEL &= ~bit; }; + virtual bool getSwitchState() { + return P2IN & bit; + }; }; class SwitchPort1 : public SwitchPort { public: - SwitchPort1(uint16_t bit) : SwitchPort(bit, 1) { + SwitchPort1(uint16_t bit) : SwitchPort() { P1REN |= bit; P1DIR &= ~bit; P1SEL &= ~bit; }; + virtual bool getSwitchState() { + return P1IN & bit; + }; }; -SwitchPort intensitySwitch = SwitchPort2(BIT1); -SwitchPort onTimeSwitch = SwitchPort2(BIT2); -SwitchPort offTimeSwitch = SwitchPort2(BIT3); +SwitchPort2 rawIntensitySwitch = SwitchPort2(BIT1); +&SwitchPort intensitySwitch = rawIntensitySwitch; + +SwitchPort2 rawOnTimeSwitch = SwitchPort2(BIT2); +&SwitchPort onTimeSwitch = rawOnTimeSwitch; + +SwitchPort2 rawOffTimeSwitch = SwitchPort2(BIT3); +&SwitchPort offTimeSwitch = rawOffTimeSwitch; //SwitchPort modeSwitch = SwitchPort2(BIT4); int16_t intensity = 0; @@ -100,20 +111,32 @@ void hmiInit() { } void hmiExec() { - if (intensitySwitch.get()) { + if (intensitySwitch.get() == SwitchPort::SWITCH_SHORT) { intensity += intensityStep; if (intensity > maxIntensity) { intensity = maxIntensity; } debugWrite(intensity); engineSetPwmValue(intensity); + } else if (intensitySwitch.get() == SwitchPort::SWITCH_LONG) { + intensity -= intensityStep; + if (intensity < 0) { + intensity = 0; + } + debugWrite(intensity); + engineSetPwmValue(intensity); } - if (onTimeSwitch.get()) { + + if (onTimeSwitch.get() == SwitchPort::SWITCH_SHORT) { engineIncOnTime(); + } else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) { + engineDecOnTime(); } - if (offTimeSwitch.get()) { + if (offTimeSwitch.get() == SwitchPort::SWITCH_SHORT) { engineIncOffTime(); + } else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) { + engineDecOffTime(); } }