luat_lib_gtfont.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. @module gtfont
  3. @summary 高通字库芯片
  4. @version 1.0
  5. @date 2021.11.11
  6. @tag LUAT_USE_GTFONT
  7. @usage
  8. -- 已测试字体芯片型号 GT5SLCD1E-1A
  9. -- 如需要支持其他型号,请报issue
  10. */
  11. #include "luat_base.h"
  12. #include "luat_spi.h"
  13. #include "luat_lcd.h"
  14. #include "luat_mem.h"
  15. #include "epdpaint.h"
  16. #include "GT5SLCD2E_1A.h"
  17. #define LUAT_LOG_TAG "gt"
  18. #include "luat_log.h"
  19. #ifdef LUAT_USE_LCD
  20. extern luat_color_t lcd_str_fg_color,lcd_str_bg_color;
  21. #else
  22. static luat_color_t lcd_str_fg_color = LCD_BLACK ,lcd_str_bg_color = LCD_WHITE ;
  23. #endif
  24. extern luat_spi_device_t* gt_spi_dev;
  25. //横置横排显示
  26. unsigned int gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int size,unsigned int widt,unsigned int high,int(*point)(void*,uint16_t, uint16_t, uint32_t),void* userdata,int mode){
  27. unsigned int i=0,j=0,k=0,n=0,dw=0;
  28. unsigned char temp;
  29. int w = ((widt+7)>> 3);
  30. for( i = 0;i < high; i++){
  31. for( j = 0;j < w;j++){
  32. temp = pBits[n++];
  33. for(k = 0;k < 8;k++){
  34. if (widt < size){
  35. if (j==(w-1) && k==widt%8){
  36. break;
  37. }
  38. }
  39. if(((temp << k)& 0x80) == 0 ){//背景色
  40. // /* 显示一个像素点 */
  41. // if (mode == 0)point((luat_lcd_conf_t *)userdata, x+k+(j*8), y+i, lcd_str_bg_color);
  42. // else if (mode == 1)point((Paint *)userdata, x+k+(j*8), y+i, 0xFFFF);
  43. }else{
  44. /* 显示一个像素点 */
  45. if (dw<k+(j*8)) dw = k+(j*8);
  46. if (mode == 0)point((luat_lcd_conf_t *)userdata, x+k+(j*8), y+i, lcd_str_fg_color);
  47. else if (mode == 1)point((Paint *)userdata, x+k+(j*8), y+i, 0x0000);
  48. else if (mode == 2)point((u8g2_t *)userdata, x+k+(j*8), y+i, 0x0000);
  49. }
  50. }
  51. }
  52. if (widt < size){
  53. n += (size-widt)>>3;
  54. }
  55. }
  56. return ++dw;
  57. }
  58. /*----------------------------------------------------------------------------------------
  59. * 灰度数据显示函数 1阶灰度/2阶灰度/4阶灰度
  60. * 参数 :
  61. * data灰度数据; x,y=显示起始坐标 ; w 宽度, h 高度,grade 灰度阶级[1阶/2阶/4阶]
  62. * HB_par 1 白底黑字 0 黑底白字
  63. *------------------------------------------------------------------------------------------*/
  64. 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){
  65. unsigned int temp=0,gray,x_temp=x;
  66. unsigned int i=0,j=0,t;
  67. unsigned char c,c2,*p;
  68. unsigned long color4bit,color3bit[8],color2bit,color;
  69. t=(w+7)/8*grade;//
  70. p=data;
  71. if(grade==2){
  72. for(i=0;i<t*h;i++){
  73. c=*p++;
  74. for(j=0;j<4;j++){
  75. color2bit=(c>>6);//获取像素点的2bit颜色值
  76. if(HB_par==1)color2bit= (3-color2bit)*250/3;//白底黑字
  77. else color2bit= color2bit*250/3;//黑底白字
  78. gray=color2bit/8;
  79. color=(0x001f&gray)<<11; //r-5
  80. color=color|(((0x003f)&(gray*2))<<5); //g-6
  81. color=color|(0x001f&gray); //b-5
  82. temp=color;
  83. temp=temp;
  84. c<<=2;
  85. if(x<(x_temp+w)){
  86. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  87. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  88. }
  89. x++;
  90. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  91. }
  92. }
  93. }
  94. else if(grade==3){
  95. for(i=0;i<t*h;i+=3){
  96. c=*p; c2=*(p+1);
  97. color3bit[0]=(c>>5)&0x07;
  98. color3bit[1]=(c>>2)&0x07;
  99. color3bit[2]=((c<<1)|(c2>>7))&0x07;
  100. p++;
  101. c=*p; c2=*(p+1);
  102. color3bit[3]=(c>>4)&0x07;
  103. color3bit[4]=(c>>1)&0x07;
  104. color3bit[5]=((c<<2)|(c2>>6))&0x07;
  105. p++;
  106. c=*p;
  107. color3bit[6]=(c>>3)&0x07;
  108. color3bit[7]=(c>>0)&0x07;
  109. p++;
  110. for(j=0;j<8;j++){
  111. if(HB_par==1)color3bit[j]= (7-color3bit[j])*255/7;//白底黑字
  112. else color3bit[j]=color3bit[j]*255/7;//黑底白字
  113. gray =color3bit[j]/8;
  114. color=(0x001f&gray)<<11; //r-5
  115. color=color|(((0x003f)&(gray*2))<<5); //g-6
  116. color=color|(0x001f&gray); //b-5
  117. temp =color;
  118. if(x<(x_temp+w)){
  119. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  120. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  121. }
  122. x++;
  123. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  124. }
  125. }
  126. }
  127. else if(grade==4){
  128. for(i=0;i<t*h;i++){
  129. c=*p++;
  130. for(j=0;j<2;j++){
  131. color4bit=(c>>4);
  132. if(HB_par==1)color4bit= (15-color4bit)*255/15;//白底黑字
  133. else color4bit= color4bit*255/15;//黑底白字
  134. gray=color4bit/8;
  135. color=(0x001f&gray)<<11; //r-5
  136. color=color|(((0x003f)&(gray*2))<<5); //g-6
  137. color=color|(0x001f&gray); //b-5
  138. temp=color;
  139. c<<=4;
  140. if(x<(x_temp+w)){
  141. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  142. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  143. }
  144. x++;
  145. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  146. }
  147. }
  148. }
  149. else{
  150. for(i=0;i<t*h;i++){
  151. c=*p++;
  152. for(j=0;j<8;j++){
  153. if(c&0x80) color=0x0000;
  154. else color=0xffff;
  155. c<<=1;
  156. if(x<(x_temp+w)){
  157. if(color == 0x0000 && HB_par == 1){
  158. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,color);
  159. else if (mode == 1)point((Paint *)userdata, x,y,color);
  160. }else if(HB_par == 0 && color == 0x0000){
  161. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,~color);
  162. else if (mode == 1)point((Paint *)userdata, x,y,~color);
  163. }
  164. }
  165. x++;
  166. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  167. }
  168. }
  169. }
  170. }
  171. #ifndef LUAT_COMPILER_NOWEAK
  172. LUAT_WEAK int GT_Font_Init(void) {
  173. return 0;
  174. }
  175. #endif
  176. /**
  177. 初始化高通字体芯片
  178. @api gtfont.init(spi_device)
  179. @userdata 仅支持spi device 生成的指针数据
  180. @return boolean 成功返回true,否则返回false
  181. @usage
  182. -- 特别提醒: 使用本库的任何代码, 都需要 额外 的 高通字体芯片 !!
  183. -- 没有额外芯片是跑不了的!!
  184. gtfont.init(spi_device)
  185. */
  186. static int l_gtfont_init(lua_State* L) {
  187. if (gt_spi_dev == NULL) {
  188. gt_spi_dev = lua_touserdata(L, 1);
  189. }
  190. const char data = 0xff;
  191. luat_spi_device_send(gt_spi_dev, &data, 1);
  192. int font_init = GT_Font_Init();
  193. lua_pushboolean(L, font_init == 0 ? 1 : 0);
  194. return 1;
  195. }
  196. #include "rotable2.h"
  197. static const rotable_Reg_t reg_gtfont[] =
  198. {
  199. { "init" , ROREG_FUNC(l_gtfont_init)},
  200. { NULL, ROREG_INT(0)}
  201. };
  202. LUAMOD_API int luaopen_gtfont( lua_State *L ) {
  203. luat_newlib2(L, reg_gtfont);
  204. return 1;
  205. }