add button stuff and timer stuff
This commit is contained in:
parent
7c854e7353
commit
8e08c2045e
60
src/button.c
Normal file
60
src/button.c
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* button.c
|
||||
*
|
||||
* Created on: 08.09.2016
|
||||
* Author: dehottgw
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "button.h"
|
||||
#include "gpio.h"
|
||||
|
||||
|
||||
const uint32_t BUTTON_CYCLE = 10;
|
||||
const uint8_t ACTIVE_STATE_CNT_REQUIRED = 5;
|
||||
|
||||
tPin buttonPins[BUTTONS_END] = { BUTTON_1 };
|
||||
|
||||
typedef struct {
|
||||
void (*action)(void *);
|
||||
void *actionArg;
|
||||
uint8_t activeStateCnt;
|
||||
} tButtonHandle;
|
||||
|
||||
|
||||
tButtonHandle buttonHandles[BUTTONS_END];
|
||||
|
||||
|
||||
|
||||
void buttonInit(void *handleArg) {
|
||||
for (tButton b = BUTTONS_FIRST; b < BUTTONS_END; b++) {
|
||||
buttonHandles[b].action = NULL;
|
||||
buttonHandles[b].actionArg = NULL;
|
||||
buttonHandles[b].activeStateCnt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void buttonExec(void *handleArg) {
|
||||
for (tButton b = BUTTONS_FIRST; b < BUTTONS_END; b++) {
|
||||
if (buttonHandles[b].action != NULL) {
|
||||
if (gpioGetPin(buttonPins[b]) == LOW) {
|
||||
buttonHandles[b].activeStateCnt++;
|
||||
if (buttonHandles[b].activeStateCnt > ACTIVE_STATE_CNT_REQUIRED) {
|
||||
buttonHandles[b].action(buttonHandles[b].actionArg);
|
||||
buttonHandles[b].activeStateCnt = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void buttonRegister(void (*action)(void *), void *actionArg, tButton button) {
|
||||
buttonHandles[button].action = action;
|
||||
buttonHandles[button].actionArg = actionArg;
|
||||
buttonHandles[button].activeStateCnt = 0;
|
||||
}
|
||||
|
28
src/button.h
Normal file
28
src/button.h
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* button.h
|
||||
*
|
||||
* Created on: 08.09.2016
|
||||
* Author: dehottgw
|
||||
*/
|
||||
|
||||
#ifndef BUTTON_H_
|
||||
#define BUTTON_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern uint32_t BUTTON_CYCLE;
|
||||
|
||||
typedef enum {
|
||||
BUTTONS_FIRST,
|
||||
BUTTON_START_TIMER = BUTTONS_FIRST,
|
||||
BUTTONS_END
|
||||
} tButton;
|
||||
|
||||
|
||||
|
||||
void buttonInit(void *handleArg);
|
||||
void buttonExec(void *handleArg);
|
||||
void buttonRegister(void (*action)(void *), void *actionArg, tButton button);
|
||||
|
||||
#endif /* BUTTON_H_ */
|
@ -13,6 +13,8 @@
|
||||
#include "displayMuxer.h"
|
||||
#include "display.h"
|
||||
|
||||
const uint32_t DISPLAY_MUXER_CYCLE = 500;
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t value;
|
||||
|
@ -11,6 +11,10 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
extern const uint32_t DISPLAY_MUXER_CYCLE;
|
||||
|
||||
|
||||
typedef enum {
|
||||
FIRST_MUX = 0,
|
||||
TIMER_MUX = FIRST_MUX,
|
||||
|
49
src/eggTimer.c
Normal file
49
src/eggTimer.c
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* eggTimer.c
|
||||
*
|
||||
* Created on: 08.09.2016
|
||||
* Author: dehottgw
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "eggTimer.h"
|
||||
#include "displayMuxer.h"
|
||||
#include "button.h"
|
||||
|
||||
|
||||
const uint32_t EGG_TIMER_CYCLE = 1000;
|
||||
|
||||
const uint8_t COOKING_TIME = 120;
|
||||
uint8_t restCookingTime;
|
||||
bool timerRunning;
|
||||
bool timerStarted;
|
||||
|
||||
void eggTimerInit(void *handleArg) {
|
||||
restCookingTime = COOKING_TIME;
|
||||
timerRunning = false;
|
||||
timerStarted = false;
|
||||
buttonRegister(eggTimerStart, NULL, BUTTON_START_TIMER);
|
||||
}
|
||||
|
||||
void eggTimerExec(void *handleArg) {
|
||||
if (timerRunning) {
|
||||
restCookingTime--;
|
||||
displayMuxerSetValue(restCookingTime, true, TIMER_MUX);
|
||||
if (restCookingTime == 0) {
|
||||
// activate alarm
|
||||
timerRunning = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void eggTimerStart(void *handleArg) {
|
||||
if (! timerStarted) {
|
||||
timerRunning = true;
|
||||
timerStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
21
src/eggTimer.h
Normal file
21
src/eggTimer.h
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* eggTimer.h
|
||||
*
|
||||
* Created on: 08.09.2016
|
||||
* Author: dehottgw
|
||||
*/
|
||||
|
||||
#ifndef EGGTIMER_H_
|
||||
#define EGGTIMER_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
void eggTimerInit(void *handleArg);
|
||||
void eggTimerExec(void *handleArg);
|
||||
void eggTimerStart(void *handleArg);
|
||||
|
||||
|
||||
|
||||
#endif /* EGGTIMER_H_ */
|
46
src/gpio.c
46
src/gpio.c
@ -22,13 +22,37 @@ void gpioInitPins() {
|
||||
for (tPin p = PINS_FIRST; p < PINS_END; p += 1) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
if (pin.portId == PORT1) {
|
||||
P1DIR |= pin.bit;
|
||||
P1SEL &= ~pin.bit;
|
||||
P1SEL2 &= ~pin.bit;
|
||||
if (pin.direction == PIN_OUT) {
|
||||
P1DIR |= pin.bit;
|
||||
P1SEL &= ~pin.bit;
|
||||
P1SEL2 &= ~pin.bit;
|
||||
} else if (pin.direction == PIN_IN) {
|
||||
P1DIR &= ~pin.bit;
|
||||
P1SEL &= ~pin.bit;
|
||||
P1SEL2 &= ~pin.bit;
|
||||
P1REN &= ~pin.bit;
|
||||
} else if (pin.direction == PIN_IN_PULLUP) {
|
||||
P1DIR &= ~pin.bit;
|
||||
P1SEL &= ~pin.bit;
|
||||
P1SEL2 &= ~pin.bit;
|
||||
P1REN |= pin.bit;
|
||||
}
|
||||
} else if (pin.portId == PORT2) {
|
||||
P2DIR |= pin.bit;
|
||||
P2SEL &= ~pin.bit;
|
||||
P2SEL2 &= ~pin.bit;
|
||||
if (pin.direction == PIN_OUT) {
|
||||
P2DIR |= pin.bit;
|
||||
P2SEL &= ~pin.bit;
|
||||
P2SEL2 &= ~pin.bit;
|
||||
} else if (pin.direction == PIN_IN) {
|
||||
P2DIR &= ~pin.bit;
|
||||
P2SEL &= ~pin.bit;
|
||||
P2SEL2 &= ~pin.bit;
|
||||
P2REN &= ~pin.bit;
|
||||
} else if (pin.direction == PIN_IN_PULLUP) {
|
||||
P2DIR &= ~pin.bit;
|
||||
P2SEL &= ~pin.bit;
|
||||
P2SEL2 &= ~pin.bit;
|
||||
P2REN |= pin.bit;
|
||||
}
|
||||
}
|
||||
gpioSetPin(p, pin.defaultOut);
|
||||
}
|
||||
@ -51,3 +75,13 @@ void gpioSetPin(tPin p, tPinState v) {
|
||||
}
|
||||
}
|
||||
|
||||
tPinState gpioGetPin(tPin p) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
uint16_t pinValue = 0;
|
||||
if (pin.portId == PORT1) {
|
||||
pinValue = P1IN;
|
||||
} else if (pin.portId == PORT2) {
|
||||
pinValue = P2IN;
|
||||
}
|
||||
return (pinValue & pin.bit) ? HIGH : LOW;
|
||||
}
|
||||
|
11
src/gpio.h
11
src/gpio.h
@ -24,9 +24,16 @@ typedef enum {
|
||||
HIGH,
|
||||
} tPinState;
|
||||
|
||||
typedef enum {
|
||||
PIN_OUT,
|
||||
PIN_IN,
|
||||
PIN_IN_PULLUP
|
||||
} tPinDirection;
|
||||
|
||||
typedef struct {
|
||||
tPort portId;
|
||||
uint16_t bit;
|
||||
tPinDirection direction;
|
||||
tPinState defaultOut;
|
||||
} tPinCfg;
|
||||
|
||||
@ -42,13 +49,13 @@ typedef enum {
|
||||
SEG_G,
|
||||
DIGIT_0,
|
||||
DIGIT_1,
|
||||
TESTPIN1,
|
||||
TESTPIN2,
|
||||
BUTTON_1,
|
||||
PINS_END
|
||||
} tPin;
|
||||
|
||||
void gpioInitPins();
|
||||
void gpioSetPin(tPin p, tPinState v);
|
||||
tPinState gpioGetPin(tPin p);
|
||||
|
||||
|
||||
|
||||
|
@ -8,15 +8,14 @@
|
||||
#include "gpio.h"
|
||||
|
||||
tPinCfg pinCfg[PINS_END] = {
|
||||
{PORT1, BIT7, LOW}, //A
|
||||
{PORT1, BIT6, LOW}, //B
|
||||
{PORT2, BIT1, LOW}, //C
|
||||
{PORT2, BIT3, LOW}, //D
|
||||
{PORT2, BIT4, LOW}, //E
|
||||
{PORT2, BIT5, LOW}, //F
|
||||
{PORT2, BIT2, LOW}, //G
|
||||
{PORT2, BIT0, HIGH}, //0
|
||||
{PORT1, BIT5, HIGH}, //1
|
||||
{PORT1, BIT1, LOW}, // TESTPIN1
|
||||
{PORT1, BIT2, LOW}, // TESTPIN2
|
||||
{PORT1, BIT7, PIN_OUT, LOW}, //A
|
||||
{PORT1, BIT6, PIN_OUT, LOW}, //B
|
||||
{PORT2, BIT1, PIN_OUT, LOW}, //C
|
||||
{PORT2, BIT3, PIN_OUT, LOW}, //D
|
||||
{PORT2, BIT4, PIN_OUT, LOW}, //E
|
||||
{PORT2, BIT5, PIN_OUT, LOW}, //F
|
||||
{PORT2, BIT2, PIN_OUT, LOW}, //G
|
||||
{PORT2, BIT0, PIN_OUT, HIGH}, //0
|
||||
{PORT1, BIT5, PIN_OUT, HIGH}, //1
|
||||
{PORT1, BIT1, PIN_IN, LOW}, // BUTTON_1
|
||||
};
|
||||
|
16
src/main.c
16
src/main.c
@ -16,6 +16,8 @@
|
||||
#include "displayMuxer.h"
|
||||
#include "PontCoopScheduler.h"
|
||||
#include "measure.h"
|
||||
#include "eggTimer.h"
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
@ -38,17 +40,23 @@ int main() {
|
||||
__disable_interrupt();
|
||||
|
||||
measureInit(NULL);
|
||||
// displayMuxerInit(NULL);
|
||||
displayMuxerInit(NULL);
|
||||
eggTimerInit(NULL);
|
||||
|
||||
|
||||
schAdd(displayExec, NULL, 0, DISPLAY_CYCLE);
|
||||
// schAdd(displayExec, NULL, 0, DISPLAY_CYCLE);
|
||||
schAdd(measureStartConversion, NULL, 0, MEASURE_CYCLE);
|
||||
// schAdd(displayMuxerExec, NULL, 0, 500);
|
||||
schAdd(displayMuxerExec, NULL, 0, DISPLAY_MUXER_CYCLE);
|
||||
schAdd(eggTimerExec, NULL, 0, EGG_TIMER_CYCLE);
|
||||
|
||||
__enable_interrupt();
|
||||
|
||||
while (1) {
|
||||
schExec();
|
||||
|
||||
// put it into the idle loop, it is not time critical
|
||||
// but should get as much time as possible to avoid
|
||||
// flicker
|
||||
displayExec(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include "measure.h"
|
||||
#include "PontCoopScheduler.h"
|
||||
#include "displayMuxer.h"
|
||||
#include "display.h"
|
||||
// #include "display.h"
|
||||
#include "gpio.h"
|
||||
|
||||
|
||||
@ -75,8 +75,8 @@ void measureCollectAndProcessConversion(void *handleArg) {
|
||||
|
||||
uint8_t temperature = (t < 0) ? 0 : ((uint8_t)t);
|
||||
|
||||
displaySetValue(temperature);
|
||||
// displayMuxerSetValue(temperature, true, TEMPERATURE_MUX);
|
||||
// displaySetValue(temperature);
|
||||
displayMuxerSetValue(temperature, true, TEMPERATURE_MUX);
|
||||
|
||||
averagingCnt = 0;
|
||||
averagingSum = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user