Top Level Namespace

Constant Summary collapse

DAT =
23
CLK =
24
NUM_PIXELS =
8
BRIGHTNESS =
7

Instance Method Summary collapse

Instance Method Details

#_eofObject

Emit exactly enough clock pulses to latch the small dark die APA102s which are weird for some reason it takes 36 clocks, the other IC takes just 4 (number of pixels/2)



47
48
49
50
51
52
53
# File 'lib/blinkt.rb', line 47

def _eof
  RPi::GPIO.set_low DAT
  (0..35).each do |_x|
    RPi::GPIO.set_high CLK
    RPi::GPIO.set_low CLK
  end
end

#_sofObject



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

def _sof
  RPi::GPIO.set_low DAT
  (0..31).each do |_x|
    RPi::GPIO.set_high CLK
    RPi::GPIO.set_low CLK
  end
end

#_write_byte(byte) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/blinkt.rb', line 32

def _write_byte(byte)
  (0..7).each do |_x|
    if byte & 0b10000000 > 0
      RPi::GPIO.set_high DAT
    else
      RPi::GPIO.set_low DAT
    end
    RPi::GPIO.set_high CLK
    byte <<= 1
    RPi::GPIO.set_low CLK
  end
end

#clearObject

Clear the pixel buffer



26
27
28
29
30
# File 'lib/blinkt.rb', line 26

def clear
  (0..(NUM_PIXELS - 1)).each do |x|
    $pixels[x][0..2] = [0, 0, 0]
  end
end

#set_all(r, g, b, brightness = nil) ⇒ Object

Set the RGB value and optionally brightness of all pixels If you don’t supply a brightness value, the last value set for each pixel be kept. Params:

r

Amount of red: 0 to 255

g

Amount of green: 0 to 255

b

Amount of blue: 0 to 255

brightness

Brightness: 0.0 to 1.0 (default around 0.2)



94
95
96
97
98
# File 'lib/blinkt.rb', line 94

def set_all(r, g, b, brightness = nil)
  (0..(NUM_PIXELS - 1)).each do |x|
    set_pixel(x, r, g, b, brightness)
  end
end

#set_brightness(brightness) ⇒ Object

Set the brightness of all pixels Params:

brightness

Brightness: 0.0 to 1.0



19
20
21
22
23
# File 'lib/blinkt.rb', line 19

def set_brightness(brightness)
  (0..NUM_PIXELS - 1).each do |x|
    $pixels[x][3] = (31.0 * brightness).to_i & 0b11111
  end
end

#set_clear_on_exit(value = true) ⇒ Object

Set whether Blinkt! should be cleared upon exit

By default Blinkt! will turn off the pixels on exit, but calling

blinkt.set_clear_on_exit(False)

Will ensure that it does not. Params:

value

true or false (default true)



124
125
126
# File 'lib/blinkt.rb', line 124

def set_clear_on_exit(value = true)
  $_clear_on_exit = value
end

#set_pixel(x, r, g, b, brightness = nil) ⇒ Object

Set the RGB value, and optionally brightness, of a single pixel If you don’t supply a brightness value, the last value will be kept. Params:

x

The horizontal position of the pixel: 0 to 7

r

Amount of red: 0 to 255

g

Amount of green: 0 to 255

b

Amount of blue: 0 to 255

brightness

Brightness: 0.0 to 1.0 (default around 0.2)



108
109
110
111
112
113
114
115
116
# File 'lib/blinkt.rb', line 108

def set_pixel(x, r, g, b, brightness = nil)
  brightness = if brightness.nil?
                 $pixels[x][3]
               else
                 (31.0 * brightness).to_i & 0b11111
               end

  $pixels[x] = [r.to_i & 0xff, g.to_i & 0xff, b.to_i & 0xff, brightness]
end

#showObject

Output the buffer to Blinkt!



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/blinkt.rb', line 64

def show
  unless $_gpio_setup
    RPi::GPIO.set_numbering :bcm
    RPi::GPIO.set_warnings true
    RPi::GPIO.setup DAT, as: :output
    RPi::GPIO.setup CLK, as: :output
    $_gpio_setup = true
  end

  _sof

  $pixels.each do |pixel|
    r, g, b, brightness = pixel
    _write_byte(0b11100000 | brightness)
    _write_byte(b)
    _write_byte(g)
    _write_byte(r)
  end

  _eof
end