Skip to content

Commit

Permalink
Handle RequestState messages; update python example
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbez1 committed Oct 28, 2023
1 parent dafc5ca commit 6d946c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions firmware/src/serial/serial_protocol_protobuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ void SerialProtocolProtobuf::handlePacket(const uint8_t* buffer, size_t size) {
config_callback_(pb_rx_buffer_.payload.smartknob_config);
break;
}
case PB_ToSmartknob_request_state_tag:
state_requested_ = true;
break;
default: {
char buf[200];
snprintf(buf, sizeof(buf), "Unknown payload type: %d", pb_rx_buffer_.which_payload);
Expand Down
20 changes: 15 additions & 5 deletions software/python/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ def _run_example():

p = ask_for_serial_port()
with smartknob_context(p) as s:
# Initialize with an empty state object
last_state = smartknob_pb2.SmartKnobState()
def log_state(message):

# Callback function to handle state updates
def log_state(new_state):
nonlocal last_state
if last_state.config.SerializeToString(deterministic=True) != message.config.SerializeToString(deterministic=True):
logging.info('State: ' + str(message))
last_state = message

# We'll log the state if it's changed substantially since the last state we recieved
config_changed = last_state.config.SerializeToString(deterministic=True) != new_state.config.SerializeToString(deterministic=True)
position_changed = last_state.current_position != new_state.current_position
sub_position_large_change = abs(last_state.sub_position_unit * last_state.config.position_width_radians - new_state.sub_position_unit * new_state.config.position_width_radians) > math.radians(5)
press_nonce_changed = last_state.press_nonce != new_state.press_nonce
if config_changed or position_changed or sub_position_large_change or press_nonce_changed:
logging.info('State: ' + str(new_state))
last_state = new_state

# Register our state handler function
s.add_handler('smartknob_state', log_state)
s.request_state()

# Run forever, set config when enter is pressed
while True:
Expand Down

0 comments on commit 6d946c4

Please sign in to comment.