Lancement fonctions dont le nom est en variable dans une classe....

Lancement fonctions dont le nom est en variable dans une classe.... - Python - Programmation

Marsh Posté le 08-11-2015 à 19:35:33    

Bonjour à tous,  
Le titre n'est pas clair et pourtant je suis sur que la solution est toute bete...
je vous soumet mon soucis en python :
J'ai ce code que j'ai pondu pour gerer mon encodeur rotatif avec un rasp.
 
Le principe est simple, ma classe encodeur prend en paramètres les pins, et deux variables contenant le noms des fonctions du programme principale a exécuter si y'a rotation ou click.
 
Je vous passe le code, vous allez voir.
 

Code :
  1. #!/usr/bin/env python
  2. import RPi.GPIO as GPIO
  3. import time
  4. from datetime import datetime
  5. import Adafruit_CharLCD as LCD
  6. import os
  7. import subprocess
  8. import sys
  9. #from class_encoder import encoder
  10. class encoder:
  11.     def __init__(self,pinA,pinB,pinS,functionrot,functionswitch):
  12.         self.RoAPin = pinA
  13.         self.RoBPin = pinB
  14.         self.RoSwitch = pinS
  15.         self.Last_RoB_Status = 0
  16.         self.Current_RoB_Status = 0
  17.         self.Encoder_A_old = 0
  18.         self.Encoder_B_old = 0
  19.         self.sens = 0
  20.         GPIO.setmode(GPIO.BCM)       # Numbers GPIOs by physical location
  21.         GPIO.setup(self.RoAPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    # input mode
  22.         GPIO.setup(self.RoBPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  23.         GPIO.setup(self.RoSwitch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  24.         GPIO.add_event_detect(self.RoAPin, GPIO.BOTH, callback=self.rotate)
  25.         GPIO.add_event_detect(self.RoBPin, GPIO.BOTH, callback=self.rotate)
  26.         GPIO.add_event_detect(self.RoSwitch, GPIO.RISING, callback=self.click)
  27.         self.funcrot = functionrot
  28.         self.funcsw = functionswitch
  29.     def rotate(self,term):
  30.         self.Encoder_A,self.Encoder_B = GPIO.input(self.RoAPin),GPIO.input(self.RoBPin)
  31.         if ((self.Encoder_A,self.Encoder_B) == (0,0)) and ((self.Encoder_A_old,self.Encoder_B_old) == (1,0)):
  32.             globals()[self.funcrot](1)
  33.         if ((self.Encoder_A,self.Encoder_B) == (0,0)) and ((self.Encoder_A_old,self.Encoder_B_old) == (0,1)):
  34.             globals()[self.funcrot](-1)
  35.         self.Encoder_A_old,self.Encoder_B_old = self.Encoder_A,self.Encoder_B
  36.         return 1
  37.     def click(self,term):
  38.         globals()[self.funcsw]()
  39.         return 1
  40. def setup():
  41.     e=encoder(5,6,13,"protate","pclick" )
  42.     print "load"
  43. def protate(sens):
  44.     print "rotate " + str(sens)
  45. def pclick():
  46.     print "click"
  47. def loop():
  48.     global sens
  49.     while True:
  50.         time.sleep(2)
  51.         print "loop"
  52. def destroy():
  53.     GPIO.cleanup()
  54. if __name__ == '__main__':
  55.     setup()
  56.     try:
  57.         loop()
  58.     except KeyboardInterrupt: 
  59.         destroy()
  60.     except Exception as inst:
  61.         destroy()


 
Comme cela, cela fonctionne.
Par contre, si je met ma classe dans un fichier et que je l'importe, ca merde en me sortant :
une keyerror sur les globals()[self.funcsw]() etglobals()[self.funcrotate]()
 
 
Ce qui me parait assez logique, sauf que je vois pas par quoi remplacer
 
J'aurais pu faire en sorte que les interruptions ne soient pas geres dans la classe ce qui m'aurait éviter ce problème en n'ayant au final que deux méthodes dans ma classe pour savoir le sens de rotation et le click en appelant ces méthodes dans le callback...
Mais je veux que ce soit ma classe qui le gere, et je suppose qu'il doit pas manquer grand chose...

Reply

Marsh Posté le 08-11-2015 à 19:35:33   

Reply

Marsh Posté le 26-11-2015 à 14:30:45    

Pourquoi tu passes pas directement les fonctions en paramètre à ta classe [:pingouino dei]


---------------
Stick a parrot in a Call of Duty lobby, and you're gonna get a racist parrot. — Cody
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed