-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a python countdown program #1660
Open
vsumpi
wants to merge
1
commit into
hzeller:master
Choose a base branch
from
vsumpi:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
''' | ||
Have fun using this simple program. | ||
|
||
Logic: Varga "vsumpi" Zsombor | ||
Base-lib: https://github.com/hzeller/rpi-rgb-led-matrix | ||
Other: | ||
gpiozero: https://github.com/gpiozero | ||
''' | ||
|
||
import time # type: ignore | ||
import datetime # type: ignore | ||
from gpiozero import Button # type: ignore | ||
from signal import pause # type: ignore | ||
from samplebase import SampleBase # type: ignore | ||
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics # type: ignore | ||
|
||
|
||
# Matrix and Graphics Configuration (moved outside loop for clarity) | ||
options = RGBMatrixOptions() | ||
options.rows = 32 | ||
options.cols = 64 | ||
options.chain_length = 1 | ||
options.parallel = 1 | ||
options.hardware_mapping = 'adafruit-hat' | ||
options.drop_privileges = False | ||
options.gpio_slowdown = 2 | ||
options.pwm_bits = 1 | ||
matrix = RGBMatrix(options=options) | ||
font = graphics.Font() | ||
font.LoadFont("~/rpi-rgb-led-matrix/fonts/6x10.bdf") | ||
font1 = graphics.Font() | ||
font1.LoadFont("~/rpi-rgb-led-matrix/fonts/8x13B.bdf") | ||
font2 = graphics.Font() | ||
font2.LoadFont("~/rpi-rgb-led-matrix/fonts/6x9.bdf") | ||
red = graphics.Color(255, 0, 0) | ||
green = graphics.Color(0, 255, 0) | ||
blue = graphics.Color(0, 0, 255) | ||
white = graphics.Color(255, 255, 255) | ||
button = Button(19) #gpiozero BCM 19 | ||
|
||
|
||
# Utility Functions | ||
def clear_screen(): | ||
matrix.Clear() | ||
|
||
|
||
def display_clock(): | ||
clear_screen() | ||
graphics.DrawText(matrix, font, 3, 10, red, clockwork) #This is the clock render | ||
#This is the countdown | ||
if countdown > 5: | ||
graphics.DrawText(matrix, font, 15, 26, white, f": {countdown}") | ||
if countdown <= 5: | ||
graphics.DrawText(matrix, font, 15, 26, red, f": {countdown}") | ||
if countdown == 1: | ||
clear_screen() | ||
graphics.DrawText(matrix, font1, 12, 21, green, "START") | ||
time.sleep(1) | ||
time.sleep(1) | ||
|
||
def on_press(): | ||
#Buttton press logic | ||
print(clockwork) | ||
time_buffer.append(clockwork) | ||
#Make a file and sabe lap time | ||
with open("~/laptime.txt", "a") as f: | ||
f.write("\n".join(time_buffer) + "\n") | ||
time_buffer.clear() | ||
button.when_pressed = None #Used to stop the infinite loop of gpiozero | ||
|
||
|
||
# Main Loop | ||
countdown_length = 10 #Set the countdown length here | ||
time_buffer = [] | ||
|
||
while True: | ||
clockwork = datetime.datetime.now().strftime("%H:%M:%S") #This is your clock | ||
countdown = abs((int(clockwork[-2:]) % countdown_length) - countdown_length) # This is the logic for the countdown | ||
if not button.when_pressed: #Button press listener | ||
button.when_pressed = on_press #DO NOT USE as a function() it will break the code, beacuse it's just referring to the function! | ||
display_clock() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @vsumpi, if you look at the source tree, you'll see that there are already a bunch of examples written in Python, but they are in the
bindings/python/samples
directory, rather than inbindings/python
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I have a question, how to set flags globally on my rpi so it is not embedded in the file, is it possible?