Skip to content

Commit 682d01c

Browse files
committed
Add solution to blink exercise
1 parent c35f7a6 commit 682d01c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

pslab/blink.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""FOSSASIA Summit 2025 - PSLab Development 101 exercises."""
2+
3+
import time
4+
5+
import pslab
6+
import pslab.protocol as CP
7+
8+
9+
def blink(psl: pslab.ScienceLab, color: tuple[int, int, int], period: int) -> None:
10+
"""Blink the onbard RGB LED.
11+
12+
Parameters
13+
----------
14+
psl : pslab.ScienceLab
15+
color : tuple[int, int, int]
16+
Green, red, blue, each in range 0-255.
17+
period : int
18+
Blink period in milliseconds.
19+
"""
20+
toggle = time.time()
21+
state = 0
22+
23+
while True:
24+
if period / 2 < (time.time() - toggle) * 1000:
25+
if state:
26+
# Turn off LED.
27+
psl.rgb_led((0, 0, 0))
28+
else:
29+
# Turn on LED.
30+
psl.rgb_led(color)
31+
32+
state = not state
33+
toggle = time.time()
34+
35+
36+
def blink_c(psl: pslab.ScienceLab, color: tuple[int, int, int], period: int) -> None:
37+
"""Blink the RGB LED using firmware implementation.
38+
39+
Parameters
40+
----------
41+
psl : pslab.ScienceLab
42+
color : tuple[int, int, int]
43+
Green, red, blue, each in range 0-255.
44+
period : int
45+
Blink period in milliseconds.
46+
"""
47+
if not period:
48+
cmd = CP.NONSTANDARD_IO + CP.Byte.pack(11)
49+
psl.device.exchange(cmd)
50+
psl.rgb_led(color)
51+
return
52+
53+
cmd = CP.NONSTANDARD_IO + CP.Byte.pack(10)
54+
args = CP.ShortInt.pack(period)
55+
args += bytes(color)
56+
psl.device.exchange(cmd, args)

0 commit comments

Comments
 (0)