-
Notifications
You must be signed in to change notification settings - Fork 224
Summit/blink/skeleton #249
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
Merged
bessman
merged 4 commits into
fossasia:summit/blink/skeleton
from
bessman:summit/blink/skeleton
Mar 14, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,42 @@ | ||
"""FOSSASIA Summit 2025 - PSLab Development 101 exercises.""" | ||
|
||
import time | ||
|
||
import pslab | ||
import pslab.protocol as CP | ||
|
||
|
||
def blink(psl: pslab.ScienceLab, color: tuple[int, int, int], period: int) -> None: | ||
"""Blink the onbard RGB LED. | ||
|
||
Parameters | ||
---------- | ||
psl : pslab.ScienceLab | ||
color : tuple[int, int, int] | ||
Green, red, blue, each in range 0-255. | ||
period : int | ||
Blink period in milliseconds. | ||
""" | ||
|
||
|
||
def blink_c(psl: pslab.ScienceLab, color: tuple[int, int, int], period: int) -> None: | ||
"""Blink the RGB LED using firmware implementation. | ||
|
||
Parameters | ||
---------- | ||
psl : pslab.ScienceLab | ||
color : tuple[int, int, int] | ||
Green, red, blue, each in range 0-255. | ||
period : int | ||
Blink period in milliseconds. | ||
""" | ||
if not period: | ||
cmd = CP.NONSTANDARD_IO + CP.Byte.pack(11) | ||
psl.device.exchange(cmd) | ||
psl.rgb_led(color) | ||
return | ||
|
||
cmd = CP.NONSTANDARD_IO + CP.Byte.pack(10) | ||
args = CP.ShortInt.pack(period) | ||
args += bytes(color) | ||
psl.device.exchange(cmd, args) |
This file contains hidden or 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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -70,6 +70,36 @@ def write(self, data: bytes) -> int: | |||||||||
""" | ||||||||||
... | ||||||||||
|
||||||||||
def exchange(self, cmd: bytes, data: bytes = b"") -> bytes: | ||||||||||
"""Send command and input data to device, and return output data. | ||||||||||
|
||||||||||
Parameters | ||||||||||
---------- | ||||||||||
cmd : int | ||||||||||
Command code. | ||||||||||
data : bytes, default b'' | ||||||||||
Input data for command, if any. | ||||||||||
|
||||||||||
Returns | ||||||||||
------- | ||||||||||
bytes | ||||||||||
Output data from command, if any. | ||||||||||
""" | ||||||||||
(cmd_int,) = CP.ShortInt.unpack(cmd) | ||||||||||
header = CP.Header.pack(cmd_int, len(data)) | ||||||||||
self.write(header + data) | ||||||||||
status, response_size = CP.Header.unpack(self.read(CP.Header.size)) | ||||||||||
|
||||||||||
if status: | ||||||||||
raise Exception(status) | ||||||||||
|
||||||||||
response = self.read(response_size) | ||||||||||
|
||||||||||
if len(response) < response_size: | ||||||||||
raise TimeoutError | ||||||||||
Comment on lines
+98
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider providing more context when raising TimeoutError. Including details such as the expected response size and the actual number of bytes received would aid in debugging timeout issues.
Suggested change
|
||||||||||
|
||||||||||
return response | ||||||||||
|
||||||||||
def get_byte(self) -> int: | ||||||||||
"""Read a single one-byte of integer value. | ||||||||||
|
||||||||||
|
@@ -164,10 +194,7 @@ def get_version(self) -> str: | |||||||||
str | ||||||||||
Version string. | ||||||||||
""" | ||||||||||
self.send_byte(CP.COMMON) | ||||||||||
self.send_byte(CP.GET_VERSION) | ||||||||||
version_length = 9 | ||||||||||
version = self.read(version_length) | ||||||||||
version = self.exchange(CP.COMMON + CP.GET_VERSION) | ||||||||||
|
||||||||||
try: | ||||||||||
if b"PSLab" not in version: | ||||||||||
|
@@ -177,23 +204,16 @@ def get_version(self) -> str: | |||||||||
msg = "device not found" | ||||||||||
raise ConnectionError(msg) from exc | ||||||||||
|
||||||||||
return version.decode("utf-8") | ||||||||||
return version.rstrip(b"\x00").decode("utf-8") | ||||||||||
|
||||||||||
def get_firmware_version(self) -> FirmwareVersion: | ||||||||||
"""Get firmware version. | ||||||||||
|
||||||||||
Returns | ||||||||||
------- | ||||||||||
tuple[int, int, int] | ||||||||||
FirmwareVersion | ||||||||||
major, minor, patch. | ||||||||||
|
||||||||||
""" | ||||||||||
self.send_byte(CP.COMMON) | ||||||||||
self.send_byte(CP.GET_FW_VERSION) | ||||||||||
|
||||||||||
# Firmware version query was added in firmware version 3.0.0. | ||||||||||
major = self.get_byte() | ||||||||||
minor = self.get_byte() | ||||||||||
patch = self.get_byte() | ||||||||||
|
||||||||||
major, minor, patch = self.exchange(CP.COMMON + CP.GET_FW_VERSION) | ||||||||||
return FirmwareVersion(major, minor, patch) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
issue (complexity): Consider refactoring the
exchange
method into smaller helper functions to separate the header processing and response validation steps, improving code modularity and readability without altering functionality .The
exchange
method now does too many things in one go. Consider splitting up the responsibilities into separate helper functions. For example, separate out the header processing and response validation:Then update
exchange
to use these helpers while retaining functionality:This refactoring maintains all functionality while reducing complexity in
exchange
.