|
| 1 | +import ..devices |
| 2 | +import ..services |
| 3 | +import ..messages.messages_gen as messages |
| 4 | +import .survey |
| 5 | +import ..modules.eink.menu-selection show MenuSelection |
| 6 | +import .survey.eink-batch show eink-do-batch |
| 7 | +import .survey.strobe-once show strobe-once |
| 8 | +import log |
| 9 | +import watchdog show Watchdog |
| 10 | + |
| 11 | +class Apps: |
| 12 | + |
| 13 | + device_/Device |
| 14 | + dog_/Watchdog |
| 15 | + is-running_/bool := false |
| 16 | + showing-page_/int := 0 |
| 17 | + menu-selection/MenuSelection? := null |
| 18 | + app_/any? := null // TODO make an app interface? |
| 19 | + logger_/log.Logger := log.default.with-name "apps" |
| 20 | + buttons-subscriber-id_/int? := null |
| 21 | + |
| 22 | + MENU-OPTIONS := [ |
| 23 | + "Survey", |
| 24 | + "Reboot", |
| 25 | + "Go Back", |
| 26 | + ] |
| 27 | + MENU-OPTION-SURVEY := 0 |
| 28 | + MENU-OPTION-REBOOT:= 1 |
| 29 | + MENU-OPTION-GO-BACK := 2 |
| 30 | + |
| 31 | + PAGE-HOME := 1 |
| 32 | + PAGE-MENU := 20 |
| 33 | + |
| 34 | + constructor device/Device dog/Watchdog: |
| 35 | + device_ = device |
| 36 | + dog_ = dog |
| 37 | + self := this |
| 38 | + |
| 39 | + show-home: |
| 40 | + eink-do-batch --important: |
| 41 | + // logger_.info "HOME" |
| 42 | + device_.eink.show-preset --page-id=PAGE-HOME |
| 43 | + showing-page_ = PAGE-HOME |
| 44 | + menu-selection = null |
| 45 | + |
| 46 | + show_menu: |
| 47 | + eink-do-batch --important: |
| 48 | + // logger_.info "MENU" |
| 49 | + device_.eink.send-menu --page-id=PAGE-MENU --items=MENU-OPTIONS --selected-item=0 |
| 50 | + showing-page_ = PAGE-MENU |
| 51 | + menu-selection = MenuSelection --start=0 --size=MENU-OPTIONS.size |
| 52 | + |
| 53 | + // Basic app control, similar to SurveyApp. |
| 54 | + start: |
| 55 | + // logger_.info "START" |
| 56 | + is-running_ = true |
| 57 | + showing-page_ = PAGE-HOME // We assume we start on the home page.. |
| 58 | + self := this |
| 59 | + |
| 60 | + // Subscribe to buttons if we're not already subscribed. |
| 61 | + if not buttons-subscriber-id_: |
| 62 | + e := catch: |
| 63 | + id := device_.buttons.subscribe --timeout=null --callback=(:: |button-data| |
| 64 | + if button-data.duration <= 0: |
| 65 | + // Not actually a press... TODO make this nicer |
| 66 | + else: |
| 67 | + task:: |
| 68 | + strobe-once: |
| 69 | + device_.strobe.blue |
| 70 | + sleep --ms=50 |
| 71 | + device_.strobe.off |
| 72 | + if button-data.duration >= 3000: // 3s any button = home (for now) |
| 73 | + show-home |
| 74 | + else if self.app_ and self.app_.is-running: |
| 75 | + // If an app is running, let it handle button presses |
| 76 | + else if showing-page_ == PAGE-HOME: // TODO use a preset page const ID |
| 77 | + if button-data.button-id == messages.ButtonPress.BUTTON-ID-RIGHT-DOWN: |
| 78 | + self.show_menu |
| 79 | + else if showing-page_ == PAGE-MENU: |
| 80 | + if button-data.button-id == messages.ButtonPress.BUTTON-ID-ACTION: |
| 81 | + if menu-selection.current == MENU-OPTION-SURVEY: |
| 82 | + task:: open-survey-app |
| 83 | + else if menu-selection.current == MENU-OPTION-REBOOT: |
| 84 | + log.info "Rebooting device" |
| 85 | + device_.comms.send messages.CPU1Reset.do-msg |
| 86 | + else if menu-selection.current == MENU-OPTION-GO-BACK: |
| 87 | + show-home |
| 88 | + else if button-data.button-id == messages.ButtonPress.BUTTON-ID-RIGHT-DOWN: |
| 89 | + menu-selection.up |
| 90 | + else if button-data.button-id == messages.ButtonPress.BUTTON-ID-LEFT-UP: |
| 91 | + menu-selection.down |
| 92 | + else: |
| 93 | + // logger_.info "BTN miss $button-data" |
| 94 | + ) |
| 95 | + |
| 96 | + if not id: |
| 97 | + logger_.warn "BTN sub fail" |
| 98 | + else: |
| 99 | + buttons-subscriber-id_ = id |
| 100 | + logger_.warn "BTN sub $(id)" |
| 101 | + if e: |
| 102 | + logger_.error "BTN sub fail $e" |
| 103 | + |
| 104 | + open-survey-app: |
| 105 | + // logger_.info "OPEN SURVEY" |
| 106 | + stop |
| 107 | + app_ = SurveyApp device_ this dog_ |
| 108 | + app_.start |
| 109 | + app_.show-survey |
| 110 | + |
| 111 | + stop: |
| 112 | + // logger_.info "STOP" |
| 113 | + showing-page_ = 0 // We no longer assume to know |
| 114 | + is-running_ = false |
| 115 | + // Unsubscribe from buttons if we have a subscriber id |
| 116 | + if buttons-subscriber-id_: |
| 117 | + e := catch: device_.buttons.unsubscribe --subscriber-id=buttons-subscriber-id_ --timeout=null |
| 118 | + // Ignore errors from unsubscribe but clear our id. |
| 119 | + buttons-subscriber-id_ = null |
| 120 | + |
| 121 | + is-running -> bool: |
| 122 | + return is-running_ |
0 commit comments