Difference between revisions of "Template:E-Paper Codes Description"

From Waveshare Wiki
Jump to: navigation, search
(C (Used for Jetson Nano、Raspberry Pi、STM32))
(C (Used for Jetson Nano、Raspberry Pi、STM32))
Line 296: Line 296:
 
<pre>
 
<pre>
 
void Paint_DrawString_EN(UWORD Xstart, UWORD Ystart, const char * pString, sFONT* Font, UWORD Color_Foreground, UWORD Color_Background)
 
void Paint_DrawString_EN(UWORD Xstart, UWORD Ystart, const char * pString, sFONT* Font, UWORD Color_Foreground, UWORD Color_Background)
参数:
+
Parameters:
 
  Xstart: X coordinate of left-top pixel of characters;
 
  Xstart: X coordinate of left-top pixel of characters;
 
  Ystart: Y coordinate of left-top pixel of characters;
 
  Ystart: Y coordinate of left-top pixel of characters;
Line 310: Line 310:
 
</pre>
 
</pre>
  
*写中文字符串:在图像缓存中,在 (Xstart Ystart) 为左顶点,写一串中文字符,可以选择GB2312编码字符字库、字体前景色、字体背景色;
+
*Draw Chinese charactgers: this function is used to draw Chinese fonts based ON GB2312 fonts.
 
<pre>
 
<pre>
 
void Paint_DrawString_CN(UWORD Xstart, UWORD Ystart, const char * pString, cFONT* font, UWORD Color_Foreground, UWORD Color_Background)
 
void Paint_DrawString_CN(UWORD Xstart, UWORD Ystart, const char * pString, cFONT* font, UWORD Color_Foreground, UWORD Color_Background)
参数:
+
Parameter:
  Xstart: 字符的左顶点X坐标
+
  Xstart: Coordinate of left-top pixel of characters;
  Ystart: 字体的左顶点Y坐标
+
  Ystart: Coordinate of left-top pixel of characters;
  pString:字符串,字符串是一个指针
+
  pString:Pointer of string;
  Font: GB2312编码字符字库,在Fonts文件夹中提供了以下字体:
+
  Font: GB2312 fonts:
  font12CN:ascii字符字体11*21,中文字体16*21
+
  font12CN:11*21(ascii),16*21 (Chinese)
  font24CN:ascii字符字体24*41,中文字体32*41
+
  font24CN:24*41(ascii),32*41 (Chinese)
  Color_Foreground: 字体颜色
+
  Color_Foreground: color of string
  Color_Background: 背景颜色
+
  Color_Background: color of background
 
</pre>
 
</pre>
  

Revision as of 10:47, 9 September 2019

About the codes

We provide examples for four popular hardware platforms: Arduino UNO, Jetson UNO, Raspberry Pi, and STM32. (This is common Template for all e-Paper, some of the description/function may not be used by the e-Paper you have)
Every project is divided into hardware interface, EPD driver and the application function;
The programming languages are C\C++\python:

  • Arduino UNO:C++
  • Jetson Nano:C and python
  • Raspberry Pi:C and python
  • STM32:C

Note:
The EPD driver of C codes of Jetson Nano, Raspberry Pi and STM32 are compatible. Except the hardware interface, the codes are same;

C (Used for Jetson Nano、Raspberry Pi、STM32)

Hardware interface

Because of multiple hardware platforms, we packge the bottom, for details of how it realizes, you go to related directory for certain codes

In file DEV_Config.c(.h):
For Raspberry Pi, the files are located in: RaspberryPi&JetsonNano\c\lib\Config

Here we use two libraries: bcm2835 and wiringPi
WiringPi library is used by default, if you want to use bcm2835 libraries, you just need to modify RaspberryPi&JetsonNano\c\Makefile file, change the lines 13 and 14 as below::
E-paper Driver HAT RPI Makefile.png

For Jetson Nano, the files are located in RaspberryPi&JetsonNano\c\lib\Config
For STM32, the files are located in STM32\STM32-F103ZET6\User\Config

  • Data type:
#define UBYTE   uint8_t
#define UWORD   uint16_t
#define UDOUBLE uint32_t
  • Module Init and Exit handle:
void DEV_Module_Init(void);
void DEV_Module_Exit(void);

Note:

1.The functions are used to set GPIP before and after driving e-Paper.

2.If the board you have is printed with Rev2.1, module enter low-ultra mode after DEV_Module_Exit(). (as we test, the current is about 0 in this mode);

  • GPIO Read/Write:
