Skip to content
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

Merged
merged 2 commits into from
Sep 12, 2023
Merged

Conversation

haydenroche5
Copy link
Contributor

  • 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.

@haydenroche5 haydenroche5 self-assigned this Aug 31, 2023
This was referenced Aug 31, 2023
notecard/notecard.py Outdated Show resolved Hide resolved
notecard/notecard.py Show resolved Hide resolved
notecard/notecard.py Show resolved Hide resolved
notecard/notecard.py Show resolved Hide resolved
notecard/notecard.py Show resolved Hide resolved
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:
Copy link
Contributor

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.
@haydenroche5 haydenroche5 changed the title [skip-ci] feat: Refactor serial and I2C code. feat: Refactor serial and I2C code. Sep 6, 2023
@haydenroche5
Copy link
Contributor Author

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.

@haydenroche5 haydenroche5 merged commit 7d9f2a3 into blues:main Sep 12, 2023
7 checks passed
@haydenroche5 haydenroche5 deleted the refactor branch September 12, 2023 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants