Template: 0.91inch-OLED-Module-RaspberryPi-Guides

From Waveshare Wiki
Revision as of 09:53, 17 January 2022 by Eng35 (talk | contribs)
Jump to: navigation, search

Hardware connection

Please connect the LCD to your Raspberry Pi by the 7PIn cable according to the table below

Connect to Raspberry Pi
OLED Raspberry Pi
BCM2835 Board
VCC 3.3V 3.3V
GND GND GND
DIN SPI:10 / I2C:2 SPI:MOSI / I2C:SDA.1
CLK SPI:11 / I2C:3 SPI:SCLK / I2C:SCL.1

Enable SPI interface

  • Open terminal, use command to enter the configuration page
sudo raspi-config
Choose Interfacing Options -> SPI -> Yes  to enable SPI interface

RPI open spi.png
Reboot Raspberry Pi:

sudo reboot

Please make sure that SPI interface was not used by other devices
I2C is the same, enter the configuration interface and select Interfaceing Options -> I2C -> Yes to open the IIC interface, and then restart

Install Libraries

  • Install BCM2835 libraries
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.68.tar.gz
tar zxvf bcm2835-1.68.tar.gz 
cd bcm2835-1.68/
sudo ./configure && sudo make && sudo make check && sudo make install
#For more details, please refer to http://www.airspayce.com/mikem/bcm2835/
  • Install wiringPi libraries
sudo apt-get install wiringpi

#For the Raspberry Pi system after May 2019, it may need to be upgraded

wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
#You will get 2.52 information if you install it correctly
  • Install Python libraries
#python2
sudo apt-get update
sudo apt-get install python-pip
sudo apt-get install python-pil
sudo apt-get install python-numpy
sudo pip install RPi.GPIO
sudo pip install spidev
#python3
sudo apt-get update
sudo apt-get install python3-pip
sudo apt-get install python3-pil
sudo apt-get install python3-numpy
sudo pip3 install RPi.GPIO
sudo pip3 install spidev

Download Examples

Open Raspberry Pi terminal and run the following command

sudo apt-get install p7zip-full
sudo wget  https://www.waveshare.com/w/upload/2/2c/OLED_Module_Code.7z
7z x OLED_Module_Code.7z -O./OLED_Module_Code
cd OLED_Module_Code/RaspberryPi

Run the demo codes

Please go into the RaspberryPi directory (demo codes) first and run the commands in terminal

C codes

  • Re-compile the demo codes
cd c
sudo make clean
sudo make -j 8
  • After the compilation is complete, the main file is generated, enter the command according to the OLED model you are using. If you have purchased a 1.3inch OLED Module (C), please enter the following command:
sudo ./main 1.3c

If you have purchased a 1.5inch RGB OLED Module, please enter the following command:

sudo ./main 1.5rgb

The command of each LCD model can check in the following table:

LCD model Command
0.91inch OLED Module
sudo ./main 0.91
0.95inch RGB OLED (A)/(B)
sudo ./main 0.95rgb
0.96inch OLED (A)/(B)
sudo ./main 0.96
1.3inch OLED (A)/(B)
sudo ./main 1.3
1.3inch OLED Module (C)
sudo ./main 1.3c
1.5inch OLED Module
sudo ./main 1.5
1.5inch RGB OLED Module
sudo ./main 1.5rgb
  • Please make sure that the SPI is not occupied by other devices, you can check in the middle of /boot/config.txt

python

  • Enter the python directory and run
cd python/examples
  • Run the demo of the corresponding model OLED, the program supports python2/3

If you have purchased a 1.3inch OLED Module (C), please enter:

# python2
sudo python OLED_1in3_c_test.py
# python3
sudo python3 OLED_1in3_c_test.py

If you have purchased a 1.5inch RGB OLED Module, please enter:

# python2
sudo python OLED_1in5_rgb_test.py
# python3
sudo python3 OLED_1in5_rgb_test.pyre>

The command of each LCD model can check in the following table:

LCD Model Command(python2)
0.91inch OLED Module
sudo python OLED_0in91_test.py
0.95inch RGB OLED (A)/(B)
sudo python OLED_0in95_rgb_test.py
0.96inch OLED (A)/(B)
sudo python OLED_0in96_test.py
1.3inch OLED (A)/(B)
sudo python OLED_1in3_c_test.py
1.3inch OLED Module (C)
sudo python OLED_1in3_test.py
1.5inch OLED Module
sudo python OLED_1in5_rgb_test.py
1.5inch RGB OLED Module
sudo python OLED_1in5_test.py
  • Please make sure that the SPI is not occupied by other devices, you can check in the middle of /boot/config.txt

