Class: Smalruby::Hardware::NeoPixel

Inherits:
Smalrubot::Components::BaseComponent
  • Object
show all
Includes:
Util
Defined in:
lib/smalruby/hardware/neo_pixel.rb

Overview

Adafruit NeoPixel RGB LED class

Instance Method Summary collapse

Methods included from Util

osx?, print_exception, process_options, raspberrypi?, windows?

Constructor Details

#initialize(options) ⇒ NeoPixel

Returns a new instance of NeoPixel.



11
12
13
14
15
16
17
18
19
20
# File 'lib/smalruby/hardware/neo_pixel.rb', line 11

def initialize(options)
  pin = Pin.smalruby_to_smalrubot(options[:pin])
  case pin
  when 3, 5, 6, 9, 10, 11
    super(board: world.board, pin: pin)
  else
    fail "ピン番号が間違っています: {options[:pin]}"
  end
  @indexes = Set.new
end

Instance Method Details

#set(options) ⇒ Object

set color



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smalruby/hardware/neo_pixel.rb', line 23

def set(options)
  defaults = {
    index: 0,
    color: "black",
    show: true,
  }
  opts = process_options(options, defaults)
  @indexes << opts[:index]
  color = Color.smalruby_to_dxruby(opts[:color])
  board.set_neo_pixel_color(opts[:index], *color)
  if opts[:show]
    show
  end
end

#showObject

apply color settings



39
40
41
# File 'lib/smalruby/hardware/neo_pixel.rb', line 39

def show
  board.show_neo_pixel
end

#stopObject

destruction



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

def stop
  if @indexes.length > 0
    turn_off
    @indexes.clear
  end
end

#turn_offObject

turn off



44
45
46
47
48
49
50
51
# File 'lib/smalruby/hardware/neo_pixel.rb', line 44

def turn_off
  if @indexes.length > 0
    @indexes.each do |i|
      board.set_neo_pixel_color(i, 0, 0, 0)
    end
    board.show_neo_pixel
  end
end