void DEV_Digital_Write(UWORD Pin, UBYTE Value);
UBYTE DEV_Digital_Read(UWORD Pin);
  • SPI Write data
void DEV_SPI_WriteByte(UBYTE Value);
EPD driver

For Raspberry Pi and Jetson Nano, epd driver are saved in:RaspberryPi&JetsonNano\c\lib\e-Paper
For STM32, the epd driver are saved in: STM32\STM32-F103ZET6\User\e-Paper
E-paper Driver HAT RPI epd.png
Open .h file, functions are declarated here

  • Initialization: It should be used to initialize e-Paper or wakeup e-Paper from sleep mode.
//1.54inch e-Paper、1.54inch e-Paper V2、2.13inch e-Paper、2.13inch e-Paper  V2、2.13inch e-Paper (D)、2.9inch e-Paper、2.9inch e-Paper (D)
void EPD_xxx_Init(UBYTE Mode); // Mode = 0 Initialize full refresh; Mode = 1 Initilize partial refresh 
//Other types
void EPD_xxx_Init(void);

xxx is the type of e-paper, for example, if the e-paper you have is 2inch e-Paper (D), then it should be EPD_2IN13D_Init(0) or EPD_2IN13D_Init(1); If it is 7.5inch e-Paper (B), the function should be EPD_7IN5BC_Init(). B type and C type of 7.5inch e-Paper use the same codes.

  • Clear display: This function is used to clear the e-paper to white
void EPD_xxx_Clear(void);

xxx is the type of e-Paper. For example, if the e-Paper you have is 4.2inch e-Paper, it should be EPD-4IN2_Clear()

  • Transmit a frame of image and display
//Black/White e-Paper
void EPD_xxx_Display(UBYTE *Image);
//Three colors e-Paper
void EPD_xxx_Display(const UBYTE *blackimage, const UBYTE *ryimage);


There are some exceptions:

//To partial refresh 2.13inch e-paper (D)、2.9inch e-paper (D), you should use
 void EPD_2IN13D_DisplayPart(UBYTE *Image);
 void EPD_2IN9D_DisplayPart(UBYTE *Image);


//Because controllers of 1.54inch e-Paper V2 and 2.13inch e-Paper V2 were updated, you need to use EPD_xxx_DisplayPartBaseImage to display static image and ten use EPD_xxx_displayPart() to dymatic display when partial refreshing.
void EPD_1IN54_V2_DisplayPartBaseImage(UBYTE *Image);
void EPD_1IN54_V2_DisplayPart(UBYTE *Image);
void EPD_2IN13_V2_DisplayPart(UBYTE *Image);
void EPD_2IN13_V2_DisplayPartBaseImage(UBYTE *Image);


//Because STM32103ZET5 has no enough RAM for image, therefore 7.5B、7.5C、5.83B、5.83C can only display half of the screen:'''
void EPD_7IN5BC_DisplayHalfScreen(const UBYTE *blackimage, const UBYTE *ryimage);
void EPD_5IN83BC_DisplayHalfScreen(const UBYTE *blackimage, const UBYTE *ryimage);

xxx is the type of e-Paper

  • Enter sleep mode
void EPD_xxx_Sleep(void);

Note, You should hardware reset or use initialize function to wake up e-Paper from sleep mode
xxx is type of e-Paper

Application function

Basic drawing functions are provided here. You can find then in:
Raspbian Pi & Jetson Nano: RaspberryPi&JetsonNano\c\lib\GUI\GUI_Paint.c(.h)
STM32: STM32\STM32-F103ZET6\User\GUI\GUI_Paint.c(.h)
E-paper Driver HAT GUI.png
The fonts are saved in the directory:
Raspberry Pi & Jetson Nano: RaspberryPi&JetsonNano\c\lib\Fonts
STM32: STM32\STM32-F103ZET6\User\Fonts
E-paper Driver HAT Fonts.png

  • Create a new image buffer: This function is used to create a new image with width, height, Rotate degree and its color.
void Paint_NewImage(UBYTE *image, UWORD Width, UWORD Height, UWORD Rotate, UWORD Color)
Paratemeters:
 	image : The buffer of image, this is an pointer of buffer address;
 	Width : width of the image;
 	Height: height of the image;
 	Rotate:Rotate degree;
 	Color :Initial color of the image;
  • Select image buffer: this function is used to select the image buffer. You can create multiple image buffer with last function, then select the buffer for every image.
