/
digitalSandClock.cpp
66 lines (53 loc) · 1.23 KB
/
digitalSandClock.cpp
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
#include "digitalSandClock.h"
SandClock digitalSandClock;
volatile int T25Minutes = 25*60;
volatile bool clockPoint = false;
static void SandClockTimingISR()
{
if(!clockPoint) T25Minutes--;
if(T25Minutes <= 0) T25Minutes = 0;
clockPoint = !clockPoint;
}
void SandClock::deleteInterrupt()
{
Timer1.stop();
Timer1.detachInterrupt();
initFlag = false;
}
void SandClock::run()
{
if(!initFlag)
{
initFlag = true;
init();
}
if(tilt_value != digitalRead(TILT_PIN))
{
tilt_value = digitalRead(TILT_PIN);
T25Minutes = 25*60;
displayflag = 1;
}
TimeUpdate();
if(tilt_value == 0) tm1637.display(TimeDisp,DISPLAY_FLAG_F);
if(tilt_value == 1) tm1637.display(TimeDisp,DISPLAY_FLAG_B);
}
void SandClock::init()
{
pinMode(TILT_PIN,INPUT_PULLUP);
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);
tm1637.clearDisplay();
Timer1.initialize(500000); //500ms freq
Timer1.attachInterrupt(SandClockTimingISR);
}
void SandClock::TimeUpdate(void)
{
minute = T25Minutes/60;
second = T25Minutes%60;
if(clockPoint)tm1637.point(POINT_ON);
else tm1637.point(POINT_OFF);
TimeDisp[0] = minute / 10;
TimeDisp[1] = minute % 10;
TimeDisp[2] = second / 10;
TimeDisp[3] = second % 10;
}