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/neo_pixel.rb,
lib/smalruby/hardware/motor_driver.rb,
lib/smalruby/hardware/smalrubot_s1.rb,
lib/smalruby/hardware/smalrubot_v3.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, NeoPixel, NullHardware, RgbLedAnode, RgbLedCathode, Sensor, Servo, SmalrubotS1, SmalrubotV3, TwoWheelDriveCar

Class Method Summary collapse

Class Method Details

.create_hardware(klass, pin = nil) ⇒ Object

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

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

Parameters:

  • klass (Class)

    ハードウェアのクラス

  • pin (String|Numeric) (defaults to: nil)

    ピン番号

Returns:

  • (Object)

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



75
76
77
78
79
80
81
82
# File 'lib/smalruby/hardware.rb', line 75

def create_hardware(klass, pin = nil)
  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)


64
65
66
# File 'lib/smalruby/hardware.rb', line 64

def failed?
  @failed_init_hardware
end

.init(options = {}) ⇒ Object

ハードウェアを準備する

Parameters:

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

    オプション

Options Hash (options):

  • :device (String)

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smalruby/hardware.rb', line 34

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

  defaults = {
    device: ENV["SMALRUBOT_DEVICE"] || nil,
    baud: 19_200,
  }
  opt = Util.process_options(options, defaults)

  txrx = Smalrubot::TxRx.new(opt)
  begin
    world.board = Smalrubot::Board.new(txrx)
    @initialized_hardware = true
  rescue Exception
    Util.print_exception($!)
    @failed_init_hardware = true
  end
end

.stopObject

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



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

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