diff --git a/src/time.cpp b/src/engine.cpp similarity index 89% rename from src/time.cpp rename to src/engine.cpp index 6607b26..e7c270e 100644 --- a/src/time.cpp +++ b/src/engine.cpp @@ -8,7 +8,7 @@ #include #include -#include "time.h" +#include "engine.h" volatile uint16_t timestamp = 0; @@ -64,7 +64,7 @@ interrupt (TIMER0_A0_VECTOR) timer0a0_isr(void) { } -void timeInit() { +void engineInit() { timestamp = 0; P1DIR |= BIT0 | BIT1 | BIT6; @@ -88,4 +88,10 @@ uint32_t ticks() { return t; } +uint16_t engineMaxPwmValue() { + return TICK; +} +void engineSetPwmValue(uint16_t val) { + TACCR1 = val; +} diff --git a/src/engine.h b/src/engine.h new file mode 100644 index 0000000..93495b8 --- /dev/null +++ b/src/engine.h @@ -0,0 +1,21 @@ +/* + * time.h + * + * Created on: 17.01.2016 + * Author: wn + */ + +#ifndef ENGINE_H_ +#define ENGINE_H_ + + + +void engineInit(); +uint32_t ticks(); +uint16_t engineMaxPwmValue(); +void engineSetPwmValue(uint16_t val); + + + + +#endif /* ENGINE_H_ */ diff --git a/src/hmi.cpp b/src/hmi.cpp new file mode 100644 index 0000000..bc456d1 --- /dev/null +++ b/src/hmi.cpp @@ -0,0 +1,121 @@ +#include +#include + +#include "hmi.h" +#include "engine.h" + + + + +class SwitchPort { +public: + enum e_Value { e_NIL, e_SHORT, e_LONG }; + SwitchPort(uint16_t bit, uint16_t inRegister) : m_bit(bit), m_state(0), m_cnt(0), m_inRegister(inRegister) { + }; + e_Value get() { + e_Value result = e_NIL; + switch (m_state) { + case 0: + if (getInValue()) { + m_state = 1; + } else { + m_cnt++; + } + break; + case 1: + if (m_cnt > LONG_THRESHOLD) { + result = e_LONG; + m_state = 2; + } else if (m_cnt > SHORT_THRESHOLD) { + result = e_SHORT; + } else { + m_state = 0; + } + m_cnt = 0; + break; + case 2: + if (getInValue()) { + m_cnt++; + } else { + m_cnt = 0; + } + if (m_cnt > COOLDOWN_THRESHOLD) { + m_state = 0; + m_cnt = 0; + } + break; + default: + m_state = 0; + break; + } + return result; + }; +protected: + virtual bool getInValue() =0; + uint8_t m_inRegister; +private: + const uint16_t SHORT_THRESHOLD = 100; + const uint16_t LONG_THRESHOLD = 300; + const uint16_t COOLDOWN_THRESHOLD = 20; + uint16_t m_bit; + uint8_t m_state; + uint16_t m_cnt; +}; + +class SwitchPort2 : public SwitchPort { +public: + SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) { + P2REN |= bit; + P2DIR &= ~bit; + P2SEL &= ~bit; + }; +protected: + virtual bool getInValue() { + return P2IN & m_inRegister; + }; +}; +class SwitchPort1 : public SwitchPort { +public: + SwitchPort1(uint16_t bit) : SwitchPort(bit, 1) { + P1REN |= bit; + P1DIR &= ~bit; + P1SEL &= ~bit; + }; +protected: + virtual bool getInValue(uint8_t bit) { + return P1IN & m_inRegister; + }; +}; +}; + +SwitchPort intensitySwitch = SwitchPort2(BIT0); +SwitchPort onTimeSwitch = SwitchPort2(BIT1); +SwitchPort offTimeSwitch = SwitchPort2(BIT2); +SwitchPort modeSwitch = SwitchPort2(BIT3); + +int16_t intensity = 0; +uint16_t maxIntensity = 0; +uint16_t intensityStep = 0; +const uint16_t intensityStepCnt = 8; + + +void hmiInit() { + maxIntensity = engineMaxPwmValue(); + intensityStep = maxIntensity / intensityStepCnt; +} + +void hmiExec() { + if (SwitchPort::e_SHORT == intensitySwitch.get()) { + intensity += intensityStep; + if (intensity > maxIntensity) { + intensity = maxIntensity; + } + engineSetPwmValue(intensity); + } else if (SwitchPort::e_LONG == intensitySwitch.get()) { + intensity -= intensityStep; + if (intensity < 0) { + intensity = 0; + } + engineSetPwmValue(intensity); + } +} \ No newline at end of file diff --git a/src/hmi.h b/src/hmi.h new file mode 100644 index 0000000..644f7d3 --- /dev/null +++ b/src/hmi.h @@ -0,0 +1,9 @@ +#ifndef HMI_H_ +#define HMI_H_ + + +void hmiInit(); +void hmiExec(); + + +#endif /* HMI_H_ */ diff --git a/src/main.cpp b/src/main.cpp index d6c604c..216e4a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ #include #include -#include "time.h" +#include "engine.h" @@ -18,7 +18,7 @@ int main() { WDTCTL = WDTPW | WDTHOLD; - timeInit(); + engineInit(); __enable_interrupt(); diff --git a/src/switch.cpp b/src/switch.cpp deleted file mode 100644 index 67bec25..0000000 --- a/src/switch.cpp +++ /dev/null @@ -1,25 +0,0 @@ - -/* -void switch_interrupt() { - unsigned long currentEvent = millis(); - - if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) { - lastEvent = currentEvent; - - switchState = true; - } -} - - - -bool getAndResetSwitchState() { - bool res = false; - - noInterrupts(); - res = switchState; - switchState = false; - interrupts(); - - return res; -} -*/ \ No newline at end of file diff --git a/src/time.h b/src/time.h deleted file mode 100644 index 358f5d1..0000000 --- a/src/time.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * time.h - * - * Created on: 17.01.2016 - * Author: wn - */ - -#ifndef TIME_H_ -#define TIME_H_ - - - -void timeInit(); -uint32_t ticks(); - - - - -#endif /* TIME_H_ */