mirror of
https://github.com/scottbez1/smartknob.git
synced 2025-09-26 23:09:27 +08:00
Handle RequestState messages; update python example
This commit is contained in:
parent
dafc5ca1e2
commit
6d946c49e5
@ -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);
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user