# This is a thread test Das ist ein Thread-Test
# When is a thread terminated Wann wird ein Thread beendet
# and which threads are still available? und welche Threads sind noch verfügbar?
# Improved version with LOCK Verbesserte Version mit LOCK
# NOTE: _thread is NOT official supported by micropythen
# but it looks like to work for multitasking
# HINWEIS: _thread wird NICHT offiziell von MicroPythen unterstützt,
# aber es sieht so aus, als würde es für Multitasking funktionieren!!!
# Do the imports
# Importe machen
from machine import Pin
import time
import _thread
# Define the LEDs
# LEDs setzen
red = Pin(32, Pin.OUT)
yello = Pin(33, Pin.OUT)
green = Pin(25, Pin.OUT)
# How often should the LED flash
# Wie oft soll die LED blinken
do_blinks = 5
# List of active Threads
# Liste der aktiven Threads
thread_list = set()
# Semapho
# Signalgeber
lock = _thread.allocate_lock()
def blink(name, led, pause):
# HERE IS _thread STARTING
# Hier startet der Thread
lock.acquire()
thread_list.add(name)
lock.release()
for i in range(do_blinks):
led.on()
time.sleep(pause)
led.off()
time.sleep(pause)
# HERE IS _thread ENDING
# Hier wird der Thread beendet
lock.acquire()
thread_list.discard(name)
lock.release()
# Run the Treads-Funtion "blink"(Print out the Name of Thread, Color of LED, Seconds for on/off)
_thread.start_new_thread(blink,("RED", red, 1))
_thread.start_new_thread(blink,("YELLO", yello, 0.5))
_thread.start_new_thread(blink,("GREEN", green, 0.25))
# wait for the first thread is started
# warten bis der erste Thread gestartet ist
while not thread_list: pass
# Print active threads until there are none left
# aktive Threads ausgeben, bis keiner mehr vorhanden ist
while thread_list:
print(thread_list)
time.sleep(0.10)
print("Now I'm really done! - Jetzt bin ich wirklich fertig!")