#!/usr/bin/python3

from time import sleep
import os
import subprocess
import re
import wiringpi

p = subprocess.Popen(['gpio', 'i2cdetect'],stdout=subprocess.PIPE,)
#cmdout = str(p.communicate())

mcps = list()
p.stdout.readline()
for i in range(1,9):
  line = str(p.stdout.readline()).split(' ')[1:]
  base_multiplier = 1
  for mcp in line:
    base_multiplier += 1
    if re.search('\d\d', mcp):
        base = 64+16*base_multiplier
        mcps.append((mcp,base))

wiringpi.wiringPiSetup()
for mcp in mcps:
  print("Setting base for MCP: ", mcp[0], " with base: ",  mcp[1])
  wiringpi.mcp23017Setup(mcp[1], int(mcp[0], 16))
  for pin in range(16):
    wiringpi.pinMode(mcp[1] + pin, 1)
    wiringpi.digitalWrite(mcp[1] + pin, 1)
    sleep(0.1)

except_pins = [2, 3]  

for i in range(32):
    wiringpi.pinMode(i, 1)
    wiringpi.digitalWrite(pin, 1)
    sleep(0.1)
