camera_demo.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. /*===========================================================================
  13. * include files
  14. ===========================================================================*/
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "ql_api_osi.h"
  19. #include "ql_log.h"
  20. #include "ql_api_camera.h"
  21. #include "camera_demo.h"
  22. #include "ql_i2c.h"
  23. #include "ql_lcd.h"
  24. /*===========================================================================
  25. * Macro Definition
  26. ===========================================================================*/
  27. #define QL_CAMERADEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  28. #define QL_CAMERADEMO_LOG(msg, ...) QL_LOG(QL_CAMERADEMO_LOG_LEVEL, "ql_CAMERADEMO", msg, ##__VA_ARGS__)
  29. #define QL_CAMERADEMO_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_CAMERADEMO", msg, ##__VA_ARGS__)
  30. #define QL_CAMERA_TASK_STACK_SIZE 10240
  31. #define QL_CAMERA_TASK_PRIO APP_PRIORITY_NORMAL
  32. #define QL_CAMERA_TASK_EVENT_CNT 5
  33. #define SalveAddr_w (0x42 >> 1)
  34. #define SalveAddr_r (0x43 >> 1)
  35. #define TEST_CAPTURE_COUNTS (10)
  36. #define TEST_PREVIEW_COUNTS (500)
  37. /*===========================================================================
  38. * Struct
  39. ===========================================================================*/
  40. typedef enum
  41. {
  42. preview_TEST = 0, //test preview
  43. capture_TEST = 1, //test capture
  44. }QL_CAM_TEST_E;
  45. /*===========================================================================
  46. * Variate
  47. ===========================================================================*/
  48. /*===========================================================================
  49. * Functions
  50. ===========================================================================*/
  51. void ql_CamUYVY640to320(unsigned char* indata,unsigned char* outBuf)
  52. {
  53. for(int y = 0; y < 240; y++)
  54. {
  55. for(int x = 0; x < 320*2; x++)
  56. {
  57. outBuf[320*2*y+x] = indata[640*2*2*y+x+(x/4)*4];
  58. }
  59. }
  60. }
  61. //if you need to rotate lcd,you could use this api
  62. int ql_camPrintExt(uint16_t *imageBuf)
  63. {
  64. uint16_t *dataBUf = NULL;
  65. int offset = 0;
  66. //unsigned char* outBuf = NULL;
  67. //outBuf = (unsigned char*)malloc(320*240*2);//Space for a display screen
  68. ql_lcd_config_t lcd_config = {0};
  69. ql_lcd_get_config(&lcd_config);
  70. lcd_config.lcdrotation = 1;
  71. lcd_config.widthoriginal = 320;
  72. lcd_config.lcdcolorformat = QL_LCD_YUV422_UYVY;
  73. lcd_config.clean_screen = false;
  74. lcd_config.bus_mode = QL_LCD_SPI_LINE_4;
  75. lcd_config.freq = 0; // use default freq
  76. if(imageBuf == NULL)
  77. {
  78. if(ql_CamPreviewDQBUF(&dataBUf) != QL_CAMERA_SUCCESS)
  79. {
  80. QL_CAMERADEMO_LOG("Cam not preview");
  81. return -1;
  82. }
  83. //Preview without rotation, use this section
  84. lcd_config.lcdrotation = 1;
  85. ql_lcd_set_config(&lcd_config);
  86. //ql_CamUYVY640to320((unsigned char*)dataBUf,outBuf);
  87. offset = 320 * (240 - 1) * 2;//width of camera:320,height of camera:240,byte of a pixel:2
  88. ql_lcd_write((uint16_t *)((uint32_t)dataBUf + offset), 0, 0, 239, 319);
  89. ql_CamPreviewQBUF((uint16_t *)dataBUf);
  90. //Preview the rotation using this section
  91. //lcd_config.lcdrotation = 0;
  92. //ql_lcd_set_config(&lcd_config);
  93. //ql_lcd_write(dataBUf, 0, 0, 319, 239);
  94. //ql_CamPreviewQBUF((uint16_t *)dataBUf);
  95. }
  96. else
  97. {
  98. //ATTENTION:If you do not rotate the LCD, please set the LCD to landscape, i.e. 320 x 240
  99. lcd_config.lcdrotation = 0;
  100. ql_lcd_set_config(&lcd_config);
  101. //ql_CamUYVY640to320((unsigned char*)imageBuf,outBuf);
  102. ql_lcd_set_display_offset(0, 0, -80, 80);
  103. ql_spi_lcd_write_cmd(0x36);
  104. ql_spi_lcd_write_data(0x60);
  105. ql_lcd_write((uint16_t *)imageBuf, 0, 0, 319, 239);
  106. }
  107. //free(outBuf);
  108. return 0;
  109. }
  110. void ql_CamTest(QL_CAM_TEST_E TestMode)
  111. {
  112. uint16_t *pCamDataBuffer = NULL;
  113. uint16_t i;
  114. if(TestMode == preview_TEST)
  115. {
  116. for(i=0;i<TEST_PREVIEW_COUNTS;i++)
  117. {
  118. ql_camPrint(pCamDataBuffer);
  119. //ql_camPrintExt(pCamDataBuffer);
  120. //下面延时设置请根据cam的接口(spi\mipi)以及buf配置适当调节
  121. ql_rtos_task_sleep_ms(60);
  122. }
  123. }
  124. else
  125. {
  126. for(i=0;i<TEST_CAPTURE_COUNTS;i++)
  127. {
  128. ql_CamCaptureImage(&pCamDataBuffer);
  129. ql_camPrint(pCamDataBuffer);
  130. //ql_camPrintExt(pCamDataBuffer);
  131. ql_rtos_task_sleep_ms(2000);
  132. }
  133. }
  134. }
  135. void ql_camera_demo_thread(void *param)
  136. {
  137. int lcdstatus;
  138. //uint8_t read_data = 0;
  139. //uint8_t data = 0xaa;
  140. ql_CamInit(320, 240);
  141. //ql_CamInit(640, 480);
  142. //ql_CamInit0310SPI(320, 240);
  143. //ql_CamInit0310SPI(640, 480);
  144. ql_CamPowerOn();
  145. lcdstatus = ql_lcd_init();
  146. if(lcdstatus == QL_SUCCESS)
  147. ql_lcd_display_on();
  148. //The api is used to read the register of sensor,after the camera is successfully powered on.
  149. //ql_CamI2CRead(0xf0, &read_data, 1);
  150. //The api is used to write the register of sensor,after the camera is successfully powered on.
  151. //ql_CamI2CWrite(0x55, data);
  152. /*Camera test*/
  153. ql_CamPreview();
  154. ql_CamTest(preview_TEST);
  155. ql_CamTest(capture_TEST);
  156. ql_CamStopPreview();
  157. ql_CamPowerOff();
  158. ql_rtos_task_sleep_ms(2000);
  159. ql_CamPowerOn();
  160. ql_CamPreview();
  161. ql_CamTest(preview_TEST);
  162. ql_CamTest(capture_TEST);
  163. ql_CamStopPreview();
  164. ql_CamClose();
  165. if(lcdstatus == QL_SUCCESS)
  166. ql_lcd_display_off();
  167. ql_rtos_task_delete(NULL);
  168. }
  169. void ql_camera_app_init(void)
  170. {
  171. QlCAMERAStatus err = QL_OSI_SUCCESS;
  172. ql_task_t camera_task = NULL;
  173. err = ql_rtos_task_create(&camera_task, QL_CAMERA_TASK_STACK_SIZE, QL_CAMERA_TASK_PRIO, "camera DEMO", ql_camera_demo_thread, NULL, QL_CAMERA_TASK_EVENT_CNT);
  174. if (err != QL_OSI_SUCCESS)
  175. {
  176. QL_CAMERADEMO_LOG("CAMERA demo task created failed");
  177. }
  178. }