rotary and logic integrated, rotary not working good
This commit is contained in:
134
my_src/display.c
Normal file
134
my_src/display.c
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* display.c
|
||||
*
|
||||
* Created on: May 31, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "display.h"
|
||||
#include <PontCoopScheduler.h>
|
||||
|
||||
|
||||
|
||||
|
||||
volatile tDisplay display = {
|
||||
.toggle = 0, .setModeTemperature = 0, .setModeTime = 0,
|
||||
.timerState = IDLE,
|
||||
.toggleModeState = 1,
|
||||
.targetTemperature = 80, .currentTemperature = 75,
|
||||
.targetTime = 120, .currentTime = 0,
|
||||
.overrunTime = 0
|
||||
};
|
||||
|
||||
|
||||
void updateDisplay(void *handle) {
|
||||
tDisplay *lDisplay = (tDisplay*) handle;
|
||||
lDisplay->toggle ^= 0x01;
|
||||
char buf[32];
|
||||
|
||||
sprintf(buf, "ThermometerTeaTimer");
|
||||
LED_P6x8Str(0, 0, buf);
|
||||
|
||||
if (lDisplay->setModeTemperature && lDisplay->toggle) {
|
||||
sprintf(buf, " %3d'C", lDisplay->currentTemperature);
|
||||
} else {
|
||||
sprintf(buf, "%3d'C %3d'C", lDisplay->targetTemperature, lDisplay->currentTemperature);
|
||||
}
|
||||
LED_P8x16Str(0, 2, buf);
|
||||
|
||||
if (lDisplay->setModeTime && lDisplay->toggle) {
|
||||
sprintf(buf, " %3ds", lDisplay->currentTime);
|
||||
} else {
|
||||
sprintf(buf, "%3ds %3ds", lDisplay->targetTime, lDisplay->currentTime);
|
||||
}
|
||||
LED_P8x16Str(0, 4, buf);
|
||||
|
||||
sprintf(buf, " %5ds", lDisplay->overrunTime);
|
||||
LED_P8x16Str(0, 6, buf);
|
||||
}
|
||||
|
||||
void clearSetMode(void *handle) {
|
||||
tDisplay *lDisplay = (tDisplay*) handle;
|
||||
lDisplay->setModeTemperature = 0;
|
||||
lDisplay->setModeTime = 0;
|
||||
lDisplay->toggleModeState = 1;
|
||||
}
|
||||
|
||||
static void disableSetModeTimer() {
|
||||
schDel(clearSetMode, (void*)&display);
|
||||
}
|
||||
|
||||
static void reEnableSetModeTimer() {
|
||||
schDel(clearSetMode, (void*)&display);
|
||||
schAdd(clearSetMode, (void*)&display, 10000, 0);
|
||||
}
|
||||
|
||||
static void toggleSetMode() {
|
||||
switch (display.toggleModeState) {
|
||||
default:
|
||||
case 0:
|
||||
disableSetModeTimer();
|
||||
display.setModeTemperature = 0;
|
||||
display.setModeTime = 0;
|
||||
display.toggleModeState = 1;
|
||||
break;
|
||||
case 1:
|
||||
reEnableSetModeTimer();
|
||||
display.setModeTemperature = 1;
|
||||
display.setModeTime = 0;
|
||||
display.toggleModeState = 2;
|
||||
break;
|
||||
case 2:
|
||||
reEnableSetModeTimer();
|
||||
display.setModeTemperature = 0;
|
||||
display.setModeTime = 1;
|
||||
display.toggleModeState = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void displayIncValue() {
|
||||
if (display.setModeTemperature == 1) {
|
||||
reEnableSetModeTimer();
|
||||
if (display.targetTemperature < 100) {
|
||||
display.targetTemperature++;
|
||||
}
|
||||
}
|
||||
if (display.setModeTime == 1) {
|
||||
reEnableSetModeTimer();
|
||||
if (display.targetTime < 999) {
|
||||
display.targetTime += 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void displayDecValue() {
|
||||
if (display.setModeTemperature == 1) {
|
||||
reEnableSetModeTimer();
|
||||
if (display.targetTemperature > 0) {
|
||||
display.targetTemperature--;
|
||||
}
|
||||
}
|
||||
if (display.setModeTime == 1) {
|
||||
reEnableSetModeTimer();
|
||||
if (display.targetTime >= 10) {
|
||||
display.targetTime -= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void buttonShort() {
|
||||
if (display.setModeTemperature == 1 || display.setModeTime == 1) {
|
||||
toggleSetMode();
|
||||
} else if (display.timerState == IDLE) {
|
||||
startTimer();
|
||||
}
|
||||
}
|
||||
|
||||
void buttonLong() {
|
||||
if (display.timerState == IDLE) {
|
||||
toggleSetMode();
|
||||
}
|
||||
}
|
35
my_src/display.h
Normal file
35
my_src/display.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* display.h
|
||||
*
|
||||
* Created on: May 31, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef DISPLAY_H_
|
||||
#define DISPLAY_H_
|
||||
|
||||
|
||||
#include "timer.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t toggle;
|
||||
uint8_t setModeTemperature;
|
||||
uint8_t setModeTime;
|
||||
tTimerState timerState;
|
||||
uint8_t toggleModeState;
|
||||
uint32_t targetTemperature;
|
||||
uint32_t currentTemperature;
|
||||
uint32_t targetTime;
|
||||
uint32_t currentTime;
|
||||
uint32_t overrunTime;
|
||||
} tDisplay;
|
||||
|
||||
void updateDisplay(void *handle);
|
||||
void displayToggleSetMode();
|
||||
void displayDecValue();
|
||||
void displayIncValue();
|
||||
|
||||
|
||||
#endif /* DISPLAY_H_ */
|
@ -21,83 +21,10 @@
|
||||
#include "stm32f1xx_hal.h"
|
||||
|
||||
#include "oled.h"
|
||||
#include "timer.h"
|
||||
#include "display.h"
|
||||
|
||||
typedef enum {
|
||||
IDLE = 0,
|
||||
STARTED = 1,
|
||||
RUNNING = 2,
|
||||
OVERRUN = 3
|
||||
} tTimerState;
|
||||
|
||||
typedef struct {
|
||||
uint8_t toggle;
|
||||
uint8_t setModeTemperature;
|
||||
uint8_t setModeTime;
|
||||
tTimerState timerState;
|
||||
uint32_t targetTemperature;
|
||||
uint32_t currentTemperature;
|
||||
uint32_t targetTime;
|
||||
uint32_t currentTime;
|
||||
uint32_t overrunTime;
|
||||
} tDisplay;
|
||||
|
||||
|
||||
tDisplay display = {
|
||||
.toggle = 0, .setModeTemperature = 0, .setModeTime = 0,
|
||||
.timerState = IDLE,
|
||||
.targetTemperature = 80, .currentTemperature = 75,
|
||||
.targetTime = 180, .currentTime = 175,
|
||||
.overrunTime = 0
|
||||
};
|
||||
|
||||
|
||||
void updateDisplay(void *handle) {
|
||||
tDisplay *lDisplay = (tDisplay*) handle;
|
||||
lDisplay->toggle ^= 0x01;
|
||||
char buf[32];
|
||||
|
||||
sprintf(buf, "ThermometerTeaTimer");
|
||||
LED_P6x8Str(0, 0, buf);
|
||||
|
||||
if (lDisplay->setModeTemperature && lDisplay->toggle) {
|
||||
sprintf(buf, " %3d'C", lDisplay->currentTemperature);
|
||||
} else {
|
||||
sprintf(buf, "%3d'C %3d'C", lDisplay->targetTemperature, lDisplay->currentTemperature);
|
||||
}
|
||||
LED_P8x16Str(0, 2, buf);
|
||||
|
||||
if (lDisplay->setModeTime && lDisplay->toggle) {
|
||||
sprintf(buf, " %3ds", lDisplay->currentTime);
|
||||
} else {
|
||||
sprintf(buf, "%3ds %3ds", lDisplay->targetTime, lDisplay->currentTime);
|
||||
}
|
||||
LED_P8x16Str(0, 4, buf);
|
||||
|
||||
sprintf(buf, " %5ds", lDisplay->overrunTime);
|
||||
LED_P8x16Str(0, 6, buf);
|
||||
}
|
||||
|
||||
void secondTick(void *handle) {
|
||||
tDisplay *lDisplay = (tDisplay*) handle;
|
||||
|
||||
switch (lDisplay->timerState) {
|
||||
case STARTED:
|
||||
lDisplay->currentTime = lDisplay->targetTime;
|
||||
lDisplay->timerState = RUNNING;
|
||||
break;
|
||||
case RUNNING:
|
||||
lDisplay->currentTime -= 1;
|
||||
if (lDisplay->currentTime == 0) {
|
||||
lDisplay->timerState = OVERRUN;
|
||||
}
|
||||
break;
|
||||
case OVERRUN:
|
||||
lDisplay->overrunTime += 1;
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
||||
extern tDisplay display;
|
||||
|
||||
void blink(void *handle) {
|
||||
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
|
||||
@ -105,11 +32,9 @@ void blink(void *handle) {
|
||||
|
||||
void my_setup_1() {
|
||||
schInit();
|
||||
schAdd(blink, NULL, 0, 100);
|
||||
// schAdd(blink, NULL, 0, 100);
|
||||
schAdd(updateDisplay, &display, 0, 250);
|
||||
schAdd(secondTick, &display, 0, 1000);
|
||||
|
||||
display.timerState = STARTED;
|
||||
}
|
||||
|
||||
void my_loop() {
|
||||
@ -120,13 +45,13 @@ void HAL_SYSTICK_Callback() {
|
||||
schUpdate();
|
||||
}
|
||||
|
||||
|
||||
void my_errorHandler() {
|
||||
HAL_GPIO_WritePin(ERROR_GPIO_Port, ERROR_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void my_setup_2() {
|
||||
LED_Init();
|
||||
LED_P6x8Str(0,0,"welcome to");
|
||||
|
||||
}
|
||||
|
||||
|
119
my_src/switches.c
Normal file
119
my_src/switches.c
Normal file
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* switches.c
|
||||
*
|
||||
* Created on: May 31, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "switches.h"
|
||||
#include "display.h"
|
||||
|
||||
|
||||
|
||||
extern tDisplay display;
|
||||
volatile uint32_t lastEvent = 0;
|
||||
|
||||
|
||||
void switch_interrupt() {
|
||||
static uint8_t state = 0;
|
||||
uint32_t currentEvent = HAL_GetTick();
|
||||
|
||||
uint16_t line = HAL_GPIO_ReadPin(SWITCH_GPIO_Port, SWITCH_Pin);
|
||||
|
||||
switch (state) {
|
||||
case 0:
|
||||
if (line == RESET) {
|
||||
lastEvent = currentEvent;
|
||||
state = 1;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (line != RESET) {
|
||||
if (lastEvent + LONG_BUTTON_TIME + DEBOUNCING_DEAD_TIME < currentEvent) {
|
||||
lastEvent = currentEvent;
|
||||
buttonLong();
|
||||
state = 0;
|
||||
} else if (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent) {
|
||||
lastEvent = currentEvent;
|
||||
buttonShort();
|
||||
state = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
state = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int myDigitalRead(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
|
||||
int16_t r = 0;
|
||||
for (uint32_t i = 0; i < DEBOUNCING_REPEAT; i++) {
|
||||
if (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) != RESET) {
|
||||
r++;
|
||||
} else {
|
||||
r--;
|
||||
}
|
||||
}
|
||||
int res = -1;
|
||||
if (r >= (((int32_t)DEBOUNCING_REPEAT) / 2)) {
|
||||
res = 1;
|
||||
} else if (r <= -1 * (((int32_t)DEBOUNCING_REPEAT) / 2)) {
|
||||
res = 0;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void rotary_a_interrupt() {
|
||||
uint32_t currentEvent = HAL_GetTick();
|
||||
|
||||
if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) {
|
||||
lastEvent = currentEvent;
|
||||
|
||||
int a = myDigitalRead(ROTARY_A_GPIO_Port, ROTARY_A_Pin);;
|
||||
int b = myDigitalRead(ROTARY_B_GPIO_Port, ROTARY_B_Pin);;
|
||||
|
||||
if (((a != -1) && (b != -1))) {
|
||||
if (a == b) {
|
||||
displayIncValue();
|
||||
} else {
|
||||
displayDecValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rotary_b_interrupt() {
|
||||
uint32_t currentEvent = HAL_GetTick();
|
||||
|
||||
if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) {
|
||||
lastEvent = currentEvent;
|
||||
|
||||
int a = myDigitalRead(ROTARY_A_GPIO_Port, ROTARY_A_Pin);;
|
||||
int b = myDigitalRead(ROTARY_B_GPIO_Port, ROTARY_B_Pin);;
|
||||
|
||||
if (((a != -1) && (b != -1))) {
|
||||
if (a == b) {
|
||||
displayDecValue();
|
||||
} else {
|
||||
displayIncValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
|
||||
if (GPIO_Pin == GPIO_PIN_5) {
|
||||
switch_interrupt();
|
||||
}
|
||||
if (GPIO_Pin == GPIO_PIN_3) {
|
||||
rotary_a_interrupt();
|
||||
}
|
||||
if (GPIO_Pin == GPIO_PIN_4) {
|
||||
rotary_b_interrupt();
|
||||
}
|
||||
}
|
||||
|
18
my_src/switches.h
Normal file
18
my_src/switches.h
Normal file
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* switches.h
|
||||
*
|
||||
* Created on: May 31, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef SWITCHES_H_
|
||||
#define SWITCHES_H_
|
||||
|
||||
const uint32_t DEBOUNCING_DEAD_TIME = 100;
|
||||
const uint32_t DEBOUNCING_REPEAT = 20;
|
||||
const uint32_t LONG_BUTTON_TIME = 1000;
|
||||
|
||||
void buttonShort();
|
||||
void buttonLong();
|
||||
|
||||
#endif /* SWITCHES_H_ */
|
38
my_src/timer.c
Normal file
38
my_src/timer.c
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* timer.c
|
||||
*
|
||||
* Created on: May 31, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include "timer.h"
|
||||
#include "display.h"
|
||||
|
||||
|
||||
extern tDisplay display;
|
||||
|
||||
void startTimer() {
|
||||
display.timerState = STARTED;
|
||||
}
|
||||
|
||||
void secondTick(void *handle) {
|
||||
tDisplay *lDisplay = (tDisplay*) handle;
|
||||
|
||||
switch (lDisplay->timerState) {
|
||||
case STARTED:
|
||||
lDisplay->currentTime = lDisplay->targetTime;
|
||||
lDisplay->timerState = RUNNING;
|
||||
break;
|
||||
case RUNNING:
|
||||
lDisplay->currentTime -= 1;
|
||||
if (lDisplay->currentTime == 0) {
|
||||
lDisplay->timerState = OVERRUN;
|
||||
}
|
||||
break;
|
||||
case OVERRUN:
|
||||
lDisplay->overrunTime += 1;
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
21
my_src/timer.h
Normal file
21
my_src/timer.h
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* timer.h
|
||||
*
|
||||
* Created on: May 31, 2017
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef TIMER_H_
|
||||
#define TIMER_H_
|
||||
|
||||
typedef enum {
|
||||
IDLE = 0,
|
||||
STARTED = 1,
|
||||
RUNNING = 2,
|
||||
OVERRUN = 3
|
||||
} tTimerState;
|
||||
|
||||
void secondTick(void *handle);
|
||||
void startTimer();
|
||||
|
||||
#endif /* TIMER_H_ */
|
Reference in New Issue
Block a user