-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_clock.vala
187 lines (151 loc) · 4.65 KB
/
binary_clock.vala
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using Gtk;
public class BinaryDigitWidget : DrawingArea {
private int _digit = 0;
public int digit { get { return this._digit; }
set { this.foo(value); }}
public BinaryDigitWidget () {
// Set favored widget size
set_size_request (50, 200);
}
private void foo(int num)
requires(num >= 0 && num <= 9)
{
if (num != this._digit) {
this._digit = num;
this.redraw_canvas();
}
}
private void redraw_canvas () {
var window = get_window ();
if (null == window) {
return;
}
var region = window.get_clip_region ();
// redraw the cairo canvas completely by exposing it
window.invalidate_region (region, true);
window.process_updates (true);
}
/* Widget is asked to draw itself */
public override bool draw (Cairo.Context cr) {
int width = get_allocated_width ();
int height = get_allocated_height ();
// Black background
cr.save();
cr.set_source_rgba(0.0, 0.0, 0.0, 1.0);
cr.rectangle(0, 0, width, height);
cr.fill();
cr.restore();
// Draw each LED
// Lowest LED, calculate center, radius, and decide whether it's on or off.
cr.save();
switch (this.digit) {
case 1:
case 3:
case 5:
case 7:
case 9:
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
break;
default:
cr.set_source_rgba(0.0, 1.0, 0.0, 0.2);
break;
}
cr.arc(25, 125, 10, 0, 2 * Math.PI);
cr.fill();
cr.restore();
cr.save();
switch (this.digit) {
case 2:
case 3:
case 6:
case 7:
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
break;
default:
cr.set_source_rgba(0.0, 1.0, 0.0, 0.2);
break;
}
cr.arc(25, 100, 10, 0, 2 * Math.PI);
cr.fill();
cr.restore();
cr.save();
switch (this.digit) {
case 4:
case 5:
case 6:
case 7:
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
break;
default:
cr.set_source_rgba(0.0, 1.0, 0.0, 0.2);
break;
}
cr.arc(25, 75, 10, 0, 2 * Math.PI);
cr.fill();
cr.restore();
cr.save();
switch (this.digit) {
case 8:
case 9:
cr.set_source_rgba(0.0, 1.0, 0.0, 1.0);
break;
default:
cr.set_source_rgba(0.0, 1.0, 0.0, 0.2);
break;
}
cr.arc(25, 50, 10, 0, 2 * Math.PI);
cr.fill();
cr.restore();
return false;
}
}
class App : GLib.Object {
private BinaryDigitWidget seconds_1;
private BinaryDigitWidget seconds_2;
private BinaryDigitWidget minutes_1;
private BinaryDigitWidget minutes_2;
private BinaryDigitWidget hours_1;
private BinaryDigitWidget hours_2;
public App (string[] args) {
Gtk.init (ref args);
var window = new Window ();
var hbox = new HBox(true, 0);
seconds_1 = new BinaryDigitWidget ();
seconds_2 = new BinaryDigitWidget ();
minutes_1 = new BinaryDigitWidget ();
minutes_2 = new BinaryDigitWidget ();
hours_1 = new BinaryDigitWidget ();
hours_2 = new BinaryDigitWidget ();
hbox.add(hours_1);
hbox.add(hours_2);
hbox.add(minutes_1);
hbox.add(minutes_2);
hbox.add(seconds_1);
hbox.add(seconds_2);
window.add (hbox);
window.destroy.connect (Gtk.main_quit);
this.update_display();
window.show_all ();
Timeout.add(250, update_display);
Gtk.main ();
}
private bool update_display() {
// Get the current time
var now = new DateTime.now_local();
int hour = now.get_hour();
int minute = now.get_minute();
int second = now.get_second();
// Update the binary digit displays
hours_1.digit = hour / 10;
hours_2.digit = hour % 10;
minutes_1.digit = minute / 10;
minutes_2.digit = minute % 10;
seconds_1.digit = second / 10;
seconds_2.digit = second % 10;
return true;
}
}
int main (string[] args) {
new App (args);
return 0;
}