blinkt 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/blinkt.rb +134 -0
  3. metadata +63 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d393362a86eba555d554ef63eae018094e40a6b8
4
+ data.tar.gz: a6d807d01040bba22ef9616f7924f9d900281439
5
+ SHA512:
6
+ metadata.gz: e87d539505cab957cfad3698b6dadbfac85af5f5292ae1b6285b6ed7a572d916e28eb79207ef9e7a2b41d42ec3d60c8c9d03abca9de662f0ce623a3093c3fc57
7
+ data.tar.gz: 9ecd7ced85f719de502bfdd26a290a4b81ebccb622a3e0736e9f85b3959115ad1e74b5290a4b04a794b0c259af1eaec829510cca135c841332393f6e09259b39
@@ -0,0 +1,134 @@
1
+ # Ported from the Pimoroni Blinkt Python library
2
+ # - https://github.com/pimoroni/blinkt
3
+
4
+ require 'rpi_gpio'
5
+
6
+ DAT = 23
7
+ CLK = 24
8
+ NUM_PIXELS = 8
9
+ BRIGHTNESS = 7
10
+
11
+ $pixels = [[0, 0, 0, BRIGHTNESS]] * 8
12
+
13
+ $_gpio_setup = false
14
+ $_clear_on_exit = true
15
+
16
+ # Set the brightness of all pixels
17
+ # Params:
18
+ # +brightness+:: Brightness: 0.0 to 1.0
19
+ def set_brightness(brightness)
20
+ (0..NUM_PIXELS - 1).each do |x|
21
+ $pixels[x][3] = (31.0 * brightness).to_i & 0b11111
22
+ end
23
+ end
24
+
25
+ # Clear the pixel buffer
26
+ def clear
27
+ (0..(NUM_PIXELS - 1)).each do |x|
28
+ $pixels[x][0..2] = [0, 0, 0]
29
+ end
30
+ end
31
+
32
+ def _write_byte(byte)
33
+ (0..7).each do |_x|
34
+ if byte & 0b10000000 > 0
35
+ RPi::GPIO.set_high DAT
36
+ else
37
+ RPi::GPIO.set_low DAT
38
+ end
39
+ RPi::GPIO.set_high CLK
40
+ byte <<= 1
41
+ RPi::GPIO.set_low CLK
42
+ end
43
+ end
44
+
45
+ # Emit exactly enough clock pulses to latch the small dark die APA102s which are weird
46
+ # for some reason it takes 36 clocks, the other IC takes just 4 (number of pixels/2)
47
+ def _eof
48
+ RPi::GPIO.set_low DAT
49
+ (0..35).each do |_x|
50
+ RPi::GPIO.set_high CLK
51
+ RPi::GPIO.set_low CLK
52
+ end
53
+ end
54
+
55
+ def _sof
56
+ RPi::GPIO.set_low DAT
57
+ (0..31).each do |_x|
58
+ RPi::GPIO.set_high CLK
59
+ RPi::GPIO.set_low CLK
60
+ end
61
+ end
62
+
63
+ # Output the buffer to Blinkt!
64
+ def show
65
+ unless $_gpio_setup
66
+ RPi::GPIO.set_numbering :bcm
67
+ RPi::GPIO.set_warnings true
68
+ RPi::GPIO.setup DAT, as: :output
69
+ RPi::GPIO.setup CLK, as: :output
70
+ $_gpio_setup = true
71
+ end
72
+
73
+ _sof
74
+
75
+ $pixels.each do |pixel|
76
+ r, g, b, brightness = pixel
77
+ _write_byte(0b11100000 | brightness)
78
+ _write_byte(b)
79
+ _write_byte(g)
80
+ _write_byte(r)
81
+ end
82
+
83
+ _eof
84
+ end
85
+
86
+ # Set the RGB value and optionally brightness of all pixels
87
+ # If you don't supply a brightness value, the last value set
88
+ # for each pixel be kept.
89
+ # Params:
90
+ # +r+:: Amount of red: 0 to 255
91
+ # +g+:: Amount of green: 0 to 255
92
+ # +b+:: Amount of blue: 0 to 255
93
+ # +brightness+:: Brightness: 0.0 to 1.0 (default around 0.2)
94
+ def set_all(r, g, b, brightness = nil)
95
+ (0..(NUM_PIXELS - 1)).each do |x|
96
+ set_pixel(x, r, g, b, brightness)
97
+ end
98
+ end
99
+
100
+ # Set the RGB value, and optionally brightness, of a single pixel
101
+ # If you don't supply a brightness value, the last value will be kept.
102
+ # Params:
103
+ # +x+:: The horizontal position of the pixel: 0 to 7
104
+ # +r+:: Amount of red: 0 to 255
105
+ # +g+:: Amount of green: 0 to 255
106
+ # +b+:: Amount of blue: 0 to 255
107
+ # +brightness+:: Brightness: 0.0 to 1.0 (default around 0.2)
108
+ def set_pixel(x, r, g, b, brightness = nil)
109
+ brightness = if brightness.nil?
110
+ $pixels[x][3]
111
+ else
112
+ (31.0 * brightness).to_i & 0b11111
113
+ end
114
+
115
+ $pixels[x] = [r.to_i & 0xff, g.to_i & 0xff, b.to_i & 0xff, brightness]
116
+ end
117
+
118
+ # Set whether Blinkt! should be cleared upon exit
119
+ # By default Blinkt! will turn off the pixels on exit, but calling::
120
+ # blinkt.set_clear_on_exit(False)
121
+ # Will ensure that it does not.
122
+ # Params:
123
+ # +value+:: true or false (default true)
124
+ def set_clear_on_exit(value = true)
125
+ $_clear_on_exit = value
126
+ end
127
+
128
+ at_exit do
129
+ if $_clear_on_exit
130
+ clear
131
+ show
132
+ end
133
+ RPi::GPIO.clean_up
134
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blinkt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - John McCabe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rpi_gpio
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.3.0
33
+ description: Ruby Library for Blinkt; 8 APA102 LEDs for your Raspberry Pi https://shop.pimoroni.com/products/blinkt
34
+ email: john@johnmccabe.net
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/blinkt.rb
40
+ homepage: https://github.com/johnmccabe/blinkt-ruby
41
+ licenses: []
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.5.1
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Pimoroni Blinkt
63
+ test_files: []