Index>Robot Kit>RaspArm-S Kit for RPi>Lesson 9 Multithreading OLED Screen Control

Lesson 9 Multithreading OLED Screen Control

2051

       In this lesson, we will use multithreading to display text and other information on the OLED screen.

9.1 Components used in this course


image.png


9.2 Introduction of OLED Screen

OLED (Organic Light-Emitting Diode), also known as organic electric laser display, organic light emitting semiconductor (Organic Electroluminesence Display, OLED). OLED is a kind of current-type organic light-emitting device, which produces light by the injection and recombination of carriers, and the luminous intensity is proportional to the injected current. The Alter robot uses an OLED screen to display the  expressions or some parameters of the robot. OLED Screen is a commonly used module on robot products. Due to the black non-luminous feature of OLED Screen, this type of screen has extremely high contrast. Even if the ambient light is strong, you can see the information on the OLED Screen clearly, and the power consumption is relatively low. We only need to connect the power supply and the GPIO port to control it.

If you want to use OLED Screen, you need to use a 4-pin cable with anti-reverse interface to connect the OLED screen to the IIC interface on the Robot HAT.

image.png

If you do not use Robot HAT driver board to connect with Raspberry Pi driver board, then you need to connect Vin of OLED screen to 5V or 3.3V of Raspberry Pi, and connect GND of OLED screen to GND of Raspberry Pi. Connect SCL of Robot HAT to SCL of OLED, and SCA of Robot HAT to SCA of Raspberry Pi. Please refer to the pin definition diagram of Raspberry Pi for specific pins.



9.3 Preparation

        You need to prepare a robot in the RaspArm-S that has been assembled, and make sure that the robot has been connected to the OLED Screen module.

image.png


9.4 Using multithreading to display text on OLED Screen

9.4.1 Learning the code program of this lesson

We use multi-threading to display text on the OLED Screen. Here, we use Subline IDE to view and edit the code program of this lesson. For the specific method, please see "2.4 Editing the Code Program in the Raspberry Pi" in Lesson 2. The specific code and comments are as follows:
    In the file manager of the MobaXterm terminal, find adeept_rasparms/CourseCode/05OLED_Multithreading, and open the code of this lesson: OLED.py.

Import related dependent libraries.

image.png 


Set to display the initial content of each line on the OLED.

image.png 


ID used to pause the thread.

image.png 


Set to True.

image.png 


ID used to stop the thread.

image.png 


Set running to True.

image.png

 

Return immediately when it is True, block when it is False, and return when the internal flag is True.

image.png 


Set to False to block the thread.

image.png 


Set to True to stop blocking with thread. 

image.png 


Resume the thread from the suspended state, if it has been suspended.

image.png 


Set to False.

image.png 


Call this function to control the OLED screen. Position is the number of the line where you want to change the content, can be 1-6, text is the content.

image.png 


Instantiate the OLED screen object.

image.png 


Start the thread.

image.png 


Set the content of the first line to 12345678.

image.png 


Since the above operation will not block the thread, we need to loop here to avoid the program exit after the execution is complete.

image.png



9.4.2 Run theprogram of this course

1. Open the terminal software MobaXterm:


image.png


2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):

image.png


3. The relevant code programs of the RaspArm-S robot are stored in the adeept_rasparms folder, which has been explained in "2.1 Downloading the code program for controlling the robot" in Lesson 2. First, you need to enter a command with the command window of the Raspberry Pi to enter the folder where the course code is stored: CourseCode, this folder stores the sample code program for each course, enter the following command:

cd  adeept_rasparms/CourseCode

image.png


4. Enter the command to display the contents of the current directory:

ls

image.png


5.  The 05OLED_Multithreading folder contains the sample code of this lesson. Enter the command to enter this folder:

cd  05OLED_Multithreading

image.png


6. Enter the command to display the contents of the current directory:

ls

image.png


7.When using the OLED Screen module, we need to install the Python dependent library needed to control the OLED screen, called luma.oled library, and enter the following commands in the console of the command window:

sudo pip3 install luma.oled


image.png 


8. OLED.py is the sample code for this lesson, enter the command to run this program:

sudo  python3  OLED.py


image.png 

9. After running the program successfully, you will see some text messages on the OLED screen of the robot.