Repository navigation
Expand file tree
/
Copy pathbattery_view.cpp
More file actions
137 lines (109 loc) · 2.96 KB
/
Copy pathbattery_view.cpp
File metadata and controls
137 lines (109 loc) · 2.96 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
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
#include <platform.h>
#include "battery_view.h"
BatteryView::BatteryView(const Rect &rct) : View(rct),
blinker(650, false)
{
lastBatteryPercent = 0;
criticalAmount = 20;
dotSize = 1;
lastBatteryPixelLen = 0;
lastBatteryPercent = 0;
batteryCriticalColor = RedColor;
borderColor = StandardBorderColor;
backgroundColor = StandardBackgroundColor;
blinking = false;
lastBlinkDrawed = false;
blinker.setCallback<BatteryView>(this, &BatteryView::scheduleRepaint);
}
// MARK: Acessors
void BatteryView::setBatteryPercent(uint8_t percent)
{
if (lastBatteryPercent == percent)
return;
lastBatteryPercent = percent;
int fullLength = viewRect.Width() - dotSize - 4;
lastBatteryPixelLen = lastBatteryPercent * fullLength / 100;
scheduleRepaint();
}
uint8_t BatteryView::batteryPercent() const
{
return lastBatteryPercent;
}
void BatteryView::setBatteryCritical(const Color &col)
{
batteryCriticalColor = col;
}
const Color& BatteryView::batteryCritical() const
{
return batteryCriticalColor;
}
void BatteryView::setBorder(const Color &col)
{
borderColor = col;
}
const Color& BatteryView::border() const
{
return borderColor;
}
void BatteryView::setBackground(const Color &col)
{
backgroundColor = col;
}
const Color& BatteryView::background() const
{
return backgroundColor;
}
void BatteryView::setBlinkWhenCritical(bool blink)
{
blinking = blink;
if (lastBlinkDrawed <= criticalAmount)
scheduleRepaint();
}
bool BatteryView::blinkWhenCritical() const
{
return blinking;
}
void BatteryView::setCriticalPercentAmount(uint8_t critical)
{
criticalAmount = critical;
}
uint8_t BatteryView::criticalPercentAmount() const
{
return criticalAmount;
}
// MARK: View overloads
void BatteryView::repaint()
{
// battery dot size
painter.setForegroundColor(borderColor);
//draw bounds
Rect bounds(viewRect.X(), viewRect.Y(), viewRect.Width() - dotSize, viewRect.Height());
painter.drawFillRect(bounds, true);
painter.drawRect(bounds);
//draw dot
Point d1(viewRect.X2() - dotSize, viewRect.Y() + viewRect.Height()/4);
Point d2(viewRect.X2() - dotSize, viewRect.Y2() - viewRect.Height()/4);
painter.drawLine(d1, d2);
// update percentage if delegate is installed
if (batteryPercentDelegate)
this->setBatteryPercent(batteryPercentDelegate.call());
if (lastBatteryPercent <= criticalAmount)
{
if (blinking)
blinker.start();
if (blinking && lastBlinkDrawed)
painter.setForegroundColor(backgroundColor);
else
painter.setForegroundColor(batteryCriticalColor);
}
else
blinker.stop();
// draw battery level
Rect bat(bounds.X()+2, bounds.Y()+2,
lastBatteryPixelLen, bounds.Height() - 4);
painter.drawFillRect(bat);
if (blinking && lastBatteryPercent <= criticalAmount)
{
lastBlinkDrawed = !lastBlinkDrawed;
}
}