smalruby 0.0.12 → 0.0.13

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e79742c38c61cf25de31f1b89aba03ef25a971a5
4
- data.tar.gz: 3c62f94a839dfaba9bf2f42c334ceac56516b583
3
+ metadata.gz: c6cb8becdbc9075484b7d28401dbb3d19fb6fae8
4
+ data.tar.gz: 6438411a47198e5e70a51f02b35ad2e4224e7933
5
5
  SHA512:
6
- metadata.gz: de0e92d51cb318d240eeac52dfe18819a0157e0ecbe5681d97f4bf5632df371c107683c6a81bb0e643e12e15b90547cdba134fe280e56f94ddf31e0a1f01db5c
7
- data.tar.gz: d327022ed04602022b1b25e3089984e5927cdc1744efe2856e021e17750608da0b20422583dd2678630a22601dacdbe1b5d9fcea6a3197c5dd2b5d5348b7d8bb
6
+ metadata.gz: 13a1c625289915d8d6baae091c65bbc5fde556aa3836975a648d82b6e19b5bfc02c588f1cc6fa567cfe0d55b4d776330625e4bbc135ee5db309e9f4b41ef8214
7
+ data.tar.gz: efac9fc7bdf586241e1c459cf2b319e59390f9ecceeb69155699e6b56d94eabc03498afa0ae7c04522f86aa461e7778a5dc9414f913491f904e3aadec8a61a6a
@@ -208,6 +208,16 @@ module Smalruby
208
208
  Hardware.create_hardware(Hardware::RgbLedCathode, pin)
209
209
  end
210
210
 
211
+ # サーボモーター
212
+ def servo(pin)
213
+ Hardware.create_hardware(Hardware::Servo, pin)
214
+ end
215
+
216
+ # ボタン
217
+ def button(pin)
218
+ Hardware.create_hardware(Hardware::Button, pin)
219
+ end
220
+
211
221
  # 汎用的なセンサー
212
222
  def sensor(pin)
213
223
  Hardware.create_hardware(Hardware::Sensor, pin)
@@ -246,6 +256,8 @@ module Smalruby
246
256
  @checking_hit_targets.uniq!
247
257
  when :sensor_change
248
258
  sensor(options.first)
259
+ when :button_up, :button_down
260
+ button(options.first)
249
261
  end
250
262
  end
251
263
 
@@ -302,6 +314,20 @@ module Smalruby
302
314
  end
303
315
  end
304
316
 
317
+ def button_up(pin)
318
+ @event_handlers[:button_up].try(:each) do |h|
319
+ next unless h.options.include?(pin)
320
+ @threads << h.call
321
+ end
322
+ end
323
+
324
+ def button_down(pin)
325
+ @event_handlers[:button_down].try(:each) do |h|
326
+ next unless h.options.include?(pin)
327
+ @threads << h.call
328
+ end
329
+ end
330
+
305
331
  def alive?
