Skip to content

Conversation

@haydenroche5
Copy link
Collaborator

  • Add a serial_lock decorator that can be used on functions that attempt to
    acquire a lock on the serial port.
  • Make serialReadByte, serialReset, serialCommand, and serialTransaction member
    functions of OpenSerial instead of being free functions.
  • Refactor serialCommand and serialTransaction (now renamed Command and
    Transaction, respectively) to unify common code in a new internal function,
    _transmit.

@haydenroche5 haydenroche5 requested a review from m-mcgowan August 27, 2023 21:45
@haydenroche5 haydenroche5 self-assigned this Aug 27, 2023
@haydenroche5 haydenroche5 force-pushed the cleanup_serial branch 4 times, most recently from a01fb50 to 6ba782d Compare August 30, 2023 17:08
if seg_len > CARD_REQUEST_SEGMENT_MAX_LEN:
seg_len = CARD_REQUEST_SEGMENT_MAX_LEN
def decorator(self, *args, **kwargs):
if use_serial_lock:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nitpick, I would have used a no-op lock to avoid the conditional. But that's me. No need to change if you prefer not to.


def _read_byte(self):
"""Read a single byte from the Notecard."""
if sys.implementation.name == 'micropython':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about initializing a member variable with an appropriate function to avoid the conditional on each read?

transaction_timeout_secs = 30
if txn_manager:
txn_manager.start(transaction_timeout_secs)
def serial_lock(fn):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :-)

self._transmit(req)

rsp_json = self.uart.readline()
if self._debug:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function acquires the lock for longer than needed. Might be worth separating out the comms part here into a new function _transmitAndReceive, and move the @serial_lock to _transmit and _transmitAndReceive.

@haydenroche5 haydenroche5 force-pushed the cleanup_serial branch 4 times, most recently from 5091d4e to 64a1e95 Compare August 30, 2023 23:42
- Add a serial_lock decorator that can be used on functions that attempt to
acquire a lock on the serial port.
- Make serialReadByte, serialReset, serialCommand, and serialTransaction member
functions of OpenSerial instead of being free functions.
- Refactor serialCommand and serialTransaction (now renamed Command and
Transaction, respectively) to unify common code in a new internal function,
_transmit.
@haydenroche5
Copy link
Collaborator Author

Ok, I think I addressed all the feedback. I'm all ears if you have cleaner/better ways of doing what I did, though, @m-mcgowan.

- 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.
- Refactor low-level serial comms by adding a new function, _transact. This
replaces _transmit and _transmit_and_receive. It acquires the serial lock and
uses the transaction manager. It will read a response from the Notecard if the
rsp_expected parameter is True.
@haydenroche5
Copy link
Collaborator Author

Consolidating into this PR: #73

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