Difference between revisions of "E-paper for Arduino Use"

From ingcool
Jump to: navigation, search
(创建页面,内容为“==Arduino使用例程== 例程均在Arduino uno上进行了测试,如果需要是其他型号的Arduino需要自己确定连接的管脚是否正确。<br /> ===…”)
 
 
Line 1: Line 1:
==Arduino使用例程==
+
==Working with Arduino==
例程均在Arduino uno上进行了测试,如果需要是其他型号的Arduino需要自己确定连接的管脚是否正确。<br />
+
The demo code provide are bassed on Arduino UNO. If you want to use other Arduino board, you may need to change the connection even porting the codes yourself.<br />
===硬件连接===
+
===Hardware connection===
 
{|border=1; style="width:700px;" align="center"
 
{|border=1; style="width:700px;" align="center"
|+Arduino UNO连接引脚对应关系
+
|+ Connect to Arduino UNO
 
|-style="background:#2D6FDF; color:white;" align="center"
 
|-style="background:#2D6FDF; color:white;" align="center"
 
|e-Paper||Arduino
 
|e-Paper||Arduino
Line 23: Line 23:
 
|BUSY||D7
 
|BUSY||D7
 
|}
 
|}
===安装编译软件(windows教程)===
+
===Install Arduino IDE===
*打开浏览器,访问[https://www.arduino.cc/en/Main/Software arduinoIDE的官方下载页面],不熟练英文没关系,可以切换到中文(点击可放大):<br />
+
*Go to [https://www.arduino.cc/en/Main/Software arduino IDE], and download the IDE<br />
*点击此处下载软件<br />
+
*Install it according to the guide of Arduino website.<br />
[[file:e-paper_arduino_ide_software2.png]]<br />
+
*Open the IDE software after installing.<br />
*打开刚刚的下载的安装软件,双击安装,一路点击next即可:<br />
 
*快安装完成的时候,会弹出安装USB驱动,点击安装<br />
 
*安装完成,桌面生成了快捷打开方式,双击打开之后弹出arduino IDE软件<br />
 
  
==运行程序==
+
==Run examples==
*在资料下载示例程序,然后解压。Arduino程序位于 ~/Arduino/… <br />
+
*Download the deme codes and unzip it. Open the Arduino examples (~/Arduino/…) <br />
*打开2.13inch的程序<br />
+
*Open 2.13inch project<br />
*打开程序,选择开发板型号Arduino UNO<br />
+
*Open the project and set the board to Arduino UNO<br />
*选择对应COM口<br />
+
*Select the correct COM port<br />
*然后点击编译并下载即可<br />
+
*Build and upload the codes to board<br />
【注意】由于arduino UNO的flash十分小,几款大尺寸的屏幕使用MEGA2560效果更佳:<br />
+
【Note】Because the flash of Arduino UNO is small, we recommend you use MEGA2560 if you use the big size e-Paper<br />
  
==程序说明==
+
==Code Description==
===文件说明===
+
===Files===
以Arduino UNO控制2.13寸墨水屏为例,打开epd2in13 v2目录:<br />
+
Use 2.13inch e-Paper as example, open the folder of epd2in13 v2<br />
 
[[file:e-paper_arduino_ide_codeDescription1.png]]<br />
 
[[file:e-paper_arduino_ide_codeDescription1.png]]<br />
其中:<br />
+
epd2in13.ino:The project file <br />
epd2in13.ino:使用Arduino IDE打开即可;<br />
+
epd2in13.cpp(.h):EPD driver of e-Paper<br />
epd2in13.cpp(.h):是墨水屏的驱动程序;<br />
+
epdif.cpp(.h): Hardware interface, functions for GPIO and SPI ;<br />
epdif.cpp(.h):是硬件接口定义,里面封装了读写管脚电平,SPI传输数据,以及管脚初始化;<br />
 
 
font8.cpp、font12.cpp、font16.cpp、font20.cpp、font24.cpp、fonts.h:为不同大小字符的模;<br />
 
font8.cpp、font12.cpp、font16.cpp、font20.cpp、font24.cpp、fonts.h:为不同大小字符的模;<br />
imagedata.cpp(.h):是图片数据,这个可以通过Img2Lcd(在开发资料中可下载)把2位深度的BMP图片转换成数组。<br />
+
imagedata.cpp(.h):Image data, which is pre-coverted data for display directly.<br />
程序分为底层硬件接口、中间层墨水屏驱动、上层应用;<br />
+
The codes are include three parts: Bottom hardware interface, middle EPD driver and application functions.程;<br />
  
===底层硬件接口===
+
===Bottom hardware interface===
在epdif.cpp(.h)两个文件中定义了硬件接口,并封装好读写管脚电平、延时、SPI传输等函数。<br />
+
hardware interfaces are defined in epdif.cpp(.h) file<br />
;写管脚电平
+
;Write GPIO
 
<pre>
 
<pre>
 
void DigitalWrite(int pin, int value)
 
void DigitalWrite(int pin, int value)
 
</pre>
 
</pre>
第一个参数为管脚、第二个为高低电平。<br />
+
The first parameter is GPIO, and second parameter is level<br />
;读管脚电平
+
;Read GPIO
 
<pre>
 
<pre>
 
int DigitalRead(int pin)
 
int DigitalRead(int pin)
 
</pre>
 
</pre>
参数为管脚,返回值为读取管脚的电平。<br />
+
The  parameter is GPIO, and return value is level<br />
  
;延时
+
;Delay
 
<pre>
 
<pre>
 
DelayMs(unsigned int delaytime)
 
DelayMs(unsigned int delaytime)
 
</pre>
 
</pre>
毫秒级别延时。<br />
+
Delay time, unit is ms<br />
  
;SPI输出数据
+
;SPI transmit data
 
<pre>
 
<pre>
 
SpiTransfer(unsigned char data)
 
SpiTransfer(unsigned char data)
 
</pre>
 
</pre>
参数为char型,占8位。<br />
+
Type of parameter is char<br />
  
;硬件初始化
+
;Hardware initailze
 
<pre>
 
<pre>
 
int IfInit(void)
 
int IfInit(void)
 
</pre>
 
</pre>
里面已经封装好了各管脚的输入输出,以及SPI的初始化。<br />
+
The initialize function of SPI, input/ouptu are packaged here。<br />
  
===中间层墨水屏驱动===
+
===Middle EPD driver===
;实例化墨水屏
+
;Instantiate e-Paper class
由于Arduino是C++开发,需要把墨水屏实例化:
+
The Arduino codes is based on C++, should instantiate e-Paper class is necessary.
 
<pre>
 
<pre>
 
Epd epd;
 
Epd epd;
 
</pre>
 
</pre>
  
;墨水屏初始化,再屏幕开始工作时和退出睡眠模式之后调用
+
;Initialize e-Paper, it should be used to initialize e-Paper or wakeup e-Paper from sleep mode.
 
*2.13inch e-Paper,2.9inch e-Paper
 
*2.13inch e-Paper,2.9inch e-Paper
 
<pre>
 
<pre>
epd.Init(lut_full_update); //全刷初始化
+
epd.Init(lut_full_update); //Fully update
epd.Init(lut_partial_update); //局刷初始化
+
epd.Init(lut_partial_update); //Partial update
 
</pre>
 
</pre>
  
Line 101: Line 97:
 
</pre>
 
</pre>
  
;清屏,把墨水屏刷成白色
+
;Clear, clear the e-Paper to white
 
<pre>
 
<pre>
 
epd.clear();
 
epd.clear();
 
</pre>
 
</pre>
可能某些程序里面会被拆分成两句,但是他们最终的效果都是一样的:<br />
+
In some of project, the operation is divided to two part, they work in the same way<br />
 
<pre>
 
<pre>
 
epd.ClearFrameMemory(0xFF);  
 
epd.ClearFrameMemory(0xFF);  
epd.DisplayFrame();//打开显示
+
epd.DisplayFrame();//Display it
 
</pre>
 
</pre>
  
;传输一帧的图片数据并显示
+
;Transmit one frame of image and display
 
<pre>
 
<pre>
 
void Display(const unsigned char* frame_buffer);
 
void Display(const unsigned char* frame_buffer);
void DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red); //三色屏幕
+
void DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red); //Three color e-Paper
 
