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:39] – 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 | + | |
| + | ===== Programmstruktur | ||
| <note important> | <note important> | ||
| - | **Akiviere in die Environment** | + | **Aktiviere |
| Die Programmierung erfolgt nun immer mit der gewählten Umgebung! | Die Programmierung erfolgt nun immer mit der gewählten Umgebung! | ||
| Zeile 17: | Zeile 21: | ||
| </ | </ | ||
| - | Für das eigentliche Projekt wird der Code nun strukturiert aufgebaut. | + | Die Aufteilung in mehrere Dateien sorgt für eine klare Trennung zwischen Programmlogik und Hardware-Ansteuerung. Dadurch bleibt |
| - | Ziel ist es, mehrere LEDs (R, G, Y) flexibel ansteuern zu können, ohne bei jeder Änderung den Quellcode neu anpassen oder Hardcodierungen verändern zu müssen. | + | |
| - | 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> | ||
| Zeile 32: | Zeile 33: | ||
| └── src | └── src | ||
| ├── core | ├── core | ||
| - | │ | + | │ |
| - | │ | + | |
| │ | │ | ||
| └── ledcontrol.py | └── ledcontrol.py | ||
| Zeile 45: | Zeile 45: | ||
| <code bash> | <code bash> | ||
| + | cd ~/ | ||
| touch __init__.py | touch __init__.py | ||
| </ | </ | ||
| Zeile 55: | 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 setYellowLED(value): | |
| - | def on(): | + | |
| - | | + | |
| + | def setRedLED(value): | ||
| + | | ||
| - | def off(): | + | def setGreenLED(value): |
| - | | + | |
| 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 113: | 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 154: | Zeile 166: | ||
| <code bash> | <code bash> | ||
| - | cd ~/ | + | cd ~/devel/projects/ |
| ./ | ./ | ||
| </ | </ | ||
| Zeile 160: | 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.1771407574.txt.gz · Zuletzt geändert: von torsten.roehl
