ssd1327.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. #include "../i2c/ad0x0_i2c1.h"
  2. #include "../i2c/ad0x0_i2c2.h"
  3. #include "../i2c/ad0x0_i2c_ext.h"
  4. #include <string.h>
  5. #include "ssd1327.h"
  6. //unsigned char vbuf[100];
  7. #ifdef USE_BACKBUFFER
  8. #include "math.h"
  9. #endif
  10. #include "font7x10.h"
  11. void OLED_Init(oled_desc_s *p_desc);
  12. void OLED_test(oled_desc_s *p_desc);
  13. void OLED_test2(oled_desc_s *p_desc);
  14. void OLED_test3(oled_desc_s *p_desc);
  15. void OLED_Printf(oled_desc_s *p_desc,unsigned char* buf, unsigned char size); /* печатает строку с размерами - 0 самый мелкий 2 - увеличиный - 3 самый большой(только для цифр) */
  16. void OLED_DrawImage(unsigned char num_image); /* выводит картинку из флеш */
  17. void OLED_Bat(unsigned char y,unsigned char x, unsigned char z); /* рисует батарейку по координатам z - от 0 до 10 делений */
  18. void OLED_Mode(oled_desc_s *p_desc, char set_mode); /* 1 - inverted / 0 - normal */
  19. void OLED_Sleep(oled_desc_s *p_desc, char set); /* 1 - on sleep / 0 - off sleep */
  20. void OLED_Command(oled_desc_s *p_desc, unsigned char ControByte, unsigned char DataByte);
  21. void OLED_Command2(oled_desc_s *p_desc, unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2);
  22. void OLED_Command3(oled_desc_s *p_desc, unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2,unsigned char DataByte3);
  23. void OLED_Command_buf(oled_desc_s *p_desc, unsigned char *pbuf,uint8_t count);
  24. void OLED_PrintAt(oled_desc_s *p_desc, unsigned char x, unsigned char y,unsigned char* buf, unsigned char size);
  25. void OLED_Char2x(oled_desc_s *p_desc, unsigned int c); /* auaia neiaiea ia 2 no?iee*/
  26. void OLED_GotoXY(oled_desc_s *p_desc, unsigned char x, unsigned char y);
  27. void OLED_GotoXYWH(oled_desc_s *p_desc, unsigned char x, unsigned char y,unsigned char w, unsigned char h);
  28. void OLED_Clear(oled_desc_s *p_desc); /* i?enoea anaai aenieay */
  29. #ifdef USE_BACKBUFFER
  30. void buf_render(oled_desc_s *p_desc);
  31. portFORCE_INLINE float ad0x0_getdist(int16_t x,int16_t y);
  32. // portFORCE_INLINE uint16_t buf_getpixelcol(uint8_t x,uint8_t y);
  33. portFORCE_INLINE void buf_setpixelcol(uint8_t x,uint8_t y,uint8_t c);
  34. uint16_t sqrxi32(uint32_t y);
  35. void OLED_Command_framebuf(oled_desc_s *p_desc);
  36. #endif
  37. // void OLED_BigNum(unsigned char num); /* auaia aieuoeo oeo? */
  38. void OLED_Char(oled_desc_s *p_desc, unsigned int c); /* auaia neiaiea */
  39. oled_desc_s *p_desc_i2c1=NULL,*p_desc_i2c2=NULL;//жопой чую, что пожалею, что сунул все в один си файл
  40. #ifdef USE_BACKBUFFER
  41. __packed struct i2c_cheat_frame_buf_s{
  42. uint8_t i2c_0x40;
  43. uint8_t framebuf[8192];
  44. };
  45. struct i2c_cheat_frame_buf_s framebuf;
  46. struct demopoint_s{
  47. int16_t x,y;
  48. int8_t dir_x,dir_y;
  49. float weight;
  50. };
  51. #endif
  52. //
  53. void command_i2c1(unsigned char ControByte, unsigned char DataByte){
  54. OLED_Command(p_desc_i2c1,ControByte,DataByte);
  55. }
  56. void command_i2c2(unsigned char ControByte, unsigned char DataByte){
  57. OLED_Command(p_desc_i2c2,ControByte,DataByte);
  58. }
  59. void command_buf_i2c1(unsigned char *pbuf,uint8_t count){
  60. OLED_Command_buf(p_desc_i2c1,pbuf,count);
  61. }
  62. void command_buf_i2c2(unsigned char *pbuf,uint8_t count){
  63. OLED_Command_buf(p_desc_i2c2,pbuf,count);
  64. }
  65. void command2_i2c1(unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2){
  66. OLED_Command2(p_desc_i2c1,ControByte,DataByte1,DataByte2);
  67. }
  68. void command2_i2c2(unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2){
  69. OLED_Command2(p_desc_i2c2,ControByte,DataByte1,DataByte2);
  70. }
  71. void command3_i2c1(unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2,unsigned char DataByte3){
  72. OLED_Command3(p_desc_i2c1,ControByte,DataByte1,DataByte2,DataByte3);
  73. }
  74. void command3_i2c2(unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2,unsigned char DataByte3){
  75. OLED_Command3(p_desc_i2c2,ControByte,DataByte1,DataByte2,DataByte3);
  76. }
  77. void OLED_PrintAt_i2c1(unsigned char x, unsigned char y,unsigned char* buf, unsigned char size){
  78. OLED_PrintAt(p_desc_i2c1,x,y,buf,size);
  79. }
  80. void OLED_PrintAt_i2c2(unsigned char x, unsigned char y,unsigned char* buf, unsigned char size){
  81. OLED_PrintAt(p_desc_i2c2,x,y,buf,size);
  82. }
  83. void OLED_test_i2c1(void){
  84. OLED_test(p_desc_i2c1);
  85. }
  86. void OLED_test_i2c2(void){
  87. OLED_test(p_desc_i2c2);
  88. }
  89. void OLED_test2_i2c1(void){
  90. OLED_test2(p_desc_i2c1);
  91. }
  92. void OLED_test2_i2c2(void){
  93. OLED_test2(p_desc_i2c2);
  94. }
  95. void OLED_test3_i2c1(void){
  96. OLED_test3(p_desc_i2c1);
  97. }
  98. void OLED_test3_i2c2(void){
  99. OLED_test3(p_desc_i2c2);
  100. }
  101. void OLED_clear_i2c1(void){
  102. OLED_Clear(p_desc_i2c1);
  103. }
  104. void OLED_clear_i2c2(void){
  105. OLED_Clear(p_desc_i2c2);
  106. }
  107. void OLED_Init2(I2C_TypeDef * _I2C_BASE, oled_desc_s *p_desc){
  108. memset(p_desc,0,sizeof(oled_desc_s));
  109. p_desc->font_w=8;
  110. p_desc->font_h=10;
  111. framebuf.i2c_0x40=0x40;
  112. memset(framebuf.framebuf,0,8192);
  113. if(_I2C_BASE == (I2C_TypeDef *)I2C1_BASE){
  114. p_desc_i2c1=p_desc;
  115. p_desc->ad0x0_i2c_get_ext_async2=ad0x0_i2c1_get_ext_async2;
  116. p_desc->ad0x0_i2c_push=ad0x0_i2c1_push;
  117. p_desc->command=command_i2c1;
  118. p_desc->command_buf=command_buf_i2c1;
  119. p_desc->command2=command2_i2c1;
  120. p_desc->command3=command3_i2c1;
  121. p_desc->print_at=OLED_PrintAt_i2c1;
  122. p_desc->OLED_test=OLED_test_i2c1;
  123. p_desc->OLED_test2=OLED_test2_i2c1;
  124. p_desc->OLED_test3=OLED_test3_i2c1;
  125. p_desc->clear=OLED_clear_i2c1;
  126. OLED_Init(p_desc_i2c1);
  127. }else
  128. if(_I2C_BASE == (I2C_TypeDef *)I2C2_BASE){
  129. p_desc_i2c2=p_desc;
  130. p_desc->ad0x0_i2c_get_ext_async2=ad0x0_i2c2_get_ext_async2;
  131. p_desc->ad0x0_i2c_push=ad0x0_i2c2_push;
  132. p_desc->command=command_i2c2;
  133. p_desc->command_buf=command_buf_i2c2;
  134. p_desc->command2=command2_i2c2;
  135. p_desc->command3=command3_i2c2;
  136. p_desc->print_at=OLED_PrintAt_i2c2;
  137. p_desc->OLED_test=OLED_test_i2c2;
  138. p_desc->OLED_test2=OLED_test2_i2c2;
  139. p_desc->OLED_test3=OLED_test3_i2c2;
  140. p_desc->clear=OLED_clear_i2c2;
  141. OLED_Init(p_desc_i2c2);
  142. }else{
  143. ad0x0_err();
  144. }
  145. }
  146. /* ---------------------------------------------------------
  147. * OLED_init
  148. * ---------------------------------------------------------
  149. */
  150. void OLED_Init(oled_desc_s *p_desc)
  151. {
  152. //OLED_Sleep(0);
  153. p_desc->command(COMAND, SSD1327_DISPLAYOFF);
  154. //HAL_Delay(10);
  155. ad0x0_i2c1_push(ADQ_DELAY_MS,100);
  156. p_desc->command(COMAND, SSD1327_DISPLAYON);
  157. ad0x0_i2c1_push(ADQ_DELAY_MS,100);
  158. p_desc->command2(COMAND, SSD1327_SEGREMAP, 0x53);//ad0x0
  159. /*p_desc->command(COMAND, SSD1306_SEGREMAP | 0x0);//ssd1327
  160. p_desc->command(COMAND, 0x51);
  161. p_desc->command(COMAND, SSD1306_SETCONTRAST);
  162. p_desc->command(COMAND, 0x7f);
  163. p_desc->command(COMAND, SSD1306_SETDISPLAYCLOCKDIV);
  164. p_desc->command(COMAND, 0x80);
  165. p_desc->command(COMAND, SSD1306_SETMULTIPLEX);
  166. p_desc->command(COMAND, 0x3F);
  167. p_desc->command(COMAND, SSD1306_SETDISPLAYOFFSET);
  168. p_desc->command(COMAND, 0x00);
  169. p_desc->command(COMAND, SSD1306_SETSTARTLINE | 0x00);
  170. p_desc->command(COMAND, SSD1306_CHARGEPUMP);
  171. p_desc->command(COMAND, 0x14);
  172. p_desc->command(COMAND, SSD1306_MEMORYMODE);
  173. p_desc->command(COMAND, 0x00);
  174. //p_desc->command(COMAND, SSD1306_SEGREMAP | 0x1);//ad0x0
  175. p_desc->command(COMAND, SSD1306_SEGREMAP | 0x0);//ad0x0
  176. //p_desc->command(COMAND, SSD1306_COMSCANDEC);//ad0x0
  177. p_desc->command(COMAND, SSD1306_COMSCANINC);
  178. p_desc->command(COMAND, SSD1306_SETCOMPINS);
  179. //p_desc->command(COMAND, 0x12);
  180. p_desc->command(COMAND, 0x2);//ad0x0
  181. p_desc->command(COMAND, SSD1306_SETCONTRAST);
  182. p_desc->command(COMAND, 0xCF);
  183. p_desc->command(COMAND, SSD1306_SETPRECHARGE);
  184. p_desc->command(COMAND, 0xF1);
  185. p_desc->command(COMAND, SSD1306_SETVCOMDETECT);
  186. //p_desc->command(COMAND, 0x40);
  187. p_desc->command(COMAND, 0x20);
  188. p_desc->command(COMAND, SSD1306_DISPLAYALLON_RESUME);
  189. OLED_Mode(p_desc, 0);
  190. OLED_Sleep(p_desc, 1);*/
  191. p_desc->command(COMAND, SSD1306_DISPLAYALLON_RESUME);
  192. //OLED_Clear(p_desc);
  193. OLED_GotoXY(p_desc, 0,0);
  194. }
  195. /*128x128, это x[0-127],y[0-127]*/
  196. void OLED_GotoXY(oled_desc_s *p_desc, unsigned char x, unsigned char y)
  197. {
  198. if(y & 0x1)ad0x0_err();
  199. p_desc->char_x = x;
  200. p_desc->char_y = y;
  201. //столбцов 128, однако в одном байте "два столбца" (2 ниббла (16 градаций серого))
  202. p_desc->command3(COMAND, SSD1327_COLUMNADDR,x>>1,63); //0x21//0x15 0-0x63
  203. //p_desc->command(COMAND, x>>1);
  204. //p_desc->command(COMAND, 63);//==
  205. p_desc->command3(COMAND, SSD1327_PAGEADDR,y,0x7F);//0x22 //0x75
  206. //p_desc->command(COMAND, y);
  207. //p_desc->command(COMAND, 0x7F);
  208. }
  209. void OLED_GotoXYWH(oled_desc_s *p_desc, unsigned char x, unsigned char y,unsigned char w, unsigned char h){
  210. if(y & 0x1)ad0x0_err();
  211. p_desc->char_x = x;
  212. p_desc->char_y = y;
  213. p_desc->command3(COMAND, SSD1327_COLUMNADDR,x>>1,(x>>1)+(w>>1)-1);
  214. //p_desc->command(COMAND, x>>1); //начало строки
  215. //p_desc->command(COMAND, (x>>1)+(w>>1)); //конец (после переполенения след.столб.+начало строки)
  216. p_desc->command3(COMAND, SSD1327_PAGEADDR,y,0x7f);
  217. //p_desc->command(COMAND, y);//столбец старт
  218. //p_desc->command(COMAND, 0x7f);//
  219. }
  220. void OLED_Char2x(oled_desc_s *p_desc, unsigned int c)
  221. {
  222. ad0x0_err();
  223. /*unsigned char x = 0;
  224. uint8_t suka=4,v,nv;
  225. //вызов OLED_GotoXYWH(x,y,10,2) должен установить окно размером 10пикс х 2строки
  226. //типа было 10110111 => 1100111100111111
  227. for (x=0; x<5; x++)
  228. {
  229. v=OLED_Buffer[c*5+x];
  230. nv=0;
  231. suka=4;
  232. while(suka--){
  233. if(v & 0x8){nv|=0x3;}
  234. if(suka)nv<<=2;
  235. v<<=1;
  236. }
  237. p_desc->command(DATA, nv);
  238. p_desc->command(DATA, nv);
  239. //p_desc->command(DATA, 0x0);
  240. }
  241. for (x=0; x<5; x++)
  242. {
  243. v=OLED_Buffer[c*5+x];
  244. nv=0;
  245. suka=4;
  246. while(suka--){
  247. if(v & 0x80){nv|=0x3;}
  248. if(suka)nv<<=2;
  249. v<<=1;
  250. }
  251. p_desc->command(DATA, nv);
  252. p_desc->command(DATA, nv);
  253. //p_desc->command(DATA, 0x0);
  254. }
  255. p_desc->OLED_X += 11;
  256. if(p_desc->OLED_X>SSD1306_LCDWIDTH)
  257. {
  258. p_desc->OLED_X = SSD1306_DEFAULT_SPACE;
  259. }
  260. */
  261. }
  262. /* ---------------------------------------------------------
  263. * OLED_Clear
  264. * ---------------------------------------------------------
  265. */
  266. void OLED_Clear(oled_desc_s *p_desc)
  267. {
  268. uint16_t i;
  269. uint8_t buf16[16];
  270. OLED_GotoXYWH(p_desc, 0,0,128,128);
  271. for (i=0; i<16; i++)buf16[i]=0x0;
  272. for (i=0; i<8192/16; i++)
  273. {
  274. p_desc->command_buf(buf16,16);
  275. }
  276. p_desc->char_x = 0;
  277. p_desc->char_y = 0;
  278. }
  279. /* ---------------------------------------------------------
  280. * OLED_Char
  281. предполагается, что окно в памяти ссд1327 для отрисовки чара
  282. уже выставлено, мы просто заполняем его данными
  283. * ---------------------------------------------------------
  284. */
  285. void OLED_Char(oled_desc_s *p_desc, unsigned int c)
  286. {
  287. unsigned char y = 10;
  288. uint8_t *pfchar=(uint8_t *)Font7x10_u8 + (c-0x20)*10;
  289. uint8_t pbuf[16],pbuf_i=0;
  290. /*for (x=0; x<5; x++)
  291. {
  292. p_desc->command(DATA, OLED_Buffer[c*5+x]);
  293. } */
  294. while(y)
  295. {
  296. uint8_t b=*pfchar;
  297. uint8_t shc=4;
  298. uint8_t r;
  299. while(shc){
  300. if(b & 0x40)r=0xF0;else r=0;
  301. if(b & 0x80)r|=0x0F;else r&=0xF0;
  302. b<<=2;
  303. shc--;
  304. //p_desc->command(DATA, r);//по одному байту слать это тормозно, нах надо, будем кусками по 16 байт
  305. pbuf[pbuf_i++]=r;
  306. if(pbuf_i>15){
  307. p_desc->command_buf(pbuf,16);
  308. pbuf_i=0;
  309. }
  310. }
  311. pfchar++;
  312. y--;
  313. }
  314. if(pbuf_i>0)p_desc->command_buf(pbuf,pbuf_i);//последний кусок
  315. }
  316. void OLED_PrintAt(oled_desc_s *p_desc, unsigned char x, unsigned char y,unsigned char* buf, unsigned char size){
  317. while (*buf!=0)
  318. {
  319. if(size==0)
  320. {
  321. if((x+p_desc->font_w)>SSD1306_LCDWIDTH){
  322. x=0;
  323. y+=10;
  324. if(SSD1306_LCDHEIGHT < (y+p_desc->font_h))y=0;
  325. }
  326. OLED_GotoXYWH(p_desc, x,y,8,10);
  327. OLED_Char(p_desc, *buf++);
  328. x+=8;
  329. }
  330. }
  331. p_desc->char_x = x;
  332. }
  333. void OLED_Command(oled_desc_s *p_desc, unsigned char ControByte, unsigned char DataByte){
  334. /*if(ad0x0_i2c_isovrflow()){
  335. asm("nop");
  336. }else
  337. {
  338. asm("nop");
  339. }*/
  340. //ad0x0_i2c1_push(0x1,0);
  341. //ad0x0_i2c1_push(0x3,SSD1306_I2C_ADDRESS & 0xFE);
  342. /*ad0x0_i2c1_push(ADQ_START_ADDR,SSD1306_I2C_ADDRESS & 0xFE);//v2 start+addr
  343. ad0x0_i2c1_push(ADQ_TX,ControByte);
  344. ad0x0_i2c1_push(ADQ_TX_STOP,DataByte);
  345. ad0x0_i2c2_push(ADQ_START_ADDR,SSD1306_I2C_ADDRESS & 0xFE);//v2 start+addr
  346. ad0x0_i2c2_push(ADQ_TX,ControByte);
  347. ad0x0_i2c2_push(ADQ_TX_STOP,DataByte);*/
  348. uint8_t h_ext;
  349. ad0x0_i2c_ext_t *p_ext_t=NULL;
  350. p_desc->ad0x0_i2c_get_ext_async2(&h_ext,(void**)&p_ext_t);
  351. p_ext_t->addr_w=SSD1306_I2C_ADDRESS;
  352. p_ext_t->user_data0=ControByte | (uint32_t)(DataByte<<8);
  353. p_ext_t->pbuf_count=2;
  354. p_ext_t->pbuf_tx=(uint8_t*)&p_ext_t->user_data0;//TX!!!!!!!
  355. p_ext_t->p_pre_SetTXRXBuf=EXT_SetTXRXBuf_i2c;
  356. p_desc->ad0x0_i2c_push(ADQ_HAL_ADDR_TX_BUF_STOP_EXT,h_ext);
  357. }
  358. void OLED_Command2(oled_desc_s *p_desc, unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2){
  359. uint8_t h_ext;
  360. ad0x0_i2c_ext_t *p_ext_t=NULL;
  361. p_desc->ad0x0_i2c_get_ext_async2(&h_ext,(void**)&p_ext_t);
  362. p_ext_t->addr_w=SSD1306_I2C_ADDRESS;
  363. p_ext_t->user_data0=ControByte | (uint32_t)(DataByte1<<8) | (uint32_t)(DataByte2<<16);
  364. p_ext_t->pbuf_count=3;
  365. p_ext_t->pbuf_tx=(uint8_t*)&p_ext_t->user_data0;//TX!!!!!!!
  366. p_ext_t->p_pre_SetTXRXBuf=EXT_SetTXRXBuf_i2c;
  367. p_desc->ad0x0_i2c_push(ADQ_HAL_ADDR_TX_BUF_STOP_EXT,h_ext);
  368. }
  369. void OLED_Command3(oled_desc_s *p_desc, unsigned char ControByte, unsigned char DataByte1,unsigned char DataByte2,unsigned char DataByte3){
  370. uint8_t h_ext;
  371. ad0x0_i2c_ext_t *p_ext_t=NULL;
  372. p_desc->ad0x0_i2c_get_ext_async2(&h_ext,(void**)&p_ext_t);
  373. p_ext_t->addr_w=SSD1306_I2C_ADDRESS;
  374. p_ext_t->user_data0=ControByte | (uint32_t)(DataByte1<<8) | (uint32_t)(DataByte2<<16)| (uint32_t)(DataByte3<<24);
  375. p_ext_t->pbuf_count=4;
  376. p_ext_t->pbuf_tx=(uint8_t*)&p_ext_t->user_data0;//TX!!!!!!!
  377. p_ext_t->p_pre_SetTXRXBuf=EXT_SetTXRXBuf_i2c;
  378. p_desc->ad0x0_i2c_push(ADQ_HAL_ADDR_TX_BUF_STOP_EXT,h_ext);
  379. }
  380. void OLED_Command_buf(oled_desc_s *p_desc, unsigned char *pbuf,uint8_t count){
  381. uint8_t h_ext;
  382. ad0x0_i2c_ext_t *p_ext_t=NULL;
  383. p_desc->ad0x0_i2c_get_ext_async2(&h_ext,(void**)&p_ext_t);
  384. /*void (*_p_post_cb_func)(uint8_t _sf_id,ad0x0_i2c_desc_s *pdspi,void *p_i2c_ext_t);
  385. uint32_t user_data0,user_data1;<----------------------- 8 байт
  386. void *user_pointer0,*user_pointer1;//<----------------------- 8 байт
  387. uint8_t reference_count;//ячейка не освобождается >0!!!!!!!!!!!!!*/
  388. if(count>16)ad0x0_err();//два раза по 8 байт
  389. p_ext_t->addr_w=SSD1306_I2C_ADDRESS;
  390. //p_ext_t->user_data0=ControByte | (uint32_t)(DataByte1<<8) | (uint32_t)(DataByte2<<16)| (uint32_t)(DataByte3<<24);
  391. p_ext_t->pbuf_count=count+1;
  392. //p_ext_t->pbuf_tx=(uint8_t*)&p_ext_t->user_data0;//TX!!!!!!!
  393. p_ext_t->OLED_i2c_databyte=DATA;
  394. memcpy((uint8_t*)&p_ext_t->user_data0,pbuf,count);
  395. p_ext_t->pbuf_tx=(uint8_t*)&p_ext_t->OLED_i2c_databyte;
  396. p_ext_t->p_pre_SetTXRXBuf=EXT_SetTXRXBuf_i2c;
  397. p_desc->ad0x0_i2c_push(ADQ_HAL_ADDR_TX_BUF_STOP_EXT,h_ext);
  398. }
  399. void OLED_Command_framebuf(oled_desc_s *p_desc){
  400. uint8_t h_ext;
  401. ad0x0_i2c_ext_t *p_ext_t=NULL;
  402. p_desc->ad0x0_i2c_get_ext_async2(&h_ext,(void**)&p_ext_t);
  403. p_ext_t->addr_w=SSD1306_I2C_ADDRESS;
  404. p_ext_t->pbuf_count=8193;
  405. p_ext_t->pbuf_tx=(uint8_t*)&framebuf.i2c_0x40;
  406. p_ext_t->p_pre_SetTXRXBuf=EXT_SetTXRXBuf_i2c;
  407. p_desc->ad0x0_i2c_push(ADQ_HAL_ADDR_TX_BUF_STOP_EXT,h_ext);
  408. }
  409. /* ---------------------------------------------------------
  410. * OLED_test
  411. * ---------------------------------------------------------
  412. */
  413. void OLED_test(oled_desc_s *p_desc)
  414. {
  415. //OLED_Clear(p_desc);
  416. OLED_PrintAt(p_desc, 0,0,(unsigned char*)"Шрифт размер = 0",0);
  417. osDelay(1500);
  418. OLED_PrintAt(p_desc, 0,10,(unsigned char*)"Шрифт s = 1",0);
  419. osDelay(1500);
  420. OLED_PrintAt(p_desc, 0,20,(unsigned char*)"Шрифт s = 2",0);
  421. osDelay(1500);
  422. OLED_Clear(p_desc);
  423. }
  424. void OLED_test2(oled_desc_s *p_desc)
  425. {
  426. uint16_t i;
  427. //unsigned short x = 0; unsigned short y = 0;
  428. uint32_t stFill,stText,msFill,msText;
  429. uint8_t buf16[16];
  430. unsigned char strf[32],strt[32],str64[64];
  431. stFill=xTaskGetTickCount();
  432. OLED_GotoXYWH(p_desc, 0,0,128,128);
  433. for (i=0; i<16; i++)buf16[i]=0x33;
  434. for (i=0; i<8192/16; i++)
  435. {
  436. p_desc->command_buf(buf16,16);
  437. }
  438. msFill=xTaskGetTickCount()-stFill;
  439. //for (i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) /* (SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8) */
  440. /*osDelay(1000);
  441. OLED_GotoXYWH(p_desc, 10,0,8,10);
  442. OLED_Char(p_desc, '#');
  443. OLED_GotoXYWH(p_desc, 20,0,8,10);
  444. OLED_Char(p_desc, '1');
  445. OLED_GotoXYWH(p_desc, 30,0,8,10);
  446. OLED_Char(p_desc, '2');
  447. OLED_GotoXYWH(p_desc, 40,0,8,10);
  448. OLED_Char(p_desc, '3');*/
  449. stText=xTaskGetTickCount();
  450. for(i=0;i<16;i++){
  451. if(i<12){
  452. OLED_GotoXYWH(p_desc, 0,i*10,8,10);
  453. OLED_Char(p_desc, '0'+(i<10?i:i-10));
  454. }
  455. if(i<12){
  456. OLED_GotoXYWH(p_desc, i*8,i*10,8,10);
  457. OLED_Char(p_desc, '0'+(i<10?i:i-10));
  458. }
  459. OLED_GotoXYWH(p_desc, i*8,0,8,10);
  460. OLED_Char(p_desc, '0'+(i<10?i:i-10));
  461. }
  462. msText=xTaskGetTickCount()-stText;
  463. sprintf((char*)strf,"fill %dms",msFill);
  464. p_desc->print_at(30,10,strf,0);
  465. sprintf((char*)strt,"text %dms",msText);
  466. p_desc->print_at(30,20,strt,0);
  467. sprintf((char*)str64,"text %dms,text %dms,text %dms,text %dms,text %dms,text %dms",msText,msText,msText,msText,msText,msText);
  468. //p_desc->print_at(40,30,str64,0);
  469. /*for (i=0; i<128; i++)
  470. {
  471. p_desc->command(DATA, i);
  472. // osDelay(300);
  473. }*/
  474. }
  475. unsigned char * i16_2s(int16_t value, unsigned char *buffer)
  476. {
  477. memset(buffer,' ',6);
  478. buffer += 7;
  479. char m=(value<0)?'-':' ';
  480. if(value<0)value=-value;
  481. // 11 байт достаточно для десятичного представления 32-х байтного числа
  482. // и завершающего нуля
  483. *--buffer = 0;
  484. do
  485. {
  486. *--buffer = value % 10 + '0';
  487. value /= 10;
  488. }
  489. while (value != 0);
  490. *--buffer=m;
  491. return buffer;
  492. }
  493. unsigned char * i32_2s(int32_t value, unsigned char *buffer)
  494. {
  495. memset(buffer,' ',8);
  496. buffer += 7;
  497. char m=(value<0)?'-':' ';
  498. if(value<0)value=-value;
  499. // 11 байт достаточно для десятичного представления 32-х байтного числа
  500. // и завершающего нуля
  501. *--buffer = 0;
  502. do
  503. {
  504. *--buffer = value % 10 + '0';
  505. value /= 10;
  506. }
  507. while (value != 0);
  508. *--buffer=m;
  509. return buffer;
  510. }
  511. #ifdef USE_BACKBUFFER
  512. void buf_render(oled_desc_s *p_desc){
  513. OLED_Command_framebuf(p_desc);
  514. /* uint16_t i;
  515. uint8_t buf16[16];
  516. OLED_GotoXYWH(p_desc, 0,0,128,BUF_HEIGHT);
  517. for (i=0; i<128*BUF_HEIGHT/2; i+=16)
  518. {
  519. memcpy(buf16,framebuf.framebuf+i,16);
  520. p_desc->command_buf(buf16,16);
  521. } */
  522. }
  523. float ad0x0_getdist(int16_t x,int16_t y){
  524. // return sqrt(x*x+y*y);
  525. return sqrxi32(x*x+y*y);
  526. }
  527. void buf_setpixelcol(uint8_t x,uint8_t y,uint8_t c){
  528. uint8_t *addr=(framebuf.framebuf + (uint16_t)(y<<6) + (x>>1));
  529. if( x & 0x1){
  530. *addr&=0x0F;
  531. *addr|=c<<4;
  532. }else{
  533. *addr&=0xF0;
  534. *addr|=c;
  535. }
  536. }
  537. uint16_t sqrxi32(uint32_t y)
  538. {
  539. if(y==1) return 1;
  540. uint32_t xh = y>0x10000ul?0x10000ul:y;
  541. uint32_t xl = 0;
  542. uint32_t xc ;
  543. for(int k=0;k<16;k++)
  544. {
  545. xc = (xh+xl)>>1ul;
  546. if(xc*xc-xc>=y)
  547. {
  548. xh = xc;
  549. }
  550. else
  551. {
  552. xl = xc;
  553. }
  554. }
  555. return (xh+xl)>>1ul;
  556. }
  557. /*uint16_t buf_getpixelcol(uint8_t x,uint8_t y){
  558. uint8_t c=*(framebuf + (uint16_t)(y<<6) + (x>>1));
  559. return ( x & 0x1 ? c>>4 : c & 0xF0);
  560. }*/
  561. void OLED_test3(oled_desc_s *p_desc)
  562. {
  563. int16_t x,y;
  564. uint8_t i;
  565. static struct demopoint_s points[3]={{40,60,2,2,6.0},{100,20,-2,-2,2.5},{60,100,1,-1,1.0}};
  566. float rescol;
  567. static float dist;
  568. uint8_t *addr=framebuf.framebuf,col;
  569. for (y=0; y<BUF_HEIGHT; y++)
  570. for (x=0; x<128; x+=2)//в одном байте 2 пикселя... нахер нам бить его на куски, будем херачить как 64х128
  571. {
  572. rescol=0x0;
  573. for(i=0;i<2;i++){//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  574. dist=ad0x0_getdist(points[i].x-x,points[i].y-y);
  575. rescol+=((128-dist)/dist * points[i].weight)*0.333;// /3
  576. }
  577. col=round(rescol);
  578. col=(col>15?15:col);
  579. if(!col)col=1;
  580. //buf_setpixelcol(x,y,(rescol>15?15:round(rescol)));
  581. //*(framebuf + (uint16_t)(y<<6) + (x>>1))=(col<<4)|col;
  582. *addr++=(col<<4)|col;
  583. }
  584. for(i=0;i<3;i++){
  585. buf_setpixelcol(points[i].x,points[i].y,0);
  586. points[i].x+=points[i].dir_x;
  587. points[i].y+=points[i].dir_y;
  588. if(points[i].x<1){points[i].dir_x=-points[i].dir_x;points[i].x=1;}
  589. if(points[i].x>127){points[i].dir_x=-points[i].dir_x;points[i].x=127;}
  590. if(points[i].y<1){points[i].dir_y=-points[i].dir_y;points[i].y=1;}
  591. if(points[i].y>BUF_HEIGHT-1){points[i].dir_y=-points[i].dir_y;points[i].y=BUF_HEIGHT-1;}
  592. rescol=0xF;
  593. buf_setpixelcol(points[i].x,points[i].y,(rescol>15?15:rescol));
  594. }
  595. // buf_render(p_desc);
  596. }
  597. #endif