Skip to content

Commit 672400e

Browse files
committed
add inverted flag to chords.py to invert the signal (if needed)
1 parent 268a2bf commit 672400e

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

chords.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ def send_command(ser, command):
111111
return response
112112

113113
# 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):
115115
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+
116121
raw_data = ser.read(ser.in_waiting or 1) # Read available data from the serial port
117122
if raw_data == b'':
118123
send_command(ser, 'START')
@@ -149,7 +154,16 @@ def read_arduino_data(ser, csv_writer=None):
149154
high_byte = packet[2*channel + HEADER_LENGTH]
150155
low_byte = packet[2*channel + HEADER_LENGTH + 1]
151156
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
153167

154168
if csv_writer: # If CSV logging is enabled, write the data to the CSV file
155169
csv_writer.writerow([counter] + channel_data)
@@ -195,7 +209,7 @@ def log_ten_minute_data(verbose=False):
195209
last_ten_minute_time = time.time() # Update the last 10-minute interval start time
196210

197211
# 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):
199213
global total_packet_count, cumulative_packet_count, start_time, lsl_outlet, last_ten_minute_time, csv_filename
200214

201215
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
221235
send_command(ser, 'START')
222236

223237
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
225239
if(start_time is not None):
226240
current_time = time.time() # Get the current time
227241
elapsed_time = current_time - start_time # Time elapsed since the last second
@@ -298,6 +312,7 @@ def main():
298312
parser.add_argument('--lsl', action='store_true', help="Start LSL stream") # LSL streaming flag
299313
parser.add_argument('-v', '--verbose', action='store_true', help="Enable verbose output with statistical data") # Verbose flag
300314
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
301316

302317
args = parser.parse_args() # Parse command-line arguments
303318
verbose = args.verbose # Set verbose mode
@@ -321,7 +336,7 @@ def main():
321336
return
322337

323338
# 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)
325340

326341
# Run the main function if this script is executed
327342
if __name__ == "__main__":

0 commit comments

Comments
 (0)