-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrograms.cpp
165 lines (159 loc) · 4.46 KB
/
Programs.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <Arduino.h>
#include "RTC.h"
#include "BTN.h"
#include "Wheel.h"
#include "PixelRing.h"
#include "Programs.h"
class Clock
{
public:
static void Run();
static void Set();
private:
static const ColourPalette hourColour = ColourPalette::Blue;
static const ColourPalette minuteColour = ColourPalette::Green;
static const ColourPalette chapterColour = ColourPalette::Orange;
static const int SecondsBetweenMinuteMarks = 150; // 2.5 minutes
};
void Clock::Run()
{
// run the clock until the button is pressed
unsigned long updateMS = millis();
int lastMin = -1;
pixelRing.Clear();
pixelRing.SetCursor(-1);
pixelRing.Show();
bool blinkOn = false;
while (!btn.CheckButtonPress())
{
unsigned long nowMS = millis();
if (lastMin < 0 || (nowMS - updateMS) >= 1000UL)
{
rtc.ReadTime(true);
updateMS = nowMS;
lastMin = rtc.m_Minute;
int hourIdx = 2*(rtc.m_Hour24 % 12);
int minuteIdx = (rtc.m_Minute*60 + rtc.m_Second)/SecondsBetweenMinuteMarks;
blinkOn = !blinkOn;
pixelRing.Clear();
// chapter ring
for (int idx = 0; idx < PixelRing::Count; idx++)
pixelRing.SetPixel(idx, pixelRing.Colour((idx % 2)?ColourPalette::Black:chapterColour));
// hour hand, only 12 pixels
pixelRing.SetPixel(hourIdx, pixelRing.Colour(hourColour));
// minute hand, all 24 pixels
if (blinkOn)
pixelRing.SetPixel(minuteIdx, pixelRing.Colour(minuteColour));
else
{
// blank/background colour
if (hourIdx == minuteIdx)
pixelRing.SetPixel(minuteIdx, pixelRing.Colour(hourColour));
else if (minuteIdx % 2)
pixelRing.SetPixel(minuteIdx, pixelRing.Colour(ColourPalette::Black));
else
pixelRing.SetPixel(minuteIdx, pixelRing.Colour(chapterColour));
}
pixelRing.Show();
}
}
}
void Clock::Set()
{
unsigned long updateMS = millis();
unsigned long idleMS = millis();
rtc.ReadTime(true);
pixelRing.Clear();
pixelRing.SetCursor(-1);
pixelRing.Show();
bool blinkOn = false;
bool hour = true;
bool updateDisplay = true;
int hourIdx = 2*(rtc.m_Hour24 % 12);
int minuteIdx = (rtc.m_Minute*60 + rtc.m_Second)/SecondsBetweenMinuteMarks;
ColourPalette altChapterColour = ColourPalette::Aqua;
while (true)
{
if (btn.CheckButtonPress())
{
idleMS = millis();
if (hour)
{
hour = false;
blinkOn = updateDisplay = true;
altChapterColour = ColourPalette::Malachite;
}
else
{
break;
}
}
wheel.Update();
int rotation = wheel.GetRotation();
if (rotation)
{
idleMS = millis();
if (hour)
hourIdx += 2*rotation;
else
minuteIdx += rotation;
if (hourIdx < 0)
hourIdx += PixelRing::Count;
if (hourIdx >= PixelRing::Count)
hourIdx -= PixelRing::Count;
if (minuteIdx < 0)
minuteIdx += PixelRing::Count;
if (minuteIdx >= PixelRing::Count)
minuteIdx -= PixelRing::Count;
blinkOn = updateDisplay = true;
}
unsigned long nowMS = millis();
if ((nowMS - idleMS) >= 60000UL)
{
// sitting idle
return;
}
if (updateDisplay || (nowMS - updateMS) >= 500UL)
{
updateMS = nowMS;
pixelRing.Clear();
// chapter ring
for (int idx = 0; idx < PixelRing::Count; idx++)
pixelRing.SetPixel(idx, pixelRing.Colour((idx % 2)?ColourPalette::Black:altChapterColour));
int idx = hour?hourIdx:minuteIdx;
if (blinkOn)
pixelRing.SetPixel(idx, pixelRing.Colour(hour?hourColour:minuteColour));
else
{
// blank/background colour
if (idx % 2)
pixelRing.SetPixel(idx, pixelRing.Colour(ColourPalette::Black));
else
pixelRing.SetPixel(idx, pixelRing.Colour(altChapterColour));
}
updateDisplay = false;
pixelRing.Show();
blinkOn = !blinkOn;
}
}
// SET
rtc.ReadTime(true);
rtc.m_Hour24 = hourIdx / 2;
if (rtc.m_Hour24 == 0)
rtc.m_Hour24 = 12;
int seconds = minuteIdx*SecondsBetweenMinuteMarks;
rtc.m_Minute = seconds / 60;
rtc.m_Second = seconds % 60;
rtc.WriteTime();
}
void Program(Labels label)
{
if (label == Labels::Menu_ClockRun)
{
Clock::Run();
}
else if (label == Labels::Menu_ClockSet)
{
Clock::Set();
}
}