smalruby 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of smalruby might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop.yml +0 -3
- data/lib/smalruby/canvas.rb +10 -12
- data/lib/smalruby/character.rb +45 -2
- data/lib/smalruby/color.rb +172 -167
- data/lib/smalruby/hardware/dino/fix_gets.rb +49 -0
- data/lib/smalruby/hardware/led.rb +19 -0
- data/lib/smalruby/hardware/pin.rb +51 -0
- data/lib/smalruby/hardware/rgb_led_anode.rb +58 -0
- data/lib/smalruby/hardware/rgb_led_cathode.rb +58 -0
- data/lib/smalruby/hardware/sensor.rb +46 -0
- data/lib/smalruby/hardware.rb +61 -0
- data/lib/smalruby/scene.rb +17 -0
- data/lib/smalruby/util.rb +27 -0
- data/lib/smalruby/version.rb +1 -1
- data/lib/smalruby/world.rb +11 -0
- data/lib/smalruby.rb +28 -1
- data/samples/hardware_led.rb +24 -0
- data/samples/hardware_rgb_led_anode.rb +29 -0
- data/samples/hardware_rgb_led_cathode.rb +29 -0
- data/samples/hardware_sensor.rb +24 -0
- data/smalruby.gemspec +1 -0
- data/spec/lib/smalruby/color_spec.rb +143 -143
- metadata +29 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e252eedd76189846387d2f881f564db1afb2fe0e
|
4
|
+
data.tar.gz: 366215a8cea84e2ded92de3e7bc494274f159998
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3d242aaedb1498ac42ebaeb4dbc7bdead1c803d82971d219323ee3ddeb7eab5e7edd0c509552f2bc22435fc3d06d086aa5734b52c13a1efeff6c65db251a3d2
|
7
|
+
data.tar.gz: e79eaaccc30bb80951bf88495b0468cd9e609a2335331dc4edace62c2b88f426affafb6844f3d785a8174ffd8a6a41d71639495b6cae0437f12cb3f888064bc4
|
data/.rubocop.yml
CHANGED
data/lib/smalruby/canvas.rb
CHANGED
@@ -3,17 +3,15 @@
|
|
3
3
|
module Smalruby
|
4
4
|
# お絵かきを表現するクラス
|
5
5
|
class Canvas < Character
|
6
|
-
def initialize(
|
6
|
+
def initialize(options = {})
|
7
7
|
defaults = {
|
8
|
-
x: 0,
|
9
|
-
y: 0,
|
10
8
|
width: Window.width,
|
11
|
-
height: Window.height
|
9
|
+
height: Window.height
|
12
10
|
}
|
13
|
-
|
11
|
+
opts = Util.process_options(options, defaults)
|
14
12
|
|
15
|
-
|
16
|
-
super(
|
13
|
+
opts[:costume] = Image.new(opts[:width], opts[:height])
|
14
|
+
super(opts.reject { |k, v| defaults.keys.include?(k) })
|
17
15
|
end
|
18
16
|
|
19
17
|
# @!group ペン
|
@@ -23,7 +21,7 @@ module Smalruby
|
|
23
21
|
x: 0,
|
24
22
|
y: 0,
|
25
23
|
string: "",
|
26
|
-
size: 32
|
24
|
+
size: 32
|
27
25
|
}.merge(DEFAULT_COLOR_OPTION)
|
28
26
|
opt = process_optional_arguments(option, defaults)
|
29
27
|
|
@@ -80,7 +78,7 @@ module Smalruby
|
|
80
78
|
private
|
81
79
|
|
82
80
|
DEFAULT_COLOR_OPTION = {
|
83
|
-
color: 'white'
|
81
|
+
color: 'white'
|
84
82
|
}
|
85
83
|
private_constant :DEFAULT_COLOR_OPTION
|
86
84
|
|
@@ -93,7 +91,7 @@ module Smalruby
|
|
93
91
|
x1: nil,
|
94
92
|
y1: nil,
|
95
93
|
x2: nil,
|
96
|
-
y2: nil
|
94
|
+
y2: nil
|
97
95
|
}
|
98
96
|
private_constant :DEFAULT_RECT_OPTION
|
99
97
|
|
@@ -103,7 +101,7 @@ module Smalruby
|
|
103
101
|
left: option[:x1],
|
104
102
|
top: option[:y1],
|
105
103
|
right: option[:x2],
|
106
|
-
bottom: option[:y2]
|
104
|
+
bottom: option[:y2]
|
107
105
|
}.merge(option)
|
108
106
|
process_optional_arguments(opt, defaults)
|
109
107
|
end
|
@@ -112,7 +110,7 @@ module Smalruby
|
|
112
110
|
defaults = {
|
113
111
|
x: 5,
|
114
112
|
y: 5,
|
115
|
-
r: 5
|
113
|
+
r: 5
|
116
114
|
}.merge(DEFAULT_COLOR_OPTION)
|
117
115
|
process_optional_arguments(option, defaults)
|
118
116
|
end
|
data/lib/smalruby/character.rb
CHANGED
@@ -11,6 +11,10 @@ module Smalruby
|
|
11
11
|
self.font_cache = {}
|
12
12
|
font_cache.extend(Mutex_m)
|
13
13
|
|
14
|
+
cattr_accessor :hardware_cache
|
15
|
+
self.hardware_cache = {}
|
16
|
+
hardware_cache.extend(Mutex_m)
|
17
|
+
|
14
18
|
attr_accessor :event_handlers
|
15
19
|
attr_accessor :threads
|
16
20
|
attr_accessor :checking_hit_targets
|
@@ -20,6 +24,7 @@ module Smalruby
|
|
20
24
|
x: 0,
|
21
25
|
y: 0,
|
22
26
|
costume: nil,
|
27
|
+
angle: 0,
|
23
28
|
visible: true
|
24
29
|
}
|
25
30
|
opt = process_optional_arguments(option, defaults)
|
@@ -44,6 +49,10 @@ module Smalruby
|
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
52
|
+
if opt[:angle] != 0
|
53
|
+
rotate(opt[:angle])
|
54
|
+
end
|
55
|
+
|
47
56
|
World.instance.objects << self
|
48
57
|
end
|
49
58
|
|
@@ -132,7 +141,7 @@ module Smalruby
|
|
132
141
|
|
133
142
|
def play(option = {})
|
134
143
|
defaults = {
|
135
|
-
name: 'piano_do.wav'
|
144
|
+
name: 'piano_do.wav'
|
136
145
|
}
|
137
146
|
opt = process_optional_arguments(option, defaults)
|
138
147
|
|
@@ -143,6 +152,30 @@ module Smalruby
|
|
143
152
|
|
144
153
|
# @!endgroup
|
145
154
|
|
155
|
+
# @!group ハードウェア
|
156
|
+
|
157
|
+
# LED
|
158
|
+
def led(pin)
|
159
|
+
Hardware.create_hardware(Hardware::Led, pin)
|
160
|
+
end
|
161
|
+
|
162
|
+
# RGB LED(アノード)
|
163
|
+
def rgb_led_anode(pin)
|
164
|
+
Hardware.create_hardware(Hardware::RgbLedAnode, pin)
|
165
|
+
end
|
166
|
+
|
167
|
+
# RGB LED(カソード)
|
168
|
+
def rgb_led_cathode(pin)
|
169
|
+
Hardware.create_hardware(Hardware::RgbLedCathode, pin)
|
170
|
+
end
|
171
|
+
|
172
|
+
# 汎用的なセンサー
|
173
|
+
def sensor(pin)
|
174
|
+
Hardware.create_hardware(Hardware::Sensor, pin)
|
175
|
+
end
|
176
|
+
|
177
|
+
# @!endgroup
|
178
|
+
|
146
179
|
def draw
|
147
180
|
draw_balloon
|
148
181
|
|
@@ -172,6 +205,8 @@ module Smalruby
|
|
172
205
|
@checking_hit_targets << options
|
173
206
|
@checking_hit_targets.flatten!
|
174
207
|
@checking_hit_targets.uniq!
|
208
|
+
when :sensor_change
|
209
|
+
sensor(options.first)
|
175
210
|
end
|
176
211
|
end
|
177
212
|
|
@@ -221,6 +256,13 @@ module Smalruby
|
|
221
256
|
end
|
222
257
|
end
|
223
258
|
|
259
|
+
def sensor_change(pin, value)
|
260
|
+
@event_handlers[:sensor_change].try(:each) do |h|
|
261
|
+
next unless h.options.include?(pin)
|
262
|
+
@threads << h.call(value)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
224
266
|
def alive?
|
225
267
|
@threads.delete_if { |t|
|
226
268
|
if t.alive?
|
@@ -229,7 +271,8 @@ module Smalruby
|
|
229
271
|
begin
|
230
272
|
t.join
|
231
273
|
rescue => e
|
232
|
-
print_exception(e)
|
274
|
+
Util.print_exception(e)
|
275
|
+
exit(1)
|
233
276
|
end
|
234
277
|
true
|
235
278
|
end
|
data/lib/smalruby/color.rb
CHANGED
@@ -1,167 +1,172 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
module Smalruby
|
4
|
-
# 色を表現するモジュール
|
5
|
-
module Color
|
6
|
-
# 名前から色のコードへの変換テーブル
|
7
|
-
# 参照: http://www.colordic.org/
|
8
|
-
NAME_TO_CODE = {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Smalruby
|
4
|
+
# 色を表現するモジュール
|
5
|
+
module Color
|
6
|
+
# 名前から色のコードへの変換テーブル
|
7
|
+
# 参照: http://www.colordic.org/
|
8
|
+
NAME_TO_CODE = {
|
9
|
+
'black' => [0x00, 0x00, 0x00],
|
10
|
+
'dimgray' => [0x69, 0x69, 0x69],
|
11
|
+
'gray' => [0x80, 0x80, 0x80],
|
12
|
+
'darkgray' => [0xa9, 0xa9, 0xa9],
|
13
|
+
'silver' => [0xc0, 0xc0, 0xc0],
|
14
|
+
'lightgrey' => [0xd3, 0xd3, 0xd3],
|
15
|
+
'gainsboro' => [0xdc, 0xdc, 0xdc],
|
16
|
+
'whitesmoke' => [0xf5, 0xf5, 0xf5],
|
17
|
+
'white' => [0xff, 0xff, 0xff],
|
18
|
+
'snow' => [0xff, 0xfa, 0xfa],
|
19
|
+
'ghostwhite' => [0xf8, 0xf8, 0xff],
|
20
|
+
'floralwhite' => [0xff, 0xfa, 0xf0],
|
21
|
+
'linen' => [0xfa, 0xf0, 0xe6],
|
22
|
+
'antiquewhite' => [0xfa, 0xeb, 0xd7],
|
23
|
+
'papayawhip' => [0xff, 0xef, 0xd5],
|
24
|
+
'blanchedalmond' => [0xff, 0xeb, 0xcd],
|
25
|
+
'bisque' => [0xff, 0xe4, 0xc4],
|
26
|
+
'moccasin' => [0xff, 0xe4, 0xb5],
|
27
|
+
'navajowhite' => [0xff, 0xde, 0xad],
|
28
|
+
'peachpuff' => [0xff, 0xda, 0xb9],
|
29
|
+
'mistyrose' => [0xff, 0xe4, 0xe1],
|
30
|
+
'lavenderblush' => [0xff, 0xf0, 0xf5],
|
31
|
+
'seashell' => [0xff, 0xf5, 0xee],
|
32
|
+
'oldlace' => [0xfd, 0xf5, 0xe6],
|
33
|
+
'ivory' => [0xff, 0xff, 0xf0],
|
34
|
+
'honeydew' => [0xf0, 0xff, 0xf0],
|
35
|
+
'mintcream' => [0xf5, 0xff, 0xfa],
|
36
|
+
'azure' => [0xf0, 0xff, 0xff],
|
37
|
+
'aliceblue' => [0xf0, 0xf8, 0xff],
|
38
|
+
'lavender' => [0xe6, 0xe6, 0xfa],
|
39
|
+
'lightsteelblue' => [0xb0, 0xc4, 0xde],
|
40
|
+
'lightslategray' => [0x77, 0x88, 0x99],
|
41
|
+
'slategray' => [0x70, 0x80, 0x90],
|
42
|
+
'steelblue' => [0x46, 0x82, 0xb4],
|
43
|
+
'royalblue' => [0x41, 0x69, 0xe1],
|
44
|
+
'midnightblue' => [0x19, 0x19, 0x70],
|
45
|
+
'navy' => [0x00, 0x00, 0x80],
|
46
|
+
'darkblue' => [0x00, 0x00, 0x8b],
|
47
|
+
'mediumblue' => [0x00, 0x00, 0xcd],
|
48
|
+
'blue' => [0x00, 0x00, 0xff],
|
49
|
+
'dodgerblue' => [0x1e, 0x90, 0xff],
|
50
|
+
'cornflowerblue' => [0x64, 0x95, 0xed],
|
51
|
+
'deepskyblue' => [0x00, 0xbf, 0xff],
|
52
|
+
'lightskyblue' => [0x87, 0xce, 0xfa],
|
53
|
+
'skyblue' => [0x87, 0xce, 0xeb],
|
54
|
+
'lightblue' => [0xad, 0xd8, 0xe6],
|
55
|
+
'powderblue' => [0xb0, 0xe0, 0xe6],
|
56
|
+
'paleturquoise' => [0xaf, 0xee, 0xee],
|
57
|
+
'lightcyan' => [0xe0, 0xff, 0xff],
|
58
|
+
'cyan' => [0x00, 0xff, 0xff],
|
59
|
+
'aqua' => [0x00, 0xff, 0xff],
|
60
|
+
'turquoise' => [0x40, 0xe0, 0xd0],
|
61
|
+
'mediumturquoise' => [0x48, 0xd1, 0xcc],
|
62
|
+
'darkturquoise' => [0x00, 0xce, 0xd1],
|
63
|
+
'lightseagreen' => [0x20, 0xb2, 0xaa],
|
64
|
+
'cadetblue' => [0x5f, 0x9e, 0xa0],
|
65
|
+
'darkcyan' => [0x00, 0x8b, 0x8b],
|
66
|
+
'teal' => [0x00, 0x80, 0x80],
|
67
|
+
'darkslategray' => [0x2f, 0x4f, 0x4f],
|
68
|
+
'darkgreen' => [0x00, 0x64, 0x00],
|
69
|
+
'green' => [0x00, 0x80, 0x00],
|
70
|
+
'forestgreen' => [0x22, 0x8b, 0x22],
|
71
|
+
'seagreen' => [0x2e, 0x8b, 0x57],
|
72
|
+
'mediumseagreen' => [0x3c, 0xb3, 0x71],
|
73
|
+
'mediumaquamarine' => [0x66, 0xcd, 0xaa],
|
74
|
+
'darkseagreen' => [0x8f, 0xbc, 0x8f],
|
75
|
+
'aquamarine' => [0x7f, 0xff, 0xd4],
|
76
|
+
'palegreen' => [0x98, 0xfb, 0x98],
|
77
|
+
'lightgreen' => [0x90, 0xee, 0x90],
|
78
|
+
'springgreen' => [0x00, 0xff, 0x7f],
|
79
|
+
'mediumspringgreen' => [0x00, 0xfa, 0x9a],
|
80
|
+
'lawngreen' => [0x7c, 0xfc, 0x00],
|
81
|
+
'chartreuse' => [0x7f, 0xff, 0x00],
|
82
|
+
'greenyellow' => [0xad, 0xff, 0x2f],
|
83
|
+
'lime' => [0x00, 0xff, 0x00],
|
84
|
+
'limegreen' => [0x32, 0xcd, 0x32],
|
85
|
+
'yellowgreen' => [0x9a, 0xcd, 0x32],
|
86
|
+
'darkolivegreen' => [0x55, 0x6b, 0x2f],
|
87
|
+
'olivedrab' => [0x6b, 0x8e, 0x23],
|
88
|
+
'olive' => [0x80, 0x80, 0x00],
|
89
|
+
'darkkhaki' => [0xbd, 0xb7, 0x6b],
|
90
|
+
'palegoldenrod' => [0xee, 0xe8, 0xaa],
|
91
|
+
'cornsilk' => [0xff, 0xf8, 0xdc],
|
92
|
+
'beige' => [0xf5, 0xf5, 0xdc],
|
93
|
+
'lightyellow' => [0xff, 0xff, 0xe0],
|
94
|
+
'lightgoldenrodyellow' => [0xfa, 0xfa, 0xd2],
|
95
|
+
'lemonchiffon' => [0xff, 0xfa, 0xcd],
|
96
|
+
'wheat' => [0xf5, 0xde, 0xb3],
|
97
|
+
'burlywood' => [0xde, 0xb8, 0x87],
|
98
|
+
'tan' => [0xd2, 0xb4, 0x8c],
|
99
|
+
'khaki' => [0xf0, 0xe6, 0x8c],
|
100
|
+
'yellow' => [0xff, 0xff, 0x00],
|
101
|
+
'gold' => [0xff, 0xd7, 0x00],
|
102
|
+
'orange' => [0xff, 0xa5, 0x00],
|
103
|
+
'sandybrown' => [0xf4, 0xa4, 0x60],
|
104
|
+
'darkorange' => [0xff, 0x8c, 0x00],
|
105
|
+
'goldenrod' => [0xda, 0xa5, 0x20],
|
106
|
+
'peru' => [0xcd, 0x85, 0x3f],
|
107
|
+
'darkgoldenrod' => [0xb8, 0x86, 0x0b],
|
108
|
+
'chocolate' => [0xd2, 0x69, 0x1e],
|
109
|
+
'sienna' => [0xa0, 0x52, 0x2d],
|
110
|
+
'saddlebrown' => [0x8b, 0x45, 0x13],
|
111
|
+
'maroon' => [0x80, 0x00, 0x00],
|
112
|
+
'darkred' => [0x8b, 0x00, 0x00],
|
113
|
+
'brown' => [0xa5, 0x2a, 0x2a],
|
114
|
+
'firebrick' => [0xb2, 0x22, 0x22],
|
115
|
+
'indianred' => [0xcd, 0x5c, 0x5c],
|
116
|
+
'rosybrown' => [0xbc, 0x8f, 0x8f],
|
117
|
+
'darksalmon' => [0xe9, 0x96, 0x7a],
|
118
|
+
'lightcoral' => [0xf0, 0x80, 0x80],
|
119
|
+
'salmon' => [0xfa, 0x80, 0x72],
|
120
|
+
'lightsalmon' => [0xff, 0xa0, 0x7a],
|
121
|
+
'coral' => [0xff, 0x7f, 0x50],
|
122
|
+
'tomato' => [0xff, 0x63, 0x47],
|
123
|
+
'orangered' => [0xff, 0x45, 0x00],
|
124
|
+
'red' => [0xff, 0x00, 0x00],
|
125
|
+
'crimson' => [0xdc, 0x14, 0x3c],
|
126
|
+
'mediumvioletred' => [0xc7, 0x15, 0x85],
|
127
|
+
'deeppink' => [0xff, 0x14, 0x93],
|
128
|
+
'hotpink' => [0xff, 0x69, 0xb4],
|
129
|
+
'palevioletred' => [0xdb, 0x70, 0x93],
|
130
|
+
'pink' => [0xff, 0xc0, 0xcb],
|
131
|
+
'lightpink' => [0xff, 0xb6, 0xc1],
|
132
|
+
'thistle' => [0xd8, 0xbf, 0xd8],
|
133
|
+
'magenta' => [0xff, 0x00, 0xff],
|
134
|
+
'fuchsia' => [0xff, 0x00, 0xff],
|
135
|
+
'violet' => [0xee, 0x82, 0xee],
|
136
|
+
'plum' => [0xdd, 0xa0, 0xdd],
|
137
|
+
'orchid' => [0xda, 0x70, 0xd6],
|
138
|
+
'mediumorchid' => [0xba, 0x55, 0xd3],
|
139
|
+
'darkorchid' => [0x99, 0x32, 0xcc],
|
140
|
+
'darkviolet' => [0x94, 0x00, 0xd3],
|
141
|
+
'darkmagenta' => [0x8b, 0x00, 0x8b],
|
142
|
+
'purple' => [0x80, 0x00, 0x80],
|
143
|
+
'indigo' => [0x4b, 0x00, 0x82],
|
144
|
+
'darkslateblue' => [0x48, 0x3d, 0x8b],
|
145
|
+
'blueviolet' => [0x8a, 0x2b, 0xe2],
|
146
|
+
'mediumpurple' => [0x93, 0x70, 0xdb],
|
147
|
+
'slateblue' => [0x6a, 0x5a, 0xcd],
|
148
|
+
'mediumslateblue' => [0x7b, 0x68, 0xee]
|
149
|
+
}
|
150
|
+
|
151
|
+
# 色名の配列
|
152
|
+
NAMES = NAME_TO_CODE.keys
|
153
|
+
|
154
|
+
module_function
|
155
|
+
|
156
|
+
# Smalrubyの色名からDXRubyの色コードに変換する
|
157
|
+
def smalruby_to_dxruby(color)
|
158
|
+
if color.is_a?(String)
|
159
|
+
color = color.downcase
|
160
|
+
if color == 'random'
|
161
|
+
[rand(0..0xff), rand(0..0xff), rand(0..0xff)]
|
162
|
+
elsif NAME_TO_CODE.key?(color)
|
163
|
+
NAME_TO_CODE[color]
|
164
|
+
else
|
165
|
+
fail "色の指定が間違っています: #{color}"
|
166
|
+
end
|
167
|
+
else
|
168
|
+
color
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
# WindowsでのDinoの不具合を修正するモンキーパッチ
|
4
|
+
if Util.windows?
|
5
|
+
require 'dino'
|
6
|
+
|
7
|
+
if Dino::VERSION >= '0.11'
|
8
|
+
class Dino::TxRx::Base
|
9
|
+
def gets(timeout = 0.005)
|
10
|
+
return nil unless IO.select([io], nil, nil, timeout)
|
11
|
+
io.read_timeout = (timeout * 1000).to_i
|
12
|
+
bytes = []
|
13
|
+
until (x = io.getbyte).nil?
|
14
|
+
bytes.push(x)
|
15
|
+
end
|
16
|
+
return nil if bytes.empty?
|
17
|
+
bytes.pack("C*")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
elsif Dino::VERSION >= '0.10'
|
21
|
+
class Dino::TxRx::USBSerial
|
22
|
+
def read
|
23
|
+
@thread ||= Thread.new do
|
24
|
+
loop do
|
25
|
+
line = gets
|
26
|
+
if line
|
27
|
+
pin, message = *line.chomp.split(/::/)
|
28
|
+
pin && message && changed && notify_observers(pin, message)
|
29
|
+
end
|
30
|
+
sleep 0.004
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def gets(timeout = 0.005)
|
36
|
+
return nil unless IO.select([io], nil, nil, timeout)
|
37
|
+
io.read_timeout = (timeout * 1000).to_i
|
38
|
+
bytes = []
|
39
|
+
until (x = io.getbyte).nil?
|
40
|
+
bytes.push(x)
|
41
|
+
end
|
42
|
+
return nil if bytes.empty?
|
43
|
+
bytes.pack("C*")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
else
|
47
|
+
fail "Dinoのバージョン#{version}はサポートしていません"
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'smalruby/hardware'
|
3
|
+
|
4
|
+
module Smalruby
|
5
|
+
module Hardware
|
6
|
+
# LEDを表現するクラス
|
7
|
+
class Led < Dino::Components::Led
|
8
|
+
def initialize(options)
|
9
|
+
super(board: world.board, pin: Pin.smalruby_to_dino(options[:pin]))
|
10
|
+
end
|
11
|
+
|
12
|
+
# @!method on
|
13
|
+
# LEDをオンにする
|
14
|
+
|
15
|
+
# @!method off
|
16
|
+
# LEDをオフにする
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'dino'
|
3
|
+
|
4
|
+
module Smalruby
|
5
|
+
module Hardware
|
6
|
+
module Pin
|
7
|
+
module_function
|
8
|
+
|
9
|
+
# Smalrubyのピン番号をDinoのピン番号に変換する
|
10
|
+
#
|
11
|
+
# ピン番号が0~13、D0~D13、A0~A5でなければ例外が発生する
|
12
|
+
#
|
13
|
+
# @param [String|Numeric] pin Smalrubyのピン番号
|
14
|
+
# @return [Numeric] Dinoのデジタル入出力のピン番号
|
15
|
+
# @return [String] Dinoのアナログ入力のピン番号
|
16
|
+
def smalruby_to_dino(pin)
|
17
|
+
pin = pin.to_s
|
18
|
+
case pin
|
19
|
+
when /\A[0-9]|1[0-3]\z/
|
20
|
+
pin.to_i
|
21
|
+
when /\AD[0-9]|D1[0-3]\z/
|
22
|
+
pin[1..-1].to_i
|
23
|
+
when /\AA[0-5]\z/
|
24
|
+
pin
|
25
|
+
else
|
26
|
+
fail "ハードウェアのピンの番号が間違っています: #{pin}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Dinoのピン番号をSmalrubyのピン番号に変換する
|
31
|
+
#
|
32
|
+
# ピン番号が0~13、D0~D13、A0~A5でなければ例外が発生する
|
33
|
+
#
|
34
|
+
# @param [String|Numeric] pin Dinoのピン番号
|
35
|
+
# @return [String] Smalrubyのピン番号
|
36
|
+
def dino_to_smalruby(pin)
|
37
|
+
pin = pin.to_s
|
38
|
+
case pin
|
39
|
+
when /\A[0-9]|1[0-3]\z/
|
40
|
+
"D#{pin}"
|
41
|
+
when /\AD[0-9]|D1[0-3]\z/
|
42
|
+
pin
|
43
|
+
when /\AA[0-5]\z/
|
44
|
+
pin
|
45
|
+
else
|
46
|
+
fail "ハードウェアのピンの番号が間違っています: #{pin}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|