void Paint_SelectImage(UBYTE *image)
Parameters:
 	image: The name of image buffer, it is a pointer of buffer address;
  • Set display orientation: This function is used to set the rotate degree, it is generally be used after Paint_SelectImage(). You can set the rotate degree to 0、90、180、270 degree.
void Paint_SetRotate(UWORD Rotate)
Parameters:
 	Rotate: Rotate degree, you can choose ROTATE_0、ROTATE_90、ROTATE_180、ROTATE_270 which stands for 0、90、180、270 degree repetitively.
  • Image mirroring: This function is used to mirror image.
void Paint_SetMirroring(UBYTE mirror)
Paramters:
 	mirror: You can set it to MIRROR_NONE、MIRROR_HORIZONTAL、MIRROR_VERTICAL、MIRROR_ORIGIN
  • Set pixel: this function is used to set the position and color of pixels in the buffer. This is the basic function of GUI.
void Paint_SetPixel(UWORD Xpoint, UWORD Ypoint, UWORD Color)
Parameters:
 	Xpoint: X-axes in buffer;
 	Ypoint: Y-axes in buffer;
 	Color : color
  • Clear: This function is used to clear the screen to certain color.
void Paint_Clear(UWORD Color)
Parameter:
 	Color:
  • Clear windows:this function is used to clear a window. It is generally used for time display.
void Paint_ClearWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color)
Parameters:
 	Xstart: Start coordinate of X-axes of window;
 	Ystart: Start coordinate of Y-axes of window;
 	Xend: End coordinate of X-axes of window;
 	Yend: End coordinate of Y-axes of window;
 	Color: 
  • Draw point: Draw a point on the position (Xpoint, Ypoint)in buffer
void Paint_DrawPoint(UWORD Xpoint, UWORD Ypoint, UWORD Color, DOT_PIXEL Dot_Pixel, DOT_STYLE Dot_Style)
Parameter:
 	Xpoint: X coordinate of point;
 	Ypoint: Y coordinate of point;
 	Color: color of point;
 	Dot_Pixel: the size of point, there are 8 sizes available;
 	 	 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: style of point. 
 	 	typedef enum {
 	 	   DOT_FILL_AROUND  = 1,		
 	 	   DOT_FILL_RIGHTUP,
 	 	} DOT_STYLE;
  • Draw line: draw a line for (Xstart, Ystart) to (Xend, Yend)
void Paint_DrawLine(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color, LINE_STYLE Line_Style , LINE_STYLE Line_Style)
Parameter:
 	Xstart: Start coordinate of X-axes of line;
 	Ystart: Start coordinate of Y-axes of line;
 	Xend: End coordinate of X-axes of line;
 	Yend: End coordinate of Y-axes of line;
 	Color:  color of line
 	Line_width: the width of line, 8 sizes are avalilable;
 	 	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: Style of the line;
 	 	typedef enum {
 	 	 	 LINE_STYLE_SOLID = 0,
 	 	 	 LINE_STYLE_DOTTED,
 	 	} LINE_STYLE;
  • Draw rectangle: Draw a rectangle from (Xstart, Ystart) to (Xend, Yend).
void Paint_DrawRectangle(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend, UWORD Color, DOT_PIXEL Line_width, DRAW_FILL Draw_Fill)
Parameter:
 	Xstart: Start coordinate of X-axes of rectangle
 	Ystart: Start coordinate of Y-axes of rectangle
 	Xend: End coordinate of X-end of rectangle
 	Yend: End coordinate of Y-end of rectangle
 	Color: color of rectangle
 	Line_width: The width of edges, 8 sides are available;
 	 	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: 填充,是否填充矩形的内部
 	 	typedef enum {
 	 	 	 DRAW_FILL_EMPTY = 0,
 	 	 	 DRAW_FILL_FULL,
 	 	} DRAW_FILL;
  • Draw circle:Draw a circle, use (X_Center Y_Center) as center;
void Paint_DrawCircle(UWORD X_Center, UWORD Y_Center, UWORD Radius, UWORD Color, DOT_PIXEL Line_width, DRAW_FILL Draw_Fill)
参数:
 	X_Center: X coordinate of center
 	Y_Center: Y coordinate of center
 	Radius:Radius of circle
 	Color: color of circle
 	Line_width: width of circle, 8 sizes are avalilable
 	 	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: style of circle
 	 	typedef enum {
 	 	 	 DRAW_FILL_EMPTY = 0,
 	 	 	 DRAW_FILL_FULL,
 	 	} DRAW_FILL;
  • Draw character (ASCII): Set(Xstart Ystart) as letf-top point, draw a ASCII character.
