Skip to content

Commit a125045

Browse files
committed
Corrected max_pulse_shape.csv
1 parent e633c07 commit a125045

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

code/shproto/dispatcher.py

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,44 @@
1616

1717
max_bins = 8192
1818
logger = logging.getLogger(__name__)
19+
1920
stopflag = 0
2021
stopflag_lock = threading.Lock()
22+
2123
spec_stopflag = 0
2224
spec_stopflag_lock = threading.Lock()
25+
2326
histogram = [0] * max_bins
2427
histogram_lock = threading.Lock()
28+
2529
command = ""
2630
command_lock = threading.Lock()
31+
32+
cps = 0
33+
cps_lock = threading.Lock()
34+
35+
calibration_updated = 0
36+
calibration_lock = threading.Lock()
37+
2738
pkts03 = 0
2839
pkts04 = 0
2940
total_pkts = 0
3041
dropped = 0
3142
total_time = 0
3243
cpu_load = 0
33-
cps = 0
34-
cps_lock = threading.Lock()
3544
lost_impulses = 0
3645
last_counts = 0
37-
data_directory = None
3846
cps_list = []
3947
serial_number = ""
4048
calibration = [0., 1., 0., 0., 0.]
41-
calibration_updated = 0
42-
calibration_lock = threading.Lock()
4349
inf_str = ''
50+
data_directory = ''
51+
4452

45-
with global_vars.write_lock:
46-
data_directory = global_vars.data_directory
4753

4854
# This function communicates with the device
4955
def start(sn=None):
56+
5057
READ_BUFFER = 1
5158
pulse_file_opened = 0
5259
shproto.dispatcher.clear()
@@ -152,35 +159,55 @@ def start(sn=None):
152159
((response.payload[i * 4 + 5]) << 24)
153160
shproto.dispatcher.histogram[index] = value & 0x7FFFFFF
154161
response.clear()
162+
163+
# ----- cmd pulse mode -------------------------------------
164+
165+
166+
# Track whether the CSV file has been initialized
167+
pulse_file_initialized = False
168+
155169
if response.cmd == shproto.MODE_PULSE:
156170
pulse_data = []
171+
172+
# Extract pulse data from the payload (16-bit values)
157173
for i in range(0, len(response.payload), 2):
158174
if i + 1 < len(response.payload):
159175
value = (response.payload[i + 1] << 8) | response.payload[i]
160176
pulse_data.append(value)
161177

162178
if pulse_data:
163-
pulse_data = pulse_data[:-1] # Remove the last item if needed
179+
pulse_data = pulse_data[:-1] # Remove last item if needed
164180
logger.debug(f"Processed Pulse Data: {pulse_data}")
181+
165182
with global_vars.write_lock:
166183
global_vars.max_pulse_shape = pulse_data
167-
if pulse_file_opened != 1:
168-
fd_pulses = open(os.path.join(data_directory,"max-pulse-check.csv"), "w+")
169-
pulse_file_opened = 1
170184

171-
shproto.dispatcher.pkts01 += 1
172-
offset = response.payload[0] & 0xFF | ((response.payload[1] & 0xFF) << 8)
173-
count = int((response.len - 2) / 2)
174-
pulse = []
175-
for i in range(0, count):
176-
index = offset + i
177-
if index < len(shproto.dispatcher.histogram):
178-
value = (response.payload[i * 2 + 2]) | ((response.payload[i * 2 + 3]) << 8)
179-
pulse.append(value & 0x7FFFFFF)
180-
fd_pulses.writelines("{:d} ".format(value & 0x7FFFFFF))
181-
fd_pulses.writelines("\n")
182-
fd_pulses.flush()
185+
# Open the CSV file only once per script execution
186+
if not pulse_file_initialized:
187+
with global_vars.write_lock:
188+
data_directory = global_vars.data_directory
189+
csv_file_path = os.path.join(data_directory, "_max-pulse-shape.csv")
190+
191+
# Check if the file already exists (to avoid overwriting existing data)
192+
file_exists = os.path.isfile(csv_file_path)
193+
194+
with open(csv_file_path, "a+") as fd_pulses:
195+
# Write header only if the file is newly created
196+
if not file_exists:
197+
header = ",".join(map(str, range(len(pulse_data)))) + "\n"
198+
fd_pulses.write(header)
199+
200+
pulse_file_initialized = True # Ensure we don't write the header again
201+
202+
# Append the pulse data as a new row
203+
with open(csv_file_path, "a") as fd_pulses:
204+
fd_pulses.write(",".join(map(str, pulse_data)) + "\n")
205+
183206
response.clear()
207+
208+
209+
# ------- cmd stat mode --------------------------
210+
184211
elif response.cmd == shproto.MODE_STAT:
185212
shproto.dispatcher.pkts04 += 1
186213
shproto.dispatcher.total_time = (response.payload[0] & 0xFF) | \
@@ -549,9 +576,14 @@ def load_json_data(file_path):
549576
def process_03(_command):
550577
with shproto.dispatcher.command_lock:
551578
shproto.dispatcher.command = _command
579+
time.sleep(0.3) # Give it a little time to process
552580
logger.info(f'Completed process_03({_command})\n')
553581
return
554582

583+
584+
585+
586+
555587
def stop():
556588
logger.info('Command shproto.stop \n')
557589
with shproto.dispatcher.stopflag_lock:

0 commit comments

Comments
 (0)