模板: E-paper for arduino use
Arduino
例程均在Arduino uno上进行了测试,如果需要是其他型号的Arduino需要自己确定连接的管脚是否正确。
需要注意的是:由于Arduino UNO的RAM小的可怜,无法分配过大的内存用于处理画图等功能,所以部分大尺寸的屏幕例程只演示刷新图片,这个图片是存储在flash中,我们建议Arduino UNO控制请使用e-paper Shiled体验更佳。
硬件连接
e-Paper | Arduino UNO | Mega2560 |
Vcc | 5V | 5V |
GND | GND | GND |
DIN | D11 | D51 |
CLK | D13 | D52 |
CS | D10 | D10 |
DC | D9 | D9 |
RST | D8 | D8 |
BUSY | D7 | D7 |
PWR | D6 | D6 |
安装编译软件(windows教程)
运行程序
在产品百科界面下载程序,然后解压。Arduino程序位于 ~/Arduino/…
可以查看到所有的屏幕尺寸的测试程序,按照尺寸分类:
1.02寸(128×80):
epd1in02d:对应1.02inch e-paper测试程序;
1.54寸(1.54inch e-paper c:152×152,其他:200×200):
epd1in54: 对应1.54inch e-paper V1(黑白)版本测试程序,目前已停产;
epd1in54_V2:对应1.54inch e-paper V2(黑白)版本测试程序,购买日期晚于2019年11月为V2版本,目前出货版本,背面贴有V2标识;
epd1in54b: 对应1.54inch e-paper B(黑白红)测试程序,目前已停产;
epd1in54b_V2: 对应1.54inch e-paper B V2(黑白红)测试程序,购买日期晚于2020年4月为V2版本,目前出货版本,背面贴有V2标识;
epd1in54c: 对应1.54inch e-paper C(黑白黄)测试程序;
1.64寸(168*168):
EPD_1in64g_test(): 对应1.64inch e-paper G(黑白红黄)测试程序;
2.7寸(264×176):
epd2in7: 对应2.7inch e-paper(黑白)测试程序;
epd2in7b: 对应2.7inch e-paper B(黑白红)测试程序;
epd2in7b_V2: 对应2.7inch e-paper B V2(黑白红)测试程序,购买日期晚于2021年2月为V2版本,目前出货版本,背面贴有V2标识;
2.9寸(296×128):
epd2in9: 对应2.9inch e-paper(黑白)测试程序,目前已停产;
epd2in9_V2: 对应2.9inch e-paper V2(黑白)测试程序,购买日期晚于2020年12月为V2版本,目前出货版本,背面贴有V2标识;
epd2in9bc: 对应2.9inch e-paper B(黑白红)和2.9inch e-paper C(黑白黄)测试程序;
epd2in9b_V3: 对应2.9inch e-paper B V3(黑白红)测试程序,购买日期晚于2020年7月为V3版本,目前出货版本,背面贴有V3标识;
epd2in9d: 对应2.9inch e-paper D(黑白)测试程序;
2.13寸(2.13inch e-Paper:250×122,其他:212×104):
epd2in13: 对应2.13inch e-paper V1(黑白)版本测试程序,目前已停产;
epd2in13_V2:对应2.13inch e-paper V2(黑白)版本测试程序,购买日期晚于2019年5月为V2版本,目前已停产,背面贴有V2标识;
epd2in13_V3:对应2.13inch e-paper V3(黑白)版本测试程序,购买日期晚于2022年2月为V3版本,目前出货版本,背面贴有V3标识;
epd2in13bc:对应2.13inch e-paper B(黑白红)和2.13inch e-paper C(黑白黄)测试程序;
epd2in13b_V3:对应2.13inch e-paper B V3(黑白红)测试程序,购买日期晚于2020年4月为V3版本,目前出货版本,背面贴有V3标识;
epd2in13d:对应2.13inch e-paper D(黑白)测试程序;
2.66寸(152×296):
epd2in66:对应2.66inch e-paper(黑白)测试程序;
epd2in66b:对应2.66inch e-paper B(黑白红)测试程序;
3寸(400*168):
EPD_3in0g_test(): 对应3inch e-paper G(黑白红黄)测试程序;
3.52寸(360×240):
EPD_3in52_test():对应3.52inch e-paper(黑白)测试程序;
3.7寸(280×480):
epd3in7:对应3.7inch e-paper(黑白)测试程序;
4.01寸(640×400):
epd4in01f:对应4.01inch e-paper F(黑白绿蓝红黄橙)测试程序;
4.2寸(400×300):
epd4in2:对应4.2inch e-paper(黑白)测试程序;
epd4in2bc:对应4.2inch e-paper B(黑白红)测试程序;
epd4in2b_V2: 对应4.2inch e-paper B V2(黑白红)测试程序,购买日期晚于2020年11月为V2版本,目前出货版本,背面贴有V2标识;
5.65寸(600×558):
epd5in65:对应5.65inch e-paper F(黑白绿蓝红黄橙)测试程序;
5.83寸(600×448):
epd5in83: 对应5.83inch e-paper(黑白)测试程序,目前已停产;
epd5in83_V2: 对应5.83inch e-paper V2(黑白)测试程序,购买日期晚于2020年12月为V2版本,目前出货版本,背面贴有V2标识;
epd5in83bc: 对应5.83inch e-paper B(黑白红)和5.83inch e-paper C(黑白黄)测试程序;
epd5in83b_V2: 对应5.83inch e-paper B V2(黑白红)测试程序,购买日期晚于2020年8月为V2版本,目前出货版本,背面贴有V2标识;
7.3寸(800*480):
EPD_7in3g_test(): 对应7.3inch e-paper G(黑白红黄)测试程序;
7.5寸(V1:640×384,V2:800×480):
epd7in5: 对应7.5inch e-paper(黑白)测试程序,购买日期早于2019-12-07为V1版本,目前已停产;
epd7in5_V2: 对应7.5inch e-paper V2(黑白)测试程序,购买日期晚于2019-12-07为V2版本,目前出货版本,背面贴有V2标识;
epd7in5bc: 对应7.5inch e-paper B(黑白红)和7.5inch e-paper C(黑白黄)测试程序,7.5inch e-paper B购买日期早于2019-12-07为V1版本,目前已停产,7.5inch e-paper C正常供货;
epd7in5b_V2:对应7.5inch e-paper B V2(黑白红)测试程序,购买日期晚于2019-12-07为V2版本,背面贴有V2标识;购买日期晚于2021年3月为V3版本,目前7.5inch e-paper B出货版本,背面贴有V3标识,同样使用本程序
7.5寸(HD:880×528):
epd7in5_HD:对应7.5inch e-paper HD(黑白)测试程序;
epd7in5b_HD:对应7.5inch e-paper B HD(黑白红)测试程序;
- 注意:以上时间仅供参考,具体版本请看屏幕标识
比如1.54inch e-Paper Module. 打开epd1in54文件夹,并运行epd1in54.ino文件。
打开程序,选择开发板型号Arduino UNO
选择对应COM口
然后点击编译并下载即可
由于arduino UNO的flash十分小,几款大尺寸的屏幕使用MEGA2560效果更佳:
程序说明
文件介绍
以Arduino UNO控制1.54寸墨水屏为例,打开epd1in54目录:
其中:
epd1in54.ino:使用Arduino IDE打开即可;
epd1in54.cpp(.h):是墨水屏的驱动程序;
epdif.cpp(.h):是硬件接口定义,里面封装了读写管脚电平,SPI传输数据,以及管脚初始化;
font8.cpp、font12.cpp、font16.cpp、font20.cpp、font24.cpp、fonts.h:为不同大小字符的模;
imagedata.cpp(.h):是图片数据,这个可以通过Img2Lcd(在开发资料中可下载)把2位深度的BMP图片转换成数组。
程序分为底层硬件接口、中间层墨水屏驱动、上层应用;
底层硬件接口
在epdif.cpp(.h)两个文件中定义了硬件接口,并封装好读写管脚电平、延时、SPI传输等函数。
- 写管脚电平
void DigitalWrite(int pin, int value)
第一个参数为管脚、第二个为高低电平。
- 读管脚电平
int DigitalRead(int pin)
参数为管脚,返回值为读取管脚的电平。
- 延时
DelayMs(unsigned int delaytime)
毫秒级别延时。
- SPI输出数据
SpiTransfer(unsigned char data)
参数为char型,占8位。
- 硬件初始化
int IfInit(void)
里面已经封装好了各管脚的输入输出,以及SPI的初始化。
中间层墨水屏驱动
- 实例化墨水屏
由于Arduino是C++开发,需要把墨水屏实例化:
Epd epd;
- 墨水屏初始化,再屏幕开始工作时和退出睡眠模式之后调用
- 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)
epd.Init(lut_full_update); //全刷初始化 epd.Init(lut_partial_update); //局刷初始化
- 其他型号
epd.Init();
- 清屏,把墨水屏刷成白色
epd.clear();
可能某些程序里面会被拆分成两句,但是他们最终的效果都是一样的:
epd.ClearFrameMemory(0xFF); epd.DisplayFrame();//打开显示
- 传输一帧的图片数据并显示
void Display(const unsigned char* frame_buffer); void DisplayFrame(const unsigned char* frame_buffer_black, const unsigned char* frame_buffer_red); //三色屏幕
- 睡眠
epd.Sleep();
进入睡眠模式,墨水屏讲进入超低耗电,如果长时间不用需要刷白保存,否则长时间会有残影。
上层应用
上层应用也就是我们的需要用墨水屏实现的功能,一般就是画图、字符等功能,也就是epdpaint.cpp里面定义的功能
首先需要讲解一下缓存的坐标系,为了传输方便通常吧坐上定位原点,往右X轴增加,往下Y轴增加:
打开epdpaint.h可以看到如下:
只需要看public下函数即可:
- 初始化图片缓存
Paint(unsigned char* image, int width, int height);
第一个参数是图片缓存,第二个参数是定义图片长度,第三个参数定义图片高度,你可能会在程序中看到如下
Paint paint(image, 0, 0); // width should be the multiple of 8
第二、三个参数这里设置成0,是因为下面还可以设置。
- 设置高宽、获取高宽、设置翻转角度、获取翻转角度
int GetWidth(void); //得到宽度 void SetWidth(int width);//设置宽度 int GetHeight(void);//得到高度 void SetHeight(int height);//设置高度 int GetRotate(void);//得到翻转角度 void SetRotate(int rotate);//设置翻转角度
- 获取图像缓存
unsigned char* GetImage(void);
- 画点
void DrawPixel(int x, int y, int colored);
在坐标(x,y)
- 写字符
void DrawCharAt(int x, int y, char ascii_char, sFONT* font, int colored);
在(x,y)这一点为左顶点写字符ascii_char,字体大小为font,颜色为colored
- 写字符串
void DrawStringAt(int x, int y, const char* text, sFONT* font, int colored);
在(x,y)这一点为左顶点写字符串text,字体大小为font,颜色为colored
- 画线
void DrawLine(int x0, int y0, int x1, int y1, int colored);
以(x0,y0)为起点,(x1,y1)为终点画一条线,斜率任意;
- 画横线
void DrawHorizontalLine(int x, int y, int width, int colored);
以(x0,y0)为起点,(x1,y1)为终点画一条横线,速度比DrawLine()快
- 画竖线
void DrawVerticalLine(int x, int y, int height, int colored);
以(x0,y0)为起点,(x1,y1)为终点画一条竖线,速度比DrawLine()快
- 矩形,画空心框
void DrawRectangle(int x0, int y0, int x1, int y1, int colored);
以(x0,y0)为起点,(x1,y1)为终点画一个框,边的颜色为colored
- 填充矩形,画实心框
void DrawFilledRectangle(int x0, int y0, int x1, int y1, int colored);
以(x0,y0)为起点,(x1,y1)为终点画一个框,并且内部填充,颜色为colored
- 画空心圆
void DrawCircle(int x, int y, int radius, int colored);
以(x,y)为圆心,radius为半斤画一个空心圆,颜色为colored
- 画实心圆
void DrawFilledCircle(int x, int y, int radius, int colored);
以(x,y)为圆心,radius为半斤画一个实心心圆,颜色为colored