Root/
| 1 | #pypp 0 |
| 2 | #include <iris.hh> |
| 3 | #include <ui.hh> |
| 4 | |
| 5 | enum Ins: |
| 6 | TOTAL_TIME |
| 7 | START |
| 8 | NUM_INS |
| 9 | |
| 10 | enum Outs: |
| 11 | CURRENT_TIME |
| 12 | ALARM |
| 13 | NUM_OUTS |
| 14 | |
| 15 | typedef UI <NUM_INS, NUM_OUTS> ui_t |
| 16 | static ui_t ui |
| 17 | static ui_t::in <unsigned> total_time |
| 18 | static ui_t::in_event do_start |
| 19 | static ui_t::out <unsigned> current_time |
| 20 | static ui_t::out_event do_alarm |
| 21 | |
| 22 | static bool ticking |
| 23 | |
| 24 | static void event (unsigned code): |
| 25 | switch code: |
| 26 | case TOTAL_TIME: |
| 27 | break |
| 28 | case START: |
| 29 | current_time = total_time |
| 30 | if !ticking: |
| 31 | if !current_time: |
| 32 | do_alarm () |
| 33 | else: |
| 34 | ticking = true |
| 35 | Iris::my_receiver.set_alarm (HZ) |
| 36 | break |
| 37 | default: |
| 38 | Iris::panic (0, "invalid event for alarm clock") |
| 39 | |
| 40 | Iris::Num start (): |
| 41 | ticking = false; |
| 42 | Iris::Cap ui_cap = Iris::my_receiver.create_capability (0) |
| 43 | ui.init (ui_cap.copy ()); |
| 44 | Iris::free_cap (ui_cap) |
| 45 | total_time.init () |
| 46 | do_start.init () |
| 47 | current_time.init () |
| 48 | do_alarm.init () |
| 49 | ui.add_in (&total_time, TOTAL_TIME); |
| 50 | ui.add_in (&do_start, START); |
| 51 | ui.add_out (¤t_time, CURRENT_TIME); |
| 52 | ui.add_out (&do_alarm, ALARM); |
| 53 | Iris::my_parent.init_done () |
| 54 | |
| 55 | while true: |
| 56 | Iris::wait () |
| 57 | switch Iris::recv.protected_data.l: |
| 58 | case ~0: |
| 59 | // alarm. |
| 60 | if current_time: |
| 61 | current_time = current_time - 1 |
| 62 | if !current_time: |
| 63 | do_alarm () |
| 64 | ticking = false |
| 65 | else: |
| 66 | // TODO: use rtc for scheduling an event. |
| 67 | Iris::my_receiver.set_alarm (HZ) |
| 68 | continue |
| 69 | case 0: |
| 70 | // ui event. |
| 71 | if !ui.event (&event): |
| 72 | // Exit request. |
| 73 | return 0 |
| 74 |
Branches:
master
