Skip to content

Commit

Permalink
qr settings - sound, aim, light
Browse files Browse the repository at this point in the history
  • Loading branch information
stepansnigirev committed Apr 29, 2021
1 parent 7f4e19f commit f40deb2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/gui/screens/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ def __init__(self, controls, title="Host setttings", note=None):
self.note.align(self.title, lv.ALIGN.OUT_BOTTOM_MID, 0, 5)
self.controls = controls
self.switches = []
y = 70
y = 40
for control in controls:
label = add_label(control["label"], y, scr=self.page)
hint = add_label(
control.get("hint", ""),
y + 40,
y + 30,
scr=self.page,
style="hint",
)
switch = lv.sw(self.page)
switch.align(hint, lv.ALIGN.OUT_BOTTOM_MID, 0, 20)
switch.align(hint, lv.ALIGN.OUT_BOTTOM_MID, 0, 10)
lbl = add_label(" OFF ON ", scr=self.page)
lbl.align(switch, lv.ALIGN.CENTER, 0, 0)
if control.get("value", False):
switch.on(lv.ANIM.OFF)
self.switches.append(switch)
y += 200
y = lbl.get_y() + 80

self.confirm_button.set_event_cb(on_release(self.update))
self.cancel_button.set_event_cb(on_release(lambda: self.set_value(None)))
Expand Down
72 changes: 66 additions & 6 deletions src/hosts/qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from platform import simulator, config, delete_recursively
from io import BytesIO
import gc
from gui.screens.settings import HostSettings
from gui.screens import Alert

QRSCANNER_TRIGGER = config.QRSCANNER_TRIGGER
# OK response from scanner
Expand All @@ -16,8 +18,6 @@
""" We switch the scanner to continuous mode to initiate scanning and
to command mode to stop scanning. No external trigger is necessary """
SETTINGS_ADDR = b"\x00\x00"
SETTINGS_CMD_MODE = 0xD1 # use command mode + aim light, no flash light
SETTINGS_CONT_MODE = 0xD2 # use continuous mode + aim light, no flash light

""" After basic scanner configuration (9600 bps) uart is set to 115200 bps
to support fast scanning of animated qrs """
Expand Down Expand Up @@ -57,6 +57,14 @@ class QRHost(Host):
def __init__(self, path, trigger=None, uart="YA", baudrate=9600):
super().__init__(path)

# default settings, extend it with more settings if applicable
self.settings = {
"enabled": True,
"aim": True,
"light": False,
"sound": True,
}

if simulator:
self.EOL = b"\r\n"
else:
Expand All @@ -77,6 +85,25 @@ def __init__(self, path, trigger=None, uart="YA", baudrate=9600):
self.scanning = False
self.parts = None

@property
def MASK(self):
b = (1<<7)
if self.settings.get("sound", True):
b |= (1<<6)
if self.settings.get("aim", True):
b |= (1<<4)
if self.settings.get("light", False):
b |= (1<<2)
return b

@property
def CMD_MODE(self):
return self.MASK | 1

@property
def CONT_MODE(self):
return self.MASK | 2

def query(self, data, timeout=100):
"""Blocking query"""
self.uart.write(data)
Expand Down Expand Up @@ -122,8 +149,8 @@ def configure(self):
val = self.get_setting(SETTINGS_ADDR)
if val is None:
return False
if val != SETTINGS_CMD_MODE:
self.set_setting(SETTINGS_ADDR, SETTINGS_CMD_MODE)
if val != self.CMD_MODE:
self.set_setting(SETTINGS_ADDR, self.CMD_MODE)
save_required = True

val = self.get_setting(TIMOUT_ADDR)
Expand Down Expand Up @@ -186,6 +213,39 @@ def init(self):
self.is_configured = True
pyb.LED(3).on()

async def settings_menu(self, show_screen, keystore):
title = "QR scanner"
controls = [{
"label": "Enable QR scanner",
"hint": "Enable or disable QR scanner and remove corresponding button from the main menu",
"value": self.settings.get("enabled", True)
}, {
"label": "Sound",
"hint": "To beep or not to beep?",
"value": self.settings.get("sound", True)
}, {
"label": "Aim light",
"hint": "Laser eyes!",
"value": self.settings.get("aim", True)
}, {
"label": "Flashlight",
"hint": "Can create blicks on the screen",
"value": self.settings.get("light", False)
}]
scr = HostSettings(controls, title=title)
res = await show_screen(scr)
if res:
enabled, sound, aim, light = res
self.settings = {
"enabled": enabled,
"aim": aim,
"light": light,
"sound": sound,
}
self.save_settings(keystore)
self.configure()
await show_screen(Alert("Success!", "\n\nSettings updated!", button_text="Close"))

def clean_uart(self):
self.uart.read()

Expand All @@ -194,7 +254,7 @@ def stop_scanning(self):
if self.trigger is not None:
self.trigger.on()
else:
self.set_setting(SETTINGS_ADDR, SETTINGS_CMD_MODE)
self.set_setting(SETTINGS_ADDR, self.CMD_MODE)

def abort(self):
self.data = None
Expand All @@ -206,7 +266,7 @@ async def scan(self):
if self.trigger is not None:
self.trigger.off()
else:
self.set_setting(SETTINGS_ADDR, SETTINGS_CONT_MODE)
self.set_setting(SETTINGS_ADDR, self.CONT_MODE)
self.data = b""
if self.f is not None:
self.f.close()
Expand Down

0 comments on commit f40deb2

Please sign in to comment.