Skip to content

Commit

Permalink
python: add random timer experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed May 17, 2024
1 parent 2478539 commit 31307f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,8 @@ After the user presses the `pin_logo` button:
- `button_a` cancels/resets timero

After both the work and break timers complete, it returns to the first state.

### [Random Timer](./src/random-timer.py)

I wanted to implement a timer that used an hourglass animation.
The seconds/duration of the timer is set using an RNG.
34 changes: 34 additions & 0 deletions python/src/random-timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Imports go at the top
import random

from microbit import *


class MyImages:
hourglass_start: Image = Image("99999:" "09990:" "00900:" "09090:" "90009:")

hourglass_end: Image = Image("90009:" "09090:" "00900:" "09990:" "99999:")

hourglass_progress_0: Image = Image("99999:" "09990:" "00900:" "09690:" "90009:")

hourglass_progress_1: Image = Image("99999:" "09990:" "00900:" "09090:" "90609:")


myimages = MyImages()

print("INFO: Random Hourglass Timer")

# Code in a 'while True:' loop repeats forever
while True:
seconds: int = random.randint(10, 90)
display.show(myimages.hourglass_start)
sleep(1000)

print("INFO: starting " + str(seconds) + " second timer")
for i in range(0, seconds):
display.show(getattr(myimages, "hourglass_progress_" + str(i % 2)))
sleep(1000)

print("INFO: timer has completed")
display.show(myimages.hourglass_end)
sleep(1000)

0 comments on commit 31307f4

Please sign in to comment.