Class: Tamashii::Agent::Device::Keyboard::Base
- Inherits:
-
DeviceBase
- Object
- DeviceBase
- Tamashii::Agent::Device::Keyboard::Base
show all
- Defined in:
- lib/tamashii/agent/device/keyboard/base.rb
Instance Method Summary
collapse
Methods inherited from DeviceBase
#fetch_option, #fetch_option!, #unexport_pin
#display_name, #logger, #progname
Constructor Details
#initialize(*args) ⇒ Base
Returns a new instance of Base.
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 10
def initialize(*args)
super(*args)
initialize_hardware
@number_of_keys = fetch_option(:number_of_keys, default_number_of_keys)
@watcher_mode = fetch_option(:watch, default_watch)
@watcher_stopping = Concurrent::AtomicBoolean.new(false)
if @watcher_mode
initialize_watcher
start_watcher_thread
end
end
|
Instance Method Details
#add_callback(event, callable) ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 49
def add_callback(event, callable)
if @watcher_mode
@callbacks[event] = callable
else
puts "Callbacks are only available in watcher mode!"
end
end
|
#default_number_of_keys ⇒ Object
22
23
24
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 22
def default_number_of_keys
raise NotImplementedError
end
|
#default_watch ⇒ Object
26
27
28
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 26
def default_watch
true
end
|
#finalize_hardware ⇒ Object
38
39
40
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 38
def finalize_hardware
logger.warn "Device #{@name} does not implement hardware finalize code"
end
|
#initialize_hardware ⇒ Object
34
35
36
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 34
def initialize_hardware
logger.warn "Device #{@name} does not implement hardware initialize code"
end
|
#initialize_watcher ⇒ Object
42
43
44
45
46
47
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 42
def initialize_watcher
@last_keys_state = {}
@current_keys_state = {}
@current_key = Concurrent::AtomicFixnum.new(-1)
@callbacks = {}
end
|
#mark_key_down(key) ⇒ Object
146
147
148
149
150
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 146
def mark_key_down(key)
return unless @watcher_mode
@current_key.value = key
@current_keys_state[key] = true
end
|
#mark_key_up(key) ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 137
def mark_key_up(key)
return unless @watcher_mode
if @current_key.value == key
@current_key.value = -1
end
@current_keys_state[key] = false
end
|
#on_key_down(callable = nil, &block) ⇒ Object
57
58
59
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 57
def on_key_down(callable = nil, &block)
add_callback(:down, callable || block)
end
|
#on_key_pressed(callable = nil, &block) ⇒ Object
65
66
67
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 65
def on_key_pressed(callable = nil, &block)
add_callback(:pressed, callable || block)
end
|
#on_key_up(callable = nil, &block) ⇒ Object
61
62
63
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 61
def on_key_up(callable = nil, &block)
add_callback(:up, callable || block)
end
|
#poll_key ⇒ Object
120
121
122
123
124
125
126
127
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 120
def poll_key
if @watcher_mode
@current_key.value >= 0 ? @current_key.value : nil
else
sleep polling_interval
read_key
end
end
|
#polling_interval ⇒ Object
30
31
32
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 30
def polling_interval
0.1
end
|
#process_callbacks ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 91
def process_callbacks
@number_of_keys.times do |key|
if @current_keys_state[key]
if @last_keys_state[key]
@callbacks[:pressed]&.call(key)
else
@callbacks[:down]&.call(key)
end
else
if @last_keys_state[key]
@callbacks[:up]&.call(key)
end
end
end
end
|
#read_key ⇒ Object
152
153
154
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 152
def read_key
raise NotImplementedError, "read_key"
end
|
#record_key_state ⇒ Object
107
108
109
110
111
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 107
def record_key_state
@number_of_keys.times do |key|
@last_keys_state[key] = @current_keys_state[key]
end
end
|
#shutdown ⇒ Object
129
130
131
132
133
134
135
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 129
def shutdown
@watcher_stopping.make_true
if @watcher_mode
stop_watcher_thread
end
finalize_hardware
end
|
#start_watcher_thread ⇒ Object
73
74
75
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 73
def start_watcher_thread
@watcher_thread = Thread.new { watcher_loop }
end
|
#stop_watcher_thread ⇒ Object
113
114
115
116
117
118
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 113
def stop_watcher_thread
if !@watcher_thread.join(3)
@watcher_thread.exit
puts "watcher thread killed forcefully"
end
end
|
#watcher_loop ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 77
def watcher_loop
puts "watcher started"
loop do
if watcher_stopping?
break
end
sleep polling_interval
record_key_state
read_key
process_callbacks
end
puts "watcher thread terminated normally"
end
|
#watcher_stopping? ⇒ Boolean
69
70
71
|
# File 'lib/tamashii/agent/device/keyboard/base.rb', line 69
def watcher_stopping?
@watcher_stopping.true?
end
|