Python 如何在类的uuu init_uuuu函数中使用Raspberry pi gpio设置和pwm命令?
我是面向对象编程的新手。我正在与Raspberry pi合作,我正在构建许多具有不同GPIO引脚的类。我不知道如何建立设置和pwm命令。它们都不应该出现在类中,或者我应该将它们放在每个类的init函数中?在init函数中OOP将如何改变?你能给我举个例子吗Python 如何在类的uuu init_uuuu函数中使用Raspberry pi gpio设置和pwm命令?,python,oop,raspberry-pi,gpio,Python,Oop,Raspberry Pi,Gpio,我是面向对象编程的新手。我正在与Raspberry pi合作,我正在构建许多具有不同GPIO引脚的类。我不知道如何建立设置和pwm命令。它们都不应该出现在类中,或者我应该将它们放在每个类的init函数中?在init函数中OOP将如何改变?你能给我举个例子吗 GPIO.setup(33, GPIO.OUT) pwmservo=GPIO.PWM(33,50) pwmservo.start(6) class zmove(object): def __init__(self):
GPIO.setup(33, GPIO.OUT)
pwmservo=GPIO.PWM(33,50)
pwmservo.start(6)
class zmove(object):
def __init__(self):
pass
def update(self,angle):
duty=float(angle)/10.0+2.5
pwmservo.ChangeDutyCycle(duty)
time.sleep(0.3)
问题:类的init函数中的gpio设置和pwm命令
GPIO Zero具有伺服和角度伺服类:
class PWMServo:
"""
Base class doing setup and get PWM instance
"""
def __init__(self, pin):
GPIO.setup(pin, GPIO.OUT)
self.pwm = GPIO.PWM(pin, 50)
self.pwm.start(6)
def change_duty_cycle(self, duty):
self.pwm.ChangeDutyCycle(duty)
time.sleep(0.3)
class ZMove(PWMServo):
"""
Inherit from class PWMServo
ZMove use PIN 33
"""
def __init__(self):
super().__init__(pin=33)
self.pwm.start(6)
def update(self,angle):
duty=float(angle)/10.0+2.5
self.change_duty_cycle(duty)
if __name__ == '__main__':
zmove = ZMove()
zmove.update(45.0)
# Or, call direct
zmove.change_duty_cycle(45.0/10.0+2.5)