Tutorial

Table Of Contents
75
14 Make A Police Light or Breathing Light
14.1 Multi-threading Introduction
This chapter introduces the use of multi-threading to achieve some effects related to WS2812 LED
lights. Multi-threading is a commonly used operation in robot projects. Because robots have high requirements
for real-time response, when performing a certain task, try not to block main thread communication.
Multi-threading is similar to executing multiple different programs or tasks simultaneously.
Multi-threading has the following advantages:
·Using threads can put long-running tasks in the background for processing.
· To improve the operating efficiency of the program, the subsequent real-time video and OpenCV
processing of video frames use multi-threading to greatly increase the frame rate.
· The encapsulated multi-threaded task is more convenient to call, similar to the 10.3 non-blocking
control method is to use multi-threaded encapsulated control method
We use Python's threading library to provide thread-related work. Threads are the smallest unit of work
in an application. The current version of Python does not provide multi-thread priority, thread group, and thread
cannot be stopped, suspended, resumed and interrupted.
14.2 Realization of Police Lights / Breathing Lights
We use the following code to achieve multi-threaded control of LED lights, and when the LED does not
change, keep the thread blocked, so as not to waste CPU resources.
The wait () method is used here to block the thread, from the realization of the need to control the
thread.
import time
import sys
from rpi_ws281x import *
import threading