139 lines
2.9 KiB
Python
139 lines
2.9 KiB
Python
"""Test the IO Board."""
|
|
|
|
import mraa
|
|
import time
|
|
|
|
relay = []
|
|
|
|
for x in range(0, 6):
|
|
pin = mraa.Gpio(x)
|
|
pin.dir(mraa.DIR_OUT)
|
|
relay.append(pin)
|
|
pin.write(0)
|
|
|
|
pinRedLED = mraa.Gpio(8)
|
|
pinRedLED.dir(mraa.DIR_OUT)
|
|
|
|
pinGreenLED = mraa.Gpio(9)
|
|
pinGreenLED.dir(mraa.DIR_OUT)
|
|
|
|
input_mux = [
|
|
[],
|
|
[0, 0, 0], # Input 1
|
|
[1, 0, 0], # Input 2
|
|
[0, 1, 0], # Input 3
|
|
[1, 1, 0], # Input 4
|
|
[0, 0, 1], # Input 5
|
|
[1, 0, 1], # Input 6
|
|
[0, 1, 1], # Input 7
|
|
[1, 1, 1], # Input 8
|
|
]
|
|
|
|
pinA0 = mraa.Gpio(14)
|
|
pinA0.dir(mraa.DIR_OUT)
|
|
pinA1 = mraa.Gpio(15)
|
|
pinA1.dir(mraa.DIR_OUT)
|
|
pinA2 = mraa.Gpio(16)
|
|
pinA2.dir(mraa.DIR_OUT)
|
|
pinA3 = mraa.Gpio(17)
|
|
pinA3.dir(mraa.DIR_IN)
|
|
|
|
spi_bus = mraa.Spi(0)
|
|
print(spi_bus.lsbmode(False))
|
|
|
|
|
|
def red_LED(out):
|
|
"""Set the Red LED to the specified output."""
|
|
if out:
|
|
pinRedLED.write(1)
|
|
else:
|
|
pinRedLED.write(0)
|
|
|
|
|
|
def green_LED(out):
|
|
"""Set the Green LED to the specified output."""
|
|
if out:
|
|
pinGreenLED.write(1)
|
|
else:
|
|
pinGreenLED.write(0)
|
|
|
|
|
|
def relay_loop():
|
|
"""Loop through all relays."""
|
|
loops = 0
|
|
while loops < 10:
|
|
for p in relay:
|
|
p.write(1)
|
|
time.sleep(1)
|
|
p.write(0)
|
|
loops += 1
|
|
|
|
|
|
def read_dig_in(io_port):
|
|
"""Read IO Board digital input."""
|
|
if io_port == 0 or io_port > 8:
|
|
print("CANNOT READ A PORT THAT DOES NOT EXIST: {}".format(io_port))
|
|
return False
|
|
pin_values = input_mux[io_port]
|
|
pinA0.write(pin_values[0])
|
|
pinA1.write(pin_values[1])
|
|
pinA2.write(pin_values[2])
|
|
return pinA3.read() == 0
|
|
|
|
|
|
def dig_in_loop():
|
|
"""Read all digital inputs."""
|
|
for i in range(1, 9):
|
|
print("DIGIN {} = {}".format(i, read_dig_in(i)))
|
|
|
|
|
|
def led_flash(loops):
|
|
"""Flash the LED's from red to green."""
|
|
led_loops = 0
|
|
while led_loops < loops:
|
|
red_LED(True)
|
|
green_LED(False)
|
|
time.sleep(0.25)
|
|
red_LED(False)
|
|
green_LED(True)
|
|
time.sleep(0.25)
|
|
led_loops += 1
|
|
|
|
red_LED(False)
|
|
green_LED(False)
|
|
|
|
|
|
def read_analog_in(io_port):
|
|
"""Read IO Board digital input."""
|
|
global spi_bus
|
|
if io_port < 1 or io_port > 4:
|
|
print("CANNOT READ A PORT THAT DOES NOT EXIST: {}".format(io_port))
|
|
return False
|
|
pin_values = input_mux[io_port]
|
|
pinA0.write(pin_values[0])
|
|
pinA1.write(pin_values[1])
|
|
pinA2.write(pin_values[2])
|
|
ok_status = False
|
|
|
|
while ok_status is False:
|
|
pinA0.write(pin_values[0])
|
|
pinA1.write(pin_values[1])
|
|
pinA2.write(pin_values[2])
|
|
x = spi_bus.write(bytearray([0, 0, 0]))
|
|
print((x[0], x[1], x[2]))
|
|
c_bin = format(x[2], '#010b')[2:]
|
|
|
|
if c_bin[-3:] == "101":
|
|
ok_status = True
|
|
print("OK: {}, Status: {}".format(ok_status, c_bin))
|
|
print(x[0] * 256 + x[1])
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# led_flash(10)
|
|
|
|
print("Analog {}".format(1))
|
|
read_analog_in(1)
|