C codes

A brief analysis of underlying interfaces

We have carried out the low-level encapsulation, if you need to know the internal implementation can go to the corresponding directory to check ,for the reason that the hardware platform and the internal implementation are different

We can open DEV_Config.c(.h) to see definitions,which in the catalogue RaspberryPi\c\lib\Config

1.There are three ways for C to drive:BCM2835 library, WiringPi library and Dev library respectively
2.We use Dev library by default. If you need to use BCM2835 or WiringPi to drive the RaspberryPi\c\Makefile, open RaspberryPi\c\Makefile and modify lines 13-15 as follows:  

RPI open spi1.png

  • Interface selection:
#define USE_SPI_4W  1
#define USE_IIC     0
Note:Switch SPI/I2C directly modified here
  • Data type
#define UBYTE      uint8_t
#define UWORD      uint16_t
#define UDOUBLE    uint32_t
  • Module initialization and exit processing
void DEV_Module_Init(void);
void DEV_Module_Exit(void);
Note: here is some GPIO processing before and after using the LCD screen.
  • Write GPIO
void DEV_Digital_Write(UWORD Pin, UBYTE Value)
Parameter: UWORD Pin: GPIO Pin number
UBYTE Value: level to be output, 0 or 1
  • Read GPIO
UBYTE DEV_Digital_Read(UWORD Pin)
Parameter:UWORD Pin:GPIO Pin number
Return value: level of GPIO, 0 or 1
  • GPIO mode setting
void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
Parameters:UWORD Pin: GPIO Pin number
UWORD Mode: Mode, 0: input, 1: output

GUI simple analysis

For the screen, if you need to draw pictures, display Chinese and English characters, display pictures, etc., you can use the upper application to do.,and we provide some basic functions here about some graphics processing in the directory RaspberryPi\c\lib\GUI\GUI_Paint.c(.h).
RPI open spi2.png
RaspberryPi\c\lib\Fonts is a character font which GUI dependent in the directory.
RPI open spi3.png

  • New Image Properties: Create a new image property, this property includes the image buffer name, width, height, flip Angle, color
void Paint_NewImage(UBYTE *image, UWORD Width, UWORD Height, UWORD Rotate, UWORD Color)
Parameters:
Image: the name of the image buffer, which is actually a pointer to the first address of the image buffer;
Width: image buffer Width;
Height: the Height of the image buffer;
Rotate: Indicates the rotation Angle of an image
Color: the initial Color of the image;
  • Select image buffer: Select image buffer, the purpose of the selection is that you can create multiple image attributes, image buffer can exist multiple, you can select each image you create
void Paint_SelectImage(UBYTE *image)
Parameters:
Image: the name of the image buffer, which is actually a pointer to the first address of the image buffer;
  • Image Rotation: Set the selected image rotation Angle, preferably after Paint_SelectImage(), you can choose to rotate 0, 90, 180, 270
void Paint_SetRotate(UWORD Rotate)
Parameters:
Rotate: ROTATE_0, ROTATE_90, ROTATE_180, and ROTATE_270 correspond to 0, 90, 180, and 270 degrees
  • Sets the size of the pixels
void Paint_SetScale(UBYTE scale)
Parameters:
Scale: the size of pixels, 2: each pixel occupies one digit; 4: Each pixel occupies two bits
  • Image mirror flip: Set the mirror flip of the selected image. You can choose no mirror, horizontal mirror, vertical mirror,or image center mirror.
void Paint_SetMirroring(UBYTE mirror)
Parameters:
Mirror: indicates the image mirroring mode. MIRROR_NONE, MIRROR_HORIZONTAL, MIRROR_VERTICAL, MIRROR_ORIGIN correspond to no mirror, about horizontal mirror, about vertical mirror, and about image center mirror respectively
  • Set points to display position and color in the buffer: here is the core GUI function, processing points display position and color in the buffer;
void Paint_SetPixel(UWORD Xpoint, UWORD Ypoint, UWORD Color)
Parameters:
Xpoint: the X position of a point in the image buffer
Ypoint: Y position of a point in the image buffer
Color: indicates the Color of the dot
  • Image buffer fill color: Fills the image buffer with a color, usually used to flash the screen into blank.
void Paint_Clear(UWORD Color)
Parameters:
Color: fill Color
  • Image buffer part of the window filling color: the image buffer part of the window filled with a certain color, generally as a window whitewashing function, often used for time display, whitewashing on a second
