Root/userspace/glue/gui.ccp

1#pypp 0
2#include <iris.hh>
3#include <devices.hh>
4#include <keys.hh>
5
6// Interface: two way, started by ui.
7
8// From ui to application.
9// ~0: request reset.
10// ~1: set reply cap; send current state.
11// inum: event (with optional value) for input number num.
12
13// From application to ui.
14// onum: event (with optional value) for output number num.
15
16// For now, the code is hardwired to the alarm clock interface.
17enum outs:
18    CURRENT_TIME
19    ALARM
20
21enum ins:
22    TOTAL_TIME
23    START
24
25static Iris::Display display
26static Iris::Buzzer buzzer
27static unsigned *framebuffer
28static unsigned alarming
29
30enum PD:
31    UI
32    KBD
33
34static char const *chardef =
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
134static void draw_pixel (unsigned x, unsigned y, bool set):
135    for unsigned ty = 0; ty < 8; ++ty:
136        for unsigned tx = 0; tx < 8; ++tx:
137            framebuffer[320 * (y + ty) + x + tx] = (set ? 0xffffff : 0x000000)
138
139static void draw_num (bool upper, unsigned x0, unsigned d):
140    for unsigned y = 0; y < 8; ++y:
141        for unsigned x = 0; x < 6; ++x:
142            draw_pixel (x * 10 + 10 + x0 * 60, y * 10 + (upper ? 30 : 50 + 80), chardef[(d * 8 + y) * 6 + x] == '#')
143
144static void draw_time (bool upper, unsigned time):
145    unsigned min = time / 60
146    time %= 60
147    if min >= 100:
148        min = 99
149        time = 99
150    draw_num (upper, 0, min / 10)
151    draw_num (upper, 1, min % 10)
152    draw_num (upper, 3, time / 10)
153    draw_num (upper, 4, time % 10)
154
155static void do_alarm ():
156    static unsigned tune[] = { 4, 6, 6, 5, 7, 7 }
157    if ++alarming > sizeof (tune) / sizeof (*tune):
158        alarming = 1
159    buzzer.beep (tune[alarming - 1] * 220, 300, ~0)
160    Iris::my_receiver.set_alarm (HZ / 3)
161
162Iris::Num start ():
163    display = Iris::my_parent.get_capability <Iris::Display> ()
164    Iris::Setting bright = Iris::my_parent.get_capability <Iris::Setting> ()
165    Iris::Keyboard keyboard = Iris::my_parent.get_capability <Iris::Keyboard> ()
166    buzzer = Iris::my_parent.get_capability <Iris::Buzzer> ()
167    Iris::UI app = Iris::my_parent.get_capability <Iris::UI> ()
168
169    framebuffer = (unsigned *)0x15000
170    Iris::Caps fb = display.map_fb ((unsigned)framebuffer)
171
172    unsigned screen_max = bright.get_range ()
173    bright.set (0)
174    bool screen_on = false
175
176    Iris::Cap cb = Iris::my_receiver.create_capability (UI)
177    app.get_state (cb.copy ())
178    Iris::free_cap (cb)
179
180    cb = Iris::my_receiver.create_capability (KBD)
181    keyboard.set_cb (cb.copy ())
182    Iris::free_cap (cb)
183
184    draw_num (false, 2, 10)
185    draw_num (true, 2, 10)
186
187    unsigned total_time = 0
188    alarming = 0
189
190    Iris::my_parent.init_done ()
191
192    while true:
193        Iris::wait ()
194        switch Iris::recv.protected_data.l:
195            case ~0:
196                // Alarm.
197                if alarming:
198                    do_alarm ()
199                break
200            case UI:
201                switch Iris::recv.data[0].l:
202                    case CURRENT_TIME:
203                        draw_time (false, Iris::recv.data[1].l)
204                        break
205                    case ALARM:
206                        do_alarm ()
207                        break
208                    case TOTAL_TIME | Iris::UI::INPUT:
209                        total_time = Iris::recv.data[1].l
210                        draw_time (true, total_time)
211                        break
212                    case START | Iris::UI::INPUT:
213                        break
214                break
215            case KBD:
216                if Iris::recv.data[0].l & Iris::Keyboard::RELEASE:
217                    break
218                alarming = 0
219                switch Iris::recv.data[0].l:
220                    case Key::VOLUME_UP:
221                        total_time += 60
222                        draw_time (true, total_time)
223                        app.event (TOTAL_TIME, total_time)
224                        break
225                    case Key::VOLUME_DOWN:
226                        if total_time < 60:
227                            total_time = 0
228                        else:
229                            total_time -= 60
230                        draw_time (true, total_time)
231                        app.event (TOTAL_TIME, total_time)
232                        break
233                    case Key::UP:
234                        total_time += 10
235                        draw_time (true, total_time)
236                        app.event (TOTAL_TIME, total_time)
237                        break
238                    case Key::DOWN:
239                        if total_time < 10:
240                            total_time = 0
241                        else:
242                            total_time -= 10
243                        draw_time (true, total_time)
244                        app.event (TOTAL_TIME, total_time)
245                        break
246                    case Key::LEFT:
247                        if total_time < 1:
248                            total_time = 0
249                        else:
250                            total_time -= 1
251                        draw_time (true, total_time)
252                        app.event (TOTAL_TIME, total_time)
253                        break
254                    case Key::RIGHT:
255                        total_time += 1
256                        draw_time (true, total_time)
257                        app.event (TOTAL_TIME, total_time)
258                        break
259                    case Key::ENTER:
260                        app.event (START)
261                        break
262                    case Key::BACKSPACE:
263                        screen_on = !screen_on
264                        if screen_on:
265                            bright.set (screen_max)
266                        else:
267                            bright.set (0)
268                        break
269

Archive Download this file

Branches:
master



interactive