179def read(ctx, obj, readall, continuous, print_out, read_period, save, file_name):
180 '''
181 Read the content of the endpoint master readout buffer.
182 '''
183 lDevice = obj.mDevice
184 lHSI = obj.mHSI
185
186 read_events=0
187 if continuous:
188 secho("[INFO] Starting HSI data readout", fg='green')
189 if not (save or print_out):
190 secho('[WARNING] you have chosen to neither save or print the HSI data', fg='yellow')
191 last_ts=0
192 if save:
193 h5_file_name = datetime.now().strftime("hsi_data_%d_%m_%Y_%H_%M_%S.hdf5")
194 if file_name is not None:
195 h5_file_name=file_name
196
197 f = h5py.File(h5_file_name, "w")
198 hsi_dataset = f.create_dataset("hsi_frames", (0,7), maxshape=(None, 7), chunks=True, dtype='u4')
199 else:
200 if file_name is not None:
201 secho('[WARNING] You have provided a file name but chosen not to save the readout data', fg='yellow')
202
203 with toolbox.InterruptHandler() as h:
204 while(True):
205 n_words=0
206 hsi_words = lHSI.read_data_buffer(n_words,False,False)
207 n_hsi_events = len(hsi_words) // kHSIWordsNumber
208
209 if save:
210 hsi_dataset_start_index=hsi_dataset.shape[0]
211 hsi_dataset.resize(hsi_dataset_start_index+n_hsi_events, axis=0)
212
213 if (len(hsi_words) % kHSIWordsNumber == 0 and hsi_words.size() > 0):
214
215 for i in range(0,n_hsi_events):
216 start_index = i * kHSIWordsNumber
217 end_index = start_index + kHSIWordsNumber
218 raw_event=hsi_words[start_index:end_index]
219
220 header = raw_event[0]
221 ts_low = raw_event[1]
222 ts_high = raw_event[2]
223 data = raw_event[3]
224 trigger = raw_event[4]
225
226 ts = ts_low | (ts_high << 32)
227
228
229 counter = header & 0x0000ffff
230
231 if (header >> 16) != 0xaa00:
232 secho("[ERROR] Invalid HSIEventHeader", fg='red')
233 continue
234
235 if ts == 0:
236 secho("[ERROR] Invalid HSIEvent timestamp", fg='red')
237 continue
238
239 delta_ticks=ts-last_ts
240 if last_ts == 0:
241 delta_ticks=0
242 last_ts=ts
243
244 if print_out:
245 print(f"[INFO] got event: {counter} ts: {ts} signals: {data} delta ts: {delta_ticks}")
246
247 read_events=read_events+1
248 if save:
249 hsi_dataset[hsi_dataset_start_index+i,0]=(0x1 << 6) | 0x1
250 hsi_dataset[hsi_dataset_start_index+i,1]=ts_low
251 hsi_dataset[hsi_dataset_start_index+i,2]=ts_high
252 hsi_dataset[hsi_dataset_start_index+i,3]=data
253 hsi_dataset[hsi_dataset_start_index+i,4]=0x0
254 hsi_dataset[hsi_dataset_start_index+i,5]=trigger
255 hsi_dataset[hsi_dataset_start_index+i,6]=counter
256
257 elif len(hsi_words) != 0:
258 secho(f"[ERROR] unexpected n words {len(hsi_words)}", fg='red')
259 if h.interrupted:
260 secho(f"[INFO] Stopping HSI data readout after {read_events} events", fg='green')
261 break
262 time.sleep(read_period*1e-3)
263 if save:
264 f.close()
265 else:
266 echo(lHSI.get_data_buffer_table(readall,False))
267