@@ -111,8 +111,13 @@ def send_command(ser, command):
111
111
return response
112
112
113
113
# Function to read data from Arduino
114
- def read_arduino_data (ser , csv_writer = None ):
114
+ def read_arduino_data (ser , csv_writer = None , inverted = False ):
115
115
global total_packet_count , cumulative_packet_count , previous_sample_number , missing_samples , buffer , data
116
+
117
+ max_value = 2 ** 14 if board == "UNO-R4" else 2 ** 10
118
+ min_value = 0
119
+ mid_value = (max_value - 1 ) / 2
120
+
116
121
raw_data = ser .read (ser .in_waiting or 1 ) # Read available data from the serial port
117
122
if raw_data == b'' :
118
123
send_command (ser , 'START' )
@@ -149,7 +154,16 @@ def read_arduino_data(ser, csv_writer=None):
149
154
high_byte = packet [2 * channel + HEADER_LENGTH ]
150
155
low_byte = packet [2 * channel + HEADER_LENGTH + 1 ]
151
156
value = (high_byte << 8 ) | low_byte # Combine high and low bytes
152
- channel_data .append (float (value )) # Convert to float and add to channel data
157
+ if inverted : # Apply inversion if the flag is set
158
+ if value > mid_value :
159
+ inverted_data = (mid_value ) - abs (mid_value - value )
160
+ elif value < mid_value :
161
+ inverted_data = (mid_value ) + abs (mid_value - value )
162
+ else :
163
+ inverted_data = value
164
+ channel_data .append (float (inverted_data )) # Append the inverted value
165
+ else :
166
+ channel_data .append (float (value )) # Convert to float and add to channel data
153
167
154
168
if csv_writer : # If CSV logging is enabled, write the data to the CSV file
155
169
csv_writer .writerow ([counter ] + channel_data )
@@ -195,7 +209,7 @@ def log_ten_minute_data(verbose=False):
195
209
last_ten_minute_time = time .time () # Update the last 10-minute interval start time
196
210
197
211
# Main function to parse command-line arguments and handle data acquisition
198
- def parse_data (ser , lsl_flag = False , csv_flag = False , verbose = False , run_time = None ):
212
+ def parse_data (ser , lsl_flag = False , csv_flag = False , verbose = False , run_time = None , inverted = False ):
199
213
global total_packet_count , cumulative_packet_count , start_time , lsl_outlet , last_ten_minute_time , csv_filename
200
214
201
215
csv_writer = None # Placeholder for CSV writer
@@ -221,7 +235,7 @@ def parse_data(ser, lsl_flag=False, csv_flag=False, verbose=False, run_time=None
221
235
send_command (ser , 'START' )
222
236
223
237
while True :
224
- read_arduino_data (ser , csv_writer ) # Read and process data from Arduino
238
+ read_arduino_data (ser , csv_writer , inverted = inverted ) # Read and process data from Arduino
225
239
if (start_time is not None ):
226
240
current_time = time .time () # Get the current time
227
241
elapsed_time = current_time - start_time # Time elapsed since the last second
@@ -298,6 +312,7 @@ def main():
298
312
parser .add_argument ('--lsl' , action = 'store_true' , help = "Start LSL stream" ) # LSL streaming flag
299
313
parser .add_argument ('-v' , '--verbose' , action = 'store_true' , help = "Enable verbose output with statistical data" ) # Verbose flag
300
314
parser .add_argument ('-t' , '--time' , type = int , help = "Run the program for a specified number of seconds and then exit" ) #set time
315
+ parser .add_argument ('--inverted' , action = 'store_true' , help = "Invert the signal before streaming LSL and logging" ) # Inverted flag
301
316
302
317
args = parser .parse_args () # Parse command-line arguments
303
318
verbose = args .verbose # Set verbose mode
@@ -321,7 +336,7 @@ def main():
321
336
return
322
337
323
338
# Start data acquisition
324
- parse_data (ser , lsl_flag = args .lsl , csv_flag = args .csv , verbose = args .verbose , run_time = args .time )
339
+ parse_data (ser , lsl_flag = args .lsl , csv_flag = args .csv , verbose = args .verbose , run_time = args .time , inverted = args . inverted )
325
340
326
341
# Run the main function if this script is executed
327
342
if __name__ == "__main__" :
0 commit comments