</pre>
 
</pre>
  
;睡眠
+
;Sleep
 
<pre>
 
<pre>
 
epd.Sleep();
 
epd.Sleep();
 
</pre>
 
</pre>
进入睡眠模式,墨水屏讲进入超低耗电,如果长时间不用需要刷白保存,否则长时间会有残影。<br />
+
Set the e-Paper enter sleep mode. The consumption of the e-Paper will be reduced. However, you still need to update the display periodically to avoid ghost problem.<br />
  
===上层应用===
+
===Application functions===
上层应用也就是我们的需要用墨水屏实现的功能,一般就是画图、字符等功能,也就是epdpaint.cpp里面定义的功能<br />
+
The drawing functions are defined in this part.<br />
首先需要讲解一下缓存的坐标系,为了传输方便通常吧坐上定位原点,往右X轴增加,往下Y轴增加:<br />
+
The coordination of the image buffer:<br />
 
[[file:e-paper_arduino_pic1.png|300px]]
 
[[file:e-paper_arduino_pic1.png|300px]]
  
打开epdpaint.h可以看到如下:<br />
+
The functions are defined in epdpaint.h file<br />
 
[[file:e-paper_arduino_ide_codeDescription2.png]]<br />
 
[[file:e-paper_arduino_ide_codeDescription2.png]]<br />
只需要看public下函数即可:<br />
 
  
;初始化图片缓存
+
;Initailze image buffer
 
