| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083 |
- /*
- @module eink
- @summary 墨水屏操作库
- @version 1.0
- @date 2020.11.14
- @demo eink
- */
- #include "luat_base.h"
- #include "luat_log.h"
- #include "luat_sys.h"
- #include "luat_msgbus.h"
- #include "luat_timer.h"
- #include "luat_malloc.h"
- #include "luat_spi.h"
- #include "luat_gpio.h"
- // #include "epd1in54.h"
- // #include "epd2in9.h"
- #include "epd.h"
- #include "epdpaint.h"
- #include "imagedata.h"
- #include "qrcodegen.h"
- #include <stdlib.h>
- #include "u8g2.h"
- #include "u8g2_luat_fonts.h"
- #include "luat_zbuff.h"
- #include "luat_ufont.h"
- static const lv_font_t* font_current;
- static size_t epd_w = 0;
- static size_t epd_h = 0;
- #define COLORED 0
- #define UNCOLORED 1
- #ifndef LUAT_LOG_TAG
- #define LUAT_LOG_TAG "eink"
- #endif
- typedef struct eink_ctx
- {
- uint32_t str_color;
- Paint paint;
- uint8_t fb[];
- }eink_ctx_t;
- static uint32_t ctx_index = 0;
- static eink_ctx_t *ctxs[2]; // 暂时只支持2种颜色, 有需要的话后续继续加
- // static u8g2_t luat_eink_u8g2;
- static void (eink_ui_draw_point)(void* userdata, int x, int y, uint32_t color) {
- Paint_DrawPixel(&ctxs[ctx_index]->paint, x, y, color);
- }
- static void (eink_ui_draw_line)(void* userdata, int x1, int y1, int x2, int y2, uint32_t color) {
- Paint_DrawLine(&ctxs[ctx_index]->paint, x1, y1, x2, y2, color);
- }
- static void (eink_ui_draw_block)(void* userdata, int x, int y, int w, int h, size_t bitw, void* data) {
- LLOGD("lcd_ui_draw_block NOT support yet");
- }
- static void (eink_ui_draw_fill)(void* userdata, int x, int y, int w, int h, uint32_t color) {
- Paint_DrawFilledRectangle(&ctxs[ctx_index]->paint, x, y, x+w, y+h, color);
- }
- static void (eink_ui_draw_flush)(void* userdata) {
- // TODO
- };
- const ui_draw_opts_t eink_ui_opts = {
- .draw_point = eink_ui_draw_point,
- .draw_line = eink_ui_draw_line,
- .draw_block = eink_ui_draw_block,
- .draw_fill = eink_ui_draw_fill,
- .draw_flush = eink_ui_draw_flush
- };
- eink_conf_t econf = {0};
- #define Pin_BUSY (econf.busy_pin)
- #define Pin_RES (econf.res_pin)
- #define Pin_DC (econf.dc_pin)
- #define Pin_CS (econf.cs_pin)
- #define SPI_ID (econf.spi_id)
- static int check_init(void) {
- if (ctxs[0] == NULL) {
- LLOGW("eink NOT init yet");
- return 0;
- }
- return 1;
- }
- /**
- 初始化eink
- @api eink.setup(full, spiid, pin_busy, pin_reset, pin_dc, pin_cs)
- @int 全屏刷新0,局部刷新1,默认是全屏刷新
- @int 所在的spi,默认是0
- @int Busy 忙信号管脚
- @int Reset 复位管脚
- @int DC 数据命令选择管脚
- @int CS 使能管脚
- @return boolean 成功返回true,否则返回false
- */
- static int l_eink_setup(lua_State *L) {
- int status = 0;
- econf.full_mode = luaL_optinteger(L, 1, 1);
- econf.spi_id = luaL_optinteger(L, 2, 0);
- econf.busy_pin = luaL_optinteger(L, 3, 18);
- econf.res_pin = luaL_optinteger(L, 4, 7);
- econf.dc_pin = luaL_optinteger(L, 5, 9);
- econf.cs_pin = luaL_optinteger(L, 6, 16);
- if (lua_type(L, 7) == LUA_TUSERDATA){
- //LLOGD("luat_spi_device_send");
- econf.userdata = (luat_spi_device_t*)lua_touserdata(L, 3);
- econf.port = LUAT_EINK_SPI_DEVICE;
- luat_gpio_mode(Pin_BUSY, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
- luat_gpio_mode(Pin_RES, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
- luat_gpio_mode(Pin_DC, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
- status = 0;
- }else{
- //LLOGD("luat_spi_send");
- luat_spi_t spi_config = {0};
- spi_config.bandrate = 2000000U;//luaL_optinteger(L, 1, 2000000U); // 2000000U
- spi_config.id = SPI_ID;
- spi_config.cs = 255; // 默认无
- spi_config.CPHA = 0; // CPHA0
- spi_config.CPOL = 0; // CPOL0
- spi_config.dataw = 8; // 8bit
- spi_config.bit_dict = 1; // MSB=1, LSB=0
- spi_config.master = 1; // master=1,slave=0
- spi_config.mode = 1; // FULL=1, half=0
- //先关了再开一遍
- luat_spi_close(spi_config.id);
- //LLOGD("setup GPIO for epd");
- status = luat_spi_setup(&spi_config);
- luat_gpio_mode(Pin_BUSY, Luat_GPIO_INPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
- luat_gpio_mode(Pin_RES, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
- luat_gpio_mode(Pin_DC, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
- luat_gpio_mode(Pin_CS, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, Luat_GPIO_LOW);
- }
- if (status != 0) {
- LLOGD("spi setup fail, eink init fail");
- return 0;
- }
- size_t colors = 0;
- if(status == 0)
- {
- if(econf.full_mode)
- status = EPD_Init(1, &epd_w, &epd_h, &colors);
- else
- status = EPD_Init(0, &epd_w, &epd_h, &colors);
- if (status != 0) {
- LLOGD("e-Paper init failed");
- return 0;
- }
- LLOGD("spi setup complete, now setup epd");
- if (colors > 2) {
- LLOGE("only 2 color eink supported yet");
- return 0;
- }
- for (size_t i = 0; i < colors; i++)
- {
- ctxs[i] = luat_heap_malloc( sizeof(eink_ctx_t) + (epd_w * epd_h + 7) / 8);
- if (ctxs[i] == NULL) {
- LLOGE("out of memory when malloc buff for eink");
- for (size_t j = 0; j < i - 1; j++)
- {
- luat_heap_free(ctxs[i]);
- ctxs[i] = NULL;
- }
- return 0;
- }
- Paint_Init(&ctxs[i]->paint, ctxs[i]->fb, epd_w, epd_h);
- Paint_Clear(&ctxs[i]->paint, UNCOLORED);
- ctxs[i]->paint.inited = 1;
- }
- }
- font_current = luat_fonts_default_font();
- lua_pushboolean(L, 1);
- return 1;
- }
- /**
- 进入休眠模式,再次使用时需要重新初始化
- @api eink.sleep()
- */
- static int l_eink_sleep(lua_State *L)
- {
- EPD_Sleep();
- return 0;
- }
- /**
- 清除绘图缓冲区,默认不会马上刷新到设备
- @api eink.clear(color, force)
- @number color 可选,默认1。刷屏颜色
- @bool force 可选,默认false。如果为true则马上清屏
- @return nil 无返回值
- */
- static int l_eink_clear(lua_State *L)
- {
- int colored = luaL_optinteger(L, 1, 1);
- if (check_init() == 0)
- return 0;
- Paint_Clear(&ctxs[ctx_index]->paint, colored);
- if(lua_toboolean(L, 2))
- EPD_Clear();
- return 0;
- }
- /**
- 设置窗口
- @api eink.setWin(width, height, rotate)
- @int width 宽度
- @int height 高度
- @int rotate 显示方向,0/1/2/3, 相当于旋转0度/90度/180度/270度
- @return nil 无返回值
- */
- static int l_eink_setWin(lua_State *L)
- {
- int width = luaL_checkinteger(L, 1);
- int height = luaL_checkinteger(L, 2);
- int rotate = luaL_checkinteger(L, 3);
- if (check_init() == 0)
- return 0;
- Paint_SetWidth(&ctxs[ctx_index]->paint, width);
- Paint_SetHeight(&ctxs[ctx_index]->paint, height);
- Paint_SetRotate(&ctxs[ctx_index]->paint, rotate);
- return 0;
- }
- /**
- 获取窗口信息
- @api eink.getWin()
- @return int width 宽
- @return int height 高
- @return int rotate 旋转方向
- */
- static int l_eink_getWin(lua_State *L)
- {
- if (check_init() == 0)
- return 0;
- int width = Paint_GetWidth(&ctxs[ctx_index]->paint);
- int height = Paint_GetHeight(&ctxs[ctx_index]->paint);
- int rotate = Paint_GetRotate(&ctxs[ctx_index]->paint);
- lua_pushinteger(L, width);
- lua_pushinteger(L, height);
- lua_pushinteger(L, rotate);
- return 3;
- }
- static void drawFastHLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_t color){
- Paint_DrawHorizontalLine(conf,x, y, len,color);
- }
- static void drawFastVLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_t color){
- Paint_DrawVerticalLine(conf,x, y, len,color);
- }
- /**
- 设置字体
- @api eink.setFont(font)
- @userdata 字体
- @usage
- -- 设置为字体,对之后的print有效
- eink.setFont(eink.font_opposansm12_chinese)
- */
- static int l_eink_set_font(lua_State *L) {
- if (check_init() == 0) {
- return 0;
- }
- if (!lua_islightuserdata(L, 1)) {
- LLOGE("only font pointer is allow");
- return 0;
- }
- const uint8_t *ptr = (const uint8_t *)lua_touserdata(L, 1);
- if (ptr == NULL) {
- LLOGE("only font pointer is allow");
- return 0;
- }
- font_current = (lv_font_t*)ptr;
- lua_pushboolean(L, 1);
- return 1;
- }
- /**
- 绘制字符串
- @api eink.print(x, y, str, colored)
- @int x坐标
- @int y坐标
- @string 字符串
- @int 颜色, 可以是0或者1, 默认是0
- @return nil 无返回值
- @usage
- -- 先设置字体, 然后写字
- -- 可用字体取决于具体的固件, 如果没有你想要的大小,可以云编译一份自定义固件
- -- font_opposansm8_chinese
- -- font_opposansm10_chinese
- -- font_opposansm12_chinese
- -- font_opposansm14_chinese
- -- font_opposansm16_chinese
- eink.setFont(eink.font_opposansm12_chinese)
- eink.print(10, 20, "LuatOS")
- */
- static int l_eink_print(lua_State *L)
- {
- int x, y;
- size_t sz;
- const char* str;
- x = luaL_checkinteger(L, 1);
- y = luaL_checkinteger(L, 2);
- str = (const char*)luaL_checklstring(L, 3, &sz);
- if (sz == 0)
- return 0;
- ui_draw_str_ctx_t ctx = {
- .font = font_current,
- .bg_color = 0,
- .front_color = 1, // Colored? 会不会写反了
- .draw_mode = 0,
- .opts = eink_ui_opts,
- .ui_h = epd_h,
- .ui_w = epd_w,
- .x = x,
- .y = y,
- .utf8_letters = str,
- .userdata = NULL,
- };
- int ret = luat_ufont_drawUTF8(&ctx);
- lua_pushinteger(L, ret);
- return 0;
- }
- /**
- 将缓冲区图像输出到屏幕
- @api eink.show(x, y, noClear)
- @int x 输出的x坐标,默认0
- @int y 输出的y坐标,默认0
- @bool 可选,默认false。如果为true则不进行清屏,直接刷上新内容
- @return nil 无返回值
- */
- static int l_eink_show(lua_State *L)
- {
- // int x = luaL_optinteger(L, 1, 0);
- // int y = luaL_optinteger(L, 2, 0);
- int no_clear = lua_toboolean(L, 3);
- /* Display the frame_buffer */
- //EPD_SetFrameMemory(&epd, frame_buffer, x, y, Paint_GetWidth(&ctxs[ctx_index]->paint), Paint_GetHeight(&ctxs[ctx_index]->paint));
- //EPD_DisplayFrame(&epd);
-
- if (check_init() == 0) {
- return 0;
- }
- if(!no_clear)
- EPD_Clear();
- if (ctxs[1] == NULL)
- EPD_Display(ctxs[0]->fb, NULL);
- else
- EPD_Display(ctxs[0]->fb, ctxs[1]->fb);
- return 0;
- }
- /**
- 直接输出数据到屏幕,支持双色数据
- @api eink.draw(buff, buff2, noclear)
- @userdata zbuff指针
- @userdata zbuff指针
- @bool 可选,默认false。如果为true则不进行清屏,直接刷上新内容
- @return nil 无返回值
- */
- static int l_eink_draw(lua_State *L)
- {
- luat_zbuff_t* buff = tozbuff(L);
- luat_zbuff_t* buff2 = buff;
- int no_clear = lua_toboolean(L, 3);
- if (lua_isuserdata(L, 2)) {
- buff2 = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
- }
- if (check_init() == 0) {
- return 0;
- }
- if(!no_clear)
- EPD_Clear();
- EPD_Display(buff->addr, buff2->addr);
- return 0;
- }
- /**
- 缓冲区绘制线
- @api eink.line(x, y, x2, y2, colored)
- @int 起点x坐标
- @int 起点y坐标
- @int 终点x坐标
- @int 终点y坐标
- @return nil 无返回值
- @usage
- eink.line(0, 0, 10, 20, 0)
- */
- static int l_eink_line(lua_State *L)
- {
- int x = luaL_checkinteger(L, 1);
- int y = luaL_checkinteger(L, 2);
- int x2 = luaL_checkinteger(L, 3);
- int y2 = luaL_checkinteger(L, 4);
- int colored = luaL_optinteger(L, 5, 0);
- if (check_init() == 0) {
- return 0;
- }
- Paint_DrawLine(&ctxs[ctx_index]->paint, x, y, x2, y2, colored);
- return 0;
- }
- /**
- 缓冲区绘制矩形
- @api eink.rect(x, y, x2, y2, colored, fill)
- @int 左上顶点x坐标
- @int 左上顶点y坐标
- @int 右下顶点x坐标
- @int 右下顶点y坐标
- @int 默认是0
- @int 是否填充,默认是0,不填充
- @return nil 无返回值
- @usage
- eink.rect(0, 0, 10, 20)
- eink.rect(0, 0, 10, 20, 1) -- Filled
- */
- static int l_eink_rect(lua_State *L)
- {
- int x = luaL_checkinteger(L, 1);
- int y = luaL_checkinteger(L, 2);
- int x2 = luaL_checkinteger(L, 3);
- int y2 = luaL_checkinteger(L, 4);
- int colored = luaL_optinteger(L, 5, 0);
- int fill = luaL_optinteger(L, 6, 0);
-
- if (check_init() == 0) {
- return 0;
- }
- if(fill)
- Paint_DrawFilledRectangle(&ctxs[ctx_index]->paint, x, y, x2, y2, colored);
- else
- Paint_DrawRectangle(&ctxs[ctx_index]->paint, x, y, x2, y2, colored);
- return 0;
- }
- /**
- 缓冲区绘制圆形
- @api eink.circle(x, y, radius, colored, fill)
- @int 圆心x坐标
- @int 圆心y坐标
- @int 半径
- @int 默认是0
- @int 是否填充,默认是0,不填充
- @return nil 无返回值
- @usage
- eink.circle(0, 0, 10)
- eink.circle(0, 0, 10, 1, 1) -- Filled
- */
- static int l_eink_circle(lua_State *L)
- {
- int x = luaL_checkinteger(L, 1);
- int y = luaL_checkinteger(L, 2);
- int radius = luaL_checkinteger(L, 3);
- int colored = luaL_optinteger(L, 4, 0);
- int fill = luaL_optinteger(L, 5, 0);
- if (check_init() == 0) {
- return 0;
- }
- if(fill)
- Paint_DrawFilledCircle(&ctxs[ctx_index]->paint, x, y, radius, colored);
- else
- Paint_DrawCircle(&ctxs[ctx_index]->paint, x, y, radius, colored);
- return 0;
- }
- /**
- 缓冲区绘制QRCode
- @api eink.qrcode(x, y, str, size)
- @int x坐标
- @int y坐标
- @string 二维码的内容
- @int 可选,显示大小,不可小于21,默认21
- @return nil 无返回值
- */
- static int l_eink_qrcode(lua_State *L)
- {
- size_t len;
- int x = luaL_checkinteger(L, 1);
- int y = luaL_checkinteger(L, 2);
- const char* text = luaL_checklstring(L, 3, &len);
- int size = luaL_optinteger(L, 4,21);
- if (check_init() == 0) {
- return 0;
- }
- uint8_t *qrcode = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
- uint8_t *tempBuffer = luat_heap_malloc(qrcodegen_BUFFER_LEN_MAX);
- if (qrcode == NULL || tempBuffer == NULL) {
- if (qrcode)
- luat_heap_free(qrcode);
- if (tempBuffer)
- luat_heap_free(tempBuffer);
- LLOGE("qrcode out of memory");
- return 0;
- }
- bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, qrcodegen_Ecc_MEDIUM,
- qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
- if (ok){
- int qr_size = qrcodegen_getSize(qrcode);
- int scale = size / qr_size ;
- int margin = (size - qr_size * scale) / 2;
- x+=margin;
- y+=margin;
- for (int j = 0; j < qr_size; j++) {
- for (int i = 0; i < qr_size; i++) {
- if (qrcodegen_getModule(qrcode, i, j))
- Paint_DrawFilledRectangle(&ctxs[ctx_index]->paint,x+i*scale,y+j*scale,x+(i+1)*scale,y+(j+1)*scale,0);
- }
- }
- }
- if (qrcode)
- luat_heap_free(qrcode);
- if (tempBuffer)
- luat_heap_free(tempBuffer);
- return 0;
- }
- /**
- 缓冲区绘制电池
- @api eink.bat(x, y, bat)
- @int x坐标
- @int y坐标
- @int 电池电压,单位毫伏
- @return nil 无返回值
- */
- static int l_eink_bat(lua_State *L)
- {
- int x = luaL_checkinteger(L, 1);
- int y = luaL_checkinteger(L, 2);
- int bat = luaL_checkinteger(L, 3);
- int batnum = 0;
- // eink.rect(0, 3, 2, 6)
- // eink.rect(2, 0, 20, 9)
- // eink.rect(9, 1, 19, 8, 0, 1)
- if(bat < 4200 && bat > 4080)batnum = 100;
- if(bat < 4080 && bat > 4000)batnum = 90;
- if(bat < 4000 && bat > 3930)batnum = 80;
- if(bat < 3930 && bat > 3870)batnum = 70;
- if(bat < 3870 && bat > 3820)batnum = 60;
- if(bat < 3820 && bat > 3790)batnum = 50;
- if(bat < 3790 && bat > 3770)batnum = 40;
- if(bat < 3770 && bat > 3730)batnum = 30;
- if(bat < 3730 && bat > 3700)batnum = 20;
- if(bat < 3700 && bat > 3680)batnum = 15;
- if(bat < 3680 && bat > 3500)batnum = 10;
- if(bat < 3500 && bat > 2500)batnum = 5;
- batnum = 20 - (int)(batnum / 5) + 3;
- if (check_init() == 0) {
- return 0;
- }
- // w外框
- Paint_DrawRectangle(&ctxs[ctx_index]->paint, x+0, y+3, x+2, y+6, COLORED);
- Paint_DrawRectangle(&ctxs[ctx_index]->paint, x+2, y+0, x+23, y+9, COLORED);
- // 3 ~21 100 / 5
- Paint_DrawFilledRectangle(&ctxs[ctx_index]->paint, x+batnum, y+1, x+22, y+8, COLORED);
- return 0;
- }
- /**
- 缓冲区绘制天气图标
- @api eink.weather_icon(x, y, code)
- @int x坐标
- @int y坐标
- @int 天气代号
- @return nil 无返回值
- */
- static int l_eink_weather_icon(lua_State *L)
- {
- size_t len;
- int x = luaL_checkinteger(L, 1);
- int y = luaL_checkinteger(L, 2);
- int code = luaL_checkinteger(L, 3);
- const char* str = luaL_optlstring(L, 4, "nil", &len);
- const unsigned char * icon = gImage_999;
- if (strcmp(str, "xue") == 0)code = 401;
- if (strcmp(str, "lei") == 0)code = 302;
- if (strcmp(str, "shachen") == 0)code = 503;
- if (strcmp(str, "wu") == 0)code = 501;
- if (strcmp(str, "bingbao") == 0)code = 504;
- if (strcmp(str, "yun") == 0)code = 103;
- if (strcmp(str, "yu") == 0)code = 306;
- if (strcmp(str, "yin") == 0)code = 101;
- if (strcmp(str, "qing") == 0)code = 100;
- // xue、lei、shachen、wu、bingbao、yun、yu、yin、qing
- //if(code == 64)
- // for(int i = 0; i < 64; i++)
- // {
- // for (int j = 0; j < 8; j++)
- // {
- // for (int k = 0; k < 8; k++)
- // (gImage_103[i*8+j] << k )& 0x80 ? Paint_DrawPixel(&ctxs[ctx_index]->paint, x+j*8+k, y+i, COLORED) : Paint_DrawPixel(&ctxs[ctx_index]->paint, x+j*8+k, y+i, UNCOLORED);
- // }
- // }
- if (check_init() == 0) {
- return 0;
- }
- switch (code)
- {
- case 100:icon = gImage_100;break;
- case 101:icon = gImage_101;break;
- case 102:icon = gImage_102;break;
- case 103:icon = gImage_103;break;
- case 104:icon = gImage_104;break;
- case 200:icon = gImage_200;break;
- case 201:icon = gImage_201;break;
- case 205:icon = gImage_205;break;
- case 208:icon = gImage_208;break;
- case 301:icon = gImage_301;break;
- case 302:icon = gImage_302;break;
- case 303:icon = gImage_303;break;
- case 304:icon = gImage_304;break;
- case 305:icon = gImage_305;break;
- case 306:icon = gImage_306;break;
- case 307:icon = gImage_307;break;
- case 308:icon = gImage_308;break;
- case 309:icon = gImage_309;break;
- case 310:icon = gImage_310;break;
- case 311:icon = gImage_311;break;
- case 312:icon = gImage_312;break;
- case 313:icon = gImage_313;break;
- case 400:icon = gImage_400;break;
- case 401:icon = gImage_401;break;
- case 402:icon = gImage_402;break;
- case 403:icon = gImage_403;break;
- case 404:icon = gImage_404;break;
- case 405:icon = gImage_405;break;
- case 406:icon = gImage_406;break;
- case 407:icon = gImage_407;break;
- case 500:icon = gImage_500;break;
- case 501:icon = gImage_501;break;
- case 502:icon = gImage_502;break;
- case 503:icon = gImage_503;break;
- case 504:icon = gImage_504;break;
- case 507:icon = gImage_507;break;
- case 508:icon = gImage_508;break;
- case 900:icon = gImage_900;break;
- case 901:icon = gImage_901;break;
- case 999:icon = gImage_999;break;
- default:
- break;
- }
- for(int i = 0; i < 48; i++)
- {
- for (int j = 0; j < 6; j++)
- {
- for (int k = 0; k < 8; k++)
- (icon[i*6+j] << k )& 0x80 ? Paint_DrawPixel(&ctxs[ctx_index]->paint, x+j*8+k, y+i, COLORED) : Paint_DrawPixel(&ctxs[ctx_index]->paint, x+j*8+k, y+i, UNCOLORED);
- }
- }
- return 0;
- }
- /**
- 设置墨水屏驱动型号
- @api eink.model(m)
- @int 型号名称, 例如 eink.model(eink.MODEL_1in54_V2)
- @return nil 无返回值
- */
- static int l_eink_model(lua_State *L) {
- EPD_Model(luaL_checkinteger(L, 1));
- return 0;
- }
- #ifdef LUAT_USE_GTFONT
- #include "GT5SLCD2E_1A.h"
- extern void gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int widt,unsigned int high,int(*point)(void*),void* userdata,int mode);
- extern void gtfont_draw_gray_hz(unsigned char *data,unsigned short x,unsigned short y,unsigned short w ,unsigned short h,unsigned char grade, unsigned char HB_par,int(*point)(void*,uint16_t, uint16_t, uint32_t),void* userdata,int mode);
- static int l_eink_draw_gtfont_gb2312(lua_State *L) {
- unsigned char buf[128];
- int len;
- int i = 0;
- uint8_t strhigh,strlow ;
- uint16_t str;
- const char *fontCode = luaL_checklstring(L, 1,&len);
- unsigned char size = luaL_checkinteger(L, 2);
- int x = luaL_checkinteger(L, 3);
- int y = luaL_checkinteger(L, 4);
- while ( i < len){
- strhigh = *fontCode;
- fontCode++;
- strlow = *fontCode;
- str = (strhigh<<8)|strlow;
- fontCode++;
- get_font(buf, 1, str, size, size, size);
- gtfont_draw_w(buf , x ,y , size , size,Paint_DrawPixel,&ctxs[ctx_index]->paint,1);
- x+=size;
- i+=2;
- }
- return 0;
- }
- static int l_eink_draw_gtfont_gb2312_gray(lua_State* L) {
- unsigned char buf[2048];
- int len;
- int i = 0;
- uint8_t strhigh,strlow ;
- uint16_t str;
- const char *fontCode = luaL_checklstring(L, 1,&len);
- unsigned char size = luaL_checkinteger(L, 2);
- unsigned char font_g = luaL_checkinteger(L, 3);
- int x = luaL_checkinteger(L, 4);
- int y = luaL_checkinteger(L, 5);
- while ( i < len){
- strhigh = *fontCode;
- fontCode++;
- strlow = *fontCode;
- str = (strhigh<<8)|strlow;
- fontCode++;
- get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
- Gray_Process(buf,size,size,font_g);
- gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,Paint_DrawPixel,&ctxs[ctx_index]->paint,1);
- x+=size;
- i+=2;
- }
- return 0;
- }
- #ifdef LUAT_USE_GTFONT_UTF8
- extern unsigned short unicodetogb2312 ( unsigned short chr);
- static int l_eink_draw_gtfont_utf8(lua_State *L) {
- unsigned char buf[128];
- int len;
- int i = 0;
- uint8_t strhigh,strlow ;
- uint16_t e,str;
- const char *fontCode = luaL_checklstring(L, 1,&len);
- unsigned char size = luaL_checkinteger(L, 2);
- int x = luaL_checkinteger(L, 3);
- int y = luaL_checkinteger(L, 4);
- for(;;){
- e = utf8_next((uint8_t)*fontCode);
- if ( e == 0x0ffff )
- break;
- fontCode++;
- if ( e != 0x0fffe ){
- uint16_t str = unicodetogb2312(e);
- get_font(buf, 1, str, size, size, size);
- gtfont_draw_w(buf , x ,y , size , size,Paint_DrawPixel,&ctxs[ctx_index]->paint,1);
- x+=size;
- }
- }
- return 0;
- }
- static int l_eink_draw_gtfont_utf8_gray(lua_State* L) {
- unsigned char buf[2048];
- int len;
- int i = 0;
- uint8_t strhigh,strlow ;
- uint16_t e,str;
- const char *fontCode = luaL_checklstring(L, 1,&len);
- unsigned char size = luaL_checkinteger(L, 2);
- unsigned char font_g = luaL_checkinteger(L, 3);
- int x = luaL_checkinteger(L, 4);
- int y = luaL_checkinteger(L, 5);
- for(;;){
- e = utf8_next((uint8_t)*fontCode);
- if ( e == 0x0ffff )
- break;
- fontCode++;
- if ( e != 0x0fffe ){
- uint16_t str = unicodetogb2312(e);
- get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
- Gray_Process(buf,size,size,font_g);
- gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,Paint_DrawPixel,&ctxs[ctx_index]->paint,1);
- x+=size;
- }
- }
- return 0;
- }
- #endif // LUAT_USE_GTFONT_UTF8
- #endif // LUAT_USE_GTFONT
- static void eink_DrawHXBM(uint16_t x, uint16_t y, uint16_t len, const uint8_t *b){
- uint8_t mask = 1;
- if (check_init() == 0) {
- return;
- }
- while(len > 0) {
- if ( *b & mask ) drawFastHLine(&ctxs[ctx_index]->paint, x, y, 1,COLORED);
- else drawFastVLine(&ctxs[ctx_index]->paint, x, y, 1,UNCOLORED);
- x++;
- mask <<= 1;
- if ( mask == 0 ){
- mask = 1;
- b++;
- }
- len--;
- }
- }
- /*
- 绘制位图
- @api eink.drawXbm(x, y, w, h, data)
- @int X坐标
- @int y坐标
- @int 位图宽
- @int 位图高
- @int 位图数据,每一位代表一个像素
- @usage
- -- 取模使用PCtoLCD2002软件即可
- -- 在(0,0)为左上角,绘制 16x16 "今" 的位图
- eink.drawXbm(0, 0, 16,16, string.char(
- 0x80,0x00,0x80,0x00,0x40,0x01,0x20,0x02,0x10,0x04,0x48,0x08,0x84,0x10,0x83,0x60,
- 0x00,0x00,0xF8,0x0F,0x00,0x08,0x00,0x04,0x00,0x04,0x00,0x02,0x00,0x01,0x80,0x00
- ))
- */
- static int l_eink_drawXbm(lua_State *L){
- int x = luaL_checkinteger(L, 1);
- int y = luaL_checkinteger(L, 2);
- int w = luaL_checkinteger(L, 3);
- int h = luaL_checkinteger(L, 4);
- size_t len = 0;
- const char* data = luaL_checklstring(L, 5, &len);
- if (h < 1) return 0; // 行数必须大于0
- if (len*8/h < w) return 0; // 起码要填满一行
- if (len != h*w/8)return 0;
- if (check_init() == 0) {
- return 0;
- }
- uint8_t blen;
- blen = w;
- blen += 7;
- blen >>= 3;
- while( h > 0 ){
- eink_DrawHXBM(x, y, w, (const uint8_t*)data);
- data += blen;
- y++;
- h--;
- }
- lua_pushboolean(L, 1);
- return 1;
- }
- /*
- 切换颜色画板, 适合多色墨水屏
- @api eink.setCtx(index)
- @int 颜色索引, 黑色为0, 红色为1
- @usage
- -- 仅适合多色墨水屏, 对单色墨水屏(只有黑白)的无效
- eink.setCtx(1)
- -- 切换后, 所有drawXXX都会操作在指定颜色的画板
- */
- static int l_eink_set_ctx(lua_State *L) {
- int index = luaL_checkinteger(L, 1);
- if (index < 0 || index > 1) {
- LLOGE("invaild ctx index %d", index);
- return 0;
- }
- if (ctxs[index] == NULL) {
- LLOGE("invaild ctx index %d", index);
- return 0;
- }
- ctx_index = index;
- lua_pushboolean(L, 1);
- return 1;
- }
- #include "rotable2.h"
- static const rotable_Reg_t reg_eink[] =
- {
- { "setup", ROREG_FUNC(l_eink_setup)},
- { "sleep", ROREG_FUNC(l_eink_sleep)},
- { "clear", ROREG_FUNC(l_eink_clear)},
- { "setWin", ROREG_FUNC(l_eink_setWin)},
- { "getWin", ROREG_FUNC(l_eink_getWin)},
- { "print", ROREG_FUNC(l_eink_print)},
- { "show", ROREG_FUNC(l_eink_show)},
- { "rect", ROREG_FUNC(l_eink_rect)},
- { "circle", ROREG_FUNC(l_eink_circle)},
- { "line", ROREG_FUNC(l_eink_line)},
- { "setFont", ROREG_FUNC(l_eink_set_font)},
- { "qrcode", ROREG_FUNC(l_eink_qrcode)},
- { "bat", ROREG_FUNC(l_eink_bat)},
- { "weather_icon", ROREG_FUNC(l_eink_weather_icon)},
- { "model", ROREG_FUNC(l_eink_model)},
- { "drawXbm", ROREG_FUNC(l_eink_drawXbm)},
- { "draw", ROREG_FUNC(l_eink_draw)},
- { "setCtx", ROREG_FUNC(l_eink_set_ctx)},
- #ifdef LUAT_USE_GTFONT
- { "drawGtfontGb2312", ROREG_FUNC(l_eink_draw_gtfont_gb2312)},
- { "drawGtfontGb2312Gray", ROREG_FUNC(l_eink_draw_gtfont_gb2312_gray)},
- #ifdef LUAT_USE_GTFONT_UTF8
- { "drawGtfontUtf8", ROREG_FUNC(l_eink_draw_gtfont_utf8)},
- { "drawGtfontUtf8Gray", ROREG_FUNC(l_eink_draw_gtfont_utf8_gray)},
- #endif // LUAT_USE_GTFONT_UTF8
- #endif // LUAT_USE_GTFONT
- //@const MODEL_1in02d number 1.02寸d
- { "MODEL_1in02d", ROREG_INT(MODEL_1in02d)},
- //@const MODEL_1in54 number 1.54寸
- { "MODEL_1in54", ROREG_INT(MODEL_1in54)},
- //@const MODEL_1in54_V2 number 1.54寸_V2
- { "MODEL_1in54_V2", ROREG_INT(MODEL_1in54_V2)},
- //@const MODEL_1in54b number 1.54寸b
- { "MODEL_1in54b", ROREG_INT(MODEL_1in54b)},
- //@const MODEL_1in54b_V2 number 1.54寸b_V2
- { "MODEL_1in54b_V2", ROREG_INT(MODEL_1in54b_V2)},
- //@const MODEL_1in54_V3 number 1.54寸_V3
- { "MODEL_1in54_V3", ROREG_INT(MODEL_1in54_V3)},
- //@const MODEL_1in54c number 1.54寸c
- { "MODEL_1in54c", ROREG_INT(MODEL_1in54c)},
- //@const MODEL_1in54f number 1.54寸f
- { "MODEL_1in54f", ROREG_INT(MODEL_1in54f)},
- //@const MODEL_2in54b_V3 number 2.13寸b_V3
- { "MODEL_2in54b_V3", ROREG_INT(MODEL_2in13b_V3)},
- //@const MODEL_2in7 number 2.7寸
- { "MODEL_2in7", ROREG_INT(MODEL_2in7)},
- //@const MODEL_2in7b number 2.7寸b
- { "MODEL_2in7b", ROREG_INT(MODEL_2in7b)},
- //@const MODEL_2in9 number 2.9寸
- { "MODEL_2in9", ROREG_INT(MODEL_2in9)},
- //@const MODEL_2in9_V2 number 2.9寸_V2
- { "MODEL_2in9_V2", ROREG_INT(MODEL_2in9_V2)},
- //@const MODEL_2in9bc number 2.9寸bc
- { "MODEL_2in9bc", ROREG_INT(MODEL_2in9bc)},
- //@const MODEL_2in9b_V3 number 2.9寸b_V3
- { "MODEL_2in9b_V3", ROREG_INT(MODEL_2in9b_V3)},
- //@const MODEL_2in9d number 2.9寸d
- { "MODEL_2in9d", ROREG_INT(MODEL_2in9d)},
- //@const MODEL_2in9f number 2.9寸f
- { "MODEL_2in9f", ROREG_INT(MODEL_2in9ff)},
- //@const MODEL_3in7 number 3.7寸
- { "MODEL_3in7", ROREG_INT(MODEL_3in7)},
- // 默认只带8号字体
- //@const font_opposansm8 font 8号字体
- { "font_opposansm8", ROREG_PTR((void*)u8g2_font_opposansm8)},
- #ifdef USE_U8G2_OPPOSANSM_ENGLISH
- //@const font_unifont_t_symbols font 符号字体
- { "font_unifont_t_symbols", ROREG_PTR((void*)u8g2_font_unifont_t_symbols)},
- //@const font_open_iconic_weather_6x_t font 天气字体
- { "font_open_iconic_weather_6x_t", ROREG_PTR((void*)u8g2_font_open_iconic_weather_6x_t)},
- //@const font_opposansm10 font 10号字体
- //@const font_opposansm12 font 12号字体
- //@const font_opposansm16 font 16号字体
- //@const font_opposansm18 font 18号字体
- //@const font_opposansm20 font 20号字体
- //@const font_opposansm22 font 22号字体
- //@const font_opposansm24 font 24号字体
- //@const font_opposansm32 font 32号字体
- { "font_opposansm10", ROREG_PTR((void*)u8g2_font_opposansm10)},
- { "font_opposansm12", ROREG_PTR((void*)u8g2_font_opposansm12)},
- { "font_opposansm16", ROREG_PTR((void*)u8g2_font_opposansm16)},
- { "font_opposansm18", ROREG_PTR((void*)u8g2_font_opposansm18)},
- { "font_opposansm20", ROREG_PTR((void*)u8g2_font_opposansm20)},
- { "font_opposansm22", ROREG_PTR((void*)u8g2_font_opposansm22)},
- { "font_opposansm24", ROREG_PTR((void*)u8g2_font_opposansm24)},
- { "font_opposansm32", ROREG_PTR((void*)u8g2_font_opposansm32)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM8_CHINESE
- //@const font_opposansm8_chinese font 8号中文字体
- { "font_opposansm8_chinese", ROREG_PTR((void*)u8g2_font_opposansm8_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM10_CHINESE
- //@const font_opposansm8_chinese font 10号中文字体
- { "font_opposansm10_chinese", ROREG_PTR((void*)u8g2_font_opposansm10_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM12_CHINESE
- //@const font_opposansm12_chinese font 12号中文字体
- { "font_opposansm12_chinese", ROREG_PTR((void*)u8g2_font_opposansm12_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM16_CHINESE
- //@const font_opposansm16_chinese font 16号中文字体
- { "font_opposansm16_chinese", ROREG_PTR((void*)u8g2_font_opposansm16_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM18_CHINESE
- //@const font_opposansm18_chinese font 18号中文字体
- { "font_opposansm18_chinese", ROREG_PTR((void*)u8g2_font_opposansm18_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM20_CHINESE
- //@const font_opposansm20_chinese font 20号中文字体
- { "font_opposansm20_chinese", ROREG_PTR((void*)u8g2_font_opposansm20_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM22_CHINESE
- //@const font_opposansm22_chinese font 22号中文字体
- { "font_opposansm22_chinese", ROREG_PTR((void*)u8g2_font_opposansm22_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM24_CHINESE
- //@const font_opposansm24_chinese font 24号中文字体
- { "font_opposansm24_chinese", ROREG_PTR((void*)u8g2_font_opposansm24_chinese)},
- #endif
- #ifdef USE_U8G2_OPPOSANSM32_CHINESE
- //@const font_opposansm32_chinese font 32号中文字体
- { "font_opposansm32_chinese", ROREG_PTR((void*)u8g2_font_opposansm32_chinese)},
- #endif
- { "MODEL_1in02d", ROREG_INT(MODEL_1in02d)},
- { "MODEL_1in54", ROREG_INT(MODEL_1in54)},
- { "MODEL_1in54b", ROREG_INT(MODEL_1in54b)},
- { "MODEL_1in54b_V2", ROREG_INT(MODEL_1in54b_V2)},
- { "MODEL_1in54c", ROREG_INT(MODEL_1in54c)},
- { "MODEL_1in54f", ROREG_INT(MODEL_1in54f)},
- { "MODEL_1in54_V2", ROREG_INT(MODEL_1in54_V2)},
- { "MODEL_1in54_V3", ROREG_INT(MODEL_1in54_V3)},
- { "MODEL_2in13", ROREG_INT(MODEL_2in13)},
- { "MODEL_2in13bc", ROREG_INT(MODEL_2in13bc)},
- { "MODEL_2in13b_V3", ROREG_INT(MODEL_2in13b_V3)},
- { "MODEL_2in13d", ROREG_INT(MODEL_2in13d)},
- { "MODEL_2in13_V2", ROREG_INT(MODEL_2in13_V2)},
- { "MODEL_2in66", ROREG_INT(MODEL_2in66)},
- { "MODEL_2in66b", ROREG_INT(MODEL_2in66b)},
- { "MODEL_2in7", ROREG_INT(MODEL_2in7)},
- { "MODEL_2in7b", ROREG_INT(MODEL_2in7b)},
- { "MODEL_2in9", ROREG_INT(MODEL_2in9)},
- { "MODEL_2in9bc", ROREG_INT(MODEL_2in9bc)},
- { "MODEL_2in9b_V3", ROREG_INT(MODEL_2in9b_V3)},
- { "MODEL_2in9d", ROREG_INT(MODEL_2in9d)},
- { "MODEL_2in9ff", ROREG_INT(MODEL_2in9ff)},
- { "MODEL_2in9_V2", ROREG_INT(MODEL_2in9_V2)},
- { "MODEL_3in7", ROREG_INT(MODEL_3in7)},
- { "MODEL_4in2", ROREG_INT(MODEL_4in2)},
- { "MODEL_4in2bc", ROREG_INT(MODEL_4in2bc)},
- { "MODEL_4in2b_V2", ROREG_INT(MODEL_4in2b_V2)},
- { "MODEL_5in65f", ROREG_INT(MODEL_5in65f)},
- { "MODEL_5in83", ROREG_INT(MODEL_5in83)},
- { "MODEL_5in83bc", ROREG_INT(MODEL_5in83bc)},
- { "MODEL_5in83b_V2", ROREG_INT(MODEL_5in83b_V2)},
- { "MODEL_5in83_V2", ROREG_INT(MODEL_5in83_V2)},
- { "MODEL_7in5", ROREG_INT(MODEL_7in5)},
- { "MODEL_7in5bc", ROREG_INT(MODEL_7in5bc)},
- { "MODEL_7in5b_HD", ROREG_INT(MODEL_7in5b_HD)},
- { "MODEL_7in5b_V2", ROREG_INT(MODEL_7in5b_V2)},
- { "MODEL_7in5_HD", ROREG_INT(MODEL_7in5_HD)},
- { "MODEL_7in5_V2", ROREG_INT(MODEL_7in5_V2)},
- { NULL, ROREG_INT(0)}
- };
- LUAMOD_API int luaopen_eink( lua_State *L ){
- luat_newlib2(L, reg_eink);
- return 1;
- }
|