diff --git a/firmware/src/serial/serial_protocol_protobuf.cpp b/firmware/src/serial/serial_protocol_protobuf.cpp index 50018218..9ec499ec 100644 --- a/firmware/src/serial/serial_protocol_protobuf.cpp +++ b/firmware/src/serial/serial_protocol_protobuf.cpp @@ -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); diff --git a/software/python/simple_example.py b/software/python/simple_example.py index 12e8dad0..b32e7d30 100644 --- a/software/python/simple_example.py +++ b/software/python/simple_example.py @@ -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: