change led implementation
This commit is contained in:
128
src/hmi.cpp
128
src/hmi.cpp
@ -95,7 +95,8 @@ private:
|
|||||||
};
|
};
|
||||||
class SwitchPort1 : public SwitchPort {
|
class SwitchPort1 : public SwitchPort {
|
||||||
public:
|
public:
|
||||||
SwitchPort1(uint16_t bit) : SwitchPort(), m_bit(bit) {
|
SwitchPort1(uint16_t bit) : SwitchPort() {
|
||||||
|
m_bit = bit;
|
||||||
P1REN |= bit;
|
P1REN |= bit;
|
||||||
P1DIR &= ~bit;
|
P1DIR &= ~bit;
|
||||||
P1SEL &= ~bit;
|
P1SEL &= ~bit;
|
||||||
@ -107,58 +108,117 @@ private:
|
|||||||
uint16_t m_bit;
|
uint16_t m_bit;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Led {
|
||||||
|
public:
|
||||||
|
enum e_flashType { FLASH_SHORT, FLASH_LONG };
|
||||||
|
Led() : m_state(STATE_IDLE), m_timeout(0) {
|
||||||
|
}
|
||||||
|
void start(e_flashType ft) {
|
||||||
|
m_state = STATE_START;
|
||||||
|
if (FLASH_LONG == ft) {
|
||||||
|
m_timeout = LED_WAIT_TIME_LONG;
|
||||||
|
} else {
|
||||||
|
m_timeout = LED_WAIT_TIME_SHORT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
virtual void set(bool v) {};
|
||||||
|
void exec() {
|
||||||
|
switch (m_state) {
|
||||||
|
case STATE_IDLE:
|
||||||
|
break;
|
||||||
|
case STATE_START:
|
||||||
|
set(true);
|
||||||
|
m_state = STATE_ON;
|
||||||
|
break;
|
||||||
|
case STATE_ON:
|
||||||
|
if (m_timeout == 0) {
|
||||||
|
m_state = STATE_OFF;
|
||||||
|
}
|
||||||
|
m_timeout--;
|
||||||
|
break;
|
||||||
|
case STATE_OFF:
|
||||||
|
set(false);
|
||||||
|
m_state = STATE_IDLE;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
m_state = STATE_IDLE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
enum e_state { STATE_IDLE, STATE_START, STATE_ON, STATE_OFF };
|
||||||
|
e_state m_state;
|
||||||
|
uint32_t m_timeout;
|
||||||
|
};
|
||||||
|
class Led1 : Led {
|
||||||
|
public:
|
||||||
|
Led1(uint16_t bit) : Led() {
|
||||||
|
m_bit = bit;
|
||||||
|
P1DIR |= bit;
|
||||||
|
P1OUT &= ~bit;
|
||||||
|
}
|
||||||
|
virtual void set(bool v) {
|
||||||
|
if (v) {
|
||||||
|
P1OUT |= m_bit;
|
||||||
|
} else {
|
||||||
|
P1OUT &= ~m_bit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
uint16_t m_bit;
|
||||||
|
};
|
||||||
|
class Led2 : Led {
|
||||||
|
public:
|
||||||
|
Led1(uint16_t bit) : Led() {
|
||||||
|
m_bit = bit;
|
||||||
|
P2DIR |= bit;
|
||||||
|
P2OUT &= ~bit;
|
||||||
|
}
|
||||||
|
virtual void set(bool v) {
|
||||||
|
if (v) {
|
||||||
|
P2OUT |= m_bit;
|
||||||
|
} else {
|
||||||
|
P2OUT &= ~m_bit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
uint16_t m_bit;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
SwitchPort2 intensitySwitch = SwitchPort2(BIT1);
|
SwitchPort2 intensitySwitch = SwitchPort2(BIT1);
|
||||||
|
|
||||||
SwitchPort2 onTimeSwitch = SwitchPort2(BIT2);
|
SwitchPort2 onTimeSwitch = SwitchPort2(BIT2);
|
||||||
|
|
||||||
SwitchPort2 offTimeSwitch = SwitchPort2(BIT3);
|
SwitchPort2 offTimeSwitch = SwitchPort2(BIT3);
|
||||||
//SwitchPort modeSwitch = SwitchPort2(BIT4);
|
//SwitchPort modeSwitch = SwitchPort2(BIT4);
|
||||||
|
|
||||||
|
Led1 ledGreen = Led1(BIT5);
|
||||||
|
Led1 ledRed = Led1(BIT4);
|
||||||
|
|
||||||
uint16_t intensity = 0;
|
uint16_t intensity = 0;
|
||||||
uint16_t maxIntensity = 0;
|
uint16_t maxIntensity = 0;
|
||||||
uint16_t intensityStep = 0;
|
uint16_t intensityStep = 0;
|
||||||
const uint16_t intensityStepCnt = 16;
|
const uint16_t intensityStepCnt = 16;
|
||||||
|
|
||||||
void hmiLedGreenOn() {
|
|
||||||
P1OUT |= BIT4;
|
|
||||||
}
|
|
||||||
void hmiLedRedOn() {
|
|
||||||
P1OUT |= BIT5;
|
|
||||||
}
|
|
||||||
void hmiLedGreenOff() {
|
|
||||||
P1OUT &= ~BIT4;
|
|
||||||
}
|
|
||||||
void hmiLedRedOff() {
|
|
||||||
P1OUT &= ~BIT5;
|
|
||||||
}
|
|
||||||
|
|
||||||
void hmiInit() {
|
void hmiInit() {
|
||||||
maxIntensity = engineMaxPwmValue();
|
maxIntensity = engineMaxPwmValue();
|
||||||
intensityStep = maxIntensity / intensityStepCnt;
|
intensityStep = maxIntensity / intensityStepCnt;
|
||||||
|
|
||||||
P1DIR |= BIT4 | BIT5;
|
|
||||||
P1OUT &= ~(BIT4 | BIT5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void hmiExec() {
|
void hmiExec() {
|
||||||
static uint32_t ledWait = 0;
|
|
||||||
|
|
||||||
if (intensitySwitch.get() == SwitchPort::SWITCH_SHORT) {
|
if (intensitySwitch.get() == SwitchPort::SWITCH_SHORT) {
|
||||||
intensity += intensityStep;
|
intensity += intensityStep;
|
||||||
if (intensity > maxIntensity) {
|
if (intensity > maxIntensity) {
|
||||||
intensity = maxIntensity;
|
intensity = maxIntensity;
|
||||||
}
|
}
|
||||||
hmiLedGreenOn();
|
ledGreen.start(Led::FLASH_SHORT);
|
||||||
ledWait = LED_WAIT_TIME;
|
|
||||||
debugWrite(intensity);
|
debugWrite(intensity);
|
||||||
engineSetPwmValue(intensity);
|
engineSetPwmValue(intensity);
|
||||||
} else if (intensitySwitch.get() == SwitchPort::SWITCH_LONG) {
|
} else if (intensitySwitch.get() == SwitchPort::SWITCH_LONG) {
|
||||||
if (intensity != 0) {
|
if (intensity != 0) {
|
||||||
intensity -= intensityStep;
|
intensity -= intensityStep;
|
||||||
}
|
}
|
||||||
hmiLedRedOn();
|
ledRed.start(Led::FLASH_SHORT);
|
||||||
ledWait = LED_WAIT_TIME;
|
|
||||||
debugWrite(intensity);
|
debugWrite(intensity);
|
||||||
engineSetPwmValue(intensity);
|
engineSetPwmValue(intensity);
|
||||||
}
|
}
|
||||||
@ -166,28 +226,20 @@ void hmiExec() {
|
|||||||
|
|
||||||
if (onTimeSwitch.get() == SwitchPort::SWITCH_SHORT) {
|
if (onTimeSwitch.get() == SwitchPort::SWITCH_SHORT) {
|
||||||
engineIncOnTime();
|
engineIncOnTime();
|
||||||
hmiLedGreenOn();
|
ledGreen.start(Led::FLASH_SHORT);
|
||||||
ledWait = LED_WAIT_TIME;
|
|
||||||
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
|
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
|
||||||
engineDecOnTime();
|
engineDecOnTime();
|
||||||
hmiLedRedOn();
|
ledRed.start(Led::FLASH_SHORT);
|
||||||
ledWait = LED_WAIT_TIME;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offTimeSwitch.get() == SwitchPort::SWITCH_SHORT) {
|
if (offTimeSwitch.get() == SwitchPort::SWITCH_SHORT) {
|
||||||
engineIncOffTime();
|
engineIncOffTime();
|
||||||
hmiLedGreenOn();
|
ledGreen.start(Led::FLASH_SHORT);
|
||||||
ledWait = LED_WAIT_TIME;
|
|
||||||
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
|
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
|
||||||
engineDecOffTime();
|
engineDecOffTime();
|
||||||
hmiLedRedOn();
|
ledRed.start(Led::FLASH_SHORT);
|
||||||
ledWait = LED_WAIT_TIME;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ledWait != 0) {
|
ledRed.exec();
|
||||||
ledWait--;
|
ledGreen.exec();
|
||||||
} else {
|
|
||||||
hmiLedGreenOff();
|
|
||||||
hmiLedRedOff();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user