project:python_led
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| project:python_led [2026/02/18 09:28] – torsten.roehl | project:python_led [2026/02/20 09:56] (aktuell) – torsten.roehl | ||
|---|---|---|---|
| Zeile 3: | Zeile 3: | ||
| [[projekt: | [[projekt: | ||
| + | // | ||
| + | In diesem Projekt wird das bisherige LED-Programm neu strukturiert und in mehrere Dateien aufgeteilt. Dabei wird die Hardware-Ansteuerung von der eigentlichen Programmlogik getrennt. Ziel ist es, mehrere LEDs gezielt und übersichtlich über die Konsole steuern zu können. | ||
| + | // | ||
| - | ===== Hauptprogramm ===== | ||
| - | Für das eigentliche Projekt wird der Code nun strukturiert aufgebaut. | + | ===== Programmstruktur ===== |
| - | Ziel ist es, mehrere | + | |
| + | <note important> | ||
| + | **Aktiviere die Python-Environment** | ||
| + | |||
| + | Die Programmierung erfolgt | ||
| + | < | ||
| + | source ~/ | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | Die Aufteilung in mehrere | ||
| - | Die Umstrukturierung sorgt außerdem für eine bessere Übersichtlichkeit. Einzelne Komponenten können dadurch unabhängig voneinander erweitert oder in anderen Projekten weiterverwendet werden, wie später im Projekt mit FastAPI und Apache2 gezeigt wird. | ||
| - | ==== Programmstruktur ==== | ||
| Folgende Programmstruktur wird verwendet. | Folgende Programmstruktur wird verwendet. | ||
| * '' | * '' | ||
| - | * '' | + | |
| - | | + | * '' |
| - | * '' | + | |
| <code bash> | <code bash> | ||
| gpio_led/ | gpio_led/ | ||
| - | ├── | ||
| └── src | └── src | ||
| ├── core | ├── core | ||
| - | │ | + | │ |
| - | │ | + | |
| │ | │ | ||
| └── ledcontrol.py | └── ledcontrol.py | ||
| Zeile 37: | Zeile 45: | ||
| <code bash> | <code bash> | ||
| + | cd ~/ | ||
| touch __init__.py | touch __init__.py | ||
| </ | </ | ||
| Zeile 47: | Zeile 56: | ||
| - | ===== Projekt: LED mit Python ===== | + | ===== Hardware ===== |
| - | ==== Hardware ==== | + | |
| - | ==== Quellcode (Sourcecode) ==== | + | |
| - | Der **Sourcecode** besteht aus drei Dateien: das eigentliche Programm **%%ledcontrol.py%%** sowie die Dateien im Hintergrund (im Verzeichnis **%%core%%**), | + | |
| - | === gpio_hw.py | + | |
| - | <code python | + | < |
| + | Der Aufbau und die verwendete Hardware wurden im vorherigen [[projekt: | ||
| + | </ | ||
| + | ===== Quellcode (Sourcecode) ===== | ||
| + | Der **Sourcecode** besteht aus zwei Dateien: das eigentliche Programm **%%ledcontrol.py%%** sowie die Dateien im Hintergrund (im Verzeichnis **%%core%%**), | ||
| + | === hardware.py === | ||
| + | |||
| + | <code python | ||
| import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
| - | PIN_Y = 17 | + | # ----------------------------- |
| + | # API-Funktionen GPIO LED Ampel | ||
| + | # ----------------------------- | ||
| + | # ADJUST AREA START | ||
| + | PIN_R = 17 | ||
| + | PIN_Y = 27 | ||
| + | PIN_G = 22 | ||
| + | # ADJUST AREA END | ||
| + | |||
| + | GPIO.setwarnings(False) | ||
| GPIO.setmode(GPIO.BCM) | GPIO.setmode(GPIO.BCM) | ||
| + | |||
| + | GPIO.setup(PIN_R, | ||
| GPIO.setup(PIN_Y, | GPIO.setup(PIN_Y, | ||
| + | GPIO.setup(PIN_G, | ||
| - | def setLED(pin, | + | |
| + | def setLED(pin, value): | ||
| if value == 1: | if value == 1: | ||
| - | GPIO.output(pin, | + | |
| - | else | + | else: |
| - | | + | GPIO.output(pin, |
| - | + | ||
| - | + | ||
| - | def on(): | + | |
| - | GPIO.output(PIN, | + | |
| - | def off(): | + | def setYellowLED(value): |
| - | | + | |
| + | |||
| + | def setRedLED(value): | ||
| + | setLED(PIN_R, | ||
| + | |||
| + | def setGreenLED(value): | ||
| + | setLED(PIN_G, | ||
| def status(): | def status(): | ||
| - | | + | |
| + | g = GPIO.input(PIN_G) | ||
| + | y = GPIO.input(PIN_Y) | ||
| + | return r, g, y | ||
| def cleanup(): | def cleanup(): | ||
| GPIO.cleanup() | GPIO.cleanup() | ||
| - | </ | ||
| - | |||
| - | === logic.py === | ||
| - | |||
| - | <code python logic.py> | ||
| - | from core import gpio_hw | ||
| - | |||
| - | def turn_on(): | ||
| - | gpio_hw.on() | ||
| - | return " | ||
| - | def turn_off(): | ||
| - | gpio_hw.off() | ||
| - | return " | ||
| - | |||
| - | def get_status(): | ||
| - | return " | ||
| </ | </ | ||
| - | |||
| Zeile 105: | Zeile 118: | ||
| # | # | ||
| - | from core import | + | from core import |
| - | from core import gpio_hw | + | |
| def main(): | def main(): | ||
| - | print(" | + | print(" |
| try: | try: | ||
| while True: | while True: | ||
| - | cmd = input(" | + | cmd = input(" |
| - | if cmd == "on": | + | if cmd in ("r", " |
| - | | + | |
| - | elif cmd == "off": | + | value = 1 if mode == " |
| - | | + | |
| - | elif cmd == "status": | + | if cmd == " |
| - | | + | hardware.setRedLED(value) |
| + | elif cmd == "y": | ||
| + | | ||
| + | elif cmd == "g": | ||
| + | | ||
| + | |||
| + | elif cmd == " | ||
| + | break | ||
| except KeyboardInterrupt: | except KeyboardInterrupt: | ||
| pass | pass | ||
| - | |||
| finally: | finally: | ||
| - | | + | |
| + | hardware.cleanup() | ||
| if __name__ == " | if __name__ == " | ||
| main() | main() | ||
| + | |||
| </ | </ | ||
| Zeile 146: | Zeile 166: | ||
| <code bash> | <code bash> | ||
| - | cd ~/ | + | cd ~/devel/projects/ |
| ./ | ./ | ||
| </ | </ | ||
| Zeile 152: | Zeile 172: | ||
| Bei erfolgreichem Start erscheint die Eingabeaufforderung von '' | Bei erfolgreichem Start erscheint die Eingabeaufforderung von '' | ||
| - | < | + | < |
| === Verhalten bei Eingaben testen === | === Verhalten bei Eingaben testen === | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | * unbekannte Eingabe → das Programm | + | |
| Wenn das Verhalten wie beschrieben ist, läuft das Programm korrekt und die Einrichtung war erfolgreich. | Wenn das Verhalten wie beschrieben ist, läuft das Programm korrekt und die Einrichtung war erfolgreich. | ||
project/python_led.1771406903.txt.gz · Zuletzt geändert: von torsten.roehl
