OLED_0in96_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*****************************************************************************
  2. * | File : OLED_0in96_test.c
  3. * | Author : Waveshare team
  4. * | Function : OLED_0in96 OLED Module test demo
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-08-14
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "test.h"
  32. #include "OLED_0in96.h"
  33. #include<time.h>
  34. int OLED_0in96_test(void)
  35. {
  36. int i=0;
  37. printf("0.96inch OLED test demo\n");
  38. if(DEV_ModuleInit() != 0) {
  39. return -1;
  40. }
  41. printf("OLED Init...\r\n");
  42. OLED_0in96_Init();
  43. DEV_Delay_ms(500);
  44. // 0.Create a new image cache
  45. UBYTE *BlackImage;
  46. UWORD Imagesize = ((OLED_0in96_WIDTH%8==0)? (OLED_0in96_WIDTH/8): (OLED_0in96_WIDTH/8+1)) * OLED_0in96_HEIGHT;
  47. if((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL) {
  48. printf("Failed to apply for black memory...\r\n");
  49. return -1;
  50. }
  51. printf("Paint_NewImage\r\n");
  52. Paint_NewImage(BlackImage, OLED_0in96_WIDTH, OLED_0in96_HEIGHT, 90, BLACK);
  53. printf("Drawing\r\n");
  54. //1.Select Image
  55. Paint_SelectImage(BlackImage);
  56. DEV_Delay_ms(500);
  57. Paint_Clear(BLACK);
  58. // 2.Drawing on the image
  59. time_t t_now,t_b=time(0);
  60. struct tm *l_time;
  61. char str1[20],str2[10];
  62. while(1)
  63. {
  64. t_now=time(0);
  65. l_time=localtime(&t_now);
  66. sprintf(str1,"%04d,%02d,%02d",l_time->tm_year+1900,l_time->tm_mon+1,l_time->tm_mday);
  67. sprintf(str2,"%02d:%02d:%02d",l_time->tm_hour,l_time->tm_min,l_time->tm_sec);
  68. Paint_DrawString_EN(0, 0, str1, &Font16, WHITE, WHITE);
  69. Paint_DrawString_EN(0, 32, str2, &Font20, WHITE, WHITE);
  70. OLED_0in96_display(BlackImage);
  71. DEV_Delay_ms(1);
  72. Paint_Clear(BLACK);
  73. if(t_now-t_b>5)
  74. {
  75. break;
  76. }
  77. }
  78. // Drawing on the image
  79. printf("Drawing:page 2\r\n");
  80. Paint_DrawString_EN(10, 0, "Display", &Font16, WHITE, WHITE);
  81. Paint_DrawString_EN(10, 16, "hello world", &Font8, WHITE, WHITE);
  82. Paint_DrawNum(10, 24, 123.456789, &Font12, 4, WHITE, WHITE);
  83. Paint_DrawNum(10, 36, 987654, &Font16, 3, WHITE, WHITE);
  84. // Show image on page2
  85. //OLED_0in96_display(BlackImage);
  86. DEV_Delay_ms(3000);
  87. Paint_Clear(BLACK);
  88. // Drawing on the image
  89. printf("Drawing:page 3\r\n");
  90. Paint_DrawString_EN(0, 0, "waveshare", &Font16, WHITE, WHITE);
  91. Paint_DrawString_CN(0, 20, "΢ѩµç×Ó", &Font24CN, WHITE, WHITE);
  92. // Show image on page3
  93. //OLED_0in96_display(BlackImage);
  94. DEV_Delay_ms(1500);
  95. Paint_Clear(BLACK);
  96. // Drawing on the image
  97. printf("Drawing:page 4\r\n");
  98. GUI_ReadBmp("./pic/waveshare.bmp", 0, 0);
  99. // Show image on page4
  100. //OLED_0in96_display(BlackImage);
  101. DEV_Delay_ms(1500);
  102. Paint_Clear(BLACK);
  103. int x=1,y=1,_x=1,_y=1,t=time(NULL),_t=0,fps=0,_fps=0;
  104. srand((unsigned int)time(NULL));
  105. x=rand()%(128-9*7);
  106. y=(rand()%(48-12))+16;
  107. _x=2*rand()%2-1;
  108. _y=2*rand()%2-1;
  109. i=0;
  110. while(1)
  111. {
  112. Paint_DrawString_EN(x, y,"Waveshare", &Font12, WHITE, WHITE);
  113. Paint_DrawString_EN(0, 0,"FPS:", &Font12, WHITE, WHITE);
  114. Paint_DrawNum(48, 0, fps, &Font12, 0, WHITE, WHITE);
  115. //OLED_0in96_display(BlackImage);
  116. Paint_Clear(BLACK);
  117. if((_fps%5)==0)
  118. {
  119. x+=_x;
  120. y+=_y;
  121. }
  122. if(x>(128-9*7-1))
  123. {
  124. _x=-1;
  125. }
  126. if(x<1)
  127. {
  128. _x=1;
  129. }
  130. if(y<16)
  131. {
  132. _y=1;
  133. }
  134. if(y>(64-12-1))
  135. {
  136. _y=-1;
  137. }
  138. _t=time(NULL);
  139. if(t==_t)
  140. {
  141. _fps++;
  142. }
  143. else
  144. {
  145. fps=_fps;
  146. _fps=0;
  147. t=_t;
  148. i++;
  149. }
  150. if(i>10)
  151. {
  152. break;
  153. }
  154. }
  155. OLED_0in96_clear();
  156. return 0;
  157. }