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/motor_driver.rb,
lib/smalruby/hardware/null_hardware.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, MotorDriver, NullHardware, 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)

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



80
81
82
83
84
85
86
87
# File 'lib/smalruby/hardware.rb', line 80

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

.failed?Boolean

ハードウェアの初期化に失敗したかどうかを返す

Returns:

  • (Boolean)


69
70
71
# File 'lib/smalruby/hardware.rb', line 69

def failed?
  @failed_init_hardware
end

.init(options = {}) ⇒ Object

ハードウェアを準備する

Parameters:

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

    オプション

Options Hash (options):

  • :device (String)

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



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
# File 'lib/smalruby/hardware.rb', line 31

def init(options = {})
  return if @initialized_hardware

  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
  begin
    world.board = Dino::Board.new(txrx)
    world.board.heart_rate = opt[:heart_rate]

    @initialized_hardware = true
  rescue Exception
    Util.print_exception($!)
    @failed_init_hardware = true
  end
end

.stopObject

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



59
60
61
62
63
64
65
66
# File 'lib/smalruby/hardware.rb', line 59

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