void Paint_ClearWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color)
Parameters:
Xstart: the x-starting coordinate of the window
Ystart: indicates the Y starting point of the window
Xend: the x-end coordinate of the window
Yend: indicates the y-end coordinate of the window
Color: fill Color
  • Draw points: In the image buffer, draw points on (Xpoint, Ypoint), you can choose the color, the size of the point, the style of the point
Draw points: In the image buffer, draw points on (Xpoint, Ypoint), you can choose the color, the size of the point, the style of the point
void Paint_DrawPoint(UWORD Xpoint, UWORD Ypoint, UWORD Color, DOT_PIXEL Dot_Pixel, DOT_STYLE Dot_Style)
Parameters:
Xpoint: indicates the X coordinate of a point
Ypoint: indicates the Y coordinate of a point
Color: fill Color
Dot_Pixel: The size of the dot, providing a default of eight size points
typedef enum {
DOT_PIXEL_1X1  = 1,	// 1 x 1
DOT_PIXEL_2X2  , 		// 2 X 2
DOT_PIXEL_3X3  , 	 	// 3 X 3
DOT_PIXEL_4X4  , 	 	// 4 X 4
DOT_PIXEL_5X5  , 		// 5 X 5
DOT_PIXEL_6X6  , 		// 6 X 6
DOT_PIXEL_7X7  , 		// 7 X 7
DOT_PIXEL_8X8  , 		// 8 X 8
} DOT_PIXEL;
Dot_Style: the size of a point that expands from the center of the point or from the bottom left corner of the point to the right and up
typedef enum {
DOT_FILL_AROUND  = 1,
DOT_FILL_RIGHTUP,
} DOT_STYLE;
  • Line drawing: In the image buffer, line from (Xstart, Ystart) to (Xend, Yend), you can choose the color, line width, line style
Line drawing: In the image buffer, line from (Xstart, Ystart) to (Xend, Yend), you can choose the color, line width, line style
void Paint_DrawLine(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color, LINE_STYLE Line_Style ,  LINE_STYLE Line_Style)
Parameters:
Xstart: the x-starting coordinate of a line
Ystart: indicates the Y starting point of a line
Xend: x-terminus of a line
Yend: the y-end coordinate of a line
Color: fill Color
Line_width: The width of the line, which provides a default of eight widths
typedef enum {
DOT_PIXEL_1X1  = 1,	// 1 x 1
DOT_PIXEL_2X2  , 		// 2 X 2
DOT_PIXEL_3X3  ,		// 3 X 3
DOT_PIXEL_4X4  ,		// 4 X 4
DOT_PIXEL_5X5  , 		// 5 X 5
DOT_PIXEL_6X6  , 		// 6 X 6
DOT_PIXEL_7X7  , 		// 7 X 7
DOT_PIXEL_8X8  , 		// 8 X 8
} DOT_PIXEL;
Line_Style: line style. Select whether the lines are joined in a straight or dashed way
typedef enum {
LINE_STYLE_SOLID = 0,
LINE_STYLE_DOTTED,
} LINE_STYLE;
  • Draw rectangle: In the image buffer, draw a rectangle from (Xstart, Ystart) to (Xend, Yend), you can choose the color, the width of the line, whether to fill the inside of the rectangle
void Paint_DrawRectangle(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color, DOT_PIXEL Line_width,  DRAW_FILL Draw_Fill)
Parameters:
Xstart: the starting X coordinate of the rectangle
Ystart: indicates the Y starting point of the rectangle
Xend: X terminus of the rectangle
Yend: specifies the y-end coordinate of the rectangle
Color: fill Color
Line_width: The width of the four sides of a rectangle. Default eight widths are provided
typedef enum {
DOT_PIXEL_1X1  = 1,	// 1 x 1
DOT_PIXEL_2X2  , 		// 2 X 2
DOT_PIXEL_3X3  ,		// 3 X 3
DOT_PIXEL_4X4  ,		// 4 X 4
DOT_PIXEL_5X5  , 		// 5 X 5
DOT_PIXEL_6X6  , 		// 6 X 6
DOT_PIXEL_7X7  , 		// 7 X 7
DOT_PIXEL_8X8  , 		// 8 X 8
} DOT_PIXEL;
Draw_Fill: Fill, whether to fill the inside of the rectangle
typedef enum {
DRAW_FILL_EMPTY = 0,
DRAW_FILL_FULL,
} DRAW_FILL;
  • Draw circle: In the image buffer, draw a circle of Radius with (X_Center Y_Center) as the center. You can choose the color, the width of the line, and whether to fill the inside of the circle
