raspberry pi gpio input test script

This commit is contained in:
ohsqueezy 2022-11-04 17:49:08 -04:00
parent 0f0f771ea8
commit f68fb897c5
2 changed files with 80 additions and 1 deletions

6
NS.py
View File

@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-
# [SCRAPEBOARD] is an arcade game in development by [@diskmem] & [@snakesandrews]
# [SCRAPEBOARD] is an arcade game in development by [@diskmem] & [@snakesandrews].
#
# It requires custom hardware to play but can be tested in keyboard mode without
# the hardware. For more information on setting up and running the game, see
# README.md, or for the game in general, visit <https://scrape.nugget.fun/>.
#
# Full open source code is available at <https://git.nugget.fun/scrape/scrapeboard>.
#
# This is the main file containing all the pygame code.
import argparse, pathlib, operator, subprocess, sys
from random import randint, choice, random

75
gpio_test.py Normal file
View File

@ -0,0 +1,75 @@
# [SCRAPEBOARD] is an arcade game in development by [@diskmem] & [@snakesandrews]
#
# It requires custom hardware to play but can be tested in keyboard mode without
# the hardware. For more information on setting up and running the game, see
# README.md, or for the game in general, visit <https://scrape.nugget.fun/>.
#
# Full open source code is available at <https://git.nugget.fun/scrape/scrapeboard>.
#
# This is a utility script for testing if the GPIO interface of a Raspberry Pi is working
# with the four input wires, which correspond to the four metal floor pads in the standard
# Scrapeboard build.
import time, itertools
import RPi.GPIO as GPIO
# These represent the game pads and the GPIO pins they're connected to
LNW, LNE, LSE, LSW = range(4)
pins = {
LNW: 17,
LNE: 27,
LSE: 22,
LSW: 23
}
def connection2(pin_a, pin_b):
"""
Set `pin_a` to output a low signal, and set every other pin to pullup. If `pin_b` reads a low signal even though
it's set to pullup, that means `pin_a` is connected to it, so return `True`.
@param pin_a Pin which will be set to output LOW
@param pin_b Pin which will be read
@return `True` if a low signal is sent from `pin_a` to `pin_b`, `False` otherwise
"""
for pin_id, pin in pins.items():
if pin == pin_a:
# Set pin_a to output LOW
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
else:
# Set all other pins to input pullup
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# pin_b can now be tested to see if if reads the LOW signal from pin_a
return GPIO.input(pin_b) == GPIO.LOW
def connection(pin_a, pin_b):
"""
Tests low signals connect from both A to B and from B to A
@param pin_a Test if this pin is connected to `pin_b`
@param pin_b Test if this pin is connected to `pin_a`
@return `True` if `pin_a` and `pin_b` are connected, `False` otherwise
"""
return connection2(pin_a, pin_b) and connection2(pin_b, pin_a)
def connections():
"""
Look at all six possible connections between the four pins and print a message if one is found connected.
"""
for pin_id_a, pin_id_b in itertools.combinations(pins, 2):
if connection(pins[pin_id_a], pins[pin_id_b]):
print(f"{pin_id_a} ({pins[pin_id_a]}) <-> {pin_id_b} ({pins[pin_id_b]})")
if __name__ == "__main__":
# Use GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set all pins to pullup
for pin_id, pin in pins.items():
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
# Try all connections once each frame
connections()
time.sleep(0.1)