luat_lib_gtfont.c 6.0 KB

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