import os
import subprocess
import sys

executable_path = sys.argv[0]
path = os.path.dirname(os.path.abspath(executable_path))
resourcesPath = path + os.sep

class i2c_gpio:
    def __init__(self):
        self._read_path = resourcesPath + "read_gpio.sh"
        self._write_path = resourcesPath + "write_gpio.sh"

    def read_io(self, pin):
        try:
            command = f"sudo {self._read_path} {pin}"
            data = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT,timeout=5)  
            print(data)
            return int(data[-2])
                 
        except subprocess.CalledProcessError as e:
            print(e)  
            return None
        
    def write_io(self, pin, level):
        try:
            command = f"sudo {self._write_path} {pin} {level}"
            subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT,timeout=5)       
        except subprocess.CalledProcessError as e:
            print(e)  
            return None
          

     
    