Sha256: 31229ace899bf933bc41c772b743283922da9e9adcd9f0be07bed01f85589410

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

#
# Example of a Button connected through an input shift register (CD4021B).
# Can be used over either a bit bang or hardware SPI interface.
#
require 'bundler/setup'
require 'denko'

# SPI pins (on board)
SPI_BIT_BANG_PINS   = { clock: 13, input: 12 }
REGISTER_SELECT_PIN = 9

# Button pin (on register parallel outputs)
BUTTON_PIN = 0

board = Denko::Board.new(Denko::Connection::Serial.new)

# 1-way (input) bit bang SPI interface on any pins (slower, but flexible).
bus = Denko::SPI::BitBang.new(board: board, pins: SPI_BIT_BANG_PINS)

# Use the default hardware SPI bus (faster, but predetermined pins).
# bus = Denko::SPI::Bus.new(board: board)

# Show the hardware SPI pins to aid connection.
# MOSI = output | MISO = input | SCK = clock
# puts board.map.select { |name, number| [:MOSI, :MISO, :SCK].include?(name) }

# InputRegister needs a bus and its select pin. The CD4021 likes SPI mode 2.
# Other options and their defaults:
#     bytes:          1          - For daisy-chaining registers
#     spi_frequency:  1000000    - Only affects hardware SPI interfaces
#     spi_mode:       0
#     spi_bit_order:  :msbfirst
#
register = Denko::SPI::InputRegister.new(bus: bus, pin: REGISTER_SELECT_PIN, spi_mode: 2)

# InputRegister implements enough of the Board interface that digital input
# components can treat it as a Board. Do that with the Button.
#
# button starts listening automatically, which triggers register to start listening,
# so it can update button as needed. Registers listen with an 8ms interval by default,
# compared to the 4ms default for a Button directly connected to a Board.
#
button = Denko::DigitalIO::Button.new(pin: 0, board: register)

# Button callbacks.
button.down { puts "Button pressed"  }
button.up   { puts "Button released" }

# Sleep the main thread. Press the button and callbacks will run.
sleep

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
denko-0.13.6 examples/spi/input_register.rb
denko-0.13.5 examples/spi/input_register.rb
denko-0.13.4 examples/spi/input_register.rb
denko-0.13.3 examples/spi/input_register.rb
denko-0.13.2 examples/spi/input_register.rb
denko-0.13.1 examples/spi/input_register.rb
denko-0.13.0 examples/spi/input_register.rb