Skip to content

Commit

Permalink
IMPROVEMENT: simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
amilcarlucas committed Aug 16, 2024
1 parent 3f6ffb1 commit 0442205
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions MethodicConfigurator/frontend_tkinter_component_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def set_values_from_fc_parameters(self, fc_parameters: dict, doc: dict):

self.set_gnss_type_and_protocol_from_fc_parameters(fc_parameters)
self.set_serial_type_and_protocol_from_fc_parameters(fc_parameters, doc)
self.set_battery_from_fc_parameters(fc_parameters)
self.set_battery_type_and_protocol_from_fc_parameters(fc_parameters)
self.set_motor_poles_from_fc_parameters(fc_parameters)

def set_gnss_type_and_protocol_from_fc_parameters(self, fc_parameters: dict):
Expand Down Expand Up @@ -372,7 +372,7 @@ def set_serial_type_and_protocol_from_fc_parameters(self, fc_parameters: dict, d
self.data['Components']['ESC']['FC Connection']['Protocol'] = \
doc['MOT_PWM_TYPE']['values'][str(mot_pwm_type)]

def set_battery_from_fc_parameters(self, fc_parameters: dict):
def set_battery_type_and_protocol_from_fc_parameters(self, fc_parameters: dict):
if "BATT_MONITOR" in fc_parameters:
batt_monitor = int(fc_parameters["BATT_MONITOR"])
self.data['Components']['Battery Monitor']['FC Connection']['Type'] = \
Expand All @@ -389,35 +389,21 @@ def set_motor_poles_from_fc_parameters(self, fc_parameters: dict):
elif "SERVO_FTW_MASK" in fc_parameters and fc_parameters["SERVO_FTW_MASK"] and "SERVO_FTW_POLES" in fc_parameters:
self.data['Components']['Motors']['Specifications']['Poles'] = fc_parameters["SERVO_FTW_POLES"]


def update_esc_protocol_combobox(self, esc_connection_type):
"""
Updates the ESC Protocol combobox based on the selected ESC Type.
"""
def update_esc_protocol_combobox_entries(self, esc_connection_type):
""" Updates the ESC Protocol combobox entries based on the selected ESC Type."""
if len(esc_connection_type) > 3 and esc_connection_type[:3] == 'CAN':
protocols = ['DroneCAN']
elif len(esc_connection_type) > 6 and esc_connection_type[:6] == 'SERIAL':
protocols = [value['protocol'] for value in serial_protocols_dict.values() if value['component'] == 'ESC']
else:
protocols = list(self.local_filesystem.doc_dict['MOT_PWM_TYPE']["values"].values())

# Find the ESC Protocol combobox and update its values
protocol_path = ('ESC', 'FC Connection', 'Protocol')
if protocol_path in self.entry_widgets:
protocol_combobox = self.entry_widgets[protocol_path]
protocol_combobox['values'] = protocols # Update the values
protocol_combobox.set(protocols[0] if protocols else '') # Set the first protocol as the default selection or clear if none

def validate_and_update_esc_protocols(self, event, path):
"""
Validates the value and updates the ESC Protocol combobox based on the selected ESC Type.
"""
# Perform validation
valid = self.validate_combobox(event, path)
if valid:
# Update ESC Protocol combobox based on the selected type
self.update_esc_protocol_combobox(event.widget.get())
return valid
protocol_combobox['values'] = protocols # Update the combobox entries
protocol_combobox.set(protocols[0] if protocols else '')
protocol_combobox.update_idletasks() # re-draw the combobox ASAP

def add_entry_or_combobox(self, value, entry_frame, path):
# Default values for comboboxes in case the apm.pdef.xml metadata is not available
Expand Down Expand Up @@ -481,12 +467,12 @@ def get_combobox_values(param_name: str) -> list:
config = combobox_config.get(path)
if config:
cb = ttk.Combobox(entry_frame, values=config["values"])
if path == ('ESC', 'FC Connection', 'Type'):
cb.bind("<FocusOut>", lambda event, path=path: self.validate_and_update_esc_protocols(event, path))
cb.bind("<KeyRelease>", lambda event, path=path: self.validate_and_update_esc_protocols(event, path))
else:
cb.bind("<FocusOut>", lambda event, path=path: self.validate_combobox(event, path))
cb.bind("<KeyRelease>", lambda event, path=path: self.validate_combobox(event, path))
cb.bind("<FocusOut>", lambda event, path=path: self.validate_combobox(event, path))
cb.bind("<KeyRelease>", lambda event, path=path: self.validate_combobox(event, path))

if path == ('ESC', 'FC Connection', 'Type'): # immediate update of ESC Protocol upon ESC Type selection
cb.bind("<<ComboboxSelected>>", lambda event, path=path: self.update_esc_protocol_combobox_entries(cb.get()))

cb.set(value)
return cb

Expand Down Expand Up @@ -544,6 +530,10 @@ def validate_combobox(self, event, path) -> bool:
f"Allowed values are: {', '.join(allowed_values)}")
combobox.configure(style="comb_input_invalid.TCombobox")
return False

if path == ('ESC', 'FC Connection', 'Type'):
self.update_esc_protocol_combobox_entries(value)

combobox.configure(style="comb_input_valid.TCombobox")
return True

Expand Down

0 comments on commit 0442205

Please sign in to comment.