luat_lib_gtfont.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. @module gtfont
  3. @summary gtfont高通字库模块
  4. @version 1.0
  5. @date 2021.11.11
  6. */
  7. #include "luat_base.h"
  8. #include "luat_spi.h"
  9. #include "luat_lcd.h"
  10. #include "luat_malloc.h"
  11. #include "epdpaint.h"
  12. #include "GT5SLCD2E_1A.h"
  13. #define LUAT_LOG_TAG "gt"
  14. #include "luat_log.h"
  15. extern luat_spi_device_t* gt_spi_dev;
  16. //横置横排显示
  17. 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){
  18. unsigned int i,j,k,n;
  19. unsigned char temp;
  20. n = 0;
  21. for( i = 0;i < high; i++){
  22. for( j = 0;j < ((widt+7)>> 3);j++){
  23. temp = pBits[n++];
  24. for(k = 0;k < 8;k++){
  25. if(((temp << k)& 0x80) == 0 ){
  26. /* 显示一个像素点 */
  27. if (mode == 0)point((luat_lcd_conf_t *)userdata, x+k+(j*8), y+i, 0xFFFF);
  28. else if (mode == 1)point((Paint *)userdata, x+k+(j*8), y+i, 0xFFFF);
  29. }else{
  30. /* 显示一个像素点 */
  31. if (mode == 0)point((luat_lcd_conf_t *)userdata, x+k+(j*8), y+i, 0x0000);
  32. else if (mode == 1)point((Paint *)userdata, x+k+(j*8), y+i, 0x0000);
  33. else if (mode == 2)point((u8g2_t *)userdata, x+k+(j*8), y+i, 0x0000);
  34. }
  35. }
  36. }
  37. }
  38. }
  39. /*----------------------------------------------------------------------------------------
  40. * 灰度数据显示函数 1阶灰度/2阶灰度/4阶灰度
  41. * 参数 :
  42. * data灰度数据; x,y=显示起始坐标 ; w 宽度, h 高度,grade 灰度阶级[1阶/2阶/4阶]
  43. * HB_par 1 白底黑字 0 黑底白字
  44. *------------------------------------------------------------------------------------------*/
  45. 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){
  46. unsigned int temp=0,gray,x_temp=x;
  47. unsigned int i=0,j=0,k=0,t;
  48. unsigned char c,c2,*p;
  49. unsigned long color8bit,color4bit,color3bit[8],color2bit,color;
  50. t=(w+7)/8*grade;//
  51. p=data;
  52. if(grade==2){
  53. for(i=0;i<t*h;i++){
  54. c=*p++;
  55. for(j=0;j<4;j++){
  56. color2bit=(c>>6);//获取像素点的2bit颜色值
  57. if(HB_par==1)color2bit= (3-color2bit)*250/3;//白底黑字
  58. else color2bit= color2bit*250/3;//黑底白字
  59. gray=color2bit/8;
  60. color=(0x001f&gray)<<11; //r-5
  61. color=color|(((0x003f)&(gray*2))<<5); //g-6
  62. color=color|(0x001f&gray); //b-5
  63. temp=color;
  64. temp=temp;
  65. c<<=2;
  66. if(x<(x_temp+w)){
  67. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  68. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  69. }
  70. x++;
  71. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  72. }
  73. }
  74. }
  75. else if(grade==3){
  76. for(i=0;i<t*h;i+=3){
  77. c=*p; c2=*(p+1);
  78. color3bit[0]=(c>>5)&0x07;
  79. color3bit[1]=(c>>2)&0x07;
  80. color3bit[2]=((c<<1)|(c2>>7))&0x07;
  81. p++;
  82. c=*p; c2=*(p+1);
  83. color3bit[3]=(c>>4)&0x07;
  84. color3bit[4]=(c>>1)&0x07;
  85. color3bit[5]=((c<<2)|(c2>>6))&0x07;
  86. p++;
  87. c=*p;
  88. color3bit[6]=(c>>3)&0x07;
  89. color3bit[7]=(c>>0)&0x07;
  90. p++;
  91. for(j=0;j<8;j++){
  92. if(HB_par==1)color3bit[j]= (7-color3bit[j])*255/7;//白底黑字
  93. else color3bit[j]=color3bit[j]*255/7;//黑底白字
  94. gray =color3bit[j]/8;
  95. color=(0x001f&gray)<<11; //r-5
  96. color=color|(((0x003f)&(gray*2))<<5); //g-6
  97. color=color|(0x001f&gray); //b-5
  98. temp =color;
  99. if(x<(x_temp+w)){
  100. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  101. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  102. }
  103. x++;
  104. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  105. }
  106. }
  107. }
  108. else if(grade==4){
  109. for(i=0;i<t*h;i++){
  110. c=*p++;
  111. for(j=0;j<2;j++){
  112. color4bit=(c>>4);
  113. if(HB_par==1)color4bit= (15-color4bit)*255/15;//白底黑字
  114. else color4bit= color4bit*255/15;//黑底白字
  115. gray=color4bit/8;
  116. color=(0x001f&gray)<<11; //r-5
  117. color=color|(((0x003f)&(gray*2))<<5); //g-6
  118. color=color|(0x001f&gray); //b-5
  119. temp=color;
  120. c<<=4;
  121. if(x<(x_temp+w)){
  122. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  123. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  124. }
  125. x++;
  126. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  127. }
  128. }
  129. }
  130. else{
  131. for(i=0;i<t*h;i++){
  132. c=*p++;
  133. for(j=0;j<8;j++){
  134. if(c&0x80) color=0x0000;
  135. else color=0xffff;
  136. c<<=1;
  137. if(x<(x_temp+w)){
  138. if(color == 0x0000 && HB_par == 1){
  139. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,color);
  140. else if (mode == 1)point((Paint *)userdata, x,y,color);
  141. }else if(HB_par == 0 && color == 0x0000){
  142. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,~color);
  143. else if (mode == 1)point((Paint *)userdata, x,y,~color);
  144. }
  145. }
  146. x++;
  147. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  148. }
  149. }
  150. }
  151. }
  152. static int l_gtfont_init(lua_State* L) {
  153. if (gt_spi_dev == NULL) {
  154. gt_spi_dev = lua_touserdata(L, 1);
  155. }
  156. luat_spi_device_send(gt_spi_dev, 0xff, 1);
  157. return 0;
  158. }
  159. #include "rotable2.h"
  160. static const rotable_Reg_t reg_gtfont[] =
  161. {
  162. { "init" , ROREG_FUNC(l_gtfont_init)},
  163. { NULL, {}}
  164. };
  165. LUAMOD_API int luaopen_gtfont( lua_State *L ) {
  166. luat_newlib(L, reg_gtfont);
  167. return 1;
  168. }