Skip to content

Commit

Permalink
Closes #28
Browse files Browse the repository at this point in the history
- Add frequency and duration key dropdown to protocol step selection
- Add frequency and duration key column to treeviews
- When step updates, pass the keys into the handle_key_press function
- Heal legacy protocols
  • Loading branch information
wsarce committed Jun 18, 2023
1 parent f387818 commit 3cdd3b6
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 16 deletions.
57 changes: 47 additions & 10 deletions output_view_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, caller, parent, x, y, height, width, button_size, ksf,
field_font=field_font, header_font=header_font, button_size=button_size,
session_dir=session_dir, ble_thresh=thresholds[0:2],
ble_button=ble_output_button,
config=config)
config=config, caller=caller)
ble_frame = Frame(parent, width=width, height=height)
self.view_frames.append(ble_frame)
else:
Expand All @@ -115,7 +115,7 @@ def __init__(self, caller, parent, x, y, height, width, button_size, ksf,
height=self.height - self.button_size[1], width=self.width,
field_font=field_font, header_font=header_font, button_size=button_size,
config=config, session_dir=session_dir, woodway_thresh=thresholds[2],
woodway_button=woodway_output_button)
woodway_button=woodway_output_button, caller=caller)
woodway_frame = Frame(parent, width=width, height=height)
self.view_frames.append(woodway_frame)
else:
Expand Down Expand Up @@ -309,8 +309,9 @@ def save_session(self, filename, keystrokes):

class ViewWoodway:
def __init__(self, parent, height, width, field_font, header_font, button_size, config, session_dir,
woodway_button, woodway_thresh=None):
woodway_button, caller, woodway_thresh=None):
self.woodway = None
self.caller = caller
self.tab_button = woodway_button
self.session_dir = session_dir
self.config = config
Expand Down Expand Up @@ -343,7 +344,9 @@ def __init__(self, parent, height, width, field_font, header_font, button_size,
prot_heading_dict = {"#0": ["Duration", 'w', 20, YES, 'w']}
prot_column_dict = {"1": ["LS", 'c', 1, YES, 'c'],
"2": ["RS", 'c', 1, YES, 'c'],
"3": ["Incline", 'c', 1, YES, 'c']}
"3": ["Incline", 'c', 1, YES, 'c'],
"4": ["F", 'c', 50, NO, 'c'],
"5": ["D", 'c', 50, NO, 'c']}
treeview_offset = int(width * 0.03)

# TODO: When the session is paused the woodway and vibrotactors should stop, equalize speeds first??
Expand Down Expand Up @@ -542,6 +545,10 @@ def __update_woodway_protocol(self):
scroll_to(self.prot_treeview, self.selected_step)
if self.config.get_protocol_beep():
SessionTimeFields.beep()
if self.selected_command[4] != '':
self.caller.handle_key_press(self.selected_command[4])
if self.selected_command[5] != '':
self.caller.handle_key_press(self.selected_command[5])

def __update_woodway(self):
self.__write_incline(self.woodway_incline)
Expand Down Expand Up @@ -580,16 +587,27 @@ def populate_protocol_steps(self):
self.prot_treeview_parents.append(
self.prot_treeview.insert("", 'end', str(i + 1), text=str(self.protocol_steps[i][0]),
values=(self.protocol_steps[i][1], self.protocol_steps[i][2],
self.protocol_steps[i][3]),
self.protocol_steps[i][3], self.protocol_steps[i][4],
self.protocol_steps[i][5]),
tags=(treeview_tags[(i + 1) % 2])))

def __heal_legacy_protocol(self):
for step in self.protocol_steps:
if len(step) == 4:
step.extend(['', ''])
with open(self.prot_file, 'w') as f:
x = {"Steps": self.protocol_steps}
json.dump(x, f)

def __load_protocol_from_file(self, selected_file=None):
try:
if selected_file:
self.selected_step = 0
self.prot_file = selected_file
with open(self.prot_file, 'r') as f:
self.protocol_steps = json.load(f)['Steps']
if len(self.protocol_steps[0]) == 4:
self.__heal_legacy_protocol()
self.repopulate_treeview()
else:
selected_file = filedialog.askopenfilename(filetypes=(("JSON Files", "*.json"),))
Expand Down Expand Up @@ -667,7 +685,8 @@ def repopulate_treeview(self):
def __edit_protocol_step(self, event):
if self.selected_step:
step = self.protocol_steps[int(self.selected_step) - 1]
AddWoodwayProtocolStep(self, self.root, edit=True, dur=step[0], ls=step[1], rs=step[2], incl=step[3])
AddWoodwayProtocolStep(self, self.root, edit=True, dur=step[0], ls=step[1], rs=step[2], incl=step[3],
freq_key=step[4], dur_key=step[5])

