VMAC PiHat v1 Home AV Switch
The below is a simple piece of Python test code that will setup the HAMKit VMAC PiHat GPIO and I2C I/O and allow you to easily perform basic tests – It can also be used as a basis for a Home Audio Video Matrix.
For simplicity, you can use Thonny to run and edit the Python code. This application is included within Raspbian and also available for Windows and MAC users.
- GPIO – Ground an Input (Button or SQ1-4), flash LEDs depending on which input selected.
- SQ, or Switch, Inputs will reset the Matrix Output and Setup some Example ‘Memories’.
- I2C FMS6501A – Setup all Matrix inputs and outputs.
CODE (Provisional)
# HAMKit VMAC PiHat | Home Matrix Example Code to Setup GPIO and I2C. v1.1 # GPIO : Ground an Input (Button/SQ1-4), set matrix and flash LEDs depending on which input selected. # I2C FMS6501 : SQ1 = Video In 1 to both Video Out 1 plus 2. Audio In 4 to Audio Out 3 plus 4. LED Flash 1 # I2C FMS6501 : SQ2 = Video In 2 to both Video Out 1 plus 2. Audio In 5 to Audio Out 3 plus 4. LED Flash 2 # I2C FMS6501 : SQ3 = Video In 3 to both Video Out 1 plus 2. Audio In 6 to Audio Out 3 plus 4. LED Flash 3 # I2C FMS6501 : SQ4 = Pi Video to both Video Out 1 and 2. Pi L/R Audio to Audio Out 3 and 4. LED Flash 4 # I2C FMS6501 : Button = All outputs muted. LED Flash 5 # NB: All outputs are initially muted # NB: You could always use additional GPIO inputs to expand options, or create 'memories' # NB: The only reason for adding time.sleep(.01), is to create a slight courtesy pause between changes # Dave Williams G8PUO, January 2017 #!/usr/bin/python #Check i2c bus #sudo i2cdetect -y 1 import smbus import RPi.GPIO as GPIO import time #====== GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) #GPIO.cleanup() #Define GPIO Inputs Button = 4 SQ1 = 12 SQ2 = 16 SQ3 = 20 SQ4 = 21 #Setup Inputs with pull-ups enabled GPIO.setup(Button, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(SQ1, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(SQ2, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(SQ3, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(SQ4, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Define GPIO Outputs LEDStatus = 22 PTT = 27 #Setup Outputs GPIO.setup(LEDStatus,GPIO.OUT) GPIO.setup(PTT,GPIO.OUT) #Initiate LEDs GPIO.output(LEDStatus,GPIO.HIGH) GPIO.output(PTT,GPIO.HIGH) time.sleep(1) GPIO.output(LEDStatus,GPIO.LOW) GPIO.output(PTT,GPIO.LOW) time.sleep(1) #Good old fashion subroutine def flashleds (flashes): for x in range(flashes): GPIO.output(LEDStatus,GPIO.HIGH) GPIO.output(PTT,GPIO.HIGH) time.sleep(0.2) GPIO.output(LEDStatus,GPIO.LOW) GPIO.output(PTT,GPIO.LOW) time.sleep(0.5) #====== FMS #define values DEVICE_BUS = 1 DEVICE_ADDR = 0x43 #0x43 or 0x03 #setup i2c bus i2cbus = smbus.SMBus(DEVICE_BUS) #Set all outputs to mute. i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x0) #Mute time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x0) #Mute time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x0) #Mute time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x0) #Mute time.sleep(.01) while True: #Send Command to I2C - Set inputs to outputs, or mute, when button pressed if GPIO.input(SQ1) == False: print ("SQ Input 1 - Video In 1>Video Out 1+2 & Audio In 4>Audio Out 3+4") i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x81) #Input 1 to Output 1 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x81) #Input 1 to Output 2 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x87) #Input 4 to Output 3 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x87) #Input 4 to Output 4 time.sleep(.01) flashleds (1) if GPIO.input(SQ2) == False: print ("SQ Input 1 - Video In 2>Video Out 1+2 & Audio In 5>Audio Out 3+4") i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x83) #Input 2 to Output 1 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x83) #Input 2 to Output 2 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x89) #Input 5 to Output 3 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x89) #Input 5 to Output 4 time.sleep(.01) flashleds (2) if GPIO.input(SQ3) == False: print ("SQ Input 1 - Video In 3>Video Out 1+2 & Audio In 6>Audio Out 3+4") i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x85) #Input 3 to Output 1 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x85) #Input 3 to Output 2 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x8b) #Input 6 to Output 3 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x8b) #Input 6 to Output 4 time.sleep(.01) flashleds (3) if GPIO.input(SQ4) == False: print ("SQ Input 3 - Pi Video Out>Video 1+2 & Pi Audio L/R>Audio Out 3/4") i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x8c) #Input Pi to Output 1 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x8c) #Input Pi to Output 2 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x89) #Input 5 to Output 3 time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x8b) #Input 6 to Output 4 time.sleep(.01) flashleds (4) if GPIO.input(Button) == False: print ("Button - Mute") i2cbus.write_byte_data(DEVICE_ADDR, 0x01, 0x0) #Mute time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x03, 0x0) #Mute time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x06, 0x0) #Mute time.sleep(.01) i2cbus.write_byte_data(DEVICE_ADDR, 0x09, 0x0) #Mute time.sleep(.01) flashleds (5)