def loop(w, ev_can_i_boot): """ checks we have the best network connection possible """ # maybe no need for th_net to be blocked # wait_boot_signal(w, ev_can_i_boot, 'NET') # check our platform CELL capabilities desktop_or_no_cell_shield = (not linux_is_rpi()) or ( not ctx.cell_shield_en) cell_present = not desktop_or_no_cell_shield w.sig_net.status.emit('NET: cell features -> {}'.format(cell_present)) # loop here when desktop computer or DDH w/o cell old_s = '' while desktop_or_no_cell_shield: s = net_get_my_current_wlan_ssid() if s != old_s: w.sig_net.update.emit('no network' if not s else s) old_s = s time.sleep(TH_NET_PERIOD_S) # loop here when DDH w/ cell shield while 1: ctx.sem_ble.acquire() ctx.sem_aws.acquire() net_check_connectivity(w.sig_net) ctx.sem_aws.release() ctx.sem_ble.release() time.sleep(TH_NET_PERIOD_S)
def setup_buttons_rpi(my_app, c_log): """ link raspberry buttons with callback functions """ a = my_app if not linux_is_rpi(): c_log.debug('SYS: not a raspberry system') return def button1_pressed_cb(): a.keyPressEvent(ButtonPressEvent(Qt.Key_1)) def button2_pressed_cb(): a.keyPressEvent(ButtonPressEvent(Qt.Key_2)) # upon release, check it was a press or a hold def button3_held_cb(): a.btn_3_held = 1 def button3_released_cb(): if a.btn_3_held: a.keyPressEvent(ButtonPressEvent(Qt.Key_6)) else: a.keyPressEvent(ButtonPressEvent(Qt.Key_3)) a.btn_3_held = 0 a.button1 = Button(16, pull_up=True) a.button2 = Button(20, pull_up=True) a.button3 = Button(21, pull_up=True) a.button1.when_pressed = button1_pressed_cb a.button2.when_pressed = button2_pressed_cb a.button3.when_held = button3_held_cb a.button3.when_released = button3_released_cb
def _boot_connect_gps(w): t = ctx.BOOT_GPS_1ST_FIX_TIMEOUT assert (t >= 10) w.lbl_ble.setText(BOOT_GPS_WAIT_MESSAGE.format(t / 60)) # on Desktop, do not wait desktop_or_no_cell_shield = (not linux_is_rpi()) or ( not ctx.cell_shield_en) if desktop_or_no_cell_shield: e = 'no GPS / cell shield to connect to' w.lbl_ble.setText(e) w.sig_boot.debug.emit('BOO: {}'.format(e)) return # tries twice to enable GPS, used for position and time source if not dbg_hook_make_gps_give_fake_measurement: rv = gps_configure_quectel() if rv == 0: return time.sleep(1) rv = gps_configure_quectel() if rv: w.sig_boot.error.emit('BOO: error opening GPS port') os._exit(rv) sys.exit(rv)
def click_icon_gps(self, ev): """ clicking shift + GPS icon adjusts DDH brightness :) """ ev.accept() s = 'GUI: secret GPS tap {}'.format(self.bright_idx) c_log.debug(s) if linux_is_rpi(): self.bright_idx = (self.bright_idx + 1) % 3 v = self.bright_idx rpi_set_brightness(v)
def setup_window_center(my_app): """ on RPi, DDH app uses full screen """ a = my_app if linux_is_rpi() or linux_is_docker_on_rpi(): # rpi is 800 x 480 a.showFullScreen() # get window + screen shape, match both, adjust upper left corner r = a.frameGeometry() c = QDesktopWidget().availableGeometry().center() r.moveCenter(c) a.move(r.topLeft())
def _boot_sync_time(w): """ th_boot gets datetime source """ rv = 'local' desktop_or_no_cell_shield = (not linux_is_rpi()) or ( not ctx.cell_shield_en) list_src = ('NTP', ) if not desktop_or_no_cell_shield else ('GPS', 'NTP') for i in range(3): rv = utils_time_update_datetime_source(w) if rv in list_src: break time.sleep(5) w.sig_boot.debug.emit('BOO: sync time {}'.format(rv))
def rpi_set_brightness(v): """ adjusts DDH screen brightness """ if not linux_is_rpi(): return v *= 127 v = 50 if v < 50 else v v = 255 if v > 250 else v b = '/sys/class/backlight/rpi_backlight/brightness"' s = 'sudo bash -c "echo {} > {}'.format(str(v), b) o = sp.DEVNULL sp.run(shlex.split(s), stdout=o, stderr=o)
def _boot_sync_position(w): """ th_boot gets first GPS position """ desktop_or_no_cell_shield = (not linux_is_rpi()) or ( not ctx.cell_shield_en) if desktop_or_no_cell_shield: e = 'no GPS / cell shield to wait position' w.lbl_ble.setText(e) w.sig_boot.debug.emit('BOO: {}'.format(e)) return t = ctx.BOOT_GPS_1ST_FIX_TIMEOUT _o = utils_gps_get_one_lat_lon_dt(timeout=t, sig=w.sig_gps) w.sig_gps.update.emit(_o)
def _net_is_worth_trying_sw_wifi(interface: str) -> bool: """ in case wifis around, and known, we have a good scenario """ if not linux_is_rpi(): print('no RPI system, no wpa_cli') return False kn = _net_get_known_wlan_ssids() an = _net_get_nearby_wlan_ssids(interface) if an is None: # interface wi-fi error, sure to try switching return True for _ in an: if _ in kn: return True return False
def utils_gps_get_one_lat_lon_dt(timeout=3, sig=None): """ returns (lat, lon, dt object) or None for a dummy or real GPS measurement """ # debug hook, returns None if ctx.dbg_hook_make_gps_to_fail: # print('DBG: returning GPS None forced') return # debug hook, returns our custom GPS frame t = timeout if ctx.dbg_hook_make_gps_give_fake_measurement: if sig: sig.debug.emit('GPS: dbg_hook_gps_fake_measurement') # remember: for GPS use .utcnow(), not .now() time.sleep(5) dt = datetime.utcnow() lat = '{:+.6f}'.format(12.34567866666666) lon = '{:+.6f}'.format(-144.777777666666667) # you can also 'return None' to simulate an error return lat, lon, dt # no fake measurement, let's see what we are if not linux_is_rpi(): # sig.debug.emit('GPS: nope for desktop / laptop') return # get one measurement None / (lat, lon, gps_time) d = gps_get_rmc_data(timeout=t) # cache in case is a good one if d: utils_gps_cache_set(d) return d