diff --git a/micropython/examples/badger2040/badge.py b/micropython/examples/badger2040/badge.py index ab0652e6..352c35df 100644 --- a/micropython/examples/badger2040/badge.py +++ b/micropython/examples/badger2040/badge.py @@ -172,16 +172,17 @@ def draw_badge(): # Create a new Badger and set it to update NORMAL display = badger2040.Badger2040() +display.led(128) display.update_speed(badger2040.UPDATE_NORMAL) # Open the badge file try: badge = open("badge.txt", "r") except OSError: - badge = open("badge.txt", "w") - badge.write(DEFAULT_TEXT) - badge.flush() - badge.seek(0) + with open("badge.txt", "w") as f: + f.write(DEFAULT_TEXT) + f.flush() + badge = open("badge.txt", "r") # Read in the next 6 lines company = badge.readline() # "mustelid inc" diff --git a/micropython/examples/badger2040/clock.py b/micropython/examples/badger2040/clock.py index 834ab5ed..e1852ad7 100644 --- a/micropython/examples/badger2040/clock.py +++ b/micropython/examples/badger2040/clock.py @@ -4,9 +4,10 @@ import badger2040 rtc = machine.RTC() -screen = badger2040.Badger2040() -screen.update_speed(badger2040.UPDATE_TURBO) -screen.font("gothic") +display = badger2040.Badger2040() +display.led(128) +display.update_speed(badger2040.UPDATE_TURBO) +display.font("gothic") cursors = ["year", "month", "day", "hour", "minute"] set_clock = False @@ -36,6 +37,9 @@ def button(pin): if not pin.value(): return + if button_a.value() and button_c.value(): + machine.reset() + adjust = 0 changed = False @@ -94,42 +98,42 @@ def draw_clock(): hms = "{:02}:{:02}:{:02}".format(hour, minute, second) ymd = "{:04}/{:02}/{:02}".format(year, month, day) - hms_width = screen.measure_text(hms, 1.8) + hms_width = display.measure_text(hms, 1.8) hms_offset = int((badger2040.WIDTH / 2) - (hms_width / 2)) - h_width = screen.measure_text(hms[0:2], 1.8) - mi_width = screen.measure_text(hms[3:5], 1.8) - mi_offset = screen.measure_text(hms[0:3], 1.8) + h_width = display.measure_text(hms[0:2], 1.8) + mi_width = display.measure_text(hms[3:5], 1.8) + mi_offset = display.measure_text(hms[0:3], 1.8) - ymd_width = screen.measure_text(ymd, 1.0) + ymd_width = display.measure_text(ymd, 1.0) ymd_offset = int((badger2040.WIDTH / 2) - (ymd_width / 2)) - y_width = screen.measure_text(ymd[0:4], 1.0) - m_width = screen.measure_text(ymd[5:7], 1.0) - m_offset = screen.measure_text(ymd[0:5], 1.0) - d_width = screen.measure_text(ymd[8:10], 1.0) - d_offset = screen.measure_text(ymd[0:8], 1.0) + y_width = display.measure_text(ymd[0:4], 1.0) + m_width = display.measure_text(ymd[5:7], 1.0) + m_offset = display.measure_text(ymd[0:5], 1.0) + d_width = display.measure_text(ymd[8:10], 1.0) + d_offset = display.measure_text(ymd[0:8], 1.0) - screen.pen(15) - screen.clear() - screen.pen(0) - screen.thickness(5) - screen.text(hms, hms_offset, 40, 1.8) - screen.thickness(3) - screen.text(ymd, ymd_offset, 100, 1.0) + display.pen(15) + display.clear() + display.pen(0) + display.thickness(5) + display.text(hms, hms_offset, 40, 1.8) + display.thickness(3) + display.text(ymd, ymd_offset, 100, 1.0) if set_clock: if cursors[cursor] == "year": - screen.line(ymd_offset, 120, ymd_offset + y_width, 120) + display.line(ymd_offset, 120, ymd_offset + y_width, 120) if cursors[cursor] == "month": - screen.line(ymd_offset + m_offset, 120, ymd_offset + m_offset + m_width, 120) + display.line(ymd_offset + m_offset, 120, ymd_offset + m_offset + m_width, 120) if cursors[cursor] == "day": - screen.line(ymd_offset + d_offset, 120, ymd_offset + d_offset + d_width, 120) + display.line(ymd_offset + d_offset, 120, ymd_offset + d_offset + d_width, 120) if cursors[cursor] == "hour": - screen.line(hms_offset, 70, hms_offset + h_width, 70) + display.line(hms_offset, 70, hms_offset + h_width, 70) if cursors[cursor] == "minute": - screen.line(hms_offset + mi_offset, 70, hms_offset + mi_offset + mi_width, 70) + display.line(hms_offset + mi_offset, 70, hms_offset + mi_offset + mi_width, 70) - screen.update() + display.update() year, month, day, wd, hour, minute, second, _ = rtc.datetime() diff --git a/micropython/examples/badger2040/ebook.py b/micropython/examples/badger2040/ebook.py index c2bb2898..ae67e020 100644 --- a/micropython/examples/badger2040/ebook.py +++ b/micropython/examples/badger2040/ebook.py @@ -92,6 +92,7 @@ current_page = 0 # Create a new Badger and set it to update FAST display = badger2040.Badger2040() +display.led(128) display.update_speed(badger2040.UPDATE_FAST) # Set up the buttons @@ -100,9 +101,8 @@ button_up = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_D button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN) button_b = machine.Pin(badger2040.BUTTON_B, machine.Pin.IN, machine.Pin.PULL_DOWN) +button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN) -# Set up the activity LED -led = machine.Pin(badger2040.PIN_LED, machine.Pin.OUT) offsets = [] @@ -111,6 +111,13 @@ offsets = [] def button(pin): global next_page, prev_page, change_font_size, change_font + time.sleep(0.05) + if not pin.value(): + return + + if button_a.value() and button_c.value(): + machine.reset() + if pin == button_down: next_page = True @@ -129,6 +136,7 @@ button_down.irq(trigger=machine.Pin.IRQ_RISING, handler=button) button_up.irq(trigger=machine.Pin.IRQ_RISING, handler=button) button_a.irq(trigger=machine.Pin.IRQ_RISING, handler=button) button_b.irq(trigger=machine.Pin.IRQ_RISING, handler=button) +button_c.irq(trigger=machine.Pin.IRQ_RISING, handler=button) # ------------------------------ diff --git a/micropython/examples/badger2040/fonts.py b/micropython/examples/badger2040/fonts.py index 499bda47..b135d014 100644 --- a/micropython/examples/badger2040/fonts.py +++ b/micropython/examples/badger2040/fonts.py @@ -96,6 +96,7 @@ pressed = False # Create a new Badger and set it to update FAST display = badger2040.Badger2040() +display.led(128) display.update_speed(badger2040.UPDATE_FAST) # Set up the buttons diff --git a/micropython/examples/badger2040/help.py b/micropython/examples/badger2040/help.py index ff6102be..4f000f71 100644 --- a/micropython/examples/badger2040/help.py +++ b/micropython/examples/badger2040/help.py @@ -1,11 +1,12 @@ import badger2040 -import time from badger2040 import WIDTH TEXT_SIZE = 0.45 LINE_HEIGHT = 16 display = badger2040.Badger2040() +display.led(128) + display.pen(0) display.rectangle(0, 0, WIDTH, 16) display.thickness(1) @@ -27,7 +28,7 @@ y += LINE_HEIGHT y += LINE_HEIGHT display.thickness(2) -display.text("Holding USER button:", 0, y, TEXT_SIZE) +display.text("Hold USER after:", 0, y, TEXT_SIZE) display.thickness(1) y += LINE_HEIGHT display.text("Up / Down - Change font size", 0, y, TEXT_SIZE) @@ -36,5 +37,4 @@ display.text("a - Toggle invert", 0, y, TEXT_SIZE) y += LINE_HEIGHT display.update() -while True: - time.sleep(1) +display.halt() diff --git a/micropython/examples/badger2040/info.py b/micropython/examples/badger2040/info.py index 94c3b1af..f3b05683 100644 --- a/micropython/examples/badger2040/info.py +++ b/micropython/examples/badger2040/info.py @@ -1,11 +1,12 @@ import badger2040 -import time from badger2040 import WIDTH TEXT_SIZE = 0.45 LINE_HEIGHT = 16 display = badger2040.Badger2040() +display.led(128) + display.pen(0) display.rectangle(0, 0, WIDTH, 16) display.thickness(1) @@ -33,6 +34,4 @@ y += LINE_HEIGHT display.text("https://pimoroni.com/badger2040", 0, y, TEXT_SIZE) display.update() - -while True: - time.sleep(1) +display.halt() diff --git a/micropython/examples/badger2040/list.py b/micropython/examples/badger2040/list.py index a2846ca7..d71bc039 100644 --- a/micropython/examples/badger2040/list.py +++ b/micropython/examples/badger2040/list.py @@ -143,6 +143,7 @@ items_per_page = 0 # Create a new Badger and set it to update FAST display = badger2040.Badger2040() +display.led(128) display.update_speed(badger2040.UPDATE_FAST) # Set up the buttons @@ -177,6 +178,13 @@ items_per_page = ((LIST_HEIGHT // ITEM_SPACING) + 1) * list_columns def button(pin): global update, current_item, needs_save + time.sleep(0.05) + if not pin.value(): + return + + if button_a.value() and button_c.value(): + machine.reset() + if len(list_content) > 0 and not update: if pin == button_a: if current_item > 0: diff --git a/micropython/examples/badger2040/qrgen.py b/micropython/examples/badger2040/qrgen.py index 1c980c1e..3f0c4021 100644 --- a/micropython/examples/badger2040/qrgen.py +++ b/micropython/examples/badger2040/qrgen.py @@ -7,8 +7,8 @@ import time try: text = open("qrcode.txt", "r") except OSError: - text = open("qrcode.txt", "w") - text.write("""https://pimoroni.com/badger2040 + with open("qrcode.txt", "w") as text: + text.write("""https://pimoroni.com/badger2040 Badger 2040 * 296x128 1-bit e-ink * six user buttons @@ -18,8 +18,8 @@ Badger 2040 Scan this code to learn more about Badger 2040. """) - text.flush() - text.seek(0) + text.flush() + text = open("qrcode.txt", "r") lines = text.read().strip().split("\n") @@ -28,6 +28,7 @@ title_text = lines.pop(0) detail_text = lines display = badger2040.Badger2040() +display.led(128) code = qrcode.QRCode()