Class: DeviceInput::Event
- Inherits:
-
Object
- Object
- DeviceInput::Event
- Defined in:
- lib/device_input.rb
Defined Under Namespace
Classes: Data
Constant Summary collapse
- DEFINITION =
{ :tv_sec => 'long', :tv_usec => 'long', :type => 'uint16_t', :code => 'uint16_t', :value => 'int32_t', }
- PACK_MAP =
{ 'long' => 'l!', 'uint16_t' => 'S', 'int32_t' => 'l', }
- PACK =
DEFINITION.values.map { |v| PACK_MAP.fetch(v) }.join
- TYPES =
these are just labels, not used internally
{ 0 => 'Sync', 1 => 'Key', 2 => 'Relative', 3 => 'Absolute', 4 => 'Misc', 17 => 'LED', 18 => 'Sound', 20 => 'Repeat', 21 => 'ForceFeedback', 22 => 'Power', 23 => 'ForceFeedbackStatus', }
- NULL_DATA =
Data.new(0, 0, 0, 0, 0)
- NULL_MSG =
self.encode(NULL_DATA)
- BYTE_LENGTH =
NULL_MSG.length
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .code_str(type_code, code_code) ⇒ Object
-
.decode(binstr) ⇒ Object
convert string to Event::Data.
-
.encode(data) ⇒ Object
convert Event::Data to a string.
- .type_str(type_code) ⇒ Object
Instance Method Summary collapse
-
#initialize(data) ⇒ Event
constructor
A new instance of Event.
- #to_s ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(data) ⇒ Event
Returns a new instance of Event.
61 62 63 64 65 66 |
# File 'lib/device_input.rb', line 61 def initialize(data) @data = data @time = Time.at(data.tv_sec, data.tv_usec) @type = self.class.type_str(data.type) @code = self.class.code_str(data.type, data.code) end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
59 60 61 |
# File 'lib/device_input.rb', line 59 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
59 60 61 |
# File 'lib/device_input.rb', line 59 def data @data end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
59 60 61 |
# File 'lib/device_input.rb', line 59 def time @time end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
59 60 61 |
# File 'lib/device_input.rb', line 59 def type @type end |
Class Method Details
.code_str(type_code, code_code) ⇒ Object
49 50 51 52 53 |
# File 'lib/device_input.rb', line 49 def self.code_str(type_code, code_code) require 'device_input/codes' DeviceInput::CODES.dig(type_code, code_code) || "UNK-#{type_code}-#{code_code}" end |
.decode(binstr) ⇒ Object
convert string to Event::Data
41 42 43 |
# File 'lib/device_input.rb', line 41 def self.decode(binstr) Data.new *binstr.unpack(PACK) end |
.encode(data) ⇒ Object
convert Event::Data to a string
36 37 38 |
# File 'lib/device_input.rb', line 36 def self.encode(data) data.values.pack(PACK) end |
.type_str(type_code) ⇒ Object
45 46 47 |
# File 'lib/device_input.rb', line 45 def self.type_str(type_code) TYPES[type_code] || "UNK-#{type_code}" end |
Instance Method Details
#to_s ⇒ Object
72 73 74 |
# File 'lib/device_input.rb', line 72 def to_s [@type, @code, @data.value].join(':') end |
#value ⇒ Object
68 69 70 |
# File 'lib/device_input.rb', line 68 def value @data.value end |