separation of concerns

This commit is contained in:
Wolfgang Hottgenroth 2016-02-16 17:05:29 +01:00
parent ceac2ece33
commit 48a4d368b6
3 changed files with 71 additions and 45 deletions

View File

@ -14,7 +14,6 @@
volatile uint32_t tickCnt = 0;
const uint16_t DIVIDER = 2;
const uint16_t TICK = 1024;
uint16_t pwmVal = 512;
// unit: 100us
const uint32_t STEP_100ms = 1000;
@ -72,43 +71,67 @@ void engineInit() {
P1DIR |= BIT6;
P1OUT &= ~BIT6;
P1SEL |= BIT6;
//
TACCR0 = TICK;
TACCR1 = 0;
//
TACCTL0 = CCIE;
TACCTL1 = OUTMOD_7;
//TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR | TAIE;
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
}
uint16_t engineMaxPwmValue() {
return TICK;
uint16_t pwmVal = 0;
uint16_t pwmValStep = TICK / 8;
bool engineIncPwnValue() {
bool res = false;
pwmVal += pwmValStep;
if (pwmVal > TICK) {
pwmVal = TICK;
res = true;
}
TACCR1 = pwmVal;
return res;
}
void engineSetPwmValue(uint16_t val) {
TACCR1 = val;
bool engineDecPwmValue() {
bool res = false;
if (pwmVal < pwmValStep) {
pwmVal = 0;
res = true;
} else {
pwmVal -= pwmValStep;
}
TACCR1 = pwmVal;
return res;
}
void engineIncOffTime() {
bool engineIncOffTime() {
offTime += STEP_100ms;
return false;
}
void engineIncOnTime() {
bool engineIncOnTime() {
return false;
onTime += STEP_100ms;
}
void engineDecOffTime() {
bool res = false;
if (offTime != 0) {
offTime -= STEP_100ms;
res = true;
}
return res;
}
void engineDecOnTime() {
bool res = false;
if (onTime != 0) {
onTime -= STEP_100ms;
res = true;
}
return res;
}
uint32_t getMillis() {

View File

@ -11,12 +11,12 @@
void engineInit();
uint16_t engineMaxPwmValue();
void engineSetPwmValue(uint16_t val);
void engineIncOffTime();
void engineIncOnTime();
void engineDecOffTime();
void engineDecOnTime();
bool engineIncOffTime();
bool engineIncOnTime();
bool engineDecOffTime();
bool engineDecOnTime();
bool engineDecPwmValue();
bool engineIncPwmValue();
uint32_t getMillis();

View File

@ -6,9 +6,6 @@
#include "debug.h"
const uint32_t LOWER_SHORT_TIME = 100;
const uint32_t UPPER_SHORT_TIME = 300;
const uint32_t LED_WAIT_TIME = 500;
class SwitchPort {
public:
@ -69,6 +66,8 @@ public:
return result;
};
private:
static const uint32_t LOWER_SHORT_TIME = 100;
static const uint32_t UPPER_SHORT_TIME = 300;
enum e_state { STATE_IDLE, STATE_BUTTON_PRESSED, STATE_BUTTON_RELEASED, STATE_SHORT, STATE_LONG };
e_state m_state;
uint32_t m_timestamp;
@ -146,6 +145,8 @@ public:
}
}
private:
static const uint32_t LED_WAIT_TIME_SHORT = 500;
static const uint32_t LED_WAIT_TIME_LONG = 1000;
enum e_state { STATE_IDLE, STATE_START, STATE_ON, STATE_OFF };
e_state m_state;
uint32_t m_timeout;
@ -194,50 +195,52 @@ SwitchPort2 offTimeSwitch = SwitchPort2(BIT3);
Led1 ledGreen = Led1(BIT5);
Led1 ledRed = Led1(BIT4);
uint16_t intensity = 0;
uint16_t maxIntensity = 0;
uint16_t intensityStep = 0;
const uint16_t intensityStepCnt = 16;
void hmiInit() {
maxIntensity = engineMaxPwmValue();
intensityStep = maxIntensity / intensityStepCnt;
}
void hmiExec() {
if (intensitySwitch.get() == SwitchPort::SWITCH_SHORT) {
intensity += intensityStep;
if (intensity > maxIntensity) {
intensity = maxIntensity;
if (engineIncPwmValue()) {
ledGreen.start(Led::FLASH_SHORT);
} else {
ledGreen.start(Led::FLASH_LONG);
}
ledGreen.start(Led::FLASH_SHORT);
debugWrite(intensity);
engineSetPwmValue(intensity);
} else if (intensitySwitch.get() == SwitchPort::SWITCH_LONG) {
if (intensity != 0) {
intensity -= intensityStep;
if (engineDecPwmValue()) {
ledRed.start(Led::FLASH_SHORT);
} else {
ledRed.start(Led::FLASH_LONG);
}
ledRed.start(Led::FLASH_SHORT);
debugWrite(intensity);
engineSetPwmValue(intensity);
}
if (onTimeSwitch.get() == SwitchPort::SWITCH_SHORT) {
engineIncOnTime();
ledGreen.start(Led::FLASH_SHORT);
if (engineIncOnTime()) {
ledGreen.start(Led::FLASH_SHORT);
} else {
ledGreen.start(Led::FLASH_LONG);
}
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
engineDecOnTime();
ledRed.start(Led::FLASH_SHORT);
if (engineDecOnTime()) {
ledRed.start(Led::FLASH_SHORT);
} else {
ledRed.start(Led::FLASH_LONG);
}
}
if (offTimeSwitch.get() == SwitchPort::SWITCH_SHORT) {
engineIncOffTime();
ledGreen.start(Led::FLASH_SHORT);
if (engineIncOffTime()) {
ledGreen.start(Led::FLASH_SHORT);
} else {
ledGreen.start(Led::FLASH_LONG);
}
} else if (onTimeSwitch.get() == SwitchPort::SWITCH_LONG) {
engineDecOffTime();
ledRed.start(Led::FLASH_SHORT);
if (engineDecOffTime()) {
ledRed.start(Led::FLASH_SHORT);
} else {
ledRed.start(Led::FLASH_LONG);
}
}
ledRed.exec();