apa102_rbpi 0.1.0 → 0.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebaede96b65b0998eb0b0240b877099d3fd52acf
4
- data.tar.gz: 475dc85eb79b055ba395ad17984c8ce03841df8a
3
+ metadata.gz: 4824c05aa928d895e39caec20c953fff282aeb9b
4
+ data.tar.gz: 75e64e657e0ed45327d33e1b1db8e7d554518e01
5
5
  SHA512:
6
- metadata.gz: 2aa988f8accd6911fc6a3a1b32655f7c44616a94a670ac39fde9e1e0ef56f0774b085b2b3cfd27e73926d513d29f5d889d95417003ef9ed0862dec8079dd33b3
7
- data.tar.gz: 161f714ff6c8b79cdce2aaeb18c81f83b626de651a4f01b980e3219274ebf85a63782240e5a94f62d5cb00a2d7ce40ac33171b15fee7ce89654566568b4fc327
6
+ metadata.gz: 2893802eee9f1b7dcacbe656146f4e463b7c4bc22943588770d13b70ffb01abf0281b50ce1052c5a60403abb0701c3aeae0701284843cd49b7d55ba172feb942
7
+ data.tar.gz: 56d987c29ca7c9bdf964bc23678643244ffc29fd99c4811b1f598b1c538aaad2e708545ecf7390431d6b3175404de5ea919028a719614bf69d87512eed09e1a9
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ apa102_rbpi (0.2.0)
5
+ pi_piper (~> 1.9)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ eventmachine (1.0.9)
11
+ ffi (1.9.10)
12
+ pi_piper (1.9.9)
13
+ eventmachine (= 1.0.9)
14
+ ffi
15
+ rake (10.1.0)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ apa102_rbpi!
22
+ bundler (~> 1.9)
23
+ rake (~> 10.0)
24
+
25
+ BUNDLED WITH
26
+ 1.11.2
data/README.md CHANGED
@@ -20,7 +20,19 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ Simple blinker:
24
+ ```ruby
25
+ require 'apa102_rbpi'
26
+ include Apa102Rbpi
27
+
28
+ led = Apa102.new(1)
29
+ loop do
30
+ led.set_pixel!(0, 0xffffff)
31
+ sleep 1
32
+ led.set_pixel!(0, 0)
33
+ sleep 1
34
+ end
35
+ ```
24
36
 
25
37
  ## Development
26
38
 
data/apa102_rbpi.gemspec CHANGED
@@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_development_dependency 'bundler', '~> 1.9'
29
29
  spec.add_development_dependency 'rake', '~> 10.0'
30
- spec.add_development_dependency 'pi_piper', '~> 1.9'
30
+ spec.add_dependency 'pi_piper', '~> 1.9'
31
31
  end
@@ -1,15 +1,18 @@
1
1
  require 'pi_piper'
2
2
 
3
3
  module Apa102Rbpi
4
- include PiPiper
5
-
6
4
  class Apa102
5
+ include PiPiper
6
+
7
7
  START_FRAME = [0x00] * 4
8
8
 
9
9
  attr_accessor :brightness
10
+ attr_reader :num_leds, :spi_hz, :led_frame_rgb_offsets
11
+
12
+ def initialize(num_leds, opts = {})
13
+ @num_leds = num_leds || 1
10
14
 
11
- def initialize(opts = {})
12
- @num_leds = opts[:num_leds] || 1
15
+ # default brightness, must be within 0-31
13
16
  @brightness = opts[:brightness] || 31
14
17
  @spi_hz = opts[:spi_hz] || 8000000
15
18
 
@@ -17,39 +20,45 @@ module Apa102Rbpi
17
20
  # set offsets if using a non-default strip
18
21
  # offset: 0 1 2 3
19
22
  # frame = [header|blue|green|red],
20
- @led_frame_blue_offset = opts[:led_frame_blue_offset] || 1
21
- @led_frame_green_offset = opts[:led_frame_green_offset] || 2
22
- @led_frame_red_offset = opts[:led_frame_red_offset] || 3
23
+ @led_frame_rgb_offsets = opts[:led_frame_rgb_offsets] || { red: 3, green: 2, blue: 1 }
23
24
 
24
25
  @led_frames = []
25
26
  @end_frame = [0x00] * (@num_leds / 2.0).ceil
26
27
 
27
- # reset all pixels to off
28
- (0..@num_leds).each do |l|
29
- self.set_pixel_color(l, 0x00)
30
- end
31
- self.show!
28
+ clear!
32
29
  end
33
30
 
34
31
  # Sets color of a single pixel at specified position in the strip
35
32
  # accepts an array of [r,g,b] values or a single 3 byte value
36
- def set_pixel_color(pos, color)
33
+ def set_pixel(pos, color, brightness = @brightness)
37
34
  idx = pos * 4
38
35
  if color.is_a?(Integer)