<pre>
 
<pre>
 
Paint(unsigned char* image, int width, int height);
 
Paint(unsigned char* image, int width, int height);
 
</pre>
 
</pre>
第一个参数是图片缓存,第二个参数是定义图片长度,第三个参数定义图片高度,你可能会在程序中看到如下<br />
+
The first parameter is image buffer, the second one is the width of the picture, and the third one is the height.<br />
 
<pre>
 
<pre>
 
Paint paint(image, 0, 0);    // width should be the multiple of 8
 
Paint paint(image, 0, 0);    // width should be the multiple of 8
 
</pre>
 
</pre>
第二、三个参数这里设置成0,是因为下面还可以设置。<br />
+
The second and third parameters are set to 0, you can re-configure them with functions below:<br />
  
;设置高宽、获取高宽、设置翻转角度、获取翻转角度
+
;Set the width, height, rotate degree.
 
<pre>
 
<pre>
int  GetWidth(void); //得到宽度
+
int  GetWidth(void); //Get the width
void SetWidth(int width);//设置宽度
+
void SetWidth(int width);//Set the width
int  GetHeight(void);//得到高度
+
int  GetHeight(void);//Get the height
void SetHeight(int height);//设置高度
+
void SetHeight(int height);//Set the height
int  GetRotate(void);//得到翻转角度
+
int  GetRotate(void);//Get the degree
void SetRotate(int rotate);//设置翻转角度
+
void SetRotate(int rotate);//Set the rotate degree
 
</pre>
 
</pre>
  
;获取图像缓存
+
;Get the image data
 
<pre>
 
<pre>
 
unsigned char* GetImage(void);
 
unsigned char* GetImage(void);
 
</pre>
 
</pre>
  
;画点
+
;Draw circle
 
<pre>
 
<pre>
 
void DrawPixel(int x, int y, int colored);
 
void DrawPixel(int x, int y, int colored);
 
</pre>
 
</pre>
在坐标(x,y)
+
Coordination(x,y)
  
;写字符
+
;Draw characater
 
<pre>
 
<pre>
 
void DrawCharAt(int x, int y, char ascii_char, sFONT* font, int colored);
 
void DrawCharAt(int x, int y, char ascii_char, sFONT* font, int colored);
 
</pre>
 
</pre>
在(x,y)这一点为左顶点写字符ascii_char,字体大小为font,颜色为colored<br />
+
Set(x,y)as the start point, draw characters ascii_char, set the fonts as font, color is colored.<br />
  
;写字符串
+
;Draw string
 
<pre>
 
<pre>
 
void DrawStringAt(int x, int y, const char* text, sFONT* font, int colored);
 
void DrawStringAt(int x, int y, const char* text, sFONT* font, int colored);
 
</pre>
 
</pre>
在(x,y)这一点为左顶点写字符串text,字体大小为font,颜色为colored<br />
+
Set(x,y)as the start point, draw the string text, font is  font,color is colored<br />
  
;画线
+
;Draw line
 
<pre>
 
<pre>
 
void DrawLine(int x0, int y0, int x1, int y1, int colored);
 
