Endlich alles ins Git.

This commit is contained in:
2020-09-11 09:01:51 +02:00
commit 08a5370c0f
20 changed files with 1525 additions and 0 deletions

35
Versuch 2/aufgabe_a.py Normal file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env python3
from time import sleep
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! Run this script as root.")
exit(1)
LED_RED = 17
LED_GREEN = 18
def setup():
# Run GPIO with PI pin numbers.
GPIO.setmode(GPIO.BCM)
# Set outputs fpr led pins
GPIO.setup(LED_RED, GPIO.OUT)
GPIO.setup(LED_GREEN, GPIO.OUT)
def main():
setup()
try:
while True:
GPIO.output(LED_RED, GPIO.HIGH)
GPIO.output(LED_GREEn, GPIO.LOW)
sleep(1)
GPIO.output(LED_RED, GPIO.LOW)
GPIO.output(LED_GREEn, GPIO.HIGH)
sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
if __name__ == "__main__":
main()

58
Versuch 2/aufgabe_b.py Normal file
View File

@@ -0,0 +1,58 @@
#!/usr/bin/env python3
from time import sleep
import threading
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! Run this script as root.")
exit(1)
class LED(Thread):
def __init__(self, pin, frequency, use_pwm=False):
Thread.__init__(self)
self.__pin = pin
self.__frequency
self.__use_pwm = use_pwm
self.__pwm = None
GPIO.setup(pin, GPIO.OUT)
if use_pwm:
self.__pwm = GPIO.PWM(pin, frequency)
def new_random_frequency(self):
self.__frequency = random.randint(1, 100)
def run(self):
try:
if self.__use_pwm:
self.__pwm.start(0)
while True:
for s in range(0, 1, 0.1):
self.__pwm.ChangeDutyCycle(math.sin(s * math.pi))
#sleep(1 / self.__frequency)
sleep(1)
else:
while True:
GPIO.output(LED_GREEn, GPIO.LOW)
sleep(1 / self.__frequency)
GPIO.output(LED_GREEn, GPIO.HIGH)
sleep(1 / self.__frequency)
except KeyboardInterrupt:
GPIO.cleanup()
def setup():
# Run GPIO with PI pin numbers.
GPIO.setmode(GPIO.BCM)
def main():
setup()
# Pin, frequency in Hz
led_thread = LED(17, 1000)
led_thread.run()
if __name__ == "__main__":
main()

41
Versuch 2/aufgabe_c.py Normal file
View File

@@ -0,0 +1,41 @@
#!/usr/bin/env python3
from time import sleep
import threading
import aufgabe_b
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! Run this script as root.")
exit(1)
TASTER_PIN = 2
def setup():
# Run GPIO with PI pin numbers.
GPIO.setmode(GPIO.BCM)
GPIO.setup(TASTER_PIN, GPIO.IN)
def main():
setup()
# Pin, frequency in Hz
led_thread1 = LED(17, 1000)
led_thread1.run()
led_thread2 = LED(18, 1000)
led_thread2.run()
# read taster
try:
while True:
if GPIO.input(TASTER_PIN) == 1:
print("Taster was pressed. Set new frequency.")
led_thread1.new_random_frequency()
led_thread2.new_random_frequency()
except KeyboardInterrupt:
GPIO.cleanup()
if __name__ == "__main__":
main()

26
Versuch 2/aufgabe_g.py Normal file
View File

@@ -0,0 +1,26 @@
#!/usr/bin/env python3
from time import sleep
import threading
import aufgabe_b
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! Run this script as root.")
exit(1)
def setup():
# Run GPIO with PI pin numbers.
GPIO.setmode(GPIO.BCM)
def main():
setup()
# Pin, frequency in Hz
led_thread1 = LED(17, 1000, use_pwm=True)
led_thread1.run()
if __name__ == "__main__":
main()

View File