-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat: Refactor serial and I2C code. #73
Conversation
haydenroche5
commented
Aug 31, 2023
- Get rid of conditionals on transaction manager existence and if serial locking should be used by adding no-op implementations of each.
- Handle serial and I2C locking with decorators, serial_lock and i2c_lock, respectively.
- Add timeout functionality to I2C locking. Prior to this commit, it would spin forever if the lock couldn't be acquired.
- For both serial and I2C, combine common code from Transaction and Command into an internal function, _transact. Locking happens over this function.
- Migrate all freestanding serial functions to methods of OpenSerial.
- Create a _read_byte abstraction for the three platforms we support (CPython, MicroPython, CircuitPython).
- Merge the functionality of _preprocess_req into _prepare_request. These are always used together, so it doesn't really make sense to have them as separate functions. Additionally, have _prepare_request do everything needed to transform the request data for transmission. Namely, encode the request string as UTF-8 bytes. Finally, make _prepare_request a method of Notecard. There's no reason for it to be a free function.
- Rename I2C's _send_payload to _transmit.
45b5199
to
e7305e6
Compare
notecard/notecard.py
Outdated
write_data = bytes(json[chunk_offset:chunk_offset + chunk_len], | ||
'utf-8') | ||
write_data = data[chunk_offset:chunk_offset + chunk_len] | ||
|
||
# Send a message with the length of the incoming bytes followed | ||
# by the bytes themselves. | ||
if use_periphery: |
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.
There's an opportunity here to also use a strategy for i2c read/write to avoid the conditional.
- Get rid of conditionals on transaction manager existence and if serial locking should be used by adding no-op implementations of each. - Handle serial and I2C locking with decorators, serial_lock and i2c_lock, respectively. - Add timeout functionality to I2C locking. Prior to this commit, it would spin forever if the lock couldn't be acquired. - For both serial and I2C, combine common code from Transaction and Command into an internal function, _transact. Locking happens over this function. - Migrate all freestanding serial functions to methods of OpenSerial. - Create a _read_byte abstraction for the three platforms we support (CPython, MicroPython, CircuitPython). - Merge the functionality of _preprocess_req into _prepare_request. These are always used together, so it doesn't really make sense to have them as separate functions. Additionally, have _prepare_request do everything needed to transform the request data for transmission. Namely, encode the request string as UTF-8 bytes. Finally, make _prepare_request a method of Notecard. There's no reason for it to be a free function. - Rename I2C's _send_payload to _transmit. - Move Transaction and Command into the base Notecard class. They are now fully abstracted from the underlying comms layer.
e7305e6
to
6afd1f7
Compare
Addressed review feedback with latest commit (minus the context manager for TransactionManager, which I'm shifting to the backlog, for now). Tested locally against Swan + CircuitPython, ESP32 + MicroPython, and Raspberry Pi. |