306
332
  @threads.delete_if { |t|
307
333
  if t.alive?
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby/hardware'
3
+
4
+ module Smalruby
5
+ module Hardware
6
+ # ボタンを表現するクラス
7
+ class Button < Dino::Components::Button
8
+ def initialize(options)
9
+ super(board: world.board, pin: Pin.smalruby_to_dino(options[:pin]))
10
+ end
11
+
12
+ def up?
13
+ @state == UP
14
+ end
15
+
16
+ alias_method :on?, :up?
17
+
18
+ def down?
19
+ @state == DOWN
20
+ end
21
+
22
+ alias_method :off?, :down?
23
+
24
+ private
25
+
26
+ def after_initialize(options = {})
27
+ super(options)
28
+
29
+ s_pin = Pin.dino_to_smalruby(pin)
30
+ down { world.button_changed(s_pin, :down) }
31
+ up { world.button_changed(s_pin, :up) }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'smalruby/hardware'
3
+
4
+ module Smalruby
5
+ module Hardware
6
+ # サーボモーターを表現するクラス
7
+ class Servo < Dino::Components::Servo
8
+ def initialize(options)
9
+ super(board: world.board, pin: Pin.smalruby_to_dino(options[:pin]))
10
+ end
11
+
12
+ # @!method position=(angle)
13
+ # サーボモーターを( )度にする
14
+
15
+ private
16
+
17
+ # HACK: GROVEシステムのサーボモーターでは5よりも小さい値を指定す
18
+ # るとモーターが発振してしまう
19
+ MIN_ANGLE = 5
20
+ MAX_ANGLE = 180
21
+
22
+ def angle(value)
23
+ v = value.to_i
24
+ if v < MIN_ANGLE
25
+ MIN_ANGLE
26
+ elsif v > MAX_ANGLE
27
+ MAX_ANGLE
28
+ else
29
+ v
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -12,6 +12,9 @@ module Smalruby
12
12
  autoload :Led
13
13
  autoload :RgbLedAnode
14
14
  autoload :RgbLedCathode
15
+ autoload :Servo
16
+
17
+ autoload :Button
15
18
  autoload :Sensor
16
19
 
17
20
  module_function
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module Smalruby
4
- VERSION = '0.0.12'
4
+ VERSION = '0.0.13'
5
5
  end
@@ -9,12 +9,15 @@ module Smalruby
9
9
  attr_accessor :objects
10
10
  attr_accessor :board
11
11
  attr_reader :sensor_change_queue
12
+ attr_reader :button_change_queue
12
13
 
13
14
  def initialize
14
15
  @objects = []
15
16
  @board = nil
16
17
  @sensor_change_queue = []
17
18
  @sensor_change_queue.extend(Mutex_m)
19
+ @button_change_queue = []
20
+ @button_change_queue.extend(Mutex_m)
18
21
  end
19
22
 
20
23
  def sensor_changed(pin, value)
@@ -22,5 +25,11 @@ module Smalruby
22
25
  @sensor_change_queue.push([pin, value])
23
26
  end
24
27
  end
28
+
29
+ def button_changed(pin, up_or_down)
30
+ @button_change_queue.synchronize do
31
+ @button_change_queue.push([pin, up_or_down])
32
+ end
33
+ end
25
34
  end
26
35
  end
data/lib/smalruby.rb CHANGED
@@ -97,6 +97,8 @@ module Smalruby
97
97
 
98
98
  sensor_change
99
99
 
100
+ button_change
101
+
100
102
  world.objects.delete_if do |o|
101
103
  if !o.alive?
102
104
  o.join
@@ -189,6 +191,28 @@ module Smalruby
189
191
  end
190
192
  end
191
193
  end
194
+
195
+ def button_change
196
+ if world.button_change_queue.length > 0
197
+ queue = nil
198
+ world.button_change_queue.synchronize do
199
+ queue = world.button_change_queue.dup
200
+ world.button_change_queue.clear
201
+ end
202
+
203
+ world.objects.each do |o|
204
+ if o.respond_to?(:button_up) && o.respond_to?(:button_down)
205
+ queue.each do |pin, up_or_down|
206
+ if up_or_down == :up
207
+ o.button_up(pin)
208
+ else
209
+ o.button_down(pin)
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ end
192
216
  end
193
217
  end
194
218
 
@@ -0,0 +1,32 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ DESCRIPTION = <<EOS
4
+ ハードウェアのボタンを操作します
5
+ EOS
6
+
7
+ # デジタルの3番ピンにボタンを接続してください。
8
+
9
+ require 'smalruby'
10
+
11
+ init_hardware
12
+
13
+ stage1 = Stage.new(color: 'white')
14
+ frog1 = Character.new(costume: 'frog1.png', x: 300, y: 200, visible: false)
15
+
16
+ stage1.on(:start) do
17
+ draw_font(string: DESCRIPTION, color: 'black')
18
+
19
+ if button('D3').up?
20
+ frog1.visible = false
21
+ else
22
+ frog1.visible = true
23
+ end
24
+ end
25
+
26
+ stage1.on(:button_down, 'D3') do
27
+ frog1.visible = true
28
+ end
29
+
30
+ stage1.on(:button_up, 'D3') do
31
+ frog1.visible = false
32
+ end
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ DESCRIPTION = <<EOS
4
+ カーソルキーでサーボモーターを操作します
5
+ EOS
6
+
7
+ require 'smalruby'
8
+
9
+ init_hardware
10
+
11
+ stage1 = Stage.new(color: 'white')
12
+
13
+ stage1.on(:start) do
14
+ draw_font(string: DESCRIPTION, color: 'black')
15
+
16
+ servo('D3').position = 90
17
+ end
18
+
19
+ stage1.on(:key_down, K_UP, K_LEFT) do
20
+ servo('D3').position += 2
21
+ end
22
+
23
+ stage1.on(:key_down, K_DOWN, K_RIGHT) do
24
+ servo('D3').position -= 2
25
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smalruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-11 00:00:00.000000000 Z
11
+ date: 2014-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -252,12 +252,14 @@ files:
252
252
  - lib/smalruby/console.rb
253
253
  - lib/smalruby/event_handler.rb
254
254
  - lib/smalruby/hardware.rb
255
+ - lib/smalruby/hardware/button.rb
255
256
  - lib/smalruby/hardware/dino/fix_gets.rb
256
257
  - lib/smalruby/hardware/led.rb
257
258
  - lib/smalruby/hardware/pin.rb
258
259
  - lib/smalruby/hardware/rgb_led_anode.rb
259
260
  - lib/smalruby/hardware/rgb_led_cathode.rb
260
261
  - lib/smalruby/hardware/sensor.rb
262
+ - lib/smalruby/hardware/servo.rb
261
263
  - lib/smalruby/stage.rb
262
264
  - lib/smalruby/util.rb
263
265
  - lib/smalruby/version.rb
@@ -270,10 +272,12 @@ files:
270
272
  - samples/costume.rb
271
273
  - samples/extra_car.png
272
274
  - samples/finding_cars.rb
275
+ - samples/hardware_button.rb
273
276
  - samples/hardware_led.rb
274
277
  - samples/hardware_rgb_led_anode.rb
275
278
  - samples/hardware_rgb_led_cathode.rb
276
279
  - samples/hardware_sensor.rb
280
+ - samples/hardware_servo.rb
277
281
  - samples/hit_car.rb
278
282
  - samples/piano.rb
279
283
  - smalruby.gemspec