-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtons.cpp
More file actions
97 lines (91 loc) · 2.15 KB
/
Copy pathButtons.cpp
File metadata and controls
97 lines (91 loc) · 2.15 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Buttons.cpp
*
* Created: 2018-06-26 오전 8:26:47
* Author: Cakeng (PARK JONG SEOK)
*
* NO LICENCE INCLUDED
* Contact cakeng@naver.com to
* use, modify, or share the software for any purpose
* other than personal use.
*
*/
#include "Buttons.h"
void Buttons::checkInputs()
{
DDRB &= 0b10001111;
PORTB &= 0b10001111;
buttonPressed = NULL;
if(!(PINB&(1<<PORTB6)))
{
buttonPressed = BUTTON_UP_PRESSED;
}
else if(!(PINB&(1<<PORTB5)))
{
buttonPressed = BUTTON_DOWN_PRESSED;
}
else if(!(PINB&(1<<PORTB4)))
{
buttonPressed = BUTTON_ALARM_PRESSED;
}
}
Buttons::Buttons(uint16_t tickFreq, bool pinOn) //Ticks to reach 1 second.
{
buttonTicks = 0;
buttonCheckRateConsts[0] = (tickFreq/500*9); //18ms
buttonCheckRateConsts[1] = (tickFreq/500*75); //150ms
buttonCheckRateConsts[2] = (tickFreq/500*30); //60ms
buttonCheckRate = buttonCheckRateConsts[2];
buttonCounter = 0;
buttonPressed = NULL;
buttonLast = NULL;
DDRB &= 0b10001111;
PORTB &= 0b10001111;
}
Buttons::Buttons(uint16_t tickFreq) //Ticks to reach 1 second.
{
buttonTicks = 0;
buttonCheckRateConsts[0] = (tickFreq/500*9); //18ms
buttonCheckRateConsts[1] = (tickFreq/500*75); //150ms
buttonCheckRateConsts[2] = (tickFreq/500*30); //60ms
buttonCheckRate = buttonCheckRateConsts[2];
buttonCounter = 0;
buttonPressed = NULL;
buttonLast = NULL;
DDRB &= 0b10001111;
PORTB &= 0b10001111;
}
void Buttons::buttonFunction()
{
buttonPressedOut = BUTTON_UNDER_TICKS;
buttonCountNum = buttonCounter;
if(buttonTicks < buttonCheckRate)
{
return;
}
buttonPressedOut = NULL;
buttonTicks = 0;
checkInputs();
if(buttonPressed == NULL) // If no buttons were pressed
{
if(buttonLast != NULL) // If something was pressed before
{
buttonPressedOut = buttonLast;
buttonLast = NULL;
buttonCounter = 0;
buttonCheckRate = buttonCheckRateConsts[2];
}
return;
}
buttonCounter++;
if(buttonCounter > 24)
{
buttonCheckRate = buttonCheckRateConsts[0];
}
else if(buttonCounter > 4)
{
buttonCheckRate = buttonCheckRateConsts[1];
}
buttonPressedOut = buttonPressed;
buttonLast = buttonPressed+3;
}