L’initialisation se fait par la méthode set_pin_mode_servo(pin).
L’écriture se fait par la méthode servo_write(pin,angle).
Exemple d’utilisation des touches du clavier
from pymata4 import pymata4 import sys import time import keyboard #------------ Les notes à jouer def on_left(): global angle angle=0 carte.servo_write(Servo_pin,angle) def on_right(): global angle angle=180 carte.servo_write(Servo_pin,angle) def on_up(): global angle angle=90 carte.servo_write(Servo_pin,angle) def on_plus(): global angle if angle<=175: angle=angle+5 else : angle=180 carte.servo_write(Servo_pin, angle) def on_moins(): global angle if angle>=5: angle=angle-5 else : angle=0 carte.servo_write(Servo_pin,angle) carte = pymata4.Pymata4() print("--------------- Début --------------") # Constantes Servo_pin = 7 # port du Servo : D7 angle=0 #---------------- programme principal try : keyboard.add_hotkey("left",on_left) keyboard.add_hotkey("right",on_right) keyboard.add_hotkey("up",on_up) keyboard.add_hotkey("+",on_plus) keyboard.add_hotkey("-",on_moins) carte.set_pin_mode_servo(Servo_pin) time.sleep(1) carte.servo_write(Servo_pin,angle) while True: time.sleep(1) except KeyboardInterrupt : # si on tape Ctl + C print("--------------- Fin --------------") carte.shutdown() sys.exit(0)
En utilisant une classe
from pymata4 import pymata4 import sys import time import keyboard #------------------------ classe Servo class Servo: def __init__(self,pin,board): self.pin=pin self.angle=0 self.carte=board self.carte.set_pin_mode_servo(self.pin) time.sleep(1) self.maj_angle() keyboard.add_hotkey("left",self.on_left) keyboard.add_hotkey("right",self.on_right) keyboard.add_hotkey("up",self.on_up) keyboard.add_hotkey("+",self.on_plus) keyboard.add_hotkey("-",self.on_moins) def maj_angle(self): self.carte.servo_write(self.pin,self.angle) #------------ Les notes à jouer def on_left(self): self.angle=0 self.maj_angle() def on_right(self): self.angle=180 self.maj_angle() def on_up(self): self.angle=90 self.maj_angle() def on_plus(self): if self.angle<=175: self.angle=self.angle+5 else : self.angle=180 self.maj_angle() def on_moins(self): if self.angle>=5: self.angle=self.angle-5 else : self.angle=0 self.maj_angle() carte = pymata4.Pymata4() print("--------------- Début --------------") # Constantes Servo_pin = 7 # port du Servo : D7 #---------------- programme principal try : servo = Servo(Servo_pin,carte) while True: time.sleep(1) except KeyboardInterrupt : # si on tape Ctl + C print("--------------- Fin --------------") carte.shutdown() sys.exit(0)