Skip to content

Commit

Permalink
Update PulseIO examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Oct 2, 2024
1 parent 461ab83 commit 8a1fb46
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions examples/pulse_io/buzzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
require 'bundler/setup'
require 'denko'

board = Denko::Board.new(Denko::Connection::Serial.new)
buzzer = Denko::PulseIO::Buzzer.new(board: board, pin: 9)
PIN = 9

board = Denko::Board.new(Denko::Connection::Serial.new)
buzzer = Denko::PulseIO::Buzzer.new(board: board, pin: PIN)

C4 = 262
D4 = 294
Expand All @@ -17,13 +19,16 @@
[E4, 1], [D4, 1], [C4, 1], [D4, 1], [E4, 1], [E4, 1], [E4, 1], [E4, 1],
[D4, 1], [D4, 1], [E4, 1], [D4, 1], [C4, 4],
]

bpm = 240
beat_time = 60.to_f / bpm

notes.each do |note|
buzzer.tone(note[0])
sleep note[1] * beat_time

# Stop for a bit so notes are properly defined.
buzzer.stop; sleep 0.010
end

buzzer.stop
Expand Down
4 changes: 3 additions & 1 deletion examples/pulse_io/ir_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
require 'bundler/setup'
require 'denko'

PIN = 4

# Note: If testing with 2 boards on one computer, be explicit about which serial device
# runs denko. Use the second Serial.new call below and modify device: as needed.
# Monitor the receiver board in the Arduino (or some other) serial monitor.
Expand All @@ -31,7 +33,7 @@
# with other hardware or libraries. Try different pins. For more info:
# https://github.com/Arduino-IRremote/Arduino-IRremote?#timer-and-pin-usage
#
ir = Denko::PulseIO::IROutput.new(board: board, pin: 4)
ir = Denko::PulseIO::IROutput.new(board: board, pin: PIN)

# NEC Raw-Data=0xF708FB04. LSBFIRST, so the binary for each hex digit below is backward.
code = [ 9000, 4500, # Start bit
Expand Down
7 changes: 5 additions & 2 deletions examples/pulse_io/pwm_output.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#
# Example configuring PWM frequency and resolution. Only on ESP32 for now.
# PWM (pulse width modulation) output demo.
# Frequency and resolution are configurable (only on ESP32 and PiBoard/Linux for now).
# Capture output with logic analyzer or oscilloscope to verify accuracy.
#
require 'bundler/setup'
require 'denko'

PIN = 2

board = Denko::Board.new(Denko::Connection::Serial.new)
pwm = Denko::PulseIO::PWMOutput.new(board: board, pin: 2)
pwm = Denko::PulseIO::PWMOutput.new(board: board, pin: PIN)

# Resolution test: ~1% duty at 1 kHz / 10-bit.
# On and off approx. 10us and 990us respectively.
Expand Down

0 comments on commit 8a1fb46

Please sign in to comment.