39
- @led_frames[idx] = led_frame_hdr
40
- @led_frames[idx + @led_frame_red_offset] = (color & 0xFF0000) >> 16
41
- @led_frames[idx + @led_frame_green_offset] = (color & 0x00FF00) >> 8
42
- @led_frames[idx + @led_frame_blue_offset] = (color & 0x0000FF)
36
+ @led_frames[idx] = led_frame_hdr(brightness)
37
+ @led_frames[idx + @led_frame_rgb_offsets[:red]] = (color & 0xFF0000) >> 16
38
+ @led_frames[idx + @led_frame_rgb_offsets[:green]] = (color & 0x00FF00) >> 8
39
+ @led_frames[idx + @led_frame_rgb_offsets[:blue]] = (color & 0x0000FF)
43
40
  elsif color.is_a?(Array)
44
- @led_frames[idx] = led_frame_hdr
45
- @led_frames[idx + @led_frame_red_offset] = color[0]
46
- @led_frames[idx + @led_frame_green_offset] = color[1]
47
- @led_frames[idx + @led_frame_blue_offset] = color[2]
41
+ @led_frames[idx] = led_frame_hdr(brightness)
42
+ @led_frames[idx + @led_frame_rgb_offsets[:red]] = color[0]
43
+ @led_frames[idx + @led_frame_rgb_offsets[:green]] = color[1]
44
+ @led_frames[idx + @led_frame_rgb_offsets[:blue]] = color[2]
48
45
  else
49
46
  raise 'Invalid color'
50
47
  end
51
48
  end
52
49
 
50
+ def set_pixel!(pos, color, brightness = @brightness)
51
+ set_pixel(pos, color, brightness)
52
+ show!
53
+ end
54
+
55
+ def clear!
56
+ @num_leds.times do |l|
57
+ set_pixel(l, 0)
58
+ end
59
+ show!
60
+ end
61
+
53
62
  # Writes out the led frames to the strip
54
63
  def show!
55
64
  Spi.begin do |s|
@@ -61,8 +70,8 @@ module Apa102Rbpi
61
70
  private
62
71
 
63
72
  # First 3 bits high, then 5 bits for brightness
64
- def led_frame_hdr
65
- (@brightness & 0b00011111) | 0b11100000
73
+ def led_frame_hdr(brightness)
74
+ (brightness & 0b00011111) | 0b11100000
66
75
  end
67
76
  end
68
77
  end
@@ -0,0 +1,31 @@
1
+ module Apa102Rbpi
2
+ # Input a 1-byte value to get a color
3
+ # Colors are from r->g->b->r
4
+ def color_wheel(wheel_pos)
5
+ wheel_pos &= 255
6
+
7
+ if wheel_pos < 85
8
+ rgb_to_hex(255 - wheel_pos * 3, wheel_pos * 3, 0)
9
+ elsif wheel_pos < 170
10
+ wheel_pos -= 85
11
+ rgb_to_hex(0, 255 - wheel_pos * 3, wheel_pos * 3)
12
+ else
13
+ wheel_pos -= 170
14
+ rgb_to_hex(wheel_pos * 3, 0, 255 - wheel_pos * 3)
15
+ end
16
+ end
17
+
18
+ # convert to a 3-byte hex color value
19
+ def rgb_to_hex(red, green, blue)
20
+ (red << 16) + (green << 8) + blue
21
+ end
22
+
23
+ # convert 3-byte hex color to [red, green, blue]
24
+ def hex_to_rgb(hex)
25
+ [
26
+ (hex & 0xFF0000) >> 16,
27
+ (hex & 0x00FF00) >> 8,
28
+ (hex & 0x0000FF)
29
+ ]
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Apa102Rbpi
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0.pre'
3
3
  end
data/lib/apa102_rbpi.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'apa102_rbpi/apa102'
2
+ require 'apa102_rbpi/utils'
2
3
  require 'apa102_rbpi/version'
3
4
 
4
5
  module Apa102Rbpi
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apa102_rbpi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - matl33t
@@ -45,7 +45,7 @@ dependencies:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.9'
48
- type: :development
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -62,6 +62,7 @@ files:
62
62
  - ".gitignore"
63
63
  - CHANGELOG.md
64
64
  - Gemfile
65
+ - Gemfile.lock
65
66
  - LICENSE.txt
66
67
  - README.md
67
68
  - Rakefile
@@ -70,6 +71,7 @@ files:
70
71
  - bin/setup
71
72
  - lib/apa102_rbpi.rb
72
73
  - lib/apa102_rbpi/apa102.rb
74
+ - lib/apa102_rbpi/utils.rb
73
75
  - lib/apa102_rbpi/version.rb
74
76
  homepage:
75
77
  licenses:
@@ -87,9 +89,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
89
  version: '0'
88
90
  required_rubygems_version: !ruby/object:Gem::Requirement
89
91
  requirements:
90
- - - ">="
92
+ - - ">"
91
93
  - !ruby/object:Gem::Version
92
- version: '0'
94
+ version: 1.3.1
93
95
  requirements: []
94
96
  rubyforge_project:
95
97
  rubygems_version: 2.2.3