Class: Mindset::Packet

Inherits:
Hash
  • Object
show all
Defined in:
lib/mindset.rb

Overview

A Mindset data packet. This is usually either a Raw data packet, an eSense packet, or an ASIC EEG packet.

Constant Summary collapse

EXCODE =

Extended code

0x55
CODE_SIGNAL_QUALITY =

POOR_SIGNAL quality 0-255

0x02
CODE_ATTENTION =

ATTENTION eSense 0-100

0x04
CODE_MEDITATION =

MEDITATION eSense 0-100

0x05
0x16
CODE_WAVE =

RAW wave value: 2-byte big-endian 2s-complement

0x80
CODE_ASIC_EEG =

ASIC EEG POWER 8 3-byte big-endian integers

0x83

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decode(excode, code, value, verbose = false) ⇒ Object



79
80
81
# File 'lib/mindset.rb', line 79

def self.decode(excode, code, value, verbose=false)
  Packet.new.decode(excode, code, value, verbose)
end

.factory(name, value) ⇒ Object



83
84
85
86
87
# File 'lib/mindset.rb', line 83

def self.factory(name, value)
  pkt = self.new
  pkt[name.to_sym] = value
  pkt
end

.parse(bytes, verbose = false) ⇒ Object

returns array of packets



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mindset.rb', line 59

def self.parse(bytes, verbose=false)
  packets = []
  while not bytes.empty?
    excode = 0
    while bytes[0] == EXCODE
      excode += 1
      bytes.shift
    end

    code = bytes.shift
    vlen = (code >= 0x80) ? bytes.shift : 1
    value = bytes.slice! 0, vlen
    pkt = Packet.new
    pkt.decode(excode, code, value, verbose)
    packets << pkt
  end

  packets
end

Instance Method Details

#decode(excode, code, value, verbose = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mindset.rb', line 89

def decode(excode, code, value, verbose=nil)
  # note: currently, excode is ignored
  case code
  when CODE_SIGNAL_QUALITY
    self[:signal_quality] = value.first
  when CODE_ATTENTION
    self[:attention] = value.first
  when CODE_MEDITATION
    self[:meditation] = value.first
  when CODE_BLINK
    self[:blink] = value.first
  when CODE_WAVE
    self[:wave] = value[0,2].join('').unpack("s>").first
  when CODE_ASIC_EEG
    unpack_asic_eeg(value[0,24])
  else
    $stderr.puts "Unrecognized code: %02X" % code if verbose
  end
  self
end

#is_asic_wave?Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/mindset.rb', line 110

def is_asic_wave?
  ([:lo_beta, :hi_beta, :delta, :lo_gamma, :theta, :mid_gamma, :lo_alpha, 
    :hi_alpha] & self.keys).count > 0
end

#is_esense?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/mindset.rb', line 115

def is_esense?
  (self.keys.include? :attention) || (self.keys.include? :meditation)
end

#to_jsonObject



119
120
121
# File 'lib/mindset.rb', line 119

def to_json
  super
end