def __add_protocol_step(self):
AddWoodwayProtocolStep(self, self.root)
Expand Down Expand Up @@ -741,8 +760,9 @@ def __write_incline(self, incline):

class ViewBLE:
def __init__(self, parent, height, width, field_font, header_font, button_size,
session_dir, ble_button, config, ble_thresh=None):
session_dir, ble_button, config, caller, ble_thresh=None):
self.root = parent
self.caller = caller
self.config = config
self.tab_button = ble_button
self.session_dir = session_dir
Expand Down Expand Up @@ -778,7 +798,9 @@ def __init__(self, parent, height, width, field_font, header_font, button_size,
self.prot_treeview_parents = []
prot_heading_dict = {"#0": ["Duration", 'w', 20, YES, 'w']}
prot_column_dict = {"1": ["Left", 'c', 1, YES, 'c'],
"2": ["Right", 'c', 1, YES, 'c']}
"2": ["Right", 'c', 1, YES, 'c'],
"3": ["F", 'c', 50, NO, 'c'],
"4": ["D", 'c', 50, NO, 'c']}
treeview_offset = int(width * 0.03)
self.prot_treeview, self.prot_filescroll = build_treeview(parent, x=treeview_offset, y=40,
height=height - element_height_adj - 40,
Expand Down Expand Up @@ -972,7 +994,7 @@ def __edit_protocol_step(self, event):
if self.selected_step:
step = self.protocol_steps[int(self.selected_step) - 1]
AddBleProtocolStep(self, self.root, edit=True, dur=step[0],
motor_1=step[1], motor_2=step[2])
motor_1=step[1], motor_2=step[2], freq_key=step[3], dur_key=step[4])

def next_protocol_step(self, current_time):
if self.selected_step >= len(self.protocol_steps):
Expand Down Expand Up @@ -1011,6 +1033,10 @@ def __update_ble_protocol(self):
scroll_to(self.prot_treeview, self.selected_step)
if self.config.get_protocol_beep():
SessionTimeFields.beep()
if self.selected_command[3] != '':
self.caller.handle_key_press(self.selected_command[3])
if self.selected_command[4] != '':
self.caller.handle_key_press(self.selected_command[4])

def __update_ble(self):
for slider in self.slider_objects:
Expand All @@ -1028,16 +1054,27 @@ def populate_protocol_steps(self):
for i in range(0, len(self.protocol_steps)):
self.prot_treeview_parents.append(
self.prot_treeview.insert("", 'end', str(i + 1), text=str(self.protocol_steps[i][0]),
values=(self.protocol_steps[i][1], self.protocol_steps[i][2]),
values=(self.protocol_steps[i][1], self.protocol_steps[i][2],
self.protocol_steps[i][3], self.protocol_steps[i][4]),
tags=(treeview_tags[(i + 1) % 2])))

def __heal_legacy_protocol(self):
for step in self.protocol_steps:
if len(step) == 3:
step.extend(['', ''])
with open(self.prot_file, 'w') as f:
x = {"Steps": self.protocol_steps}
json.dump(x, f)

def __load_protocol_from_file(self, selected_file=None):
try:
if selected_file:
self.selected_step = 0
self.prot_file = selected_file
with open(self.prot_file, 'r') as f:
self.protocol_steps = json.load(f)['Steps']
if len(self.protocol_steps[0]) == 3:
self.__heal_legacy_protocol()
self.repopulate_treeview()
else:
selected_file = filedialog.askopenfilename(filetypes=(("JSON Files", "*.json"),))
Expand Down
70 changes: 64 additions & 6 deletions tkinter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,21 +630,22 @@ def set_entry_text(widget: tkinter.Entry, text):


class AddWoodwayProtocolStep:
def __init__(self, top, root, edit=False, dur=None, rs=None, ls=None, incl=None):
def __init__(self, top, root, edit=False, dur=None, rs=None, ls=None, incl=None, freq_key=None, dur_key=None):
assert top.popup_return
self.caller = top
self.entry = None
self.popup_root = None
self.name = "Add Woodway Step"
self.dur, self.rs, self.ls, self.incl = dur, rs, ls, incl
self.freq_key, self.dur_key = freq_key, dur_key
self.edit = edit
self.popup_entry(root)

def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x250")
popup_root.geometry("300x350")
popup_root.title(self.name)

# Create an Entry Widget in the Toplevel window
Expand Down Expand Up @@ -673,6 +674,27 @@ def popup_entry(self, root):
if self.incl is not None:
set_entry_text(self.incline_entry, self.incl)