void DrawLine(int x0, int y0, int x1, int y1, int colored);
 
</pre>
 
</pre>
以(x0,y0)为起点,(x1,y1)为终点画一条线,斜率任意;<br />
+
Use (x0,y0)as start point, (x1,y1) as end point;<br />
  
;画横线
+
;Draw cross line:
 
<pre>
 
<pre>
 
void DrawHorizontalLine(int x, int y, int width, int colored);
 
void DrawHorizontalLine(int x, int y, int width, int colored);
 
</pre>
 
</pre>
以(x0,y0)为起点,(x1,y1)为终点画一条横线,速度比DrawLine()快<br />
+
Set(x0,y0)as start points,draw a line, the widht is width, and color is colored</ br>
  
;画竖线
+
;Draw a vertical line
 
<pre>
 
<pre>
 
void DrawVerticalLine(int x, int y, int height, int colored);
 
void DrawVerticalLine(int x, int y, int height, int colored);
 
</pre>
 
</pre>
以(x0,y0)为起点,(x1,y1)为终点画一条竖线,速度比DrawLine()快<br />
+
Use(x0,y0) as start point, draw a vertical line, width is height and color is colored./ br>
  
;矩形,画空心框
+
;Draw a empty rectangle
 
<pre>
 
<pre>
 
void DrawRectangle(int x0, int y0, int x1, int y1, int colored);
 
void DrawRectangle(int x0, int y0, int x1, int y1, int colored);
 
</pre>
 
</pre>
以(x0,y0)为起点,(x1,y1)为终点画一个框,边的颜色为colored<br >
+
User(x0,y0) as start point,(x1,y1)is end point, draw a rectangel, color of edges are colored.<br >
  
;填充矩形,画实心框
+
;Draw a full rectangle
 
<pre>
 
<pre>
 
void DrawFilledRectangle(int x0, int y0, int x1, int y1, int colored);
 
void DrawFilledRectangle(int x0, int y0, int x1, int y1, int colored);
 
</pre>
 
</pre>
以(x0,y0)为起点,(x1,y1)为终点画一个框,并且内部填充,颜色为colored<br >
+
Use(x0,y0)as start point, (x1,y1) is end point, draw a rectange, filled it with color: colored<br >
  
;画空心圆
+
;Draw an empty circle
 
<pre>
 
<pre>
 
void DrawCircle(int x, int y, int radius, int colored);
 
void DrawCircle(int x, int y, int radius, int colored);
 
</pre>
 
</pre>
以(x,y)为圆心,radius为半斤画一个空心圆,颜色为colored<br >
+
Use (x,y)as center,draw a empty circle with radius, color is colored<br >
  
;画实心圆
+
;Draw a full circle
 
<pre>
 
<pre>
 
void DrawFilledCircle(int x, int y, int radius, int colored);
 
void DrawFilledCircle(int x, int y, int radius, int colored);
 
</pre>
 
</pre>
以(x,y)为圆心,radius为半斤画一个实心心圆,颜色为colored<br >
+
Use (x,y)as center, draw a circle, radius is radius, filled with color: colored<br >

Latest revision as of 15:39, 8 June 2020

Working with Arduino

The demo code provide are bassed on Arduino UNO. If you want to use other Arduino board, you may need to change the connection even porting the codes yourself.

Hardware connection

Connect to Arduino UNO
e-Paper Arduino
Vcc 5V
GND GND
DIN D11
CLK D13
CS D10
DC D9
RST D8
BUSY D7

Install Arduino IDE

  • Go to arduino IDE, and download the IDE
  • Install it according to the guide of Arduino website.
  • Open the IDE software after installing.

Run examples

  • Download the deme codes and unzip it. Open the Arduino examples (~/Arduino/…)
  • Open 2.13inch project
  • Open the project and set the board to Arduino UNO
  • Select the correct COM port
  • Build and upload the codes to board

【Note】Because the flash of Arduino UNO is small, we recommend you use MEGA2560 if you use the big size e-Paper

Code Description

Files

