lib/mindwave.rb in mindwave-0.1.3 vs lib/mindwave.rb in mindwave-0.1.4

- old
+ new

@@ -124,11 +124,11 @@ # stores the current headsetstatus-value # @!attribute [r] heart # stores the current heart-value # @!attribute [r] runner # @see #stop -attr_reader :attention, :meditation, :asic,:poor, :headsetstatus, :heart, :runner +attr_reader :attention, :meditation, :asic, :poor, :headsetstatus, :heart, :runner # If connectserial is true, then this constructor opens a serial connection # and automatically connects to the headset # # @param [Integer] headsetid it's on the sticker in the battery-case @@ -374,10 +374,41 @@ end end +# this method parses the raw ASIC values and returns the values of each +# of the wave types +# +# @param [Integer] asic value +# +# @returns [Array<Integer>] Array of: delta,theta,lowAlpha,highAlpha,lowBeta,highBeta,lowGamma,midGamma +def parseASIC(asic) + # assign #{asic} to the array 'a' + a = "#{asic}" + # strip off square brackets + a = a.delete! '[]' + # convert to array of integers + a = a.split(",").map(&:to_i) + + # define wave values + delta = convertToBigEndianInteger(a[0..3]) + theta = convertToBigEndianInteger(a[3..6]) + lowAlpha = convertToBigEndianInteger(a[6..9]) + highAlpha = convertToBigEndianInteger(a[9..12]) + lowBeta = convertToBigEndianInteger(a[12..15]) + highBeta = convertToBigEndianInteger(a[15..18]) + lowGamma = convertToBigEndianInteger(a[18..21]) + midGamma = convertToBigEndianInteger(a[21..24]) + + # stuff wave values in array + asicArray = [delta,theta,lowAlpha,highAlpha,lowBeta,highBeta,lowGamma,midGamma] + + # return array of wave values + return asicArray +end + # this method sends a byte to the serial connection # (Mindwave only) # # @param [Integer] hexbyte byte to send def sendbyte(hexbyte) @@ -524,15 +555,34 @@ # @param [Integer] rawval1 first byte-packet of the raw-wave-code # @param [Integer] rawval2 second byte-packet of the raw-wave-code # # @return [Integer] single value generated from the 2 bytes def convertRaw(rawval1,rawval2) + raw = rawval1*256 + rawval2 if raw >= 32768 raw = raw - 65536 end return raw end + +# converts a raw ASIC power packet of three bytes to a single value +# +# @param [Integer] threeBytes[0] first byte-packet of the ASIC wave code +# @param [Integer] threeBytes[1] second byte-packet of the ASIC wave code +# @param [Integer] threeBytes[2] third byte-packet of the ASIC wave code +# +# @return [Integer] single value generated from the 3 bytes +def convertToBigEndianInteger(threeBytes) + # see MindwaveDataPoints.py at + # https://github.com/robintibor/python-mindwave-mobile + # + bigEndianInteger = (threeBytes[0] << 16) |\ + (((1 << 16) - 1) & (threeBytes[1] << 8)) |\ + ((1 << 8) -1) & threeBytes[2] + return bigEndianInteger +end + end end