Class: Knipper::Device
- Inherits:
-
Object
- Object
- Knipper::Device
- Defined in:
- lib/knipper/device.rb
Constant Summary collapse
- @@lib =
nil
Class Method Summary collapse
Instance Method Summary collapse
- #clear_pattern ⇒ Object
- #close ⇒ Object
- #fade_to_rgb(millis, red, green, blue) ⇒ Object
-
#initialize(path = nil, &block) ⇒ Device
constructor
A new instance of Device.
- #read_pattern_line(pos) ⇒ Object
- #set_rgb(red, green, blue) ⇒ Object
- #start_pattern(pos) ⇒ Object
- #stop_pattern ⇒ Object
- #write_pattern(pattern, clear = true) ⇒ Object
- #write_pattern_line(millis, red, green, blue, pos) ⇒ Object
Constructor Details
#initialize(path = nil, &block) ⇒ Device
Returns a new instance of Device.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/knipper/device.rb', line 22 def initialize(path = nil, &block) @@lib ||= Blink1Lib.new if path @device = @@lib.blink1_openByPath path else @device = @@lib.blink1_open end if block_given? yield self @@lib.blink1_close @device return end end |
Class Method Details
.device_present? ⇒ Boolean
18 19 20 |
# File 'lib/knipper/device.rb', line 18 def self.device_present? devices.length > 0 end |
.devices ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/knipper/device.rb', line 6 def self.devices @@lib ||= Blink1Lib.new num_devices = @@lib.blink1_enumerate devices = [] num_devices.times do |i| devices << @@lib.blink1_getCachedPath(i) end unless num_devices < 1 devices end |
Instance Method Details
#clear_pattern ⇒ Object
67 68 69 70 71 |
# File 'lib/knipper/device.rb', line 67 def clear_pattern 12.times do |i| write_pattern_line 0, 0, 0, 0, i end end |
#close ⇒ Object
36 37 38 |
# File 'lib/knipper/device.rb', line 36 def close @@lib.blink1_close @device end |
#fade_to_rgb(millis, red, green, blue) ⇒ Object
40 41 42 |
# File 'lib/knipper/device.rb', line 40 def fade_to_rgb(millis, red, green, blue) @@lib.blink1_fadeToRGB(@device, millis, red, green, blue) end |
#read_pattern_line(pos) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/knipper/device.rb', line 52 def read_pattern_line(pos) millis = FFI::MemoryPointer.new(:ushort) r = FFI::MemoryPointer.new(:uchar) g = FFI::MemoryPointer.new(:uchar) b = FFI::MemoryPointer.new(:uchar) @@lib.blink1_readPatternLine(@device, millis, r, g, b, pos) Knipper::Pattern::PatternLine.new({ millis: millis.read_uint16, red: r.read_uint8, green: g.read_uint8, blue: b.read_uint8 }) end |
#set_rgb(red, green, blue) ⇒ Object
44 45 46 |
# File 'lib/knipper/device.rb', line 44 def set_rgb(red, green, blue) @@lib.blink1_setRGB(@device, red, green, blue) end |
#start_pattern(pos) ⇒ Object
87 88 89 |
# File 'lib/knipper/device.rb', line 87 def start_pattern(pos) @@lib.blink1_play(@device, 1, pos) end |
#stop_pattern ⇒ Object
91 92 93 |
# File 'lib/knipper/device.rb', line 91 def stop_pattern @@lib.blink1_play(@device, 0, 0) end |
#write_pattern(pattern, clear = true) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/knipper/device.rb', line 73 def write_pattern(pattern, clear=true) if pattern.is_a? String pattern = Knipper::Pattern::Pattern.parse(pattern) end if pattern.is_a? Knipper::Pattern::Pattern clear_pattern if clear pattern.lines.each do |pattern_line| write_pattern_line pattern_line.millis, pattern_line.red, pattern_line.green, pattern_line.blue, pattern.lines.index(pattern_line) end end end |
#write_pattern_line(millis, red, green, blue, pos) ⇒ Object
48 49 50 |
# File 'lib/knipper/device.rb', line 48 def write_pattern_line(millis, red, green, blue, pos) @@lib.blink1_writePatternLine(@device, millis, red, green, blue, pos) end |