field_font = ('Purisa', 12)
freq_label = tkinter.Label(popup_root, font=field_font, text="Frequency Key Association", bg='white')
freq_label.pack()
self.freq_var = tkinter.StringVar(popup_root, value=self.freq_key)
freq_box = Combobox(popup_root, textvariable=self.freq_var, font=field_font)
freq_box['values'] = ['None'] + self.caller.caller.ovu.key_view.bindings
freq_box['state'] = 'readonly'
freq_box.config(font=field_font)
freq_box.pack()
freq_box.option_add('*TCombobox*Listbox.font', field_font)

dur_label = tkinter.Label(popup_root, font=field_font, text="Duration Key Association", bg='white')
dur_label.pack()
self.dur_var = tkinter.StringVar(popup_root, self.dur_key)
dur_box = Combobox(popup_root, textvariable=self.dur_var, font=field_font)
dur_box['values'] = ['None'] + self.caller.caller.ovu.key_view.dur_bindings
dur_box['state'] = 'readonly'
dur_box.config(font=field_font)
dur_box.pack()
dur_box.option_add('*TCombobox*Listbox.font', field_font)

# Create a Button Widget in the Toplevel Window
button = tkinter.Button(popup_root, text="OK", command=self.close_win)
button.pack(pady=5, side=TOP)
Expand All @@ -682,8 +704,15 @@ def popup_entry(self, root):

def close_win(self):
try:
freq_key = ''
if self.freq_var.get() not in ['None', '']:
freq_key = str(self.freq_var.get()[0]).lower()
dur_key = ''
if self.dur_var.get() not in ['None', '']:
dur_key = str(self.dur_var.get()[0]).lower()
new_step = [float(self.duration_entry.get()), float(self.ls_entry.get()),
float(self.rs_entry.get()), float(self.incline_entry.get())]
float(self.rs_entry.get()), float(self.incline_entry.get()),
freq_key, dur_key]
self.caller.popup_return(new_step, self.edit)
self.popup_root.destroy()
except ValueError:
Expand All @@ -692,21 +721,22 @@ def close_win(self):


class AddBleProtocolStep:
def __init__(self, top, root, edit=False, dur=None, motor_1=None, motor_2=None):
def __init__(self, top, root, edit=False, dur=None, motor_1=None, motor_2=None, freq_key=None, dur_key=None):
assert top.popup_return
self.caller = top
self.entry = None
self.popup_root = None
self.name = "Add BLE Step"
self.edit = edit
self.dur, self.motor_1, self.motor_2 = dur, motor_1, motor_2
self.freq_key, self.dur_key = freq_key, dur_key
self.popup_entry(root)

def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x175")
popup_root.geometry("300x300")
popup_root.title(self.name)

# Create an Entry Widget in the Toplevel window
Expand All @@ -729,6 +759,27 @@ def popup_entry(self, root):
if self.motor_2 is not None:
set_entry_text(self.motor_2_entry, self.motor_2)

field_font = ('Purisa', 12)
freq_label = tkinter.Label(popup_root, font=field_font, text="Frequency Key Association", bg='white')
freq_label.pack()
self.freq_var = tkinter.StringVar(popup_root, value=self.freq_key)
freq_box = Combobox(popup_root, textvariable=self.freq_var, font=field_font)
freq_box['values'] = ['None'] + self.caller.caller.ovu.key_view.bindings
freq_box['state'] = 'readonly'
freq_box.config(font=field_font)
freq_box.pack()
freq_box.option_add('*TCombobox*Listbox.font', field_font)

dur_label = tkinter.Label(popup_root, font=field_font, text="Duration Key Association", bg='white')
dur_label.pack()
self.dur_var = tkinter.StringVar(popup_root, value=self.dur_key)
dur_box = Combobox(popup_root, textvariable=self.dur_var, font=field_font)
dur_box['values'] = ['None'] + self.caller.caller.ovu.key_view.dur_bindings
dur_box['state'] = 'readonly'
dur_box.config(font=field_font)
dur_box.pack()
dur_box.option_add('*TCombobox*Listbox.font', field_font)

# Create a Button Widget in the Toplevel Window
button = tkinter.Button(popup_root, text="OK", command=self.close_win)
button.pack(pady=5, side=TOP)
Expand All @@ -738,8 +789,15 @@ def popup_entry(self, root):

def close_win(self):
try:
freq_key = ''
if self.freq_var.get() not in ['None', '']:
freq_key = str(self.freq_var.get()[0]).lower()
dur_key = ''
if self.dur_var.get() not in ['None', '']:
dur_key = str(self.dur_var.get()[0]).lower()
new_step = [float(self.duration_entry.get()), float(self.motor_1_entry.get()),
float(self.motor_2_entry.get())]
float(self.motor_2_entry.get()),
freq_key, dur_key]
self.caller.popup_return(new_step, edit=self.edit)
self.popup_root.destroy()
except ValueError:
Expand Down

0 comments on commit 3cdd3b6

Please sign in to comment.