-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
144 lines (137 loc) · 3.51 KB
/
timer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "timer.h"
#include "print.h"
#include "keycodes.h"
#include <tice.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
static void printint(int i, int minlen)
{
real_t r;
int slen;
char buf[16];
r = os_Int24ToReal(i);
slen = os_RealToStr(buf, &r, 0, 0, 0);
if (slen < minlen)
{
memmove(buf + minlen - slen, buf, slen + 1);
for (int i = 0; i < minlen - slen; i++)
buf[i] = '0';
}
os_PutStrFull(buf);
}
void print_time(uint32_t millis, uint8_t row, uint8_t col)
{
os_SetCursorPos(row, col);
printint(millis / 10 / 60 / 60, 2);
os_PutStrLine(":");
printint(millis / 10 / 60 % 60, 2);
os_PutStrLine(":");
printint(millis / 10 % 60, 2);
os_PutStrLine(".");
printint(millis % 10, 1);
}
uint32_t time_adddigit(uint32_t secs, uint8_t val)
{
uint8_t h, m, s, h2, m2, s2;
s = secs % 60; // seconds
m = secs / 60 % 60; // minutes
h = secs / 60 / 60; // hours
s2 = s % 10 * 10 + val;
m2 = s / 10 % 10 + m % 10 * 10;
h2 = m / 10 % 10 + h * 10;
if (s2 >= 60)
{
m2++;
s2 -= 60;
if (m2 >= 60)
{
h2++;
m2 -= 60;
if (h2 > 10)
return secs;
}
}
return (uint32_t)s2 + (uint32_t)m2 * 60 + (uint32_t)h2 * 60 * 60;
}
void do_timer()
{
uint8_t started;
uint8_t mode = 0;
int8_t val;
sk_key_t key;
uint32_t tim;
os_ClrHome();
puts("Stopwatch");
puts("enter=Start/Stop");
puts("vars=Reset");
started = 0;
timer_Enable(1, TIMER_32K, TIMER_NOINT, TIMER_UP);
timer_Disable(1);
timer_Set(1, 0);
timer_SetReload(1, 0);
while (true)
{
key = os_GetCSC();
switch (key)
{
case sk_Enter:
started = 1 - started;
if (started)
{
if (mode == 0)
timer_Enable(1, TIMER_32K, TIMER_NOINT, TIMER_UP);
else
timer_Enable(1, TIMER_32K, TIMER_0INT, TIMER_DOWN);
}
else
timer_Disable(1);
break;
case sk_Clear:
timer_Disable(1);
return;
case sk_Vars:
if (mode == 1)
timer_Disable(1);
timer_Set(1, 0);
break;
case sk_Mode:
os_ClrHome();
timer_Disable(1);
timer_Set(1, 0);
if (mode == 0)
{
mode = 1;
puts("Countdown");
puts("enter=Start/Stop");
puts("vars=Clear 0-9=Set");
tim = 0;
}
else
{
mode = 0;
puts("Stopwatch");
puts("enter=Start/Stop");
puts("vars=Reset");
}
break;
case sk_Del:
timer_Disable(1);
timer_SetReload(1, tim = 0);
timer_Set(1, 0);
break;
default:
if (mode != 1)
break;
if ((val = sk2int(key)) == -1)
break;
timer_Disable(1);
timer_Set(1, (tim = time_adddigit(timer_Get(1) / 32768, val)) * 32768);
timer_SetReload(1, tim * 32768);
break;
}
print_time(timer_Get(1) * 10 / 32768, 3, 0);
if (timer_ChkInterrupt(1, TIMER_RELOADED))
timer_Disable(1), timer_AckInterrupt(1, TIMER_RELOADED), started = 0, timer_Set(1, timer_GetReload(1));
}
}