Skip to content

Commit

Permalink
Update a bunch of LED examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Oct 1, 2024
1 parent 81240c8 commit c75c4bb
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 20 deletions.
4 changes: 3 additions & 1 deletion examples/led/builtin_blink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
require 'bundler/setup'
require 'denko'

PIN = :LED_BUILTIN

board = Denko::Board.new(Denko::Connection::Serial.new)
led = Denko::LED.new(board: board, pin: :LED_BUILTIN)
led = Denko::LED.new(board: board, pin: PIN)

led.blink 0.5

Expand Down
4 changes: 3 additions & 1 deletion examples/led/builtin_fade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
require 'bundler/setup'
require 'denko'

PIN = :LED_BUILTIN

board = Denko::Board.new(Denko::Connection::Serial.new)
led = Denko::LED.new(board: board, pin: :LED_BUILTIN)
led = Denko::LED.new(board: board, pin: PIN)

min = 0
max = 100
Expand Down
31 changes: 31 additions & 0 deletions examples/led/rgb_led.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# A standard 3 pin RGB LED (not single pin addressable), on 3 PWM pins.
#
require 'bundler/setup'
require 'denko'

RED_PIN = 11
GREEN_PIN = 10
BLUE_PIN = 9

board = Denko::Board.new(Denko::Connection::Serial.new)
rgb_led = Denko::LED::RGB.new board: board,
pins: {red: RED_PIN, green: GREEN_PIN, blue: BLUE_PIN}

# Set these predefined colors with symbols.
[:red, :green, :blue, :cyan, :yellow, :magenta, :white, :off].each do |color|
rgb_led.color = color
sleep 0.5
end

# Set duty cycle for each "sub LED".
loop do
rgb_led.red.duty = 100
rgb_led.green.duty = 50
rgb_led.blue.duty = 0
sleep 0.5
rgb_led.red.duty = 100
rgb_led.green.duty = 0
rgb_led.blue.duty = 50
sleep 0.5
end
6 changes: 3 additions & 3 deletions examples/led/ws2812_bounce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
require 'bundler/setup'
require 'denko'

WS2812_PIN = 4
PIXELS = 8
PIN = 4
PIXELS = 8

RED = [255, 0, 0]
GREEN = [0, 255, 0]
Expand All @@ -16,7 +16,7 @@
positions = (0..PIXELS-1).to_a + (1..PIXELS-2).to_a.reverse

board = Denko::Board.new(Denko::Connection::Serial.new)
strip = Denko::LED::WS2812.new(board: board, pin: WS2812_PIN, length: PIXELS)
strip = Denko::LED::WS2812.new(board: board, pin: PIN, length: PIXELS)

loop do
COLORS.each do |color|
Expand Down
5 changes: 3 additions & 2 deletions examples/led/ws2812_builtin_blink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
require 'bundler/setup'
require 'denko'

PIN = :LED_BUILTIN
WHITE = [255, 255, 255]
OFF = [0, 0, 0]
OFF = [0, 0, 0]

board = Denko::Board.new(Denko::Connection::Serial.new)
strip = Denko::LED::WS2812.new(board: board, pin: :LED_BUILTIN, length: 1)
strip = Denko::LED::WS2812.new(board: board, pin: PIN, length: 1)

loop do
strip[0] = WHITE
Expand Down
6 changes: 3 additions & 3 deletions examples/led/ws2812_fade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
require 'bundler/setup'
require 'denko'

WS2812_PIN = 4
PIXELS = 8
PIN = 4
PIXELS = 8

board = Denko::Board.new(Denko::Connection::Serial.new)
strip = Denko::LED::WS2812.new(board: board, pin: WS2812_PIN, length: PIXELS)
strip = Denko::LED::WS2812.new(board: board, pin: PIN, length: PIXELS)

min = 0
max = 255
Expand Down
10 changes: 8 additions & 2 deletions lib/denko/led/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ module Denko
module LED
class Base < PulseIO::PWMOutput
def blink(interval=0.5)
threaded_loop do
self.blink_interval = interval
threaded_loop do
toggle
sleep interval
sleep @blink_interval
end
end

def blink_interval=(interval)
@blink_interval = interval
end
attr_reader :blink_interval
end
end
end
3 changes: 2 additions & 1 deletion test/led/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ def test_led_new_creates_base_led
assert_equal Denko::LED::Base, part.class
end

def test_blink_runs_in_thread
def test_blink_runs_in_thread_and_sets_ivar
mock = Minitest::Mock.new.expect :call, nil
part.stub(:threaded_loop, mock) do
part.blink(0.5)
end
mock.verify
assert_equal 0.5, part.blink_interval
end
end
5 changes: 3 additions & 2 deletions tutorial/02-button/button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# listening for changes to the physical button. When that happens, our button
# object is notified. To catch these notifications, we have to use a callback.
# button.add_callback saves a block of code to run each time the button state changes.
#
#
# The callback below checks if the state is 0 (the button went from up to down),
# then counts and prints the number of times pressed.
#
Expand All @@ -38,6 +38,7 @@
end

# Wait for the button to be pressed 3 times.
puts "Press button 3 times to continue..."
sleep 0.005 while presses < 3

# Button keeps its callbacks in a Hash. We can be specific and use keys to add and remove.
Expand All @@ -56,7 +57,7 @@
#
led = Denko::LED.new(board: board, pin: 13)

button.up { led.off }
button.up { led.off }
button.down { led.on }

puts "Press the button to turn on the LED... (Ctrl+C to exit)"
Expand Down
14 changes: 9 additions & 5 deletions tutorial/03-potentiometer/potentiometer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pins. On most boards these pins start with 'A' and can be given as strings.
#
# See potentiometer.pdf in this folder for a hook-up diagram.
#
#
potentiometer = Denko::AnalogIO::Potentiometer.new(pin: 'A0', board: board)

#
Expand All @@ -33,7 +33,7 @@
# The default resolution for Analog Input is 10-bits, so you should have seen
# values from 0 - 1023. We can use the value to control the blinking
# speed of the LED from the earlier example.
#
#
led = Denko::LED.new(board: board, pin: 13)

# Helper method to calculate the blink time.
Expand All @@ -46,17 +46,21 @@ def map_pot_value(value)
k = 5
linearized = (fraction * (k + 1)) / ((k * fraction) + 1)
# Use this for linear potentiometers instead.
# linearized = fraction
# linearized = fraction

# Map to the 0.1 to 0.5 seconds range in reverse. Clockwise = faster.
0.5 - (linearized * 0.4)
end

# Start blinking with interval of 0.3
led.blink(0.3)

# Callback that calculates the blink interval and tells the LED.
potentiometer.on_change do |value|
interval = map_pot_value(value)
print "LED blink interval: #{interval.round(4)} seconds \r"
led.blink(interval)
# Update the LED's blink interval without stopping the blink.
led.blink_interval = interval
end
puts "Turn potentiometer to control the LED blink. Press Ctrl+C to exit..."
sleep

0 comments on commit c75c4bb

Please sign in to comment.