Class: Rbbit::Microbit
- Inherits:
-
Object
- Object
- Rbbit::Microbit
- Defined in:
- lib/rbbit.rb
Overview
Class Library
Constant Summary collapse
- TONE =
{ C3: 131 , # do3 Cs3: 139 , # do#3 D3: 147 , # re3 Ds3: 156 , # re#3 E3: 165 , # mi3 F3: 175 , # fa3 Fs3: 185 , # fa#3 G3: 196 , # so3 Gs3: 208 , # so#3 A3: 220 , # la3 As3: 233 , # la#3 B3: 247 , # ti3 C4: 262 , # do4 Cs4: 277 , # do#4 D4: 294 , # re4 Ds4: 311 , # re#4 E4: 330 , # mi4 F4: 349 , # fa4 Fs4: 370 , # fa#4 G4: 392 , # so4 Gs4: 415 , # so#4 A4: 440 , # la4 As4: 466 , # la#4 B4: 494 , # ti4 C5: 523 , # do5 Cs5: 554 , # do#5 D5: 587 , # re5 Ds5: 622 , # re#5 E5: 659 , # mi5 F5: 698 , # fa5 Fs5: 740 , # fa#5 G5: 784 , # so5 Gs5: 831 , # so#5 A5: 880 , # la5 As5: 932 , # la#5 B5: 988 , # ti5 C6: 1047 , # do6 }
- WAIT =
do6
0.05
Instance Attribute Summary collapse
-
#l ⇒ Object
readonly
Returns the value of attribute l.
-
#p ⇒ Object
readonly
Returns the value of attribute p.
-
#r ⇒ Object
readonly
Returns the value of attribute r.
-
#t ⇒ Object
readonly
Returns the value of attribute t.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Instance Method Summary collapse
- #break ⇒ Object
- #button_down?(k) ⇒ Boolean
- #close(delay = 0) ⇒ Object
-
#initialize(port = nil, ws_server = nil) ⇒ Microbit
constructor
A new instance of Microbit.
- #led_off(x = nil, y = nil) ⇒ Object (also: #off)
-
#led_on(x = nil, y = nil) ⇒ Object
(also: #on)
Pending…
- #led_puts(str) ⇒ Object (also: #puts)
- #led_show(pattern) ⇒ Object (also: #show)
- #led_turn(x, y) ⇒ Object (also: #turn)
- #mainloop(&block) ⇒ Object
- #on_press_a(&block) ⇒ Object
- #on_press_b(&block) ⇒ Object
- #on_release_a(&block) ⇒ Object
- #on_release_b(&block) ⇒ Object
- #port ⇒ Object
- #reset ⇒ Object
- #sound_play(freq, beat) ⇒ Object (also: #play)
- #sound_rest(beat) ⇒ Object (also: #rest)
- #sound_tempo=(bpm) ⇒ Object (also: #tempo=)
- #sound_volume=(v) ⇒ Object (also: #volume=)
- #wait(ms) ⇒ Object
Constructor Details
#initialize(port = nil, ws_server = nil) ⇒ Microbit
Returns a new instance of Microbit.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/rbbit.rb', line 155 def initialize(port = nil, ws_server = nil) @agent = Agent.new(self, ws_server) if ws_server @q = Queue.new @x = nil @y = nil @z = nil @p = nil @r = nil @l = nil @t = nil @button_down = {} @button_down_last = {} @button_press = {} @button_release = {} @button_down[:a] = nil @button_down[:b] = nil @button_down_last[:a] = nil @button_down_last[:b] = nil @button_press[:a] = nil @button_press[:b] = nil @button_release[:a] = nil @button_release[:b] = nil @on_press_a = nil @on_press_b = nil @on_release_a = nil @on_release_b = nil @volume = 127 @bpm = 120 # ウェイト調整のため、この値で明示的に初期化(initialize末尾) @continue_thread = nil @continue_loop = nil @port = port || ENV['MB_PORT'] baud_rate = 115200 data_bits = 8 stop_bits = 1 parity = 0 raise Rbbit::Error, "serialport 'nil' is not available" unless @port begin @sp = SerialPort.open(@port, baud_rate, data_bits, stop_bits, parity) rescue => e #Kernel.puts e #exit raise Rbbit::Error, "serialport '#{@port}' is not available" end Kernel.sleep 0.5 # -- for write @thread_w = Thread.new do loop do Thread.pass # for other threads unless @q.empty? cmd = @q.pop if cmd.class == Array @sp.write cmd[0]; Kernel.sleep cmd[1] else @sp.write cmd end end end end # -- for read @thread_r = Thread.new do # Thread.pass @sp.read_timeout = 200 @continue_thread = true loop do break unless @continue_thread Thread.pass # for other threads begin value = @sp.readline.chomp.strip.split(',') # Kernel.p value rescue EOFError Kernel.sleep 0.1 retry rescue ArgumentError # 回避:`strip': invalid byte sequence in UTF-8 (ArgumentError) # `split': invalid byte sequence in UTF-8 (ArgumentError) Kernel.sleep 0.1 retry end @x = value[0].to_i @y = value[1].to_i @z = value[2].to_i @p = value[3].to_i # pitch @r = value[4].to_i # roll @l = value[5].to_i # light @t = value[6].to_i # temp @button_down[:a] = (value[7] == "1" ? true : false) @button_down[:b] = (value[8] == "1" ? true : false) event_proc if @agent senddata = JSON.generate({x: @x, y: @y, z: @z, p: @p, r: @r, l: @l, t: @t, a_down: @button_down[:a], a_press: @button_press[:a], a_release: @button_release[:a], b_down: @button_down[:b], b_press: @button_press[:b], b_release: @button_release[:b]}) @agent.send_to_ws senddata end end end # -- init self.sound_volume = @volume self.sound_tempo = @bpm end |
Instance Attribute Details
#l ⇒ Object (readonly)
Returns the value of attribute l.
153 154 155 |
# File 'lib/rbbit.rb', line 153 def l @l end |
#p ⇒ Object (readonly)
Returns the value of attribute p.
153 154 155 |
# File 'lib/rbbit.rb', line 153 def p @p end |
#r ⇒ Object (readonly)
Returns the value of attribute r.
153 154 155 |
# File 'lib/rbbit.rb', line 153 def r @r end |
#t ⇒ Object (readonly)
Returns the value of attribute t.
153 154 155 |
# File 'lib/rbbit.rb', line 153 def t @t end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
153 154 155 |
# File 'lib/rbbit.rb', line 153 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
153 154 155 |
# File 'lib/rbbit.rb', line 153 def y @y end |
#z ⇒ Object (readonly)
Returns the value of attribute z.
153 154 155 |
# File 'lib/rbbit.rb', line 153 def z @z end |
Instance Method Details
#break ⇒ Object
304 305 306 307 |
# File 'lib/rbbit.rb', line 304 def break Kernel.sleep 0.5 ### @continue_loop = false end |
#button_down?(k) ⇒ Boolean
352 353 354 |
# File 'lib/rbbit.rb', line 352 def (k) @button_down[k] end |
#close(delay = 0) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/rbbit.rb', line 309 def close(delay = 0) @continue_thread = false Kernel.sleep 1 until @q.empty? # wait for until command queue has been empty Kernel.sleep 0.5 # Kernel.puts "sync #{@q.size}" end Kernel.sleep 0.5 + delay / 1000 @thread_w.kill @thread_r.kill @sp.close end |
#led_off(x = nil, y = nil) ⇒ Object Also known as: off
378 379 380 381 382 383 384 |
# File 'lib/rbbit.rb', line 378 def led_off(x = nil, y = nil) if (x == nil and y == nil) @q << ["LEDclr\n", WAIT] else @q << ["LEDoff#{x}#{y}\n", WAIT] end end |
#led_on(x = nil, y = nil) ⇒ Object Also known as: on
Pending… def button_release?(k)
status = @button_release[k]
@button_release[k] = false if @button_release[k] # avoid continuous judgment
status
end
370 371 372 373 374 375 376 |
# File 'lib/rbbit.rb', line 370 def led_on(x = nil, y = nil) if (x == nil and y == nil) @q << ["LEDfil\n", WAIT] else @q << ["LEDon #{x}#{y}\n", WAIT] end end |
#led_puts(str) ⇒ Object Also known as: puts
390 391 392 |
# File 'lib/rbbit.rb', line 390 def led_puts(str) @q << ["LEDput#{str}\n", WAIT + str.length * 0.5] end |
#led_show(pattern) ⇒ Object Also known as: show
394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/rbbit.rb', line 394 def led_show(pattern) ptn = [] 5.times do |j| ptn[j] = 0 5.times do |i| ptn[j] += (2 ** (4 - i)) unless pattern[j][i] == 0 # 5-digits to decimal end end #Kernel.p ptn @q << ["LEDshw%02d%02d%02d%02d%02d\n" % ptn, WAIT] end |
#led_turn(x, y) ⇒ Object Also known as: turn
386 387 388 |
# File 'lib/rbbit.rb', line 386 def led_turn(x, y) @q << ["LEDtrn#{x}#{y}\n", WAIT] end |
#mainloop(&block) ⇒ Object
294 295 296 297 298 299 300 301 302 |
# File 'lib/rbbit.rb', line 294 def mainloop(&block) @b = block # ブロックを登録 @continue_loop = true loop do break unless @continue_loop @b.call Kernel.sleep WAIT # ブロック1回処理ごとにウェイト end end |
#on_press_a(&block) ⇒ Object
278 279 280 |
# File 'lib/rbbit.rb', line 278 def on_press_a(&block) @on_press_a = block end |
#on_press_b(&block) ⇒ Object
286 287 288 |
# File 'lib/rbbit.rb', line 286 def on_press_b(&block) @on_press_b = block end |
#on_release_a(&block) ⇒ Object
282 283 284 |
# File 'lib/rbbit.rb', line 282 def on_release_a(&block) @on_release_a = block end |
#on_release_b(&block) ⇒ Object
290 291 292 |
# File 'lib/rbbit.rb', line 290 def on_release_b(&block) @on_release_b = block end |
#port ⇒ Object
330 331 332 |
# File 'lib/rbbit.rb', line 330 def port @port end |
#reset ⇒ Object
326 327 328 |
# File 'lib/rbbit.rb', line 326 def reset @q << "RESET \n" end |
#sound_play(freq, beat) ⇒ Object Also known as: play
416 417 418 |
# File 'lib/rbbit.rb', line 416 def sound_play(freq, beat) @q << ["SNDply%04d%s\n" % [TONE[freq], _beat(beat)], WAIT * 0 + 60.0 / @bpm * beat] ### end |
#sound_rest(beat) ⇒ Object Also known as: rest
420 421 422 |
# File 'lib/rbbit.rb', line 420 def sound_rest(beat) @q << ["SNDrst%s\n" % [_beat(beat)], WAIT + 60.0 / @bpm * beat] end |
#sound_tempo=(bpm) ⇒ Object Also known as: tempo=
411 412 413 414 |
# File 'lib/rbbit.rb', line 411 def sound_tempo=(bpm) @q << ["SNDbpm#{bpm}\n", WAIT] @bpm = bpm end |
#sound_volume=(v) ⇒ Object Also known as: volume=
406 407 408 409 |
# File 'lib/rbbit.rb', line 406 def sound_volume=(v) @q << ["SNDvol#{v}\n", WAIT] @volume = v end |
#wait(ms) ⇒ Object
322 323 324 |
# File 'lib/rbbit.rb', line 322 def wait(ms) @q << ["SLEEP %03d\n" % [ms], WAIT + ms / 1000.0] end |