Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Created February 16, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixtyfive/6a1ade5697f94c49331ba9727c720a0a to your computer and use it in GitHub Desktop.
Save sixtyfive/6a1ade5697f94c49331ba9727c720a0a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'serialport'
require 'date'
require 'csv'
filename = Time.now.strftime('mindflex-data-%d%m%Y%H%M.csv')
headers = %w{signal_strength attention meditation delta theta low_alpha high_alpha low_beta high_beta low_gamma high_gamma}
begin
tty = SerialPort.new(ARGV[0] || '/dev/ttyUSB0', ARGV[1] ? ARGV[1].to_i : 115200, 8, 1, SerialPort::NONE)
rescue
puts "Error reading from TTY. Is a device connected?"
exit
end
puts "Reading..."
csv = CSV.open(filename, 'a', write_headers: true, headers: headers)
# csv.close
begin
response = tty.readline("\r\n")
begin
datapoint = response.chomp.split(',')
if datapoint.length == headers.length
puts datapoint.join("\t")
# csv = CSV.open(filename, 'a')
csv << datapoint
csv.sync
# csv.close
end
rescue Exception => e
puts "Error writing to #{filename}: #{e}"
csv.close
end
end while true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment