Skip to content

Commit

Permalink
Badger OS: Add keepalive to fix #7.
Browse files Browse the repository at this point in the history
Holding down a button, or pressing a button with the "right" timing would keep the Badger on, despite the 3v3 en pin being turned off.

This would result in abrupt power loss when the button is released, causing screen corruption.
  • Loading branch information
Gadgetoid committed Mar 21, 2023
1 parent 8a8244a commit 92e62e0
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 4 deletions.
4 changes: 4 additions & 0 deletions badger_os/examples/badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ def draw_badge():
draw_badge()

while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()

if display.pressed(badger2040.BUTTON_A) or display.pressed(badger2040.BUTTON_B) or display.pressed(badger2040.BUTTON_C) or display.pressed(badger2040.BUTTON_UP) or display.pressed(badger2040.BUTTON_DOWN):
badger_os.warning(display, "To change the text, connect Badger2040 to a PC, load up Thonny, and modify badge.txt")
time.sleep(4)
Expand Down
4 changes: 4 additions & 0 deletions badger_os/examples/ebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def render_page():
state["offsets"] = []

while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()

# Was the next page button pressed?
if display.pressed(badger2040.BUTTON_DOWN):
state["current_page"] += 1
Expand Down
4 changes: 4 additions & 0 deletions badger_os/examples/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def draw_fonts():
# ------------------------------

while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()

if display.pressed(badger2040.BUTTON_UP):
state["selected_font"] -= 1
if state["selected_font"] < 0:
Expand Down
1 change: 1 addition & 0 deletions badger_os/examples/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
# Call halt in a loop, on battery this switches off power.
# On USB, the app will exit when A+C is pressed because the launcher picks that up.
while True:
display.keepalive()
display.halt()
4 changes: 4 additions & 0 deletions badger_os/examples/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def show_image(n):


while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()

if display.pressed(badger2040.BUTTON_UP):
if state["current_image"] > 0:
state["current_image"] -= 1
Expand Down
1 change: 1 addition & 0 deletions badger_os/examples/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
# Call halt in a loop, on battery this switches off power.
# On USB, the app will exit when A+C is pressed because the launcher picks that up.
while True:
display.keepalive()
display.halt()
4 changes: 4 additions & 0 deletions badger_os/examples/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ def draw_checkbox(x, y, size, background, foreground, thickness, tick, padding):
# ------------------------------

while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()

if len(list_items) > 0:
if display.pressed(badger2040.BUTTON_A):
if state["current_item"] > 0:
Expand Down
1 change: 1 addition & 0 deletions badger_os/examples/net_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@
# Call halt in a loop, on battery this switches off power.
# On USB, the app will exit when A+C is pressed because the launcher picks that up.
while True:
display.keepalive()
display.halt()
3 changes: 1 addition & 2 deletions badger_os/examples/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ def draw_page():

draw_page()

while 1:

while True:
changed = False

if button_down.value():
Expand Down
4 changes: 4 additions & 0 deletions badger_os/examples/qrgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def draw_qr_file(n):
changed = True

while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()

if TOTAL_CODES > 1:
if display.pressed(badger2040.BUTTON_UP):
if state["current_qr"] > 0:
Expand Down
1 change: 1 addition & 0 deletions badger_os/examples/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ def draw_page():
# Call halt in a loop, on battery this switches off power.
# On USB, the app will exit when A+C is pressed because the launcher picks that up.
while True:
display.keepalive()
display.halt()
4 changes: 4 additions & 0 deletions badger_os/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def button(pin):
display.set_update_speed(badger2040.UPDATE_FAST)

while True:
# Sometimes a button press or hold will keep the system
# powered *through* HALT, so latch the power back on.
display.keepalive()

if display.pressed(badger2040.BUTTON_A):
button(badger2040.BUTTON_A)
if display.pressed(badger2040.BUTTON_B):
Expand Down
11 changes: 10 additions & 1 deletion firmware/PIMORONI_BADGER2040/lib/badger2040.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@

WAKEUP_MASK = 0

enable = machine.Pin(ENABLE_3V3, machine.Pin.OUT)
enable.on()


def is_wireless():
return False
Expand Down Expand Up @@ -88,9 +91,12 @@ def system_speed(speed):
pass


def turn_on():
enable.on()


def turn_off():
time.sleep(0.05)
enable = machine.Pin(ENABLE_3V3, machine.Pin.OUT)
enable.off()
# Simulate an idle state on USB power by blocking
# until a button event
Expand Down Expand Up @@ -150,6 +156,9 @@ def thickness(self, thickness):
def halt(self):
turn_off()

def keepalive(self):
turn_on()

def pressed(self, button):
return BUTTONS[button].value() == (0 if button == BUTTON_USER else 1) or pressed_to_wake_get_once(button)

Expand Down
11 changes: 10 additions & 1 deletion firmware/PIMORONI_BADGER2040W/lib/badger2040.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
i2c.writeto_mem(0x51, 0x00, b'\x00') # ensure rtc is running (this should be default?)
rtc.enable_timer_interrupt(False)

enable = machine.Pin(ENABLE_3V3, machine.Pin.OUT)
enable.on()


def is_wireless():
return True
Expand Down Expand Up @@ -92,9 +95,12 @@ def system_speed(speed):
pass


def turn_on():
enable.on()


def turn_off():
time.sleep(0.05)
enable = machine.Pin(ENABLE_3V3, machine.Pin.OUT)
enable.off()
# Simulate an idle state on USB power by blocking
# until an RTC alarm or button event
Expand Down Expand Up @@ -187,6 +193,9 @@ def thickness(self, thickness):
def halt(self):
turn_off()

def keepalive(self):
turn_on()

def pressed(self, button):
return BUTTONS[button].value() == 1 or pressed_to_wake_get_once(button)

Expand Down

0 comments on commit 92e62e0

Please sign in to comment.