void Paint_DrawChar(UWORD Xstart, UWORD Ystart, const char Ascii_Char, sFONT* Font, UWORD Color_Foreground, UWORD Color_Background)
Parameter:
 	Xstart: X coordinate of left-top pixel of character;
 	Ystart: Y coordinate of left-top pixel of character;
 	Ascii_Char:Ascii character;
 	Font: 5 fonts are available;
 	 	font8:5*8
 	 	font12:7*12
 	 	font16:11*16
 	 	font20:14*20
 	 	font24:17*24
 	Color_Foreground: color of character;
 	Color_Background: color of background;
  • Draw String: Set point (Xstart Ystart) as the left-top pixel, draw a string.
void Paint_DrawString_EN(UWORD Xstart, UWORD Ystart, const char * pString, sFONT* Font, UWORD Color_Foreground, UWORD Color_Background)
Parameters:
 	Xstart: X coordinate of left-top pixel of characters;
 	Ystart: Y coordinate of left-top pixel of characters;
 	pString;Pointer of string
 	Font: 5 fonts are available:
 	 	font8:5*8
 	 	font12:7*12
 	 	font16:11*16
 	 	font20:14*20
 	 	font24:17*24
 	Color_Foreground: color of string
 	Color_Background: color of background
  • Draw Chinese charactgers: this function is used to draw Chinese fonts based ON GB2312 fonts.
void Paint_DrawString_CN(UWORD Xstart, UWORD Ystart, const char * pString, cFONT* font, UWORD Color_Foreground, UWORD Color_Background)
Parameter:
 	Xstart: Coordinate of left-top pixel of characters;
 	Ystart: Coordinate of left-top pixel of characters;
 	pString:Pointer of string;
 	Font: GB2312 fonts:
 	 	font12CN:11*21(ascii),16*21 (Chinese)
 	 	font24CN:24*41(ascii),32*41 (Chinese)
 	Color_Foreground: color of string
 	Color_Background: color of background
  • 写数字:在图像缓存中,在 (Xstart Ystart) 为左顶点,写一串数字,可以选择Ascii码可视字符字库、字体前景色、字体背景色
void Paint_DrawNum(UWORD Xpoint, UWORD Ypoint, int32_t Nummber, sFONT* Font, UWORD Color_Foreground, UWORD Color_Background)
参数:
 	Xstart: 字符的左顶点X坐标
 	Ystart: 字体的左顶点Y坐标
 	Nummber:显示的数字,这里使用的是32位长的int型保存,可以最大显示到2147483647
 	Font: Ascii码可视字符字库,在Fonts文件夹中提供了以下字体:
 	 	font8:5*8的字体
 	 	font12:7*12的字体
 	 	font16:11*16的字体
 	 	font20:14*20的字体
 	 	font24:17*24的字体
 	Color_Foreground: 字体颜色
 	Color_Background: 背景颜色
  • 显示时间:在图像缓存中,在 (Xstart Ystart) 为左顶点,显示一段时间,可以选择Ascii码可视字符字库、字体前景色、字体背景色;这里是方便测试局部刷新而写的,因为局部刷新需要的时间为0.3S,整体显示少于1S加上数据的传输,可以做到1S刷新一次
void Paint_DrawTime(UWORD Xstart, UWORD Ystart, PAINT_TIME *pTime, sFONT* Font, UWORD Color_Background, UWORD Color_Foreground)
参数:
 	Xstart: 字符的左顶点X坐标
 	Ystart: 字体的左顶点Y坐标
 	pTime:显示的时间,这里定义好了一个时间的结构体,只要把时分秒各位数传给参数;
 	Font: Ascii码可视字符字库,在Fonts文件夹中提供了以下字体:
 	 	font8:5*8的字体
 	 	font12:7*12的字体
 	 	font16:11*16的字体
 	 	font20:14*20的字体
 	 	font24:17*24的字体
 	Color_Foreground: 字体颜色
 	Color_Background: 背景颜色
  • 写图片:把一个位图写入图像缓存中
void Paint_DrawBitMap(const unsigned char* image_buffer)
参数:
 	image_buffer: 图像数据的缓存中的首地址
  • 读取本地的bmp图片并写到缓存中

