millis, extend switch behaviour

This commit is contained in:
2016-02-15 13:53:21 +01:00
parent 585c920b34
commit 45fc80911d
3 changed files with 90 additions and 55 deletions

View File

@ -21,6 +21,9 @@ const uint32_t STEP_100ms = 1000;
volatile uint32_t onTime = STEP_100ms * 1; // 1000 = 100ms volatile uint32_t onTime = STEP_100ms * 1; // 1000 = 100ms
volatile uint32_t offTime = 0; volatile uint32_t offTime = 0;
volatile uint32_t millis = 0;
const uint32_t DIVIDER_MILLIS = 20;
interrupt (TIMER0_A0_VECTOR) timer0a0_isr(void) { interrupt (TIMER0_A0_VECTOR) timer0a0_isr(void) {
static uint8_t state = 0; static uint8_t state = 0;
static uint32_t timestamp = 0; static uint32_t timestamp = 0;
@ -34,6 +37,12 @@ interrupt (TIMER0_A0_VECTOR) timer0a0_isr(void) {
P1OUT |= BIT7; P1OUT |= BIT7;
} }
static uint16_t divCntMillis = 0;
divCntMillis++;
if (divCntMillis >= DIVIDER_MILLIS) {
divCntMillis = 0;
millis++;
}
switch (state) { switch (state) {
case 0: case 0:
@ -102,4 +111,7 @@ void engineDecOnTime() {
} }
} }
uint32_t getMillis() {
return millis;
}

View File

@ -17,7 +17,7 @@ void engineIncOffTime();
void engineIncOnTime(); void engineIncOnTime();
void engineDecOffTime(); void engineDecOffTime();
void engineDecOnTime(); void engineDecOnTime();
uint32_t getMillis();

View File

@ -6,83 +6,94 @@
#include "debug.h" #include "debug.h"
const uint32_t LOWER_SHORT_TIME = 100;
const uint32_t UPPER_SHORT_TIME = 1000;
class SwitchPort { class SwitchPort {
public: 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() { virtual bool getSwitchState() = 0;
bool result = false; e_switchResult get() {
bool inValue = false; e_switchResult result = SWITCH_IDLE;
switch (m_inRegister) { bool inValue = getSwitchState();
case 1:
inValue = P1IN & m_bit;
break;
case 2:
inValue = P2IN & m_bit;
break;
}
switch (m_state) { switch (m_state) {
case 0: STATE_IDLE:
if (inValue == false) {
if (inValue) { m_state = STATE_BUTTON_PRESSED;
m_cnt = 0; m_timestamp = getMillis();
} else {
m_cnt++;
}
if (m_cnt > 100) {
m_state = 1;
m_cnt = 0;
} }
break; break;
case 1: STATE_BUTTON_PRESSED:
result = true; if (inValue == true) {
m_state = 2; m_state = STATE_BUTTON_RELEASED;
}
break; break;
case 2: STATE_BUTTON_RELEASED:
if (inValue) { uint32_t duration = m_timestamp - getMillis();
m_cnt++; if ((duration => LOWER_SHORT_TIME) && (duration <= UPPER_SHORT_TIME)) {
m_state = STATE_SHORT;
} else if (duration > UPPER_SHORT_TIME) {
m_state = STATE_LONG;
} else { } else {
m_cnt = 0; m_state = STATE_IDLE;
}
if (m_cnt > 20) {
m_state = 0;
m_cnt = 0;
} }
break; break;
STATE_SHORT:
result = SWITCH_SHORT;
m_state = STATE_IDLE;
break;
STATE_LONG:
result = SWITCH_LONG;
m_state = STATE_IDLE;
break;
default: default:
m_state = 0; m_state = STATE_IDLE;
break; break;
} }
return result; return result;
}; };
private: private:
uint16_t m_bit; enum e_state { STATE_IDLE, STATE_BUTTON_PRESSED, STATE_BUTTON_RELEASED, STATE_SHORT, STATE_LONG };
uint8_t m_state; e_state m_state;
uint16_t m_cnt; uint32_t m_timestamp;
uint8_t m_inRegister;
}; };
class SwitchPort2 : public SwitchPort { class SwitchPort2 : public SwitchPort {
public: public:
SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) { SwitchPort2(uint16_t bit) : SwitchPort() {
P2REN |= bit; P2REN |= bit;
P2DIR &= ~bit; P2DIR &= ~bit;
P2SEL &= ~bit; P2SEL &= ~bit;
}; };
virtual bool getSwitchState() {
return P2IN & bit;
};
}; };
class SwitchPort1 : public SwitchPort { class SwitchPort1 : public SwitchPort {
public: public:
SwitchPort1(uint16_t bit) : SwitchPort(bit, 1) { SwitchPort1(uint16_t bit) : SwitchPort() {
P1REN |= bit; P1REN |= bit;
P1DIR &= ~bit; P1DIR &= ~bit;
P1SEL &= ~bit; P1SEL &= ~bit;
}; };
virtual bool getSwitchState() {
return P1IN & bit;
};
}; };
SwitchPort intensitySwitch = SwitchPort2(BIT1); SwitchPort2 rawIntensitySwitch = SwitchPort2(BIT1);
SwitchPort onTimeSwitch = SwitchPort2(BIT2); &SwitchPort intensitySwitch = rawIntensitySwitch;
SwitchPort offTimeSwitch = SwitchPort2(BIT3);
SwitchPort2 rawOnTimeSwitch = SwitchPort2(BIT2);
&SwitchPort onTimeSwitch = rawOnTimeSwitch;
SwitchPort2 rawOffTimeSwitch = SwitchPort2(BIT3);
&SwitchPort offTimeSwitch = rawOffTimeSwitch;
//SwitchPort modeSwitch = SwitchPort2(BIT4); //SwitchPort modeSwitch = SwitchPort2(BIT4);
int16_t intensity = 0; int16_t intensity = 0;
@ -100,20 +111,32 @@ void hmiInit() {
} }
void hmiExec() { void hmiExec() {
if (intensitySwitch.get()) { if (intensitySwitch.get() == SwitchPort::SWITCH_SHORT) {
intensity += intensityStep; intensity += intensityStep;
if (intensity > maxIntensity) { if (intensity > maxIntensity) {
intensity = maxIntensity; intensity = maxIntensity;
} }
debugWrite(intensity); debugWrite(intensity);
engineSetPwmValue(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(); engineIncOnTime();
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
engineDecOnTime();
} }
if (offTimeSwitch.get()) { if (offTimeSwitch.get() == SwitchPort::SWITCH_SHORT) {
engineIncOffTime(); engineIncOffTime();
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
engineDecOffTime();
} }
} }