-
Notifications
You must be signed in to change notification settings - Fork 5
/
encoderbutton.c
76 lines (63 loc) · 1.75 KB
/
encoderbutton.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "encoderbutton.h"
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "stm8.h"
#include "systemtimer.h"
#define ENCODERBUTTON_BOUNCE_TIME_MS 20
#define ENCODERBUTTON_LONG_TIME_MS 1000
// FIXME merge with button.c
enum PressValue {
PressValue_None,
PressValue_Short,
PressValue_Long
};
static volatile enum PressValue pressValue;
static enum PressValue pressValueCopy;
static volatile bool pressed;
static volatile bool pressProcessed;
static volatile uint32_t lastCheck;
static volatile uint32_t pressTime;
void ENCODERBUTTON_init(void) {
GPIOC->CR1 |= GPIO_CR1_3; // pull-up
}
// ~3.8 us
void ENCODERBUTTON_cycle(void) {
bool p;
if(pressValue != PressValue_None) return;
if((SYSTEMTIMER_ms - lastCheck) < ENCODERBUTTON_BOUNCE_TIME_MS) return;
p = !(GPIOC->IDR & GPIO_CR1_3);
if(p && pressed && (SYSTEMTIMER_ms - pressTime) >= ENCODERBUTTON_LONG_TIME_MS) {
if(!pressProcessed) {
pressValue = PressValue_Long;
pressProcessed = true;
}
}
else if(p != pressed) {
if(p) {
pressTime = SYSTEMTIMER_ms;
}
else if(!pressProcessed) {
pressValue = PressValue_Short;
}
pressed = p;
lastCheck = SYSTEMTIMER_ms;
pressProcessed = false;
}
}
void ENCODERBUTTON_process(void) {
// Atomic:
// pressValueCopy = pressValue;
// pressValue = 0;
__asm
CLR A
EXG A, _pressValue
LD _pressValueCopy, A
__endasm;
switch(pressValueCopy) {
case PressValue_None: break;
case PressValue_Short: ENCODERBUTTON_onRelease(false); break;
case PressValue_Long: ENCODERBUTTON_onRelease(true); break;
}
}