对于Jetson Nano, Raspberry Pi这些Linux操作系统的,可以读写图片
对于Raspberry Pi和Jetson Nano,在目录:RaspberryPi&JetsonNano\c\lib\GUI\GUI_BMPfile.c(.h)

UBYTE GUI_ReadBmp(const char *path, UWORD Xstart, UWORD Ystart)
参数:
	path:BMP图片的相对路径
 	Xstart: 图片的左顶点X坐标,一般默认传0
 	Ystart: 图片的左顶点Y坐标,一般默认传0

用户测试代码

前三个章节介绍了经典的linux三层代码结构,这里稍微讲解一下用户测试代码
对于Raspberry Pi和Jetson Nano,在目录:RaspberryPi&JetsonNano\c\examples,为全部的测试代码,在本目录下的main.c中可以多个屏蔽;
E-Paper Shield c test.png
如果需要运行7.5inch e-paper测试程序,你需要把42行的屏蔽去掉

// EPD_7in5_test();

改成

EPD_7in5_test();

在linux命令模式下重新执行如下:

make clean
make
sudo ./epd

对于STM32,在目录:STM32\STM32-F103ZET6\User\Examples,为全部的测试代码,可以打开工程后在mai.c中本目录下的main.c中可以多个屏蔽;
打开工程:STM32\STM32-F103ZET6\MDK-ARM\epd-demo.uvprojx
E-paper Driver stm32 main.png
如果需要运行7.5inch e-paper测试程序,你需要把96行的屏蔽去掉

// EPD_7in5_test();

改成

EPD_7in5_test();

在Keil中重新编译并选择下载器下载

Python(适用于Jetson Nano\Raspberry Pi)

适用于python2.7和python3
对于python而言他的调用没有C复杂
Raspberry Pi和Jetson Nano:RaspberryPi&JetsonNano\python\lib\
E-paper Driver python lib.png

epdconfig.py

  • 模块初始化与退出的处理:
def module_init()
def module_exit()
 注意:
 1.这里是处理使用墨水屏前与使用完之后一些GPIO的处理。
 2.对于PCB带有Rev2.1的,module_exit()之后整个模块会进入低功耗,经过测试这个功耗基本为0;
  • GPIO读写:
def  digital_write(pin, value)
def  digital_read(pin)
  • SPI写数据
def spi_writebyte(data)

epdxxx.py(xxx表示尺寸,若是2.13inch e-paper,则为epd2in13.py,依此类推)

  • 墨水屏初始化,再屏幕开始工作时和退出睡眠模式之后调用
对于1.54inch e-Paper、1.54inch e-Paper V2、2.13inch e-Paper、2.13inch e-Paper  V2、2.13inch e-Paper (D)、2.9inch e-Paper、2.9inch e-Paper (D)
def init(self, update) # 选择lut_full_update或lut_partial_update
其他型号
def init(self)
  • 清屏,把墨水屏刷成白色
def Clear(self)
def Clear(self, color) # 对于某几个屏幕需要调用这个
  • 把图片转换成数组
def getbuffer(self, image)
  • 传输一帧的图片数据并打开显示
黑白双色墨水屏
def display(self, image)
黑白红或黑白黄墨水屏
def display(self, blackimage, redimage)

需要注意以下的几个是特例:<br />
对于2.13inch e-paper (D)、2.9inch e-paper (D)两款柔性屏幕,局部刷新
def DisplayPartial(self, image)

对于1.54inch e-paper V2、2.13inch e-paper V2由于控制芯片升级,对于局部刷新,需要调用displayPartBaseImage()显示静态的背景图片,也就是以这个图片为基础进行局部刷新,然后调用动态的displayPart()
def displayPartBaseImage(self, image)
def displayPart(self, image)
  • 进入睡眠模式
def sleep(self)

epd_xxx_test.py(xxx表示尺寸,若是2.13inch e-paper,则为epd_2in13_test.py,依此类推)

python在如下目录:
Raspberry Pi和Jetson Nano:RaspberryPi&JetsonNano\python\examples\
E-paper Driver python examples.png
如果你的python版本是python2,且需要运行7.5inch e-paper测试程序,在linux命令模式下重新执行如下:

sudo python epd_7in5_test.py

如果你的python版本是python3,且需要运行7.5inch e-paper测试程序,在linux命令模式下重新执行如下:

sudo python3 epd_7in5_test.py

Arduino

对于Arduino,由于Arduino UNO的内存不够,部分例程写了怎么写字符,但是不推荐这样使用,这样复杂化了Arduino的操作,如果需要用e-paper Sheild驱动