Add examples: ADC, Digital-out, Fix Digital-in; Add shift_register class

This commit is contained in:
jonny 2025-01-24 11:27:03 +01:00
parent 335490b185
commit 4abdc2f565
5 changed files with 142 additions and 1 deletions

View File

@ -0,0 +1,20 @@
# Include external libraries
import os
import sys
import time
from mcp3208 import MCP3208
# Include custom files
# Add the parent directory to the module search path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from interface_board_libs.adc_mcp3208 import MCP3208
from interface_board_pins import ADC_CHANNELS
adc = MCP3208()
while True:
for i in range(8):
print('ADC[{}]: {:.2f}'.format(i, adc.read(i)))
time.sleep(0.5)

View File

@ -1,5 +1,6 @@
# Add the parent directory to the module search path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
# Include the common pin assignment (from parent folder)
from interface_board_pins.py import GPIO_DIGITAL_INPUTS # map of which GPIO pins are associated with which input terminal (1-8)
import RPi.GPIO as GPIO
@ -17,7 +18,7 @@ for pin in DIGITAL_INPUTS.values():
print("Reading digital inputs:")
try:
while True:
for label, pin in DIGITAL_INPUTS.items():
for label, pin in GPIO_DIGITAL_INPUTS.items():
state = GPIO.input(pin)
print(f"Input {label} (GPIO {pin}): {'HIGH' if state else 'LOW'}")
print("-" * 40)

View File

@ -0,0 +1,66 @@
# Include external libraries
import os
import sys
from time import sleep
# Add the parent directory to the module search path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
# Include custom files
from interface_board_libs.shift_register import ShiftRegister # custom shift register class
from interface_board_pins import ( # pin / channel assignment
GPIO_SHIFT_REG_DATA,
GPIO_SHIFT_REG_LATCH,
GPIO_SHIFT_REG_CLOCK,
SHIFT_REG_CHANNEL_BUZZER,
SHIFT_REG_CHANNEL_RELAY1,
SHIFT_REG_CHANNEL_RELAY2,
)
# Initialize the shift register
sr = ShiftRegister(GPIO_SHIFT_REG_DATA, GPIO_SHIFT_REG_LATCH, GPIO_SHIFT_REG_CLOCK)
try:
print("Writing to shift register...")
# repeatedly write to shift register
while True:
# # Cycle through all combinations of pin states (write entire byte)
# print("Writing all byte values (0-255)...")
# for value in range(256):
# sr.write_byte(value) # Write the current value
# print(f"Output: {bin(value)}") # Print binary representation
# sleep(0.05) # Delay between each byte
# Turn each pin on and off one by one
print("\nToggling each pin one by one...")
# channels 0-4 are connected to terminal only
for pin in range(4): # 0-4 are connected to terminal only
print(f"Setting pin {pin} HIGH.")
sr.set_pin(pin, True) # Set the pin HIGH
sleep(0.5)
print(f"Setting pin {pin} LOW.")
sr.set_pin(pin, False) # Set the pin LOW
sleep(0.5)
# channels 5-7 are connected to terminal AND buzzer/relays
print("Activating buzzer...")
sr.set_pin(SHIFT_REG_CHANNEL_BUZZER, True) # Turn buzzer ON
sleep(0.5)
sr.set_pin(SHIFT_REG_CHANNEL_BUZZER, False) # Turn buzzer OFF
print("Toggling Relay 1...")
sr.toggle_pin(SHIFT_REG_CHANNEL_RELAY1)
sleep(0.5)
sr.toggle_pin(SHIFT_REG_CHANNEL_RELAY1)
print("Activating Relay 2...")
sr.set_pin(SHIFT_REG_CHANNEL_RELAY2, True) # Turn relay ON
sleep(0.5)
sr.set_pin(SHIFT_REG_CHANNEL_RELAY2, False) # Turn relay OFF
finally:
sr.clear() # Clear the shift register
print("Shift register cleared.")
GPIO.cleanup() # Clean up GPIO settings

View File

@ -0,0 +1,49 @@
import RPi.GPIO as GPIO
from time import sleep
class ShiftRegister:
def __init__(self, data_pin, latch_pin, clock_pin):
self.data_pin = data_pin
self.latch_pin = latch_pin
self.clock_pin = clock_pin
self.current_byte = 0 # Tracks the current state of the shift register
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.data_pin, GPIO.OUT)
GPIO.setup(self.latch_pin, GPIO.OUT)
GPIO.setup(self.clock_pin, GPIO.OUT)
def write_byte(self, byte):
"""Writes an 8-bit value to the shift register."""
GPIO.output(self.latch_pin, 0)
for i in range(8):
GPIO.output(self.clock_pin, 0)
GPIO.output(self.data_pin, (byte >> (7 - i)) & 1) # MSB first
GPIO.output(self.clock_pin, 1)
GPIO.output(self.latch_pin, 1)
self.current_byte = byte # Update the internal state
def clear(self):
"""Clears the shift register (sets all outputs to 0)."""
self.write_byte(0)
def set_pin(self, pin, state):
"""
Sets an individual pin to HIGH (1) or LOW (0).
Pins are numbered 0-7, with 0 being the MSB.
"""
if not 0 <= pin <= 7:
raise ValueError("Pin must be in range 0-7.")
if state:
self.current_byte |= (1 << (7 - pin)) # Set the bit to 1
else:
self.current_byte &= ~(1 << (7 - pin)) # Set the bit to 0
self.write_byte(self.current_byte)
def toggle_pin(self, pin):
"""Toggles the state of an individual pin."""
if not 0 <= pin <= 7:
raise ValueError("Pin must be in range 0-7.")
self.current_byte ^= (1 << (7 - pin)) # Flip the bit
self.write_byte(self.current_byte)

View File

@ -23,6 +23,11 @@ GPIO_SHIFT_REG_DATA = 27
GPIO_SHIFT_REG_LATCH = 17
GPIO_SHIFT_REG_CLOCK = 4
# Shift Register Channel Assignments
SHIFT_REG_CHANNEL_BUZZER = 7 # Buzzer connected to shift register channel 7
SHIFT_REG_CHANNEL_RELAY1 = 6 # Relay 1 connected to shift register channel 6
SHIFT_REG_CHANNEL_RELAY2 = 5 # Relay 2 connected to shift register channel 5