files renamed, start with HMI
This commit is contained in:
121
src/hmi.cpp
Normal file
121
src/hmi.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user