代码在最底部,这里是api文档:

GPIO类

初始化方法:

def __init__(self, gpio_index)

  • 描述:创建一个GPIO对象并初始化相关属性。
  • 参数:
    • gpio_index:整数,GPIO引脚的索引。

方法:

is_exported()

def is_exported(self) -> bool

  • 描述:检查GPIO引脚是否已导出。
  • 返回值:
    • bool:如果GPIO引脚已导出,则为True;否则为False。
export_gpio()

def export_gpio(self)

  • 描述:将GPIO引脚导出。
  • 注意:在导出之前,需要确保GPIO引脚未被导出。
unexport_gpio()

def unexport_gpio(self)

  • 描述:取消导出GPIO引脚。
  • 注意:在取消导出之后,GPIO引脚的方向和值将被重置为None。
set_direction(direction)

def set_direction(self, direction)

  • 描述:设置GPIO引脚的方向。
  • 参数:
    • direction:字符串,GPIO引脚的方向。有效值为'in'(输入)或'out'(输出)。
  • 注意:在设置方向之前,需要确保GPIO引脚已导出。
get_value()

def get_value(self) -> int

  • 描述:获取GPIO引脚的值。
  • 返回值:
    • int:GPIO引脚的值(0或1)。
  • 注意:在获取值之前,需要确保GPIO引脚已导出且方向为输入。
set_value(value)

def set_value(self, value)

  • 描述:设置GPIO引脚的值。
  • 参数:
    • value:整数,GPIO引脚的值。有效值为0或1。
  • 注意:在设置值之前,需要确保GPIO引脚已导出且方向为输出。
get_direction()

def get_direction(self) -> str

  • 描述:获取GPIO引脚的方向。
  • 返回值:
    • str:GPIO引脚的方向('in'或'out')。
  • 注意:在获取方向之前,需要确保GPIO引脚已导出。
refresh_direction()

def refresh_direction(self)

  • 描述:刷新GPIO引脚的方向。
  • 注意:通过读取/sys/class/gpio/gpio{gpio_index}/direction文件来获取最新的方向。
refresh_value()

def refresh_value(self)

  • 描述:刷新GPIO引脚的值。
  • 注意:通过读取/sys/class/gpio/gpio{gpio_index}/value文件来获取最新的值。

代码如下

class GPIO:
    def __init__(self, gpio_index):
        self.gpio_index = gpio_index
        self.sysfs_path = f"/sys/class/gpio/gpio{gpio_index}"
        self.direction = None
        self.value = None

        self.export_gpio()
        self.refresh_direction()

    def is_exported(self):
        return os.path.exists(self.sysfs_path)

    def export_gpio(self):
        if not self.is_exported():
            with open("/sys/class/gpio/export", "w") as export_file:
                export_file.write(str(self.gpio_index))

    def unexport_gpio(self):
        if self.is_exported():
            with open("/sys/class/gpio/unexport", "w") as unexport_file:
                unexport_file.write(str(self.gpio_index))
            self.direction = None
            self.value = None
    
    def set_direction(self, direction):
        if not self.is_exported():
            raise ValueError("GPIO pin has not been exported")

        if direction not in ["in", "out"]:
            raise ValueError("Invalid direction. Must be 'in' or 'out'")
        
        if direction != self.direction:
            with open(f"{self.sysfs_path}/direction", "w") as direction_file:
                direction_file.write(direction)
            self.direction = direction

    def get_value(self):
        if not self.is_exported():
            raise ValueError("GPIO pin has not been exported")
        
        if self.direction == "in":
            self.refresh_value()
        
        return self.value

    def set_value(self, value):
        if not self.is_exported():
            raise ValueError("GPIO pin has not been exported")

        if self.direction != "out":
            raise ValueError("Cannot set value in input mode")
        
        if value not in [0, 1]:
            raise ValueError("Invalid value. Must be 0 or 1")
        
        if value != self.value:
            with open(f"{self.sysfs_path}/value", "w") as value_file:
                value_file.write(str(value))
            self.value = value

    def get_direction(self):
        if not self.is_exported():
            raise ValueError("GPIO pin has not been exported")

        if self.direction is None:
            self.refresh_direction()

        return self.direction

    def refresh_direction(self):
        with open(f"{self.sysfs_path}/direction", "r") as direction_file:
            self.direction = direction_file.read().strip()

    def refresh_value(self):
        with open(f"{self.sysfs_path}/value", "r") as value_file:
            value = value_file.read().strip()
        self.value = int(value)

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