Sha256: 23c91b8510d3bae4f7da5a764ad2b77a4b2e052e7c5256d0a2e5add5ceab974a

Contents?: true

Size: 1.57 KB

Versions: 7

Compression:

Stored size: 1.57 KB

Contents

#
# Example of 2 SPI devices on the same bus with different select pins.
# Combination of input_register.rb and output_register.rb
#
require 'bundler/setup'
require 'denko'

# SPI pins (on board)
SPI_BIT_BANG_PINS   = { clock: 13, input: 12, output: 11 }
OUT_REGISTER_SELECT = 10
IN_REGISTER_SELECT  = 9

# LED and Button pins (on their respective registers' parallel pins)
LED_PIN     = 0
BUTTON_PIN  = 0

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

# 2-way bit bang SPI bus (slower, but use any pins).
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) }

# OutputRegister needs a bus and its select pin.
out_register = Denko::SPI::OutputRegister.new(bus: bus, pin: OUT_REGISTER_SELECT)

# InputRegister needs a bus and its select pin. The CD4021 likes SPI mode 2.
in_register = Denko::SPI::InputRegister.new(bus: bus, pin: IN_REGISTER_SELECT, spi_mode: 2)

# LED connected to the output register.
led = Denko::LED.new(board: out_register, pin: LED_PIN)                                 

# Button connected to the input register.
button = Denko::DigitalIO::Button.new(board: in_register, pin: BUTTON_PIN)

# Button callbacks.
button.down { led.on;  puts "Button pressed"  }
button.up   { led.off; 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/two_registers.rb
denko-0.13.5 examples/spi/two_registers.rb
denko-0.13.4 examples/spi/two_registers.rb
denko-0.13.3 examples/spi/two_registers.rb
denko-0.13.2 examples/spi/two_registers.rb
denko-0.13.1 examples/spi/two_registers.rb
denko-0.13.0 examples/spi/two_registers.rb