-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.py
More file actions
336 lines (297 loc) · 12.3 KB
/
Copy pathcallbacks.py
File metadata and controls
336 lines (297 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import dearpygui.dearpygui as dpg
import time
import pandas as pd
import numpy as np
from datetime import datetime as dt
from constants import *
# def run_task():
# global running
# global paused
# global progress
# print("Running...")
#
# for i in range(1, 101):
# while paused:
# time.sleep(0.1)
# if not running:
# return
# progress = i
# print(i)
# dpg.set_value(progress_bar, 1 / 100 * (i))
# dpg.configure_item(progress_bar, overlay=f"{i}%")
# time.sleep(0.05)
#
# print("Finished")
# running = False
#
#
# def reset_callback():
# global running
# global paused
# global progress
# running = False
# paused = False
# progress = 0
# dpg.set_value(progress_bar, 0)
# dpg.configure_item(progress_bar, overlay="0%")
def save_file(sender, app_data, user_data):
print("=== Save File button Clicked ===")
print(f"sender is: {sender}")
print(f"app_data is: {app_data}")
print(f"user_data is: {user_data} \n")
#
# dicts = bms_data.dict_list
#
# print('DICTS LIST', dicts)
# file_path_name = app_data['file_path_name']
#
# # Save the file here
# if not os.path.exists(LOG_RECORDS_DIR):
# os.makedirs(LOG_RECORDS_DIR)
#
# with open(f'{file_path_name}', 'w', newline='') as csvfile:
# writer = csv.writer(csvfile)
# header_row = list(dicts[1].keys()) # Using the Keys as header titles
# writer.writerow(header_row)
# for key, value in dicts.items():
# data_row = list(value.values())
# writer.writerow(data_row)
def download_file(sender, app_data, user_data):
default_filename = dt.now().strftime("%m-%d-%Y__%H_%M_%S")
if dpg.does_item_exist("file_dialog_id"):
print("File dialog is already open")
dpg.configure_item("file_dialog_id", show=True)
return
with dpg.file_dialog(directory_selector=False, modal=True, callback=save_file, id="file_dialog_id",
default_filename=default_filename, default_path=DOWNLOADS_DIR, width=800, height=400):
dpg.add_file_extension(".csv")
dpg.add_file_extension(".txt")
def update_chart(chartx, charty, source_name):
dpg.set_value('series_list', [chartx, charty])
dpg.set_item_label('series_list', source_name)
dpg.fit_axis_data('x_axis')
dpg.fit_axis_data('y_axis')
def update_csv_table(data):
for item in dpg.get_item_children('csv_table', slot=1):
dpg.delete_item(item)
for index, row in data.iterrows():
with dpg.table_row(parent="csv_table"):
with dpg.table_cell(): # step
dpg.add_text(str(index + 1))
for value in row.iloc[:3]:
with dpg.table_cell():
dpg.add_text(str(value) if pd.notna(value) else "")
def create_csv_chart_data(sender, app_data):
print(sender)
global chartx, charty, chart_unit
chartx = []
charty = []
if sender == "csv_sender":
data = pd.read_csv(app_data['file_path_name'])
update_csv_table(data)
source = app_data['file_name']
else:
data = app_data
source = "MANUAL"
if int(data.at[0, 'slowrate']) == 1:
chart_unit = '(ms)'
else:
chart_unit = '(us)'
print(data)
current_time = 0.0
total_steps = int(data.at[0, 'step'])
count = int(data.at[0, 'count'])
for repeat in range(count): # Repeat full step sequence
for i in range(total_steps):
level = float(data.at[i, 'level'])
width = float(data.at[i, 'width'])
slew = float(data.at[i, 'slew'])
# Start of level
chartx.append(current_time)
charty.append(level)
# End of level (pulse width)
current_time += width
chartx.append(current_time)
charty.append(level)
if i + 1 < total_steps:
next_level = float(data.at[i + 1, 'level'])
current_time += slew
chartx.append(current_time)
charty.append(next_level)
# # Slew to next level if not last step
# if i + 1 < total_steps:
# next_level = float(data.at[i + 1, 'level'])
# delta_I = abs(next_level - level)
#
# if slew != 0:
# transition_time = delta_I / slew
# else:
# transition_time = 0.0
#
# current_time += transition_time
# chartx.append(current_time)
# charty.append(next_level)
# print(chartx)
# print(charty)
update_chart(chartx, charty, source)
def create_manual_chart_data(shape):
match shape:
case "Sin":
generate_sine_dataframe(dpg.get_value("sin_amplitude"), dpg.get_value("sin_offset"),
dpg.get_value("sin_frequency"), dpg.get_value("sin_duration"),
dpg.get_value("sin_count"))
case "Custom":
generate_sine_dataframe()
# global chartx, charty, chart_unit
# chartx = []
# charty = []
# current_time = 0.0
# print(steps, level, width, slew)
# # for i in range(steps):
# # level = float(level)
# # width = float(width)
# # slew = float(slew)
#
# for i in range(steps):
# chartx.append(current_time)
# charty.append(level)
#
# # end of pulse
# current_time += width
# chartx.append(current_time)
# charty.append(level)
#
# if i + 1 < steps:
# next_level = float(level)
# current_time += slew
# chartx.append(current_time)
# charty.append(next_level)
#
# update_chart(chartx, charty,'Manual Entry')
def generate_sine_dataframe(amp, offset, freq, duration, count):
slew = 0.01
slowrate = dpg.get_value("input_slowrate")
if slowrate == "High-rate (A/us)":
slowrate = 0
else:
slowrate = 1
rng = amp + (amp/5)
step_count = 84
time_steps = np.linspace(0, duration, step_count)
levels = amp * np.sin(2 * np.pi * freq * time_steps) + offset
widths = np.diff(time_steps, append=duration) # width per step
df = pd.DataFrame({
"level": np.round(levels, 3),
"slew": slew,
"width": np.round(widths, 6),
})
# Add control parameters only to the first row
df.loc[0, "slowrate"] = slowrate
df.loc[0, "range"] = rng
df.loc[0, "count"] = count
df.loc[0, "step"] = step_count
create_csv_chart_data("MANUAL SIN", df)
def update_manual_configs(sender, app_data, user_data):
app = user_data
print(app_data)
for item in dpg.get_item_children('manual_configs', slot=1):
print(item)
dpg.delete_item(item)
print("")
dpg.render_dearpygui_frame()
match app_data:
case 'Square':
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Amplitude: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, min_clamped=True, default_value=app.input_level,
max_clamped=True, tag='input_level', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('High-level time: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, min_clamped=True, default_value=app.input_level,
max_clamped=True, tag='input_highwidth', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Low-level time: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, min_clamped=True, default_value=app.input_level,
max_clamped=True, tag='input_lowwidth', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Slew: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, min_clamped=True, default_value=app.input_level,
max_clamped=True, tag='input_slew', width=-1)
case 'Sin':
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Amplitude: ')
with dpg.table_cell():
dpg.add_input_int(min_value=0, min_clamped=True, default_value=0,
max_clamped=True, tag='sin_amplitude', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Offset: ')
with dpg.table_cell():
dpg.add_input_int(min_value=0, min_clamped=True, default_value=0,
max_clamped=True, tag='sin_offset', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Frequency: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, min_clamped=True, default_value=1,
max_clamped=True, tag='sin_frequency', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Duration: ')
with dpg.table_cell():
dpg.add_input_int(min_value=0, min_clamped=True, default_value=1,
max_clamped=True, tag='sin_duration', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Count: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, min_clamped=True, default_value=1,
max_clamped=True, tag='sin_count', width=-1)
case 'Custom':
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Range: ')
with dpg.table_cell():
dpg.add_input_int(min_value=0, min_clamped=True, default_value=app.input_range,
max_clamped=True, tag='input_range', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Count: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, max_value=65536, min_clamped=True,
default_value=app.input_count,
max_clamped=True, tag='input_count', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Step: ')
with dpg.table_cell():
dpg.add_input_int(min_value=2, max_value=83, min_clamped=True, default_value=app.input_step,
max_clamped=True, tag='input_step', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Level: ')
with dpg.table_cell():
dpg.add_input_int(min_value=1, min_clamped=True,
default_value=app.input_level,
max_clamped=True, tag='input_level', width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Width(s): ')
with dpg.table_cell():
dpg.add_input_float(min_value=0, min_clamped=True, tag='input_width',
default_value=app.input_width, width=-1)
with dpg.table_row(parent='manual_configs'):
with dpg.table_cell():
dpg.add_text('Slew: ')
with dpg.table_cell():
dpg.add_input_float(tag='input_slew', default_value=app.input_slew,
width=-1)