Last active
August 29, 2015 14:01
-
-
Save ericboehs/9a388095c16dc7c1fec0 to your computer and use it in GitHub Desktop.
Shifting an 74HC595 (595) 8-bit shift register on a Raspberry Pi using Pi Piper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# To get pi_piper on Raspbian: | |
# | |
# apt-get install ruby-dev | |
# gem install pi_piper --no-ri --no-rdoc | |
# gem install rb-inotify --no-ri --no-rdoc | |
# | |
# These pins work but send a weird signal at boot. | |
# | |
# This script will take a string array and output it to the outputs on the shift register | |
# | |
# "0" = OFF | |
# | |
# "1" = ON | |
# | |
# The pins on the shift register should be set up as follows on the RPi | |
# (Shift Register 74HC595) | |
# | |
# Ds <-> GPIO 24 | |
# | |
# SHCP <-> GPIO 4 | |
# | |
# STCP <-> GPIO 25 | |
# | |
# !MR <-> Vcc (+5V) | |
# !OE <-> Gnd | |
require 'pi_piper' | |
require 'rb-inotify' | |
NUMBER_OF_OUTPUTS = 16 | |
PINS = { | |
data: PiPiper::Pin.new(pin: 24, direction: :out), | |
clock: PiPiper::Pin.new(pin: 4, direction: :out), | |
latch: PiPiper::Pin.new(pin: 25, direction: :out), | |
} | |
def shift(key) | |
PINS[key].on | |
PINS[key].off | |
end | |
def reset | |
PINS.values.each{|pin| pin.off } | |
end | |
def watch_shift_file | |
shift_state_filename = '/etc/shifter/shift_state.txt' | |
send_bits File.open(shift_state_filename, 'r'){|f| f.read } | |
notifier = INotify::Notifier.new | |
notifier.watch(shift_state_filename, :modify) do | |
send_bits File.open(shift_state_filename, 'r'){|f| f.read } | |
end | |
notifier.run | |
end | |
def send_bits(data) | |
reset | |
NUMBER_OF_OUTPUTS.times do |byte| | |
PINS[:data].on if data[byte] == "1" | |
shift :clock | |
PINS[:data].off | |
end | |
shift :latch | |
end | |
if ARGV[0] | |
send_bits ARGV[0] | |
else | |
watch_shift_file | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment