vib/src/hmi.cpp
2016-01-26 20:23:24 +01:00

137 lines
2.6 KiB
C++

#include <msp430g2553.h>
#include <stdint.h>
#include "hmi.h"
#include "engine.h"
#include "uart.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:
//uartWrite('0');
if (getInValue()) {
m_state = 1;
} else {
m_cnt++;
}
break;
case 1:
//uartWrite('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:
//uartWrite('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() {
//uartWrite('x');
return true;
};
uint8_t m_inRegister;
private:
static const uint16_t SHORT_THRESHOLD;
static const uint16_t LONG_THRESHOLD;
static const uint16_t COOLDOWN_THRESHOLD;
uint16_t m_bit;
uint8_t m_state;
uint16_t m_cnt;
};
const uint16_t SwitchPort::SHORT_THRESHOLD = 100;
const uint16_t SwitchPort::LONG_THRESHOLD = 300;
const uint16_t SwitchPort::COOLDOWN_THRESHOLD = 20;
class SwitchPort2 : public SwitchPort {
public:
SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) {
P2REN |= bit;
P2DIR &= ~bit;
P2SEL &= ~bit;
};
protected:
virtual bool getInValue() {
bool r = P2IN & m_inRegister;
if (! r) uartWrite('-');
return r;
};
};
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) {
bool r = P1IN & m_inRegister;
if (! r) uartWrite('-');
return r;
};
};
SwitchPort intensitySwitch = SwitchPort2(BIT1);
SwitchPort onTimeSwitch = SwitchPort2(BIT2);
SwitchPort offTimeSwitch = SwitchPort2(BIT3);
SwitchPort modeSwitch = SwitchPort2(BIT4);
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);
}
}