Module: Smalruby::Hardware

Extended by:
ActiveSupport::Autoload
Defined in:
lib/smalruby/hardware.rb,
lib/smalruby/hardware/led.rb,
lib/smalruby/hardware/pin.rb,
lib/smalruby/hardware/servo.rb,
lib/smalruby/hardware/button.rb,
lib/smalruby/hardware/sensor.rb,
lib/smalruby/hardware/rgb_led_anode.rb,
lib/smalruby/hardware/rgb_led_cathode.rb,
lib/smalruby/hardware/two_wheel_drive_car.rb

Overview

ハードウェアの名前空間

Defined Under Namespace

Modules: Pin Classes: Button, Led, RgbLedAnode, RgbLedCathode, Sensor, Servo, TwoWheelDriveCar

Class Method Summary collapse

Class Method Details

.create_hardware(klass, pin) ⇒ Object

ハードウェアのインスタンスを生成する

作成したハードウェアのインスタンスはキャッシュする

Parameters:

  • klass (Class)

    ハードウェアのクラス

  • pin (String|Numeric)

    ピン番号

Returns:

  • (Object)

    ハードウェアのインスタンス



68
69
70
71
72
73
74
# File 'lib/smalruby/hardware.rb', line 68

def create_hardware(klass, pin)
  key = [klass, pin]
  @hardware_cache.synchronize do
    @hardware_cache[key] ||= klass.new(pin: pin)
  end
  @hardware_cache[key]
end

.init(options = {}) ⇒ Object

ハードウェアを準備する

Parameters:

  • options (Hash) (defaults to: {})

    オプション

Options Hash (options):

  • :device (String)

    シリアルポートのデバイス名。WindowsだとCOM1など



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smalruby/hardware.rb', line 28

def init(options = {})
  return if @initialized_hardware
  fail 'already started.' if @started

  defaults = {
    device: nil,
    baud: 115_200,
    heart_rate: 40 # HACK: 実機で調整した値
  }
  opt = Util.process_options(options, defaults)

  if Dino::VERSION >= '0.11'
    txrx = Dino::TxRx.new(opt)
  elsif Dino::VERSION >= '0.10'
    txrx = Dino::TxRx.new
    txrx.io = opt[:device] if opt[:device]
  end
  world.board = Dino::Board.new(txrx)
  world.board.heart_rate = opt[:heart_rate]

  @initialized_hardware = true
end

.stopObject

ハードウェアを停止させる



52
53
54
55
56
57
58
59
# File 'lib/smalruby/hardware.rb', line 52

def stop
  @hardware_cache.synchronize do
    @hardware_cache.values.each do |h|
      h.stop if h.respond_to?(:stop)
    end
    @hardware_cache.clear
  end
end