Ligne 1 : |
Ligne 1 : |
| {{RASP-PYGAME-GUI-NAV}} | | {{RASP-PYGAME-GUI-NAV}} |
− |
| |
− | {{traduction}}
| |
| | | |
| == Entrée Analogique == | | == Entrée Analogique == |
Ligne 21 : |
Ligne 19 : |
| | | |
| == Démarrage == | | == Démarrage == |
| + | Au tout début du script nous faisons quelques {{fname|import}} pour le support multitâche (''threading'') et la conversion ADC. |
| | | |
− | At the top of this script now we have some more imports for the threading and ADC.
| + | Le gain et le taux d'échantillonnage sont configurés et le convertisseur ADC est initialisé. |
− | | |
− | The gain and sample rate are configured and the ADC initialized.
| |
| | | |
| <syntaxhighlight lang="python"> | | <syntaxhighlight lang="python"> |
Ligne 43 : |
Ligne 40 : |
| ADS1115 = 0x01 # 16-bit ADC | | ADS1115 = 0x01 # 16-bit ADC |
| | | |
− | # Select the gain | + | # Sélectionner le gain |
| # gain = 6144 # +/- 6.144V | | # gain = 6144 # +/- 6.144V |
| gain = 4096 # +/- 4.096V | | gain = 4096 # +/- 4.096V |
Ligne 51 : |
Ligne 48 : |
| # gain = 256 # +/- 0.256V | | # gain = 256 # +/- 0.256V |
| | | |
− | # Select the sample rate | + | # Sélectionner le taux d'échantillonnage |
− | sps = 8 # 8 samples per second | + | sps = 8 # 8 échantillons par seconde |
− | # sps = 16 # 16 samples per second | + | # sps = 16 # 16 échantillons par seconde |
− | # sps = 32 # 32 samples per second | + | # sps = 32 # 32 échantillons par seconde |
− | # sps = 64 # 64 samples per second | + | # sps = 64 # 64 échantillons par seconde |
− | # sps = 128 # 128 samples per second | + | # sps = 128 # 128 échantillons par seconde |
− | # sps = 250 # 250 samples per second | + | # sps = 250 # 250 échantillons par seconde |
− | # sps = 475 # 475 samples per second | + | # sps = 475 # 475 échantillons par seconde |
− | # sps = 860 # 860 samples per second | + | # sps = 860 # 860 échantillons par seconde |
| | | |
− | # Initialise the ADC using the default mode (use default I2C address) | + | # Initialiser le convertisseur ADC en utilisant le mode par défaut (et adresse I2C par défaut) |
− | # Set this to ADS1015 or ADS1115 depending on the ADC you are using! | + | # Utilisez ADS1015 ou ADS1115 en fonction de l'ADC que vous utilisez! |
| adc = ADS1x15(ic=ADS1115) | | adc = ADS1x15(ic=ADS1115) |
| | | |
− | #Setup the GPIOs as outputs - only 4 and 17 are available | + | # Configurer les GPIOs en sortie (OUTPUT) - seuls 4 et 17 sont disponibles |
| GPIO.setmode(GPIO.BCM) | | GPIO.setmode(GPIO.BCM) |
| GPIO.setup(4, GPIO.OUT) | | GPIO.setup(4, GPIO.OUT) |
Ligne 85 : |
Ligne 82 : |
| | | |
| == Le thread du potentiomètre == | | == Le thread du potentiomètre == |
− | A thread is used to constantly read the potentiometer. If you take the reading in-line in the scene update method then you'll slow down the screen refresh rate. This separate thread allows the screen to be redrawn in parallel with us reading a value from the ADC.
| + | Un thread est utilisé pour lire continuellement la valeur du potentiomètre. Si l'on fait la lecture du potentiomètre dans la mise-à-jour de la Scene alors le taux de rafraîchissement de l'écran diminuera de façon importante. |
| + | |
| + | Ce thread séparé permet à l'écran d'être redessiné en même temps (en parallèle) que la lecture de la valeur depuis l'ADC. |
| | | |
− | The class is defined as a callable - hence the {{đname|__call__}} method. It takes a reference to the pitft class so we can update values on it. There's a simple termination scheme used by checking a flag on every loop. Later on you'll see how we cleanly close the program by setting this flag.
| + | La classe est définie comme {{fname|callable}} appela - d'où la méthode {{fname|__call__}}. Il prend une référence vers la classe pitft de sorte qu'il est possible d'y mettre les valeurs à-jour. Il y a un schéma d'arrêt relativement simple basé sur la vérification d'un variable (un ''flag''/''drapeau'') à chaque tour de boucle. Nous verrons plus tard comment terminer proprement le programme en modifiant la variable ''flag''. |
| | | |
− | So we simply read the voltage value from the ADC and set the label and progress bar position on the pitft.
| + | Donc, nous lisons simplement la valeur depuis l'ADC et modifie la valeur du libellé et la position de la barre de progression (visible sur le pitft). |
| | | |
| <syntaxhighlight lang="python"> | | <syntaxhighlight lang="python"> |
Ligne 102 : |
Ligne 101 : |
| def __call__(self): | | def __call__(self): |
| while not self.terminated: | | while not self.terminated: |
− | # Read channel 0 in single-ended mode using the settings above | + | # Lecture du canal 0 en mode single-ended en utilisant la configuration ci-avant |
| volts = adc.readADCSingleEnded(0, gain, sps) / 1000 | | volts = adc.readADCSingleEnded(0, gain, sps) / 1000 |
| self.pitft.set_volts_label(volts) | | self.pitft.set_volts_label(volts) |
Ligne 108 : |
Ligne 107 : |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
| + | == Définition de l'interface utilisateur == |
| + | Nous avons ajouté quelques widget supplémentaires sur la Scene et modifié la taille des boutons (pour que tout se présente correctement). |
| + | |
| + | Nous allons afficher la tension sur un widget label (''libellé'') et un widget progress (''barre de progression''). |
| | | |
− | == Définition de l'interface utilisateur ==
| + | Les méthodes {{fname|set_progress}} et {{fname|set_volts_label}} sont appelées depuis le thread PotReader pour faire la mise-à-jour des valeurs dans ces Widgets. |
− | We've added a couple more widgets to the scene now and resized the buttons to accommodate them. We're showing the voltage on a label and a progress widget. The {{fname|set_progress}} and {{fname|set_volts_label}} methods are called from the PotReader thread to update the values in these widgets.
| |
| | | |
| <syntaxhighlight lang="python"> | | <syntaxhighlight lang="python"> |
Ligne 162 : |
Ligne 164 : |
| | | |
| == Et pour finir... == | | == Et pour finir... == |
− | To start everything going we initialise pygameui, construct the pitft class and then start the potreader thread with a reference to pitft.
| + | Pour démarrer le programme nous initialisons pygameui, construisons la classe pitft class et démarrons le thread potreader avec le pitft en référence. |
| + | |
| + | Puisque nous exécutons un thread supplémentaire, nous avons également besoin d'une '''méthode propre''' pour terminer le programme. |
| | | |
− | Since we're running this extra thread we need a clean way to stop the program. A signal handler is used to trap ctrl+c and terminate the PotReader thread before calling sys.exit - otherwise the program will not close.
| + | Un gestionnaire de signal (''signal handler'') est utilisé pour capturer ctrl+c et terminer le thread PotReader avant d'appeler sys.exit - sinon le programme ne se fermera pas. |
| | | |
| <syntaxhighlight lang="python"> | | <syntaxhighlight lang="python"> |
Ligne 172 : |
Ligne 176 : |
| pitft = PiTft() | | pitft = PiTft() |
| | | |
− | # Start the thread running the callable | + | # démarrer le thread exécutant le ''callable'' |
| potreader = PotReader(pitft) | | potreader = PotReader(pitft) |
| threading.Thread(target=potreader).start() | | threading.Thread(target=potreader).start() |
| | | |
| def signal_handler(signal, frame): | | def signal_handler(signal, frame): |
− | print 'You pressed Ctrl+C!' | + | print 'Vous avez pressé Ctrl+C!' |
| potreader.terminate() | | potreader.terminate() |
| sys.exit(0) | | sys.exit(0) |
| | | |
| + | # Enregister le gesionnaire signal_handler pour le lignal SIGINT (interruption) |
| signal.signal(signal.SIGINT, signal_handler) | | signal.signal(signal.SIGINT, signal_handler) |
| | | |
Ligne 187 : |
Ligne 192 : |
| </syntaxhighlight> | | </syntaxhighlight> |
| | | |
− | You can run this final example from the pygamelcd project:
| + | == Exécuter == |
| + | Vous pouvez exécuter cet exemple final depuis le projet pygamelcd: |
| | | |
| <syntaxhighlight lang="bash"> | | <syntaxhighlight lang="bash"> |
| sudo python test5.py | | sudo python test5.py |
| </syntaxhighlight> | | </syntaxhighlight> |
− |
| |
| | | |
| | | |
| {{RASP-PYGAME-GUI-TRAILER}} | | {{RASP-PYGAME-GUI-TRAILER}} |