Use 2.13inch e-Paper as example, open the folder of epd2in13 v2
E-paper arduino ide codeDescription1.png
epd2in13.ino:The project file
epd2in13.cpp(.h):EPD driver of e-Paper
epdif.cpp(.h): Hardware interface, functions for GPIO and SPI ;
font8.cpp、font12.cpp、font16.cpp、font20.cpp、font24.cpp、fonts.h:为不同大小字符的模;
imagedata.cpp(.h):Image data, which is pre-coverted data for display directly.
The codes are include three parts: Bottom hardware interface, middle EPD driver and application functions.程;

Bottom hardware interface

hardware interfaces are defined in epdif.cpp(.h) file

Write GPIO
void DigitalWrite(int pin, int value)

The first parameter is GPIO, and second parameter is level

Read GPIO
int DigitalRead(int pin)

The parameter is GPIO, and return value is level

Delay
DelayMs(unsigned int delaytime)

Delay time, unit is ms

SPI transmit data
SpiTransfer(unsigned char data)

Type of parameter is char

Hardware initailze
int IfInit(void)

The initialize function of SPI, input/ouptu are packaged here。

Middle EPD driver

Instantiate e-Paper class

The Arduino codes is based on C++, should instantiate e-Paper class is necessary.

Epd epd;
Initialize e-Paper, it should be used to initialize e-Paper or wakeup e-Paper from sleep mode.
  • 2.13inch e-Paper,2.9inch e-Paper
epd.Init(lut_full_update); //Fully update
epd.Init(lut_partial_update); //Partial update
  • 4.2inch e-Paper
epd.Init();
Clear, clear the e-Paper to white
epd.clear();

In some of project, the operation is divided to two part, they work in the same way

epd.ClearFrameMemory(0xFF); 
epd.DisplayFrame();//Display it
Transmit one frame of image and display
void Display(const unsigned char* frame_buffer);
void DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red); //Three color e-Paper
Sleep
epd.Sleep();

Set the e-Paper enter sleep mode. The consumption of the e-Paper will be reduced. However, you still need to update the display periodically to avoid ghost problem.

Application functions

The drawing functions are defined in this part.
The coordination of the image buffer:
E-paper arduino pic1.png

The functions are defined in epdpaint.h file
E-paper arduino ide codeDescription2.png

Initailze image buffer
Paint(unsigned char* image, int width, int height);

The first parameter is image buffer, the second one is the width of the picture, and the third one is the height.

Paint paint(image, 0, 0);    // width should be the multiple of 8

The second and third parameters are set to 0, you can re-configure them with functions below:

Set the width, height, rotate degree.
int  GetWidth(void); //Get the width
void SetWidth(int width);//Set the width
int  GetHeight(void);//Get the height
void SetHeight(int height);//Set the height
int  GetRotate(void);//Get the degree
void SetRotate(int rotate);//Set the rotate degree
Get the image data
unsigned char* GetImage(void);
Draw circle
void DrawPixel(int x, int y, int colored);

Coordination(x,y)

Draw characater
void DrawCharAt(int x, int y, char ascii_char, sFONT* font, int colored);

Set(x,y)as the start point, draw characters ascii_char, set the fonts as font, color is colored.

Draw string
void DrawStringAt(int x, int y, const char* text, sFONT* font, int colored);

Set(x,y)as the start point, draw the string text, font is font,color is colored

Draw line
void DrawLine(int x0, int y0, int x1, int y1, int colored);

Use (x0,y0)as start point, (x1,y1) as end point;

Draw cross line
void DrawHorizontalLine(int x, int y, int width, int colored);

Set(x0,y0)as start points,draw a line, the widht is width, and color is colored</ br>

Draw a vertical line
void DrawVerticalLine(int x, int y, int height, int colored);

Use(x0,y0) as start point, draw a vertical line, width is height and color is colored./ br>

Draw a empty rectangle
void DrawRectangle(int x0, int y0, int x1, int y1, int colored);

User(x0,y0) as start point,(x1,y1)is end point, draw a rectangel, color of edges are colored.

Draw a full rectangle
void DrawFilledRectangle(int x0, int y0, int x1, int y1, int colored);

Use(x0,y0)as start point, (x1,y1) is end point, draw a rectange, filled it with color: colored

Draw an empty circle
void DrawCircle(int x, int y, int radius, int colored);

Use (x,y)as center,draw a empty circle with radius, color is colored

Draw a full circle
void DrawFilledCircle(int x, int y, int radius, int colored);

Use (x,y)as center, draw a circle, radius is radius, filled with color: colored