void Paint_DrawCircle(UWORD X_Center, UWORD Y_Center, UWORD Radius, UWORD Color, DOT_PIXEL Line_width,  DRAW_FILL Draw_Fill)
Parameters:
X_Center: the x-coordinate of the center of a circle
Y_Center: Y coordinate of the center of a circle
Radius: indicates the Radius of a circle
Color: fill Color
Line_width: The width of the arc, with a default of 8 widths
typedef enum {
DOT_PIXEL_1X1  = 1,	// 1 x 1
DOT_PIXEL_2X2  , 		// 2 X 2
DOT_PIXEL_3X3  ,		// 3 X 3
DOT_PIXEL_4X4  ,		// 4 X 4
DOT_PIXEL_5X5  , 		// 5 X 5
DOT_PIXEL_6X6  , 		// 6 X 6
DOT_PIXEL_7X7  , 		// 7 X 7
DOT_PIXEL_8X8  , 		// 8 X 8
} DOT_PIXEL;
Draw_Fill: fill, whether to fill the inside of the circle
typedef enum {
DRAW_FILL_EMPTY = 0,
DRAW_FILL_FULL,
} DRAW_FILL;
  • Write Ascii character: In the image buffer, at (Xstart Ystart) as the left vertex, write an Ascii character, you can select Ascii visual character library, font foreground color, font background color
void Paint_DrawChar(UWORD Xstart, UWORD Ystart, const char Ascii_Char, sFONT* Font, UWORD Color_Foreground,  UWORD Color_Background)
Parameters:
Xstart: the x-coordinate of the left vertex of a character
Ystart: the Y coordinate of the font's left vertex
Ascii_Char: indicates the Ascii character
Font: Ascii visual character library, in the Fonts folder provides the following Fonts:
Font8:5*8 font
Font12:7*12 font
Font16:11*16 font
Font20:14*20 font
Font24:17*24 font
Color_Foreground: Font color
Color_Background: indicates the background color
  • Write English string: In the image buffer, use (Xstart Ystart) as the left vertex, write a string of English characters, can choose Ascii visual character library, font foreground color, font background color
void Paint_DrawString_EN(UWORD Xstart, UWORD Ystart, const char * pString, sFONT* Font, UWORD Color_Foreground,  UWORD Color_Background)
Parameters:
Xstart: the x-coordinate of the left vertex of a character
Ystart: the Y coordinate of the font's left vertex
PString: string, string is a pointer
Font: Ascii visual character library, in the Fonts folder provides the following Fonts:
Font8:5*8 font
Font12:7*12 font
Font16:11*16 font
Font20:14*20 font
Font24:17*24 font
Color_Foreground: Font color
Color_Background: indicates the background color
  • Write Chinese string: in the image buffer, use (Xstart Ystart) as the left vertex, write a string of Chinese characters, you can choose GB2312 encoding character font, font foreground color, font background color;
void Paint_DrawString_CN(UWORD Xstart, UWORD Ystart, const char * pString, cFONT* font, UWORD Color_Foreground,  UWORD Color_Background)
Parameters:
Xstart: the x-coordinate of the left vertex of a character
Ystart: the Y coordinate of the font's left vertex
PString: string, string is a pointer
Font: GB2312 encoding character Font library, in the Fonts folder provides the following Fonts:
Font12CN: ASCII font 11*21, Chinese font 16*21
Font24CN: ASCII font24 *41, Chinese font 32*41
Color_Foreground: Font color
Color_Background: indicates the background color
  • Write numbers: In the image buffer,use (Xstart Ystart) as the left vertex, write a string of numbers, optionally Ascii visual character library, font foreground color, font background color
void Paint_DrawNum(UWORD Xpoint, UWORD Ypoint, double Nummber, sFONT* Font, UWORD Digit, UWORD Color_Foreground,   UWORD Color_Background)
 Parameters:
 Xpoint: the x-coordinate of the left vertex of a character
 Ypoint: the Y coordinate of the left vertex of the font
 Nummber: indicates the number displayed, which can be a decimal
 Digit: It's a decimal number
 Font: Ascii visual character library, in the Fonts folder provides the following Fonts:
 Font8:5*8 font
 Font12:7*12 font
 Font16:11*16 font
 Font20:14*20 font
 Font24:17*24 font
 Color_Foreground: Font color
 Color_Background: indicates the background color
  • Display time: in the image buffer,use (Xstart Ystart) as the left vertex, display time,you can choose Ascii visual character font, font foreground color, font background color;
