-
Notifications
You must be signed in to change notification settings - Fork 6
Clean up serial comms code. #70
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
Conversation
a01fb50 to
6ba782d
Compare
notecard/notecard.py
Outdated
| if seg_len > CARD_REQUEST_SEGMENT_MAX_LEN: | ||
| seg_len = CARD_REQUEST_SEGMENT_MAX_LEN | ||
| def decorator(self, *args, **kwargs): | ||
| if use_serial_lock: |
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.
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.
notecard/notecard.py
Outdated
|
|
||
| def _read_byte(self): | ||
| """Read a single byte from the Notecard.""" | ||
| if sys.implementation.name == 'micropython': |
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.
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): |
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.
Nice :-)
| self._transmit(req) | ||
|
|
||
| rsp_json = self.uart.readline() | ||
| if self._debug: |
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.
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.
5091d4e to
64a1e95
Compare
- 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.
64a1e95 to
47f250a
Compare
|
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. |
47f250a to
8a42fc5
Compare
- 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.
|
Consolidating into this PR: #73 |
acquire a lock on the serial port.
functions of OpenSerial instead of being free functions.
Transaction, respectively) to unify common code in a new internal function,
_transmit.