luat_lib_gtfont.c 5.4 KB

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