Class: Ban::RcEvent

Inherits:
Event
  • Object
show all
Defined in:
lib/ban/events/rc_event.rb

Constant Summary collapse

CMD =
0x35

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decimal, bits, delay, protocol) ⇒ RcEvent

Returns a new instance of RcEvent.



11
12
13
# File 'lib/ban/events/rc_event.rb', line 11

def initialize(decimal, bits, delay, protocol)
  @decimal, @bits, @delay, @protocol = decimal, bits, delay, protocol
end

Instance Attribute Details

#bitsObject (readonly)

Returns the value of attribute bits.



5
6
7
# File 'lib/ban/events/rc_event.rb', line 5

def bits
  @bits
end

#decimalObject (readonly)

Returns the value of attribute decimal.



5
6
7
# File 'lib/ban/events/rc_event.rb', line 5

def decimal
  @decimal
end

#delayObject (readonly)

Returns the value of attribute delay.



5
6
7
# File 'lib/ban/events/rc_event.rb', line 5

def delay
  @delay
end

#protocolObject (readonly)

Returns the value of attribute protocol.



5
6
7
# File 'lib/ban/events/rc_event.rb', line 5

def protocol
  @protocol
end

Class Method Details

.parse(data) ⇒ Object



7
8
9
# File 'lib/ban/events/rc_event.rb', line 7

def self.parse(data)
  new(*Ban.decode7bit(data).split('|').map(&:to_i))
end

Instance Method Details

#binaryObject



43
44
45
# File 'lib/ban/events/rc_event.rb', line 43

def binary
  ("%#{bits}s" % decimal.to_s(2)).gsub(' ', '0')
end

#nameObject



15
16
17
# File 'lib/ban/events/rc_event.rb', line 15

def name
  (tristate[-1] == 'F') ? 'rc-turned-on' : 'rc-turned-off'
end

#to_hashObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/ban/events/rc_event.rb', line 26

def to_hash
  {
    'decimal' => decimal,
    'bits' => bits,
    'binary' => binary,
    'tristate' => tristate,
    'delay' => delay,
    'protocol' => protocol
  }
end

#to_sObject



37
38
39
40
41
# File 'lib/ban/events/rc_event.rb', line 37

def to_s
  "decimal: #{decimal} (#{bits} bits) binary #{binary} " +
  "tri-state: #{tristate} pulse length: #{delay} (ms) " +
  "protocol: #{protocol}"
end

#tristateObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ban/events/rc_event.rb', line 47

def tristate
  binary.gsub(/[0-9]{2}/) do |match|
    if match == '00'
      '0'
    elsif match == '01'
      'F'
    else
      raise ArgumentError, "#{match} not applicable"
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/ban/events/rc_event.rb', line 19

def valid?
  tristate
  true
rescue ArgumentError
  false
end