rbbit 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/rbbit.gemspec ADDED
@@ -0,0 +1,45 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rbbit/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rbbit"
8
+ spec.version = Rbbit::VERSION
9
+ spec.authors = ["Koki Kitamura"]
10
+ spec.email = ["spool.kitamura@nifty.ne.jp"]
11
+
12
+ spec.summary = %q{Class library and WebSocket server to use 'micro:bit'}
13
+ spec.description = %q{'rbbit' is a Class library and WebSocket server to use 'micro:bit'}
14
+ spec.homepage = "https://github.com/spoolkitamura/rbbit"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ #
22
+ # spec.metadata["homepage_uri"] = spec.homepage
23
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
24
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+ #else
26
+ # raise "RubyGems 2.0 or newer is required to protect against " \
27
+ # "public gem pushes."
28
+ #end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_runtime_dependency "serialport", "~> 1.3"
40
+ spec.add_runtime_dependency "em-websocket", "~> 0.5"
41
+ spec.add_runtime_dependency "websocket-client-simple", "~> 0.3"
42
+
43
+ spec.add_development_dependency "bundler", "~> 1.17"
44
+ spec.add_development_dependency "rake", "~> 10.0"
45
+ end
@@ -0,0 +1,28 @@
1
+ require 'rbbit.rb'
2
+
3
+ # 点滅するハート
4
+
5
+ =begin
6
+ ・ハートマークが 5回点滅(操作不要)
7
+ =end
8
+
9
+ mb = Rbbit::Microbit.new(ARGV[0])
10
+
11
+ pattern = [
12
+ [0, 1, 0, 1, 0],
13
+ [1, 1, 1, 1, 1],
14
+ [1, 1, 1, 1, 1],
15
+ [0, 1, 1, 1, 0],
16
+ [0, 0, 1, 0, 0]
17
+ ]
18
+
19
+ 5.times do
20
+ mb.led_show(pattern)
21
+ sleep 0.5
22
+ mb.led_off
23
+ sleep 0.5
24
+ end
25
+
26
+ mb.led_puts("OK!")
27
+ mb.close(5000)
28
+
@@ -0,0 +1,16 @@
1
+ require 'rbbit.rb'
2
+
3
+ # 名札
4
+
5
+ =begin
6
+ ・名前を表示(操作不要)
7
+ =end
8
+
9
+ mb = Rbbit::Microbit.new(ARGV[0])
10
+
11
+ mb.led_puts("rbbit")
12
+ mb.led_off
13
+
14
+ mb.led_puts("OK!")
15
+ mb.close(10000)
16
+
@@ -0,0 +1,63 @@
1
+ require 'rbbit.rb'
2
+
3
+ # スマイリー
4
+
5
+ =begin
6
+ ・[A]ボタン押下で笑顔
7
+ ・[B]ボタン押下で渋面
8
+ ・[A]+[B]ボタン押下で〇
9
+ ・15秒間で自動的に終了
10
+ =end
11
+
12
+
13
+ mb = Rbbit::Microbit.new(ARGV[0])
14
+
15
+ pattern_a = [
16
+ [1, 1, 0, 1, 1],
17
+ [1, 1, 0, 1, 1],
18
+ [0, 0, 0, 0, 0],
19
+ [1, 0, 0, 0, 1],
20
+ [0, 1, 1, 1, 0]
21
+ ]
22
+
23
+ pattern_b = [
24
+ [1, 1, 0, 1, 1],
25
+ [1, 1, 0, 1, 1],
26
+ [0, 0, 0, 0, 0],
27
+ [0, 1, 1, 1, 0],
28
+ [1, 0, 0, 0, 1]
29
+ ]
30
+
31
+ pattern_s = [
32
+ [0, 1, 1, 1, 0],
33
+ [1, 0, 0, 0, 1],
34
+ [1, 0, 0, 0, 1],
35
+ [1, 0, 0, 0, 1],
36
+ [0, 1, 1, 1, 0]
37
+ ]
38
+
39
+ mb.led_off
40
+
41
+ mb.on_press_a do
42
+ if mb.button_down?(:b)
43
+ mb.led_show(pattern_s)
44
+ else
45
+ mb.led_show(pattern_a)
46
+ end
47
+ end
48
+
49
+ mb.on_press_b do
50
+ if mb.button_down?(:a)
51
+ mb.led_show(pattern_s)
52
+ else
53
+ mb.led_show(pattern_b)
54
+ end
55
+ end
56
+
57
+ puts "Please press 'button-A' or 'button-B' or Both\n(exit after 15 sec)"
58
+ sleep 15
59
+
60
+ mb.led_off
61
+ mb.led_puts("OK!")
62
+ mb.close(5000)
63
+
@@ -0,0 +1,38 @@
1
+ require 'rbbit.rb'
2
+
3
+ # さいころ1
4
+
5
+ =begin
6
+ ・micro:bitを適当に揺さぶると 1~6の数字が表示される
7
+ ・[B]ボタンで終了
8
+ =end
9
+
10
+ mb = Rbbit::Microbit.new(ARGV[0])
11
+
12
+ mb.led_off
13
+
14
+ mb.on_release_b do
15
+ mb.break
16
+ end
17
+
18
+ x0 = 0
19
+ y0 = 0
20
+
21
+ puts "Please shake the micro:bit\n(press 'button-B' to exit)"
22
+ mb.mainloop do
23
+ x, y = mb.x, mb.y
24
+ next unless x or y
25
+ # puts "#{x} #{y}"
26
+ if (x - x0) > 512 or
27
+ (y - y0) > 512
28
+ # puts "#{x} #{y} #{x0} #{y0} #{x - x0} #{y - y0}"
29
+ dice = [1, 2, 3, 4, 5, 6].sample.to_s
30
+ mb.led_puts(dice)
31
+ x0, y0 = x, y
32
+ end
33
+ sleep 0.2
34
+ end
35
+
36
+ mb.puts("OK!")
37
+ mb.close(5000)
38
+
@@ -0,0 +1,82 @@
1
+ require 'rbbit.rb'
2
+
3
+ # さいころ2
4
+
5
+ =begin
6
+ ・micro:bitを適当に揺さぶると 1~6の目が表示される
7
+ ・[B]ボタンで終了
8
+ =end
9
+
10
+ mb = Rbbit::Microbit.new(ARGV[0])
11
+
12
+ mb.led_off
13
+
14
+ pattern = []
15
+ pattern << [
16
+ [0, 0, 0, 0, 0],
17
+ [0, 0, 0, 0, 0],
18
+ [0, 0, 1, 0, 0],
19
+ [0, 0, 0, 0, 0],
20
+ [0, 0, 0, 0, 0]
21
+ ]
22
+ pattern << [
23
+ [0, 0, 0, 0, 0],
24
+ [0, 1, 0, 0, 0],
25
+ [0, 0, 0, 0, 0],
26
+ [0, 0, 0, 1, 0],
27
+ [0, 0, 0, 0, 0]
28
+ ]
29
+ pattern << [
30
+ [0, 0, 0, 0, 1],
31
+ [0, 0, 0, 0, 0],
32
+ [0, 0, 1, 0, 0],
33
+ [0, 0, 0, 0, 0],
34
+ [1, 0, 0, 0, 0]
35
+ ]
36
+ pattern << [
37
+ [0, 0, 0, 0, 0],
38
+ [0, 1, 0, 1, 0],
39
+ [0, 0, 0, 0, 0],
40
+ [0, 1, 0, 1, 0],
41
+ [0, 0, 0, 0, 0]
42
+ ]
43
+ pattern << [
44
+ [1, 0, 0, 0, 1],
45
+ [0, 0, 0, 0, 0],
46
+ [0, 0, 1, 0, 0],
47
+ [0, 0, 0, 0, 0],
48
+ [1, 0, 0, 0, 1]
49
+ ]
50
+ pattern << [
51
+ [0, 0, 0, 0, 0],
52
+ [1, 0, 1, 0, 1],
53
+ [0, 0, 0, 0, 0],
54
+ [1, 0, 1, 0, 1],
55
+ [0, 0, 0, 0, 0]
56
+ ]
57
+
58
+ mb.on_release_b do
59
+ mb.break
60
+ end
61
+
62
+ x0 = 0
63
+ y0 = 0
64
+
65
+ puts "Please shake the micro:bit\n(press 'button-B' to exit)"
66
+ mb.mainloop do
67
+ x, y = mb.x, mb.y
68
+ next unless x or y
69
+ # puts "#{x} #{y}"
70
+ if (x - x0) > 512 or
71
+ (y - y0) > 512
72
+ # puts "#{x} #{y} #{x0} #{y0} #{x - x0} #{y - y0}"
73
+ dice = [1, 2, 3, 4, 5, 6].sample
74
+ mb.led_show(pattern[dice - 1])
75
+ x0, y0 = x, y
76
+ end
77
+ sleep 0.2
78
+ end
79
+
80
+ mb.puts("OK!")
81
+ mb.close(5000)
82
+
@@ -0,0 +1,39 @@
1
+ require 'rbbit.rb'
2
+
3
+ # 点滅
4
+
5
+ =begin
6
+ ・LEDが 2パターン交互に 10回点滅(操作不要)
7
+ =end
8
+
9
+ pattern1 = [
10
+ [1, 1, 1, 1, 1],
11
+ [1, 0, 0, 0, 1],
12
+ [1, 0, 1, 0, 1],
13
+ [1, 0, 0, 0, 1],
14
+ [1, 1, 1, 1, 1]
15
+ ]
16
+
17
+ pattern2 = [
18
+ [0, 0, 0, 0, 0],
19
+ [0, 1, 1, 1, 0],
20
+ [0, 1, 0, 1, 0],
21
+ [0, 1, 1, 1, 0],
22
+ [0, 0, 0, 0, 0]
23
+ ]
24
+
25
+ mb = Rbbit::Microbit.new(ARGV[0])
26
+
27
+ mb.led_off
28
+ sleep 0.2
29
+
30
+ 10.times do
31
+ mb.led_show(pattern1)
32
+ sleep 0.5
33
+ mb.led_show(pattern2)
34
+ sleep 0.5
35
+ end
36
+
37
+ mb.led_puts("OK!")
38
+ mb.close(5000)
39
+
@@ -0,0 +1,47 @@
1
+ require 'rbbit'
2
+
3
+ # ローテーション
4
+
5
+ =begin
6
+ ・LEDの点灯位置がグルグル移動する(操作不要)
7
+ ・最後にチェックマークを表示
8
+ =end
9
+
10
+ leds = [
11
+ [1, 0], [2, 0], [3, 0],
12
+ [4, 1], [4, 2], [4, 3],
13
+ [3, 4], [2, 4], [1, 4],
14
+ [0, 3], [0, 2], [0, 1]
15
+ ]
16
+
17
+ pattern = [
18
+ [0, 0, 0, 0, 0],
19
+ [0, 0, 0, 0, 1],
20
+ [0, 0, 0, 1, 0],
21
+ [1, 0, 1, 0, 0],
22
+ [0, 1, 0, 0, 0]
23
+ ]
24
+
25
+ mb = Rbbit::Microbit.new(ARGV[0])
26
+
27
+ mb.led_off
28
+ sleep 0.2
29
+
30
+ leds.each do |p|
31
+ mb.led_on(p[0], p[1])
32
+ sleep 0.2 # ゆっくり
33
+ end
34
+
35
+ 3.times do
36
+ leds.each do |p|
37
+ mb.led_turn(p[0], p[1])
38
+ end
39
+ end
40
+
41
+ mb.wait(500)
42
+ mb.led_show(pattern)
43
+ mb.wait(2000) # sleepとの差異...
44
+
45
+ mb.led_puts("OK!")
46
+ mb.close(5000)
47
+
@@ -0,0 +1,24 @@
1
+ require 'rbbit.rb'
2
+
3
+ # タイマー
4
+
5
+ =begin
6
+ ・数字がカウントダウンしていく(操作不要)
7
+ =end
8
+
9
+ mb = Rbbit::Microbit.new(ARGV[0])
10
+
11
+ mb.led_off
12
+ sleep 0.2
13
+
14
+ puts "Now, count down..."
15
+ (0..9).each do |i|
16
+ mb.led_puts("#{9 - i}")
17
+ sleep 1
18
+ end
19
+
20
+ sleep 0.5
21
+
22
+ mb.led_puts("OK!")
23
+ mb.close(5000)
24
+
@@ -0,0 +1,28 @@
1
+ require 'rbbit.rb'
2
+
3
+ # アルファベット
4
+
5
+ =begin
6
+ ・[A]ボタンを押すたびにアルファベットが1文字ずつランダムに表示される
7
+ ・[B]ボタンで終了
8
+ =end
9
+
10
+ mb = Rbbit::Microbit.new(ARGV[0])
11
+
12
+ mb.led_off
13
+ sleep 0.2
14
+
15
+ mb.on_press_a do
16
+ mb.led_puts((rand(0...26) + 65).chr)
17
+ end
18
+
19
+ mb.on_press_b do
20
+ mb.break
21
+ end
22
+
23
+ puts "Please press 'button-A'\n(press 'button-B' to exit)"
24
+ mb.mainloop {}
25
+
26
+ mb.led_puts("OK!")
27
+ mb.close(5000)
28
+
@@ -0,0 +1,33 @@
1
+ require 'rbbit.rb'
2
+
3
+ # LEDスイッチ
4
+
5
+ =begin
6
+ ・[A]ボタン押下中は LEDが点灯
7
+ ・[A]ボタンを離すと LEDが消灯
8
+ ・[B]ボタンで終了
9
+ =end
10
+
11
+ mb = Rbbit::Microbit.new(ARGV[0])
12
+
13
+ mb.led_off
14
+ sleep 0.2
15
+
16
+ mb.on_press_a do
17
+ mb.led_on
18
+ end
19
+
20
+ mb.on_release_a do
21
+ mb.led_off
22
+ end
23
+
24
+ mb.on_press_b do
25
+ mb.break
26
+ end
27
+
28
+ puts "Please press 'button-A'\n(press 'button-B' to exit)"
29
+ mb.mainloop {}
30
+
31
+ mb.led_puts("OK!")
32
+ mb.close(5000)
33
+
@@ -0,0 +1,111 @@
1
+ require 'rbbit.rb'
2
+
3
+ # ドラゴン・クエストのテーマ曲
4
+
5
+ =begin
6
+ ・自動再生(操作不要)
7
+ =end
8
+
9
+ puts "Playing 'Dragon Quest' theme song"
10
+
11
+ pattern = [
12
+ [0, 0, 1, 0, 0],
13
+ [0, 0, 1, 1, 0],
14
+ [0, 0, 1, 0, 1],
15
+ [1, 1, 1, 0, 0],
16
+ [1, 1, 0, 0, 0]
17
+ ]
18
+
19
+ mb = Rbbit::Microbit.new(ARGV[0])
20
+
21
+ mb.led_show(pattern)
22
+ sleep 0.2
23
+
24
+ mb.sound_tempo = 150
25
+ mb.sound_volume = 48
26
+
27
+ mb.sound_play(:G4, 0.5)
28
+ mb.sound_play(:G4, 0.25)
29
+ mb.sound_play(:G4, 0.25)
30
+ mb.sound_play(:C5, 1.0)
31
+ mb.sound_play(:D5, 1.0)
32
+ mb.sound_play(:E5, 1.0)
33
+ mb.sound_play(:F5, 1.0)
34
+ mb.sound_play(:G5, 1.0)
35
+ mb.sound_play(:C6, 2.0)
36
+
37
+ mb.sound_play(:B5, 0.5)
38
+ mb.sound_play(:B5, 0.25)
39
+ mb.sound_play(:A5, 0.25)
40
+ mb.sound_play(:A5, 1.0)
41
+ mb.sound_play(:A5, 0.5)
42
+ mb.sound_play(:G5, 0.5)
43
+ mb.sound_rest( 0.5)
44
+ mb.sound_play(:Fs5, 0.5)
45
+ mb.sound_play(:Fs5, 0.5)
46
+ mb.sound_play(:A5, 0.5)
47
+ mb.sound_play(:G5, 0.5)
48
+ mb.sound_rest( 0.5)
49
+ mb.sound_play(:E5, 2.0)
50
+
51
+ mb.sound_play(:E4, 0.5)
52
+ mb.sound_play(:E4, 0.25)
53
+ mb.sound_play(:E4, 0.25)
54
+ mb.sound_play(:E4, 1.0)
55
+ mb.sound_play(:E4, 1.0)
56
+ mb.sound_play(:Fs4, 1.0)
57
+ mb.sound_play(:Gs4, 1.0)
58
+ mb.sound_play(:A4, 2.0)
59
+ mb.sound_rest( 0.5)
60
+
61
+ mb.sound_play(:A4, 0.5)
62
+ mb.sound_play(:B4, 0.5)
63
+ mb.sound_play(:C5, 0.5)
64
+ mb.sound_play(:D5, 2.0)
65
+ mb.sound_rest( 0.5)
66
+
67
+ mb.sound_play(:A4, 0.5)
68
+ mb.sound_play(:A4, 0.5)
69
+ mb.sound_play(:C5, 0.5)
70
+ mb.sound_play(:C5, 1.0)
71
+ mb.sound_play(:B4, 1.0)
72
+ mb.sound_play(:A4, 1.0)
73
+ mb.sound_play(:G4, 1.0)
74
+
75
+ mb.sound_play(:E5, 2.0)
76
+ mb.sound_play(:E5, 0.5)
77
+ mb.sound_play(:F5, 0.5)
78
+ mb.sound_play(:E5, 0.5)
79
+ mb.sound_play(:D5, 0.5)
80
+ mb.sound_play(:C5, 2.0)
81
+ mb.sound_play(:A4, 1.0)
82
+
83
+ mb.sound_play(:C5, 1.0)
84
+ mb.sound_play(:D5, 2.0)
85
+ mb.sound_play(:D5, 0.5)
86
+ mb.sound_play(:E5, 0.5)
87
+ mb.sound_play(:D5, 0.5)
88
+ mb.sound_play(:C5, 0.5)
89
+ mb.sound_play(:C5, 2.0)
90
+ mb.sound_play(:B4, 1.0)
91
+
92
+ mb.sound_play(:G4, 1.0)
93
+ mb.sound_play(:G5, 2.0)
94
+ mb.sound_play(:G5, 0.5)
95
+ mb.sound_play(:E5, 0.5)
96
+ mb.sound_play(:F5, 0.5)
97
+ mb.sound_play(:G5, 0.5)
98
+ mb.sound_play(:A5, 2.0)
99
+
100
+ mb.sound_play(:A5, 0.5)
101
+ mb.sound_play(:A4, 0.5)
102
+ mb.sound_play(:B4, 0.5)
103
+ mb.sound_play(:C5, 0.5)
104
+ mb.sound_play(:F5, 2.0)
105
+ mb.sound_play(:E5, 2.0)
106
+ mb.sound_play(:C5, 2.0)
107
+ mb.sound_rest( 1.0)
108
+
109
+ mb.puts("OK!")
110
+ mb.close(5000)
111
+
@@ -0,0 +1,69 @@
1
+ require 'rbbit.rb'
2
+
3
+ # 玉ころがし
4
+
5
+ =begin
6
+ ・micro:bitを前後左右に傾けると、ボール(LED1つ)が移動
7
+ ・[B]ボタンで終了
8
+ =end
9
+
10
+ mb = Rbbit::Microbit.new(ARGV[0])
11
+
12
+ mb.on_press_b do
13
+ mb.break
14
+ end
15
+
16
+ mb.led_off
17
+ sleep 0.2
18
+
19
+ i0, j0 = nil, nil
20
+ i, j = 2, 2
21
+ pitches = []
22
+ rolls = []
23
+
24
+ puts "Please tilt the micro:bit\n(press 'button-B' to exit)"
25
+ mb.mainloop do
26
+ pitch = mb.p # pitch(前後)
27
+ roll = mb.r # roll (左右)
28
+ next unless pitch or roll # pitch, rollのいずれかが有効値でない場合はループ先頭に戻る
29
+
30
+ if i != i0 or j != j0
31
+ #puts "(#{i0}, #{j0}) -> (#{i}, #{j0})"
32
+ mb.led_off(i0, j0)
33
+ mb.led_on(i, j)
34
+ end
35
+
36
+ # 配列を使用して 5件ごとに値をサンプリング(連続値での判断だと玉の動きが一方的)
37
+ pitches << pitch
38
+ rolls << roll
39
+ pitches.clear if pitches.size > 5
40
+ rolls.clear if rolls.size > 5
41
+ ave_pitch = (pitches.size == 1 ? pitches[0] : 0)
42
+ ave_roll = (rolls.size == 1 ? rolls[0] : 0)
43
+
44
+ #puts "#{ave_pitch} #{ave_roll} #{pitches.size} #{rolls.size}"
45
+
46
+ i0 = i # 前の値を保持
47
+ j0 = j # 前の値を保持
48
+ if ave_roll > 15 # 傾き(左右)が +15度を超えた場合
49
+ i += 1
50
+ i = 4 if i >= 4
51
+ elsif ave_roll < -15 # 傾き(左右)が -15度を超えた場合
52
+ i -= 1
53
+ i = 0 if i <= 0
54
+ end
55
+
56
+ if ave_pitch > 15 # 傾き(前後)が +15度を超えた場合
57
+ j += 1
58
+ j = 4 if j >= 4
59
+ elsif ave_pitch < -15 # 傾き(前後)が -15度を超えた場合
60
+ j -= 1
61
+ j = 0 if j <= 0
62
+ end
63
+
64
+ #sleep 0.1 # 適当なウェイト...
65
+ end
66
+
67
+ mb.puts("OK!")
68
+ mb.close(5000)
69
+
@@ -0,0 +1,56 @@
1
+ require 'rbbit.rb'
2
+
3
+ # 光量計
4
+
5
+ =begin
6
+ ・明るさによって LEDが 0~25個の範囲で点灯
7
+ ・[B]ボタンで終了
8
+ =end
9
+
10
+ mb = Rbbit::Microbit.new(ARGV[0])
11
+
12
+ mb.on_press_b do
13
+ mb.break
14
+ end
15
+
16
+ mb.led_off
17
+ sleep 0.2
18
+
19
+ puts "(press 'button-B' to exit)"
20
+ mb.mainloop do
21
+ light = mb.l
22
+ next unless light # lightが有効値でない場合はループ先頭に戻る
23
+
24
+ level = (light * 25.0 / 255.0).to_i
25
+
26
+ i = level / 5 # 商
27
+ j = level % 5 # 剰余
28
+
29
+ pattern = []
30
+ line = []
31
+ 5.times do |count|
32
+ if count < j
33
+ line << 1
34
+ else
35
+ line << 0
36
+ end
37
+ end
38
+ 5.times do |count|
39
+ if count < i
40
+ pattern << [1, 1, 1, 1, 1]
41
+ elsif count == i
42
+ pattern << line
43
+ else
44
+ pattern << [0, 0, 0, 0, 0]
45
+ end
46
+ end
47
+
48
+ # puts "#{light} #{level} #{i} #{j}"
49
+
50
+ mb.led_show(pattern) # 明るさのレベルに応じて LEDが点灯
51
+ sleep 0.2 # 適当なウェイトが必要...
52
+ end
53
+
54
+ mb.puts("OK!")
55
+ mb.close(5000)
56
+