void Paint_DrawTime(UWORD Xstart, UWORD Ystart, PAINT_TIME *pTime, sFONT* Font, UWORD Color_Background,  UWORD Color_Foreground)
Parameters:
Xstart: the x-coordinate of the left vertex of a character
Ystart: the Y coordinate of the font's left vertex
PTime: display time, here defined a good time structure, as long as the hour, minute and second bits of data to the parameter;
Font: Ascii visual character library, in the Fonts folder provides the following Fonts:
Font8:5*8 font
Font12:7*12 font
Font16:11*16 font
Font20:14*20 font
Font24:17*24 font
Color_Foreground: Font color
Color_Background: indicates the background color

Python(for Raspberry Pi)

Works with Python and Python3
Python are less complex compared to C

config.py

  • Select interface
Device_SPI = 1
Device_I2C = 0
Note: Switch SPI/I2C modified here
  • Module initialization and exit processing:
def module_init()
def module_exit()
Note:
1. Here is some GPIO processing before and after using the LCD screen.
2. The module_init() function is automatically called in the INIT () initializer on the LCD, but the module_exit() function needs to be called by itself
  • SPI write data
def spi_writebyte(data)
  • IIC write data
i2c_writebyte(reg, value):

main.py

The main function, if your Python version is Python2, is re-executed in Linux command mode as follows:

sudo python main.py

If your Python version is Python3, run the following command in Linux:

sudo python3 main.py

Painting by GUI

Because Python has an image library PIL official library link, it is very powerful, do not need to write code from the logical layer like C, can directly call to the image library for image processing, the following will take 1.54inch LCD as an example, the program used in a brief description

  • It need to use the image library, you need to install the library
sudo apt-get install python3-pil  

And then import the library

from PIL import Image,ImageDraw,ImageFont

from PIL import Image,ImageDraw,ImageFont

  • Define an image buffer to drawing, writing, and other functions on pictures
image1 = Image.new("1", (disp.width, disp.height), "WHITE")

The first parameter defines the color depth of the image, which is defined as "1" to indicate a bitmap of one bit depth. The second parameter is a tuple that defines the width and height of the image. The third parameter defines the default color of the buffer, which is defined as "WHITE."

  • Create a drawing object based on Image1 on which all drawing operations will be performed
draw = ImageDraw.Draw(image1)
  • Draw line
draw.line([(0,0),(127,0)], fill = 0)

The first parameter is a four-element tuple starting at (0, 0) and ending at (127,0). Draw a line. Fill ="0" means the color of the line is white

  • Draw frame
draw.rectangle([(20,10),(70,60)],fill = "WHITE",outline="BLACK")

The first argument is a tuple of four elements. (20,10) is the coordinate value in the upper left corner of the rectangle, and (70,60) is the coordinate value in the lower right corner of the rectangle. Fill =" WHITE" means BLACK inside, and outline="BLACK" means BLACK outside.

  • Draw circle
draw.arc((150,15,190,55),0, 360, fill =(0,255,0)

Draw an inscribed circle in the square, the first parameter is a tuple of 4 elements, with (150, 15) as the upper left corner vertex of the square, (190, 55) as the lower right corner vertex of the square, specifying the level of the rectangular frame The median line is an angle of 0 degrees, the angle becomes larger clockwise, the second parameter indicates the starting angle, the third parameter identifies the ending angle, and fill = 0 indicates that the line is white If it is not a square, the drawn is an ellipse, which is actually the drawing of an arc.

Besides arc, ellipse can draw solid circles

draw.ellipse((150,65,190,105), fill = 0)

The first parameter specifies the outer tangent rectangle of the string. Fill = 0 means that the inner fill color is white. If the outer tangent matrix of the ellipse is square, the ellipse is a circle.

  • Character

The ImageFont module needs to be imported and instantiated:

Font1 = ImageFont.truetype("../Font/Font01.ttf",25)
Font2 = ImageFont.truetype("../Font/Font01.ttf",35)
Font3 = ImageFont.truetype("../Font/Font02.ttf",32)

For a better visual experience, free fonts from the web are used here, and other TTF-ending font library files are also supported.
Note: Each character library contains different characters; If some characters cannot be displayed, it is recommended that they be used according to the encoding set used by the font library Write English characters can be used directly, write Chinese, because the encoding is GB2312, so need to add a U in front:

draw.text((5, 68), 'Hello world', fill = 0, font=Font1)
text= u"微雪电子"
draw.text((5, 200), text, fill = 0, font=Font3)

The first parameter is a two-element tuple with (5,68) as the left vertex, font font1, fill for font color, fill = 0, so the font color is white, and the second sentence shows’微雪电子’, font color is white.

  • Read local picture

The parameter is the image path.

  • Other functions

Python's image library is very powerful, if need to implement other more, can learn on the website http://effbot.org/imagingbook pil.