diff --git a/boot.py b/boot.py index 0325c16..ca48145 100644 --- a/boot.py +++ b/boot.py @@ -29,6 +29,7 @@ def any_home(): return h[0] if len(h) else False if "no_boot" in root: + os.remove("no_boot") print("no_boot found, aborting boot sequence") elif "bootstrap.py" in root: print("Bootstrapping...") diff --git a/home_default/main.py b/home_default/main.py index d48d40d..129c087 100644 --- a/home_default/main.py +++ b/home_default/main.py @@ -34,5 +34,12 @@ status = ugfx.Label(0, 130, ugfx.width(), 40, "") # , justification=ugfx.Label.C # update loop while True: - status.text("wifi: %s%%\nbattery: %s%%" % (int(wifi_strength() * 100), int(battery() * 100))) + text = ""; + value_wifi_strength = wifi_strength() + value_battery = battery() + if value_wifi_strength: + text += "wifi: %s%%\n" % int(value_wifi_strength) + if value_battery: + text += "battery: %s%%\n" % int(value_battery) + status.text(text) sleep_or_exit(0.5) diff --git a/lib/homescreen.py b/lib/homescreen.py index af1d177..4e06925 100644 --- a/lib/homescreen.py +++ b/lib/homescreen.py @@ -19,7 +19,7 @@ They also *may*: ___license___ = "MIT" ___dependencies___ = ["database", "buttons", "random", "app", "sleep", "ugfx_helper", "wifi"] -import database, ugfx, random, buttons, tilda, sleep, ugfx_helper, wifi +import database, ugfx, random, buttons, tilda, sleep, ugfx_helper, wifi, time from app import App _state = None @@ -41,6 +41,12 @@ def set_state(key, value = True): def clean_up(): pass +def time_as_string(seconds=False): + t = time.localtime() + if seconds: + return "%d:%02d:%02d" % (t[3], t[4], t[5]) + return "%d:%02d" % (t[3], t[4]) #todo: add a setting for AM/PM mode + def sleep_or_exit(interval = 0.5): # todo: do this better - check button multiple times and sleep for only a short while if buttons.is_triggered(tilda.Buttons.BTN_Menu): @@ -52,9 +58,12 @@ def sleep_or_exit(interval = 0.5): def name(default = None): return database.get("homescreen.name", default) +# Strength in %, None if unavailable def wifi_strength(): - return random.random() + return wifi.get_strength() +# Charge in %, None if unavailable def battery(): - return random.random() + return None # todo: fix me, we can get this from the sim800 + diff --git a/lib/test_dialogs.py b/lib/test_dialogs.py index 25ee20c..6fdf344 100644 --- a/lib/test_dialogs.py +++ b/lib/test_dialogs.py @@ -18,19 +18,19 @@ class TestDialogs(unittest.TestCase): def tearDownClass(self): ugfx_helper.deinit() -# def test_waiting(self): -# count_max = 3 -# with WaitingMessage("Testing...", "Foo") as c: -# for i in range(1, count_max): -# c.text = "%d/%d" % (i, count_max) -# -# print("done") + def test_waiting(self): + count_max = 3 + with WaitingMessage("Testing...", "Foo") as c: + for i in range(1, count_max): + c.text = "%d/%d" % (i, count_max) - def test_(self): + print("done") + + def test_text(self): prompt_text("description") def test_option(self): - print(prompt_option(["foo", "bar", "baz"])) + print(prompt_option(["foo", "bar", "baz"])) diff --git a/lib/test_homescreen.py b/lib/test_homescreen.py new file mode 100644 index 0000000..24664ad --- /dev/null +++ b/lib/test_homescreen.py @@ -0,0 +1,27 @@ +"""Tests for hall effect sensor""" + +___license___ = "MIT" +___dependencies___ = ["upip:unittest", "homescreen", "database"] + +import unittest, homescreen, database + +class TestHomescreen(unittest.TestCase): + + def test_name(self): + o = database.get("homescreen.name") + database.delete("homescreen.name") + self.assertEqual(homescreen.name("default"), "default") + database.set("homescreen.name", "foo") + self.assertEqual(homescreen.name("default"), "foo") + database.set("homescreen.name", o) + + def test_time(self): + self.assertIn(len(homescreen.time_as_string()), [4, 5]) + self.assertIn(len(homescreen.time_as_string(True)), [7, 8]) + + def test_wifi_strength(self): + # test that it doesn't throw an exception + homescreen.wifi_strength() + +if __name__ == '__main__': + unittest.main() diff --git a/lib/wifi.py b/lib/wifi.py index b306c20..7fe8259 100644 --- a/lib/wifi.py +++ b/lib/wifi.py @@ -87,12 +87,16 @@ def connect_wifi(details, timeout, wait=False): def is_connected(): return nic().isconnected() +# returns wifi strength in %, None if unavailable def get_strength(): n = nic() if n.isconnected(): - r - else: - return None + v = n.status("rssi"); + if v: + # linear range: -60 =100%; -100= 20% + # todo: it's probably not linear, improve me. + return v * 2 + 220 + return None def get_security_level(ap): #todo: fix this diff --git a/sponsors/main.py b/sponsors/main.py index 5f768e2..759aec8 100644 --- a/sponsors/main.py +++ b/sponsors/main.py @@ -2,11 +2,11 @@ ___name___ = "Sponsors" ___license___ = "MIT" -___dependencies___ = ["wifi", "http", "ugfx_helper", "sleep"] +___dependencies___ = ["wifi", "http", "ugfx_helper", "sleep", "app"] ___categories___ = ["EMF"] ___bootstrapped___ = True -import ugfx_helper, os, wifi, ugfx, http, time, sleep +import ugfx_helper, os, wifi, ugfx, http, time, sleep, app from tilda import Buttons ugfx_helper.init() @@ -24,3 +24,4 @@ while (not Buttons.is_pressed(Buttons.BTN_A)) and (not Buttons.is_pressed(Button sleep.wfi() ugfx.clear() +app.restart_to_default()