2929import serial
3030import numpy as np
3131
32- from spectrometer import config
32+ from spectrometer . configuration import Config
3333import threading
3434import tkinter as ttk
3535import time
4242# byte[7-10]: The 4 bytes constituting the 32-bit int holding the ICG-period
4343# byte[11]: Continuous flag: 0 equals one acquisition, 1 equals continuous mode
4444# byte[12]: The number of integrations to average
45- def rxtx (panel , SerQueue , progress_var ):
45+ def rxtx (panel , SerQueue , progress_var , config : Config ):
4646 threadser = None
47- if config .AVGn [0 ] == 0 :
47+ if config .avg_n [0 ] == 0 :
4848 threadser = threading .Thread (
49- target = rxtxoncethread , args = (panel , SerQueue , progress_var ), daemon = True
49+ target = rxtxoncethread ,
50+ args = (panel , SerQueue , progress_var , config ),
51+ daemon = True ,
5052 )
51- elif config .AVGn [0 ] == 1 :
53+ elif config .avg_n [0 ] == 1 :
5254 threadser = threading .Thread (
53- target = rxtxcontthread , args = (panel , progress_var ), daemon = True
55+ target = rxtxcontthread , args = (panel , progress_var , config ), daemon = True
5456 )
5557 if threadser is not None :
5658 threadser .start ()
5759
5860
59- def rxtxoncethread (panel , SerQueue , progress_var ):
61+ def rxtxoncethread (panel , SerQueue , progress_var , config : Config ):
6062 # open serial port
6163 try :
6264 ser = serial .Serial (config .port , config .baudrate )
@@ -69,7 +71,7 @@ def rxtxoncethread(panel, SerQueue, progress_var):
6971 # start the progressbar
7072 panel .progress .config (mode = "determinate" )
7173 threadprogress = threading .Thread (
72- target = progressthread , args = (progress_var ,), daemon = True
74+ target = progressthread , args = (progress_var , config ), daemon = True
7375 )
7476 threadprogress .start ()
7577
@@ -81,7 +83,7 @@ def rxtxoncethread(panel, SerQueue, progress_var):
8183
8284 # Determine hardware vs software averaging
8385 # Firmware supports max 15 averages, so for >15 we need software averaging
84- requested_avg = config .AVGn [1 ]
86+ requested_avg = config .avg_n [1 ]
8587 if requested_avg <= 15 :
8688 # Use hardware averaging only
8789 hardware_avg = requested_avg
@@ -99,16 +101,16 @@ def rxtxoncethread(panel, SerQueue, progress_var):
99101 config .txfull [0 ] = 69
100102 config .txfull [1 ] = 82
101103 # split 32-bit integers to be sent into 8-bit data
102- config .txfull [2 ] = (config .SHperiod >> 24 ) & 0xFF
103- config .txfull [3 ] = (config .SHperiod >> 16 ) & 0xFF
104- config .txfull [4 ] = (config .SHperiod >> 8 ) & 0xFF
105- config .txfull [5 ] = config .SHperiod & 0xFF
106- config .txfull [6 ] = (config .ICGperiod >> 24 ) & 0xFF
107- config .txfull [7 ] = (config .ICGperiod >> 16 ) & 0xFF
108- config .txfull [8 ] = (config .ICGperiod >> 8 ) & 0xFF
109- config .txfull [9 ] = config .ICGperiod & 0xFF
104+ config .txfull [2 ] = (config .sh_period >> 24 ) & 0xFF
105+ config .txfull [3 ] = (config .sh_period >> 16 ) & 0xFF
106+ config .txfull [4 ] = (config .sh_period >> 8 ) & 0xFF
107+ config .txfull [5 ] = config .sh_period & 0xFF
108+ config .txfull [6 ] = (config .icg_period >> 24 ) & 0xFF
109+ config .txfull [7 ] = (config .icg_period >> 16 ) & 0xFF
110+ config .txfull [8 ] = (config .icg_period >> 8 ) & 0xFF
111+ config .txfull [9 ] = config .icg_period & 0xFF
110112 # averages to perfom (send hardware average count)
111- config .txfull [10 ] = config .AVGn [0 ]
113+ config .txfull [10 ] = config .avg_n [0 ]
112114 config .txfull [11 ] = hardware_avg
113115
114116 # Perform software averaging by collecting multiple times
@@ -120,7 +122,7 @@ def rxtxoncethread(panel, SerQueue, progress_var):
120122 ser .write (config .txfull )
121123
122124 # wait for the firmware to return data
123- config .rxData8 = ser .read (7388 )
125+ config .rxData8 = np . frombuffer ( ser .read (7388 ), dtype = np . uint8 )
124126
125127 # combine received bytes into 16-bit data
126128 for rxi in range (3694 ):
@@ -134,19 +136,21 @@ def rxtxoncethread(panel, SerQueue, progress_var):
134136 ser .close ()
135137
136138 # enable all buttons
137- panelwakeup (panel )
139+ panelwakeup (panel , config )
138140
139141 if config .stopsignal == 0 :
140142 # If we did software averaging, compute the average
141143 if software_iterations > 1 :
142144 for rxi in range (3694 ):
143- config .rxData16 [rxi ] = np .uint16 (np .round (accumulated_data [rxi ] / software_iterations ))
145+ config .rxData16 [rxi ] = np .uint16 (
146+ np .round (accumulated_data [rxi ] / software_iterations )
147+ )
144148
145149 # plot the new data
146150 panel .bupdate .invoke ()
147151 # hold values for saving data to file as the SHperiod and ICGperiod may be updated after acquisition
148- config .SHsent = config .SHperiod
149- config .ICGsent = config .ICGperiod
152+ config .sh_sent = config .sh_period
153+ config .icg_sent = config .icg_period
150154
151155 SerQueue .queue .clear ()
152156
@@ -157,7 +161,7 @@ def rxtxoncethread(panel, SerQueue, progress_var):
157161 )
158162
159163
160- def rxtxcontthread (panel , progress_var ):
164+ def rxtxcontthread (panel , progress_var , config : Config ):
161165 # open serial port
162166 try :
163167 ser = serial .Serial (config .port , config .baudrate )
@@ -181,25 +185,25 @@ def rxtxcontthread(panel, progress_var):
181185 config .txfull [0 ] = 69
182186 config .txfull [1 ] = 82
183187 # split 32-bit integers to be sent into 8-bit data
184- config .txfull [2 ] = (config .SHperiod >> 24 ) & 0xFF
185- config .txfull [3 ] = (config .SHperiod >> 16 ) & 0xFF
186- config .txfull [4 ] = (config .SHperiod >> 8 ) & 0xFF
187- config .txfull [5 ] = config .SHperiod & 0xFF
188- config .txfull [6 ] = (config .ICGperiod >> 24 ) & 0xFF
189- config .txfull [7 ] = (config .ICGperiod >> 16 ) & 0xFF
190- config .txfull [8 ] = (config .ICGperiod >> 8 ) & 0xFF
191- config .txfull [9 ] = config .ICGperiod & 0xFF
188+ config .txfull [2 ] = (config .sh_period >> 24 ) & 0xFF
189+ config .txfull [3 ] = (config .sh_period >> 16 ) & 0xFF
190+ config .txfull [4 ] = (config .sh_period >> 8 ) & 0xFF
191+ config .txfull [5 ] = config .sh_period & 0xFF
192+ config .txfull [6 ] = (config .icg_period >> 24 ) & 0xFF
193+ config .txfull [7 ] = (config .icg_period >> 16 ) & 0xFF
194+ config .txfull [8 ] = (config .icg_period >> 8 ) & 0xFF
195+ config .txfull [9 ] = config .icg_period & 0xFF
192196 # averages to perfom
193- config .txfull [10 ] = config .AVGn [0 ]
194- config .txfull [11 ] = config .AVGn [1 ]
197+ config .txfull [10 ] = config .avg_n [0 ]
198+ config .txfull [11 ] = config .avg_n [1 ]
195199
196200 # transmit everything at once (the USB-firmware does not work if all bytes are not transmittet in one go)
197201 ser .write (config .txfull )
198202
199203 # loop to acquire and plot data continuously
200204 while config .stopsignal == 0 :
201205 # wait for the firmware to return data
202- config .rxData8 = ser .read (7388 )
206+ config .rxData8 = np . frombuffer ( ser .read (7388 ), dtype = np . uint8 )
203207
204208 if config .stopsignal == 0 :
205209 # combine received bytes into 16-bit data
@@ -211,8 +215,8 @@ def rxtxcontthread(panel, progress_var):
211215 # plot the new data
212216 panel .bupdate .invoke ()
213217 # hold values for saving data to file
214- config .SHsent = config .SHperiod
215- config .ICGsent = config .ICGperiod
218+ config .sh_sent = config .sh_period
219+ config .icg_sent = config .icg_period
216220
217221 # resend settings with continuous transmission disabled to avoid flooding of the serial port
218222 config .txfull [10 ] = 0
@@ -224,7 +228,7 @@ def rxtxcontthread(panel, progress_var):
224228
225229 # close serial port
226230 ser .close ()
227- panelwakeup (panel )
231+ panelwakeup (panel , config )
228232 panel .progress .stop ()
229233
230234 except serial .SerialException :
@@ -234,11 +238,11 @@ def rxtxcontthread(panel, progress_var):
234238 )
235239
236240
237- def progressthread (progress_var ):
241+ def progressthread (progress_var , config : Config ):
238242 progress_var .set (0 )
239-
243+
240244 # Calculate total time considering software averaging
241- requested_avg = config .AVGn [1 ]
245+ requested_avg = config .avg_n [1 ]
242246 if requested_avg <= 15 :
243247 # Hardware averaging only
244248 hardware_avg = requested_avg
@@ -247,25 +251,25 @@ def progressthread(progress_var):
247251 # Software averaging with multiple collections
248252 hardware_avg = 15
249253 software_iterations = int (np .ceil (requested_avg / 15.0 ))
250-
254+
251255 # Total time is: ICGperiod * hardware_avg * software_iterations
252- total_time = config .ICGperiod * hardware_avg * software_iterations / config .MCLK
253-
256+ total_time = config .icg_period * hardware_avg * software_iterations / config .mclk
257+
254258 # Add overhead for serial communication and data processing per iteration
255259 # Estimate ~0.5 seconds per iteration for serial read and processing
256260 serial_overhead = software_iterations * 0.5
257261 total_time += serial_overhead
258-
262+
259263 for i in range (1 , 11 ):
260264 progress_var .set (i )
261265 # wait 1/10th of the total acquisition time before adding to progress bar
262266 time .sleep (total_time / 10 )
263267
264268
265- def rxtxcancel (SerQueue ):
269+ def rxtxcancel (SerQueue , config : Config ):
266270 config .stopsignal = 1
267271 # Are we stopping one very long measurement, or the continuous real-time view?
268- if config .AVGn [0 ] == 0 :
272+ if config .avg_n [0 ] == 0 :
269273 ser = SerQueue .get ()
270274 ser .cancel_read ()
271275
@@ -289,7 +293,7 @@ def panelsleep(panel):
289293 pass
290294
291295
292- def panelwakeup (panel ):
296+ def panelwakeup (panel , config : Config ):
293297 panel .bstop .config (state = ttk .DISABLED )
294298 panel .bopen .config (state = ttk .NORMAL )
295299 panel .bsave .config (state = ttk .NORMAL )
0 commit comments