-
Notifications
You must be signed in to change notification settings - Fork 8
Ingk 1193 create table object api #667
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
50f032e
INGK-1193 create minimal api for getting and setting values of table
d1ca109
INGK-1193 add mockup in virtual drive
29988f5
INGK-1193 add getter of table object from servo
b27b9bf
INGK-1193 test setter and getter of tables
6622fb5
INGK-1193 format
a584185
INGK-1193 update non axis
8f5a28f
INGK-1193 add complete api for table object
7215a15
INGK-1193 Merge branch 'INGK-1194-add-table-element-attribute-to-xdf-…
0fb1c21
INGK-1193 update virtual drive to have tables
491e12f
INGK-1193 Merge branch 'INGK-1194-add-table-element-attribute-to-xdf-…
6799f07
INGK-1193 update framework
174a391
INGK-1193 pr corrections
2955352
INGK-1193 add tables to sphinx and remove typing from docstrings
aef072d
INGK-1193 Merge remote-tracking branch 'origin/develop' into INGK-119…
9683b67
INGK-1193 lock
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ====== | ||
| Tables | ||
| ====== | ||
|
|
||
| .. automodule:: ingenialink.table | ||
| :members: | ||
| :member-order: groupwise |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| from collections.abc import Iterator, Sequence | ||
| from typing import TYPE_CHECKING, Optional | ||
|
|
||
| from ingenialink.utils._utils import REG_VALUE | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ingenialink import Servo | ||
| from ingenialink.dictionary import DictionaryTable | ||
|
|
||
|
|
||
| class Table: | ||
| """Table. | ||
|
|
||
| Internal table that stores N values that are accessed by index register | ||
| and read/written via value register. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, servo: "Servo", table: "DictionaryTable", axis: Optional[int] = None | ||
| ) -> None: | ||
| """Initializes the Table. | ||
|
|
||
| Args: | ||
| servo: Servo instance. | ||
| table: Dictionary table instance. | ||
| axis: Axis number for multi-axis servos | ||
|
|
||
| Raises: | ||
| ValueError: If index register does not have integer range. | ||
| """ | ||
| self.__servo = servo | ||
| self.__table = table | ||
|
|
||
| self.__index_register = self.__servo.dictionary.get_register( | ||
| self.__table.id_index, axis=axis | ||
| ) | ||
| self.__value_register = self.__servo.dictionary.get_register( | ||
| self.__table.id_value, axis=axis | ||
| ) | ||
|
|
||
| min_index, max_index = self.__index_register.range | ||
| if not isinstance(min_index, int) or not isinstance(max_index, int): | ||
| raise ValueError("Index register must have integer range.") | ||
|
|
||
| if min_index < 0: | ||
| # Negative indexes may be used to not request any particular index. | ||
| min_index = 0 | ||
|
|
||
| self.__min_index = min_index | ||
| self.__max_index = max_index | ||
|
|
||
| def get_value(self, index: int) -> REG_VALUE: | ||
| """Reads a value from the table. | ||
|
|
||
| Args: | ||
| index: Index of the value to read. | ||
|
|
||
| Returns: | ||
| Value at the specified index. | ||
| """ | ||
| self.__servo.write(self.__index_register, index) | ||
| return self.__servo.read(self.__value_register) | ||
|
|
||
| def set_value(self, index: int, value: REG_VALUE) -> None: | ||
| """Writes a value to the table. | ||
|
|
||
| Args: | ||
| index: Index of the value to write. | ||
| value: Value to write at the specified index. | ||
| """ | ||
| self.__servo.write(self.__index_register, index) | ||
| self.__servo.write(self.__value_register, value) | ||
|
|
||
| def __len__(self) -> int: | ||
| """Returns the number of elements in the table. | ||
|
|
||
| Returns: | ||
| Number of elements in the table | ||
| """ | ||
| return self.__max_index - self.__min_index + 1 | ||
|
|
||
| def __iter__(self) -> Iterator[REG_VALUE]: | ||
| """Iterate over all values in the table. | ||
|
|
||
| Yields: | ||
| Each value in the table from min_index to max_index. | ||
| """ | ||
| for i in range(self.__min_index, self.__max_index + 1): | ||
| yield self.get_value(i) | ||
|
|
||
| def __getitem__(self, index: int) -> REG_VALUE: | ||
| """Read a value from the table using bracket notation. | ||
|
|
||
| Args: | ||
| index: Index of the value to read. | ||
|
|
||
| Returns: | ||
| Value at the specified index. | ||
|
|
||
| Raises: | ||
| IndexError: If index is out of range. | ||
| """ | ||
| if index < self.__min_index or index > self.__max_index: | ||
| raise IndexError(f"Index {index} out of range [{self.__min_index}, {self.__max_index}]") | ||
| return self.get_value(index) | ||
|
|
||
| def __setitem__(self, index: int, value: REG_VALUE) -> None: | ||
| """Write a value to the table using bracket notation. | ||
|
|
||
| Args: | ||
| index: Index of the value to write. | ||
| value: Value to write at the specified index. | ||
|
|
||
| Raises: | ||
| IndexError: If index is out of range. | ||
| """ | ||
| if index < self.__min_index or index > self.__max_index: | ||
| raise IndexError(f"Index {index} out of range [{self.__min_index}, {self.__max_index}]") | ||
| self.set_value(index, value) | ||
|
|
||
| def read( | ||
| self, start_index: Optional[int] = None, count: Optional[int] = None | ||
| ) -> list[REG_VALUE]: | ||
| """Read multiple values from the table. | ||
|
|
||
| Args: | ||
| start_index: Starting index. Defaults to min_index. | ||
| count: Number of values to read. Defaults to all remaining. | ||
|
|
||
| Returns: | ||
| List of values read from the table. | ||
|
|
||
| Raises: | ||
| IndexError: If the range is out of bounds. | ||
| """ | ||
| if start_index is None: | ||
| start_index = self.__min_index | ||
|
|
||
| if count is None: | ||
| count = self.__max_index - start_index + 1 | ||
|
|
||
| end_index = start_index + count - 1 | ||
|
|
||
| if start_index < self.__min_index or end_index > self.__max_index: | ||
| raise IndexError( | ||
| f"Range [{start_index}, {end_index}] out of bounds " | ||
| f"[{self.__min_index}, {self.__max_index}]" | ||
| ) | ||
|
|
||
| return [self.get_value(i) for i in range(start_index, end_index + 1)] | ||
|
|
||
| def write(self, values: Sequence[REG_VALUE], start_index: Optional[int] = None) -> None: | ||
| """Write multiple values to the table. | ||
|
|
||
| Args: | ||
| values: Sequence of values to write to the table. | ||
| start_index: Starting index. Defaults to min_index. | ||
|
|
||
| Raises: | ||
| IndexError: If the range is out of bounds. | ||
| """ | ||
| if start_index is None: | ||
| start_index = self.__min_index | ||
|
|
||
| end_index = start_index + len(values) - 1 | ||
|
|
||
| if start_index < self.__min_index or end_index > self.__max_index: | ||
| raise IndexError( | ||
| f"Range [{start_index}, {end_index}] out of bounds " | ||
| f"[{self.__min_index}, {self.__max_index}]" | ||
| ) | ||
|
|
||
| for i, value in enumerate(values): | ||
| self.set_value(start_index + i, value) | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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.
Is this a possibility?
A non-checked
max_indexout of bounds that makes anything (or some things) to crash?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.
Usually min index is -1. This value is used to avoid write in the table.