You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Use Intent Result API and more local variables
This commit is contained in:
@@ -14,33 +14,35 @@ except Exception as e:
|
||||
|
||||
from mpos.apps import Activity
|
||||
|
||||
width = 240
|
||||
height = 240
|
||||
|
||||
status_label_text = "No camera found."
|
||||
status_label_text_searching = "Searching QR codes...\n\nHold still and make them big!\n10cm for simple QR codes,\n20cm for complex."
|
||||
status_label_text_found = "Decoding QR..."
|
||||
|
||||
class Camera(Activity):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.cam = None
|
||||
self.current_cam_buffer = None # Variable to hold the current memoryview to prevent garbage collection
|
||||
self.image_dsc = None
|
||||
self.image = None
|
||||
self.qr_label = None
|
||||
self.scanqr_callback = None
|
||||
self.use_webcam = False
|
||||
self.qr_button = None
|
||||
self.snap_button = None
|
||||
self.capture_timer = None
|
||||
self.status_label = None
|
||||
self.status_label_cont = None
|
||||
self.keepliveqrdecoding = False
|
||||
width = 240
|
||||
height = 240
|
||||
|
||||
status_label_text = "No camera found."
|
||||
status_label_text_searching = "Searching QR codes...\n\nHold still and make them big!\n10cm for simple QR codes,\n20cm for complex."
|
||||
status_label_text_found = "Decoding QR..."
|
||||
|
||||
cam = None
|
||||
current_cam_buffer = None # Holds the current memoryview to prevent garbage collection
|
||||
|
||||
image = None
|
||||
image_dsc = None
|
||||
scanqr_mode = None
|
||||
use_webcam = False
|
||||
keepliveqrdecoding = False
|
||||
|
||||
capture_timer = None
|
||||
|
||||
# Widgets:
|
||||
qr_label = None
|
||||
qr_button = None
|
||||
snap_button = None
|
||||
status_label = None
|
||||
status_label_cont = None
|
||||
|
||||
def onCreate(self):
|
||||
self.scanqr_callback = self.getIntent().extras.get("scanqr_callback")
|
||||
self.scanqr_mode = self.getIntent().extras.get("scanqr_mode")
|
||||
main_screen = lv.obj()
|
||||
main_screen.set_style_pad_all(0, 0)
|
||||
main_screen.set_style_border_width(0, 0)
|
||||
@@ -76,13 +78,13 @@ class Camera(Activity):
|
||||
self.image_dsc = lv.image_dsc_t({
|
||||
"header": {
|
||||
"magic": lv.IMAGE_HEADER_MAGIC,
|
||||
"w": width,
|
||||
"h": height,
|
||||
"stride": width * 2,
|
||||
"w": self.width,
|
||||
"h": self.height,
|
||||
"stride": self.width * 2,
|
||||
"cf": lv.COLOR_FORMAT.RGB565
|
||||
#"cf": lv.COLOR_FORMAT.L8
|
||||
},
|
||||
'data_size': width * height * 2,
|
||||
'data_size': self.width * self.height * 2,
|
||||
'data': None # Will be updated per frame
|
||||
})
|
||||
self.image.set_src(self.image_dsc)
|
||||
@@ -115,15 +117,16 @@ class Camera(Activity):
|
||||
print("Camera initialized, continuing...")
|
||||
self.capture_timer = lv.timer_create(self.try_capture, 100, None)
|
||||
self.status_label_cont.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
if self.scanqr_callback:
|
||||
if self.scanqr_mode:
|
||||
self.start_qr_decoding()
|
||||
else:
|
||||
self.qr_button.remove_flag(lv.obj.FLAG.HIDDEN)
|
||||
self.snap_button.remove_flag(lv.obj.FLAG.HIDDEN)
|
||||
else:
|
||||
print("No camera found, stopping camtest.py")
|
||||
if self.scanqr_callback:
|
||||
self.scanqr_callback(False,"")
|
||||
if self.scanqr_mode:
|
||||
self.finish()
|
||||
|
||||
|
||||
def onStop(self, screen):
|
||||
print("camtest.py backgrounded, cleaning up...")
|
||||
@@ -138,26 +141,26 @@ class Camera(Activity):
|
||||
def qrdecode_one(self):
|
||||
try:
|
||||
import qrdecode
|
||||
result = qrdecode.qrdecode_rgb565(self.current_cam_buffer, width, height)
|
||||
result = qrdecode.qrdecode_rgb565(self.current_cam_buffer, self.width, self.height)
|
||||
#result = bytearray("INSERT_QR_HERE", "utf-8")
|
||||
if not result:
|
||||
self.status_label.set_text(status_label_text_searching)
|
||||
self.status_label.set_text(self.status_label_text_searching)
|
||||
else:
|
||||
self.stop_qr_decoding()
|
||||
result = remove_bom(result)
|
||||
result = print_qr_buffer(result)
|
||||
print(f"QR decoding found: {result}")
|
||||
if self.scanqr_callback:
|
||||
self.scanqr_callback(True,result)
|
||||
if self.scanqr_mode:
|
||||
self.setResult(True, result)
|
||||
self.finish()
|
||||
else:
|
||||
self.status_label.set_text(result) # in the future, the status_label text should be copy-paste-able
|
||||
except ValueError as e:
|
||||
print("QR ValueError: ", e)
|
||||
self.status_label.set_text(status_label_text_searching)
|
||||
self.status_label.set_text(self.status_label_text_searching)
|
||||
except TypeError as e:
|
||||
print("QR TypeError: ", e)
|
||||
self.status_label.set_text(status_label_text_found)
|
||||
self.status_label.set_text(self.status_label_text_found)
|
||||
except Exception as e:
|
||||
print("QR got other error: ", e)
|
||||
|
||||
@@ -186,14 +189,14 @@ class Camera(Activity):
|
||||
self.keepliveqrdecoding = True
|
||||
self.qr_label.set_text(lv.SYMBOL.EYE_CLOSE)
|
||||
self.status_label_cont.remove_flag(lv.obj.FLAG.HIDDEN)
|
||||
self.status_label.set_text(status_label_text_searching)
|
||||
self.status_label.set_text(self.status_label_text_searching)
|
||||
|
||||
def stop_qr_decoding(self):
|
||||
print("Deactivating live QR decoding...")
|
||||
self.keepliveqrdecoding = False
|
||||
self.qr_label.set_text(lv.SYMBOL.EYE_OPEN)
|
||||
status_label_text = self.status_label.get_text()
|
||||
if status_label_text == status_label_text_searching or status_label_text == status_label_text_found: # if it found a QR code, leave it
|
||||
self.status_label_text = self.status_label.get_text()
|
||||
if self.status_label_text in (self.status_label_text_searching or self.status_label_text_found): # if it found a QR code, leave it
|
||||
self.status_label_cont.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
|
||||
def qr_button_click(self, e):
|
||||
@@ -220,6 +223,8 @@ class Camera(Activity):
|
||||
except Exception as e:
|
||||
print(f"Camera capture exception: {e}")
|
||||
|
||||
|
||||
|
||||
# Non-class functions:
|
||||
def init_internal_cam():
|
||||
try:
|
||||
|
||||
@@ -322,14 +322,16 @@ class SettingActivity(Activity):
|
||||
cb.add_style(style_radio_chk, lv.PART.INDICATOR | lv.STATE.CHECKED)
|
||||
return cb
|
||||
|
||||
def gotqr_callback(self, success, data):
|
||||
print(f"gotqr_callback {success}, {data}")
|
||||
if success:
|
||||
def gotqr_result_callback(self, result):
|
||||
print(f"QR capture finished, result: {result}")
|
||||
if result.get("result_code"):
|
||||
data = result.get("data")
|
||||
print(f"Setting textarea data: {data}")
|
||||
self.textarea.set_text(data)
|
||||
|
||||
def cambutton_cb(self, event):
|
||||
print("cambutton clicked!")
|
||||
self.startActivity(Intent(activity_class=Camera).putExtra("scanqr_callback", self.gotqr_callback))
|
||||
self.startActivityForResult(Intent(activity_class=Camera).putExtra("scanqr_mode", True), self.gotqr_result_callback)
|
||||
|
||||
def save_setting(self, setting):
|
||||
if setting["key"] == "wallet_type" and self.radio_container:
|
||||
|
||||
@@ -7,31 +7,26 @@ import _thread
|
||||
from mpos.apps import Activity, Intent
|
||||
import mpos.ui
|
||||
|
||||
# Global variables because they're shared between activities:
|
||||
access_points={}
|
||||
|
||||
last_tried_ssid = ""
|
||||
last_tried_result = ""
|
||||
|
||||
scan_button_scan_text = "Rescan"
|
||||
scan_button_scanning_text = "Scanning..."
|
||||
|
||||
RESULT_CODE_CONNECT = 1
|
||||
RESULT_CODE_CANCEL = 0
|
||||
|
||||
|
||||
class WiFiConfig(Activity):
|
||||
|
||||
havenetwork = True
|
||||
scan_button_scan_text = "Rescan"
|
||||
scan_button_scanning_text = "Scanning..."
|
||||
|
||||
ssids=[]
|
||||
busy_scanning=False
|
||||
busy_connecting=False
|
||||
#selected_ssid=None
|
||||
havenetwork = True
|
||||
busy_scanning = False
|
||||
busy_connecting = False
|
||||
|
||||
# Widgets:
|
||||
aplist = None
|
||||
error_label=None
|
||||
scan_button=None
|
||||
scan_button_label=None
|
||||
error_label = None
|
||||
scan_button = None
|
||||
scan_button_label = None
|
||||
|
||||
def onCreate(self):
|
||||
main_screen = lv.obj()
|
||||
@@ -50,7 +45,7 @@ class WiFiConfig(Activity):
|
||||
self.scan_button.set_size(lv.SIZE_CONTENT,lv.pct(15))
|
||||
self.scan_button.align(lv.ALIGN.BOTTOM_MID,0,0)
|
||||
self.scan_button_label=lv.label(self.scan_button)
|
||||
self.scan_button_label.set_text(scan_button_scan_text)
|
||||
self.scan_button_label.set_text(self.scan_button_scan_text)
|
||||
self.scan_button_label.center()
|
||||
self.scan_button.add_event_cb(self.scan_cb,lv.EVENT.CLICKED,None)
|
||||
self.setContentView(main_screen)
|
||||
@@ -94,7 +89,7 @@ class WiFiConfig(Activity):
|
||||
self.show_error("Wi-Fi scan failed")
|
||||
# scan done:
|
||||
self.busy_scanning = False
|
||||
lv.async_call(lambda l: self.scan_button_label.set_text(scan_button_scan_text), None)
|
||||
lv.async_call(lambda l: self.scan_button_label.set_text(self.scan_button_scan_text), None)
|
||||
lv.async_call(lambda l: self.scan_button.add_flag(lv.obj.FLAG.CLICKABLE), None)
|
||||
lv.async_call(lambda l: self.refresh_list(), None)
|
||||
|
||||
@@ -105,7 +100,7 @@ class WiFiConfig(Activity):
|
||||
else:
|
||||
self.busy_scanning = True
|
||||
self.scan_button.remove_flag(lv.obj.FLAG.CLICKABLE)
|
||||
self.scan_button_label.set_text(scan_button_scanning_text)
|
||||
self.scan_button_label.set_text(self.scan_button_scanning_text)
|
||||
_thread.stack_size(mpos.apps.good_stack_size())
|
||||
_thread.start_new_thread(self.scan_networks_thread, ())
|
||||
|
||||
@@ -146,13 +141,13 @@ class WiFiConfig(Activity):
|
||||
|
||||
def password_page_result_cb(self, result):
|
||||
print(f"PasswordPage finished, result: {result}")
|
||||
if result.get("result_code") == RESULT_CODE_CONNECT:
|
||||
if result.get("result_code"):
|
||||
data = result.get("data")
|
||||
if data:
|
||||
self.start_attempt_connecting(data.get("ssid"), data.get("password"))
|
||||
|
||||
def start_attempt_connecting(self, ssid, password):
|
||||
print(f"start_attempt_connecting: Attempting to connect to SSID: {ssid}")
|
||||
print(f"start_attempt_connecting: Attempting to connect to SSID '{ssid}' with password '{password}'")
|
||||
self.scan_button.remove_flag(lv.obj.FLAG.CLICKABLE)
|
||||
self.scan_button_label.set_text(f"Connecting to {ssid}...")
|
||||
if self.busy_connecting:
|
||||
@@ -164,7 +159,7 @@ class WiFiConfig(Activity):
|
||||
|
||||
def attempt_connecting_thread(self, ssid, password):
|
||||
global last_tried_ssid, last_tried_result
|
||||
print(f"attempt_connecting: Attempting to connect to SSID: {ssid}")
|
||||
print(f"attempt_connecting_thread: Attempting to connect to SSID '{ssid}' with password '{password}'")
|
||||
result="connected"
|
||||
try:
|
||||
if self.havenetwork:
|
||||
@@ -189,7 +184,7 @@ class WiFiConfig(Activity):
|
||||
last_tried_result = result
|
||||
self.busy_connecting=False
|
||||
# Schedule UI updates because different thread
|
||||
lv.async_call(lambda l: self.scan_button_label.set_text(scan_button_scan_text), None)
|
||||
lv.async_call(lambda l: self.scan_button_label.set_text(self.scan_button_scan_text), None)
|
||||
lv.async_call(lambda l: self.scan_button.add_flag(lv.obj.FLAG.CLICKABLE), None)
|
||||
lv.async_call(lambda l: self.refresh_list(), None)
|
||||
|
||||
@@ -299,13 +294,12 @@ class PasswordPage(Activity):
|
||||
access_points[self.selected_ssid]=password
|
||||
print(f"connect_cb: Updated access_points: {access_points}")
|
||||
save_config()
|
||||
self.setResult(RESULT_CODE_CONNECT, {"ssid": self.selected_ssid, "password": password})
|
||||
self.setResult(True, {"ssid": self.selected_ssid, "password": password})
|
||||
print("connect_cb: Restoring main_screen")
|
||||
self.finish()
|
||||
|
||||
def cancel_cb(self, event):
|
||||
print("cancel_cb: Cancel button clicked")
|
||||
self.setResult(RESULT_CODE_CANCEL, None)
|
||||
self.finish()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user