From e4bcf10f1735f2ad879e3aa1f50fc560c4a6d80d Mon Sep 17 00:00:00 2001 From: Richard <36215881+hoganri@users.noreply.github.com> Date: Wed, 5 Oct 2022 14:46:50 -0400 Subject: [PATCH 1/4] Adds digital clock example for pico_display Starts at 12:00 and allows the user to adjust the hours/minutes using the pico_display buttons --- .../examples/pico_display/pico_clock.py | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 micropython/examples/pico_display/pico_clock.py diff --git a/micropython/examples/pico_display/pico_clock.py b/micropython/examples/pico_display/pico_clock.py new file mode 100644 index 000000000..839951e47 --- /dev/null +++ b/micropython/examples/pico_display/pico_clock.py @@ -0,0 +1,85 @@ +import time +import qrcode +from pimoroni import Button +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4 + +# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM! +display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0) +display.set_backlight(1.0) + + +button_a = Button(12) +button_b = Button(13) +button_x = Button(14) +button_y = Button(15) + +WHITE = display.create_pen(255, 255, 255) +BLACK = display.create_pen(0, 0, 0) +GREEN = display.create_pen(0, 121, 64) + +def clear(): + display.set_pen(BLACK) + display.clear() + display.update() + +clear() + +WIDTH, HEIGHT = display.get_bounds() + +BG = display.create_pen(0, 0, 0) +FG = display.create_pen(255, 255, 255) + +stateChange = 0 +currentTimeHours = "12" +currentTimeMinutes = "00" + +lastAdjustment = None + +def showTime(): + clear() + display.set_pen(WHITE) + display.text(str(currentTimeHours)+":"+str(currentTimeMinutes), int(WIDTH/3.5), int(HEIGHT/3), 240, 4) + display.update() + +while True: + showTime() + + if button_a.read(): # Handle hours adjustment + currentTimeHours = str(int(currentTimeHours)+1) + if int(currentTimeHours) < 10: + currentTimeHours = "0"+currentTimeHours + elif int(currentTimeHours) > 12: + currentTimeHours = "01" + elif button_b.read(): + currentTimeHours = str(int(currentTimeHours)-1) + if int(currentTimeHours) < 1: + currentTimeHours = "12" + if int(currentTimeHours) < 10: + currentTimeHours = "0"+currentTimeHours + elif button_x.read(): # Handle minutes adjustment + currentTimeMinutes = str(int(currentTimeMinutes)+1) + if int(currentTimeMinutes) < 10: + currentTimeMinutes = "0"+currentTimeMinutes + elif int(currentTimeMinutes) > 59: + currentTimeMinutes = "01" + elif button_y.read(): + currentTimeMinutes = str(int(currentTimeMinutes)-1) + if int(currentTimeMinutes) < 1: + currentTimeMinutes = "59" + if int(currentTimeMinutes) < 10: + currentTimeMinutes = "0"+currentTimeMinutes + + if lastAdjustment is None or time.time()-lastAdjustment >= 59: + if currentTimeMinutes != "59": + currentTimeMinutes = str(int(currentTimeMinutes)+1) + if int(currentTimeMinutes) < 10: + currentTimeMinutes = "0"+currentTimeMinutes + else: + currentTimeMinutes = "00" + if currentTimeHours == "12": + currentTimeHours = "01" + else: + currentTimeHours = str(int(currentTimeHours)+1) + if int(currentTimeHours) < 10: + currentTimeHours = "0"+currentTimeHours + lastAdjustment = time.time() \ No newline at end of file From d7a96cb64a0ce4672e8a67d6452f532f04f5c179 Mon Sep 17 00:00:00 2001 From: Richard <36215881+hoganri@users.noreply.github.com> Date: Wed, 5 Oct 2022 19:55:57 -0400 Subject: [PATCH 2/4] Update pico_clock.py --- micropython/examples/pico_display/pico_clock.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/micropython/examples/pico_display/pico_clock.py b/micropython/examples/pico_display/pico_clock.py index 839951e47..435b46d0f 100644 --- a/micropython/examples/pico_display/pico_clock.py +++ b/micropython/examples/pico_display/pico_clock.py @@ -1,5 +1,4 @@ import time -import qrcode from pimoroni import Button from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4 @@ -15,7 +14,6 @@ WHITE = display.create_pen(255, 255, 255) BLACK = display.create_pen(0, 0, 0) -GREEN = display.create_pen(0, 121, 64) def clear(): display.set_pen(BLACK) @@ -26,10 +24,6 @@ def clear(): WIDTH, HEIGHT = display.get_bounds() -BG = display.create_pen(0, 0, 0) -FG = display.create_pen(255, 255, 255) - -stateChange = 0 currentTimeHours = "12" currentTimeMinutes = "00" From 647346ae60255dfc2959b2cd2e961695dadedbeb Mon Sep 17 00:00:00 2001 From: Richard <36215881+hoganri@users.noreply.github.com> Date: Thu, 6 Oct 2022 08:51:23 -0400 Subject: [PATCH 3/4] Fix linting errors, handle time as integers Fixes linting errors and removes unnecessary type conversions. Time adjustments also moved into individual functions. --- .../examples/pico_display/pico_clock.py | 96 ++++++++++--------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/micropython/examples/pico_display/pico_clock.py b/micropython/examples/pico_display/pico_clock.py index 435b46d0f..f283dd139 100644 --- a/micropython/examples/pico_display/pico_clock.py +++ b/micropython/examples/pico_display/pico_clock.py @@ -1,79 +1,85 @@ import time from pimoroni import Button -from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4 - -# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM! +from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0) display.set_backlight(1.0) - button_a = Button(12) button_b = Button(13) button_x = Button(14) button_y = Button(15) +WIDTH, HEIGHT = display.get_bounds() WHITE = display.create_pen(255, 255, 255) BLACK = display.create_pen(0, 0, 0) + def clear(): display.set_pen(BLACK) display.clear() display.update() -clear() -WIDTH, HEIGHT = display.get_bounds() +clear() -currentTimeHours = "12" -currentTimeMinutes = "00" lastAdjustment = None +currentTimeHours = 12 +currentTimeMinutes = 0 + + +def incrementHours(): + global currentTimeHours + currentTimeHours += 1 + if currentTimeHours > 12: + currentTimeHours = 1 + + +def decrementHours(): + global currentTimeHours + currentTimeHours -= 1 + if currentTimeHours < 1: + currentTimeHours = 12 + + +def incrementMinutes(): + global currentTimeMinutes + currentTimeMinutes += 1 + if currentTimeMinutes > 59: + currentTimeMinutes = 0 + + +def decrementMinutes(): + global currentTimeMinutes + currentTimeMinutes -= 1 + if currentTimeMinutes < 1: + currentTimeMinutes = 59 + def showTime(): clear() display.set_pen(WHITE) - display.text(str(currentTimeHours)+":"+str(currentTimeMinutes), int(WIDTH/3.5), int(HEIGHT/3), 240, 4) + display.text( + "{:02}:{:02}".format( + currentTimeHours, + currentTimeMinutes), int(WIDTH/3.5), int(HEIGHT/3), 240, 4) display.update() + while True: showTime() - - if button_a.read(): # Handle hours adjustment - currentTimeHours = str(int(currentTimeHours)+1) - if int(currentTimeHours) < 10: - currentTimeHours = "0"+currentTimeHours - elif int(currentTimeHours) > 12: - currentTimeHours = "01" + if button_a.read(): # Handle hours adjustment + incrementHours() elif button_b.read(): - currentTimeHours = str(int(currentTimeHours)-1) - if int(currentTimeHours) < 1: - currentTimeHours = "12" - if int(currentTimeHours) < 10: - currentTimeHours = "0"+currentTimeHours - elif button_x.read(): # Handle minutes adjustment - currentTimeMinutes = str(int(currentTimeMinutes)+1) - if int(currentTimeMinutes) < 10: - currentTimeMinutes = "0"+currentTimeMinutes - elif int(currentTimeMinutes) > 59: - currentTimeMinutes = "01" + decrementHours() + elif button_x.read(): # Handle minutes adjustment + incrementMinutes() elif button_y.read(): - currentTimeMinutes = str(int(currentTimeMinutes)-1) - if int(currentTimeMinutes) < 1: - currentTimeMinutes = "59" - if int(currentTimeMinutes) < 10: - currentTimeMinutes = "0"+currentTimeMinutes - + decrementMinutes() if lastAdjustment is None or time.time()-lastAdjustment >= 59: - if currentTimeMinutes != "59": - currentTimeMinutes = str(int(currentTimeMinutes)+1) - if int(currentTimeMinutes) < 10: - currentTimeMinutes = "0"+currentTimeMinutes + if currentTimeMinutes != 59: + incrementMinutes() else: - currentTimeMinutes = "00" - if currentTimeHours == "12": - currentTimeHours = "01" - else: - currentTimeHours = str(int(currentTimeHours)+1) - if int(currentTimeHours) < 10: - currentTimeHours = "0"+currentTimeHours - lastAdjustment = time.time() \ No newline at end of file + incrementMinutes() + incrementHours() + lastAdjustment = time.time() From 8c37b2f1e8478bd76ca501b43093cb5dfaf33066 Mon Sep 17 00:00:00 2001 From: Richard <36215881+hoganri@users.noreply.github.com> Date: Mon, 10 Oct 2022 08:36:37 -0400 Subject: [PATCH 4/4] Fix linting --- micropython/examples/pico_display/pico_clock.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/micropython/examples/pico_display/pico_clock.py b/micropython/examples/pico_display/pico_clock.py index f283dd139..7a5deb0e6 100644 --- a/micropython/examples/pico_display/pico_clock.py +++ b/micropython/examples/pico_display/pico_clock.py @@ -62,7 +62,7 @@ def showTime(): display.text( "{:02}:{:02}".format( currentTimeHours, - currentTimeMinutes), int(WIDTH/3.5), int(HEIGHT/3), 240, 4) + currentTimeMinutes), int(WIDTH / 3.5), int(HEIGHT / 3), 240, 4) display.update() @@ -76,7 +76,7 @@ def showTime(): incrementMinutes() elif button_y.read(): decrementMinutes() - if lastAdjustment is None or time.time()-lastAdjustment >= 59: + if lastAdjustment is None or time.time() - lastAdjustment >= 59: if currentTimeMinutes != 59: incrementMinutes() else: