-
Notifications
You must be signed in to change notification settings - Fork 203
Implement the remaining canopen datatypes #440
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
acolomb
merged 9 commits into
canopen-python:master
from
sveinse:feature-add-missing-datatypes
Jun 11, 2024
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1205906
Implement the remaining canopen datatypes
sveinse edfdf69
Updates after review
sveinse 0bc5d1c
Apply suggestions from code review
sveinse b4fdef6
Fixup incorrect and shortened SDO responses
sveinse a6e7ca3
Merge branch 'master' into feature-add-missing-datatypes
acolomb e2e53e0
Inheritance instead of composition.
acolomb 1a5bd8e
Annotate type hint for STRUCT_TYPES listing.
acolomb 0164e28
Styling fixes.
acolomb 9c2ec3d
Disable failing tests waiting on a fix for #436.
acolomb 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
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 was deleted.
Oops, something went wrong.
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,62 @@ | ||
import struct | ||
sveinse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class UnsignedN: | ||
""" struct-like class for packing and unpacking unsigned integers of arbitrary width. | ||
The width must be a multiple of 8 and must be between 8 and 64. | ||
""" | ||
def __init__(self, width: int): | ||
self.width = width | ||
if width % 8 != 0: | ||
raise ValueError("Width must be a multiple of 8") | ||
if width <= 0 or width > 64: | ||
raise ValueError("Invalid width for UnsignedN") | ||
elif width <= 8: | ||
self.__st = struct.Struct("B") | ||
elif width <= 16: | ||
self.__st = struct.Struct("<H") | ||
elif width <= 32: | ||
self.__st = struct.Struct("<L") | ||
elif width <= 64: | ||
sveinse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.__st = struct.Struct("<Q") | ||
|
||
def unpack(self, __buffer): | ||
return self.__st.unpack(__buffer + b'\x00' * (self.__st.size - self.size)) | ||
|
||
def pack(self, *v): | ||
return self.__st.pack(*v)[:self.size] | ||
|
||
@property | ||
def size(self): | ||
return self.width // 8 | ||
|
||
|
||
class IntegerN: | ||
""" struct-like class for packing and unpacking integers of arbitrary width. | ||
The width must be a multiple of 8 and must be between 8 and 64. | ||
""" | ||
def __init__(self, width: int): | ||
self.width = width | ||
if width % 8 != 0: | ||
raise ValueError("Width must be a multiple of 8") | ||
if width <= 0 or width > 64: | ||
raise ValueError("Invalid width for IntegerN") | ||
elif width <= 8: | ||
self.__st = struct.Struct("b") | ||
elif width <= 16: | ||
self.__st = struct.Struct("<h") | ||
elif width <= 32: | ||
self.__st = struct.Struct("<l") | ||
elif width <= 64: | ||
sveinse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.__st = struct.Struct("<q") | ||
|
||
def unpack(self, __buffer): | ||
sveinse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mask = 0x80 | ||
neg = (__buffer[self.size - 1] & mask) > 0 | ||
return self.__st.unpack(__buffer + (b'\xff' if neg else b'\x00') * (self.__st.size - self.size)) | ||
|
||
def pack(self, *v): | ||
return self.__st.pack(*v)[:self.size] | ||
|
||
@property | ||
def size(self): | ||
return self.width // 8 |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.