luat_camera_air105.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "app_interface.h"
  22. #include "luat_base.h"
  23. #include "luat_lcd.h"
  24. #include "luat_camera.h"
  25. #include "luat_msgbus.h"
  26. #include "luat_spi.h"
  27. #include "luat_gpio.h"
  28. #include "app_interface.h"
  29. #include "zbar.h"
  30. #include "symbol.h"
  31. #include "image.h"
  32. #define LUAT_LOG_TAG "camera"
  33. #include "luat_log.h"
  34. typedef struct
  35. {
  36. Buffer_Struct FileBuffer;
  37. Buffer_Struct JPEGSavePath;
  38. Timer_t *CheckTimer;
  39. uint8_t *DataCache;
  40. uint32_t TotalSize;
  41. uint32_t CurSize;
  42. uint32_t VLen;
  43. uint32_t drawVLen;
  44. uint16_t Width;
  45. uint16_t Height;
  46. uint8_t DataBytes;
  47. uint8_t IsDecode;
  48. uint8_t BufferFull;
  49. uint8_t JPEGQuality;
  50. uint8_t CaptureMode;
  51. uint8_t CaptureWait;
  52. uint8_t JPEGEncodeDone;
  53. uint8_t PWMID;
  54. uint8_t I2CID;
  55. uint8_t NewDataFlag;
  56. }Camera_CtrlStruct;
  57. static Camera_CtrlStruct prvCamera;
  58. static struct luat_camera_conf camera_conf;
  59. static luat_lcd_conf_t* lcd_conf;
  60. static uint8_t draw_lcd = 0;
  61. static int32_t prvCamera_DataTimeout(void *pData, void *pParam)
  62. {
  63. if (!prvCamera.NewDataFlag)
  64. {
  65. rtos_msg_t msg = {0};
  66. {
  67. msg.handler = l_camera_handler;
  68. msg.ptr = NULL;
  69. msg.arg1 = 0;
  70. msg.arg2 = 0;
  71. luat_msgbus_put(&msg, 1);
  72. }
  73. }
  74. prvCamera.NewDataFlag = 0;
  75. }
  76. static void Camera_SaveJPEGData(void *Cxt, void *pData, int Size)
  77. {
  78. OS_BufferWrite(Cxt, pData, Size);
  79. }
  80. static int32_t Camera_SaveJPEGDone(void *pData, void *pParam)
  81. {
  82. HANDLE fd;
  83. if (prvCamera.JPEGSavePath.Data)
  84. {
  85. fd = luat_fs_fopen(prvCamera.JPEGSavePath.Data, "w");
  86. }
  87. else
  88. {
  89. fd = luat_fs_fopen("/capture.jpg", "w");
  90. }
  91. if (fd)
  92. {
  93. LLOGD("capture data %ubyte", luat_fs_fwrite(prvCamera.FileBuffer.Data, prvCamera.FileBuffer.Pos, 1, fd));
  94. luat_fs_fclose(fd);
  95. }
  96. OS_DeInitBuffer(&prvCamera.FileBuffer);
  97. prvCamera.CaptureWait = 1;
  98. prvCamera.JPEGEncodeDone = 1;
  99. prvCamera.CaptureMode = 0;
  100. Core_EncodeJPEGSetup(NULL, &prvCamera);
  101. rtos_msg_t msg = {0};
  102. {
  103. msg.handler = l_camera_handler;
  104. msg.ptr = NULL;
  105. msg.arg1 = 0;
  106. msg.arg2 = 1;
  107. luat_msgbus_put(&msg, 1);
  108. }
  109. }
  110. void DecodeQR_CBDataFun(uint8_t *Data, uint32_t Len){
  111. prvCamera.IsDecode = 0;
  112. rtos_msg_t msg = {0};
  113. if (Data){
  114. msg.handler = l_camera_handler;
  115. msg.ptr = Data;
  116. msg.arg1 = 0;
  117. msg.arg2 = Len;
  118. luat_msgbus_put(&msg, 1);
  119. }
  120. }
  121. static int32_t Camera_DrawLcd(void *DrawData, uint8_t Scan){
  122. LCD_DrawStruct *draw = OS_Malloc(sizeof(LCD_DrawStruct));
  123. if (!draw){
  124. DBG("lcd flush no memory");
  125. return -1;
  126. }
  127. uint8_t CPHA = ((luat_spi_device_t*)(lcd_conf->lcd_spi_device))->spi_config.CPHA;
  128. uint8_t CPOL = ((luat_spi_device_t*)(lcd_conf->lcd_spi_device))->spi_config.CPOL;
  129. draw->DCDelay = lcd_conf->dc_delay_us;
  130. draw->Mode = SPI_MODE_0;
  131. if(CPHA&&CPOL)draw->Mode = SPI_MODE_3;
  132. else if(CPOL)draw->Mode = SPI_MODE_2;
  133. else if(CPHA)draw->Mode = SPI_MODE_1;
  134. draw->Speed = ((luat_spi_device_t*)(lcd_conf->lcd_spi_device))->spi_config.bandrate;
  135. if (((luat_spi_device_t*)(lcd_conf->lcd_spi_device))->bus_id == 5) draw->SpiID = HSPI_ID0;
  136. else draw->SpiID = ((luat_spi_device_t*)(lcd_conf->lcd_spi_device))->bus_id;
  137. draw->CSPin = ((luat_spi_device_t*)(lcd_conf->lcd_spi_device))->spi_config.cs;
  138. draw->DCPin = lcd_conf->pin_dc;
  139. draw->xoffset = lcd_conf->xoffset;
  140. draw->yoffset = lcd_conf->yoffset;
  141. if (Scan == 0){
  142. draw->x1 = 0;
  143. draw->x2 = lcd_conf->w -1;
  144. draw->y1 = prvCamera.VLen;
  145. draw->y2 = prvCamera.VLen + prvCamera.drawVLen -1;
  146. draw->Size = (draw->x2 - draw->x1 + 1) * (draw->y2 - draw->y1 + 1) * 2;
  147. draw->ColorMode = COLOR_MODE_RGB_565;
  148. draw->Data = OS_Malloc(draw->Size);
  149. if (!draw->Data){
  150. DBG("lcd flush data no memory");
  151. OS_Free(draw);
  152. return -1;
  153. }
  154. memcpy(draw->Data, DrawData, draw->Size);
  155. Core_LCDDraw(draw);
  156. }else if(Scan == 1){
  157. draw->x1 = 0;
  158. draw->x2 = prvCamera.Width - 1;
  159. draw->y1 = 0;
  160. draw->y2 = prvCamera.Height - 1;
  161. draw->Size = prvCamera.TotalSize;
  162. draw->ColorMode = COLOR_MODE_GRAY;
  163. draw->Data = DrawData;
  164. Core_CameraDraw(draw);
  165. }else{
  166. DBG("Scan error");
  167. OS_Free(draw);
  168. return -1;
  169. }
  170. }
  171. static int32_t prvCamera_DCMICB(void *pData, void *pParam){
  172. uint8_t zbar_scan = (uint8_t)pParam;
  173. Buffer_Struct *RxBuf = (Buffer_Struct *)pData;
  174. prvCamera.NewDataFlag = 1;
  175. if (prvCamera.CaptureMode){
  176. if (!pData){
  177. if (prvCamera.CaptureWait && prvCamera.JPEGEncodeDone)
  178. {
  179. prvCamera.CaptureWait = 0;
  180. prvCamera.JPEGEncodeDone = 0;
  181. Core_EncodeJPEGStart(prvCamera.Width, prvCamera.Height, prvCamera.JPEGQuality);
  182. }
  183. else if (!prvCamera.CaptureWait)
  184. {
  185. prvCamera.CaptureWait = 1;
  186. prvCamera.JPEGEncodeDone = 0;
  187. Core_EncodeJPEGEnd(Camera_SaveJPEGDone, 0);
  188. }
  189. }
  190. else
  191. {
  192. if (!prvCamera.CaptureWait)
  193. {
  194. uint8_t *data = malloc(RxBuf->MaxLen * 4);
  195. memcpy(data, RxBuf->Data, RxBuf->MaxLen * 4);
  196. Core_EncodeJPEGRun(data, RxBuf->MaxLen * 4, zbar_scan?COLOR_MODE_GRAY:COLOR_MODE_RGB_565);
  197. }
  198. }
  199. return 0;
  200. }
  201. if (zbar_scan == 0){
  202. if (!pData){
  203. prvCamera.VLen = 0;
  204. return 0;
  205. }
  206. if (draw_lcd)
  207. {
  208. Camera_DrawLcd(RxBuf->Data, zbar_scan);
  209. }
  210. prvCamera.VLen += prvCamera.drawVLen;
  211. return 0;
  212. }else if (zbar_scan == 1){
  213. Buffer_Struct *RxBuf = (Buffer_Struct *)pData;
  214. if (!pData)
  215. {
  216. if (!prvCamera.DataCache)
  217. {
  218. prvCamera.DataCache = malloc(prvCamera.TotalSize);
  219. prvCamera.BufferFull = 0;
  220. }
  221. else
  222. {
  223. if (prvCamera.TotalSize != prvCamera.CurSize)
  224. {
  225. // DBG_ERR("%d, %d", prvCamera.CurSize, prvCamera.TotalSize);
  226. }
  227. else if (!prvCamera.IsDecode)
  228. {
  229. prvCamera.IsDecode = 1;
  230. if (draw_lcd)
  231. Camera_DrawLcd(prvCamera.DataCache, zbar_scan);
  232. Core_DecodeQR(prvCamera.DataCache, prvCamera.Width, prvCamera.Height, DecodeQR_CBDataFun);
  233. prvCamera.DataCache = malloc(prvCamera.TotalSize);
  234. prvCamera.BufferFull = 0;
  235. }
  236. else
  237. {
  238. prvCamera.BufferFull = 1;
  239. }
  240. }
  241. prvCamera.CurSize = 0;
  242. return 0;
  243. }
  244. if (prvCamera.DataCache)
  245. {
  246. if (prvCamera.BufferFull)
  247. {
  248. if (!prvCamera.IsDecode){
  249. prvCamera.IsDecode = 1;
  250. if (draw_lcd)
  251. Camera_DrawLcd(prvCamera.DataCache, zbar_scan);
  252. Core_DecodeQR(prvCamera.DataCache, prvCamera.Width, prvCamera.Height, DecodeQR_CBDataFun);
  253. prvCamera.DataCache = malloc(prvCamera.TotalSize);
  254. prvCamera.BufferFull = 0;
  255. prvCamera.CurSize = 0;
  256. }else{
  257. return 0;
  258. }
  259. }
  260. }
  261. else
  262. {
  263. prvCamera.DataCache = malloc(prvCamera.TotalSize);
  264. prvCamera.BufferFull = 0;
  265. prvCamera.CurSize = 0;
  266. prvCamera.IsDecode = 0;
  267. }
  268. memcpy(&prvCamera.DataCache[prvCamera.CurSize], RxBuf->Data, RxBuf->MaxLen * 4);
  269. prvCamera.CurSize += RxBuf->MaxLen * 4;
  270. if (prvCamera.TotalSize < prvCamera.CurSize){
  271. DBG_ERR("%d,%d", prvCamera.TotalSize, prvCamera.CurSize);
  272. prvCamera.CurSize = 0;
  273. }
  274. return 0;
  275. }
  276. }
  277. int luat_camera_init(luat_camera_conf_t *conf){
  278. int error = ERROR_NONE;
  279. GPIO_Iomux(GPIOD_01, 3);
  280. GPIO_Iomux(GPIOD_02, 3);
  281. GPIO_Iomux(GPIOD_03, 3);
  282. GPIO_Iomux(GPIOD_08, 3);
  283. GPIO_Iomux(GPIOD_09, 3);
  284. GPIO_Iomux(GPIOD_10, 3);
  285. GPIO_Iomux(GPIOD_11, 3);
  286. GPIO_Iomux(GPIOE_00, 3);
  287. GPIO_Iomux(GPIOE_01, 3);
  288. GPIO_Iomux(GPIOE_02, 3);
  289. GPIO_Iomux(GPIOE_03, 3);
  290. if (!prvCamera.CheckTimer)
  291. {
  292. prvCamera.CheckTimer = Timer_Create(prvCamera_DataTimeout, NULL, NULL);
  293. }
  294. else
  295. {
  296. Timer_Stop(prvCamera.CheckTimer);
  297. }
  298. memcpy(&camera_conf, conf, sizeof(luat_camera_conf_t));
  299. lcd_conf = conf->lcd_conf;
  300. draw_lcd = conf->draw_lcd;
  301. prvCamera.Width = lcd_conf->w;
  302. prvCamera.Height = lcd_conf->h;
  303. if (conf->zbar_scan == 1){
  304. prvCamera.DataCache = NULL;
  305. prvCamera.TotalSize = prvCamera.Width * prvCamera.Height;
  306. prvCamera.DataBytes = 1;
  307. }
  308. GPIO_Iomux(GPIOA_05, 2);
  309. HWTimer_SetPWM(conf->pwm_id, conf->pwm_period, conf->pwm_pulse, 0);
  310. prvCamera.PWMID = conf->pwm_id;
  311. luat_i2c_setup(conf->i2c_id,1,NULL);
  312. for(size_t i = 0; i < conf->init_cmd_size; i++){
  313. if (luat_i2c_send(conf->i2c_id, conf->i2c_addr, &(conf->init_cmd[i]), 2))
  314. {
  315. error = -ERROR_OPERATION_FAILED;
  316. }
  317. i++;
  318. }
  319. prvCamera.I2CID = conf->i2c_id;
  320. DCMI_Setup(0, 0, 0, 8, 0);
  321. DCMI_SetCallback(prvCamera_DCMICB, conf->zbar_scan);
  322. // if (conf->zbar_scan == 0){
  323. // DCMI_SetCROPConfig(1, (conf->sensor_height-lcd_conf->h)/2, ((conf->sensor_width-lcd_conf->w)/2)*2, lcd_conf->h - 1, 2*lcd_conf->w - 1);
  324. // DCMI_CaptureSwitch(1, 0,lcd_conf->w, lcd_conf->h, 2, &prvCamera.drawVLen);
  325. // prvCamera.VLen = 0;
  326. // }else if(conf->zbar_scan == 1){
  327. // DCMI_SetCROPConfig(1, (conf->sensor_height-prvCamera.Height)/2, ((conf->sensor_width-prvCamera.Width)/2)*prvCamera.DataBytes, prvCamera.Height - 1, prvCamera.DataBytes*prvCamera.Width - 1);
  328. // DCMI_CaptureSwitch(1, 0,lcd_conf->w, lcd_conf->h, prvCamera.DataBytes, &prvCamera.drawVLen);
  329. // }
  330. return error;
  331. }
  332. int luat_camera_start(int id)
  333. {
  334. if (prvCamera.DataCache)
  335. {
  336. DCMI_CaptureSwitch(0, 0, 0, 0, 0, NULL);
  337. free(prvCamera.DataCache);
  338. }
  339. if (camera_conf.zbar_scan == 0){
  340. DCMI_SetCROPConfig(1, (camera_conf.sensor_height-lcd_conf->h)/2, ((camera_conf.sensor_width-lcd_conf->w)/2)*2, lcd_conf->h - 1, 2*lcd_conf->w - 1);
  341. DCMI_CaptureSwitch(1, 0, lcd_conf->w, lcd_conf->h, 2, &prvCamera.drawVLen);
  342. prvCamera.CaptureMode = 0;
  343. prvCamera.VLen = 0;
  344. }else if(camera_conf.zbar_scan == 1){
  345. DCMI_SetCROPConfig(1, (camera_conf.sensor_height-prvCamera.Height)/2, ((camera_conf.sensor_width-prvCamera.Width)/2)*prvCamera.DataBytes, prvCamera.Height - 1, prvCamera.DataBytes*prvCamera.Width - 1);
  346. DCMI_CaptureSwitch(1, 0,lcd_conf->w, lcd_conf->h, prvCamera.DataBytes, &prvCamera.drawVLen);
  347. }
  348. prvCamera.NewDataFlag = 0;
  349. Timer_StartMS(prvCamera.CheckTimer, 1000, 1);
  350. return 0;
  351. }
  352. int luat_camera_capture(int id, uint8_t quality, const char *path)
  353. {
  354. DCMI_CaptureSwitch(0, 0, 0, 0, 0, NULL);
  355. if (prvCamera.DataCache)
  356. {
  357. free(prvCamera.DataCache);
  358. prvCamera.DataCache = NULL;
  359. }
  360. if (path)
  361. {
  362. OS_ReInitBuffer(&prvCamera.JPEGSavePath, strlen(path) + 1);
  363. memcpy(prvCamera.JPEGSavePath.Data, path, strlen(path));
  364. }
  365. OS_ReInitBuffer(&prvCamera.FileBuffer, 16 * 1024);
  366. Core_EncodeJPEGSetup(Camera_SaveJPEGData, &prvCamera);
  367. luat_camera_start(id);
  368. prvCamera.JPEGQuality = quality;
  369. prvCamera.CaptureMode = 1;
  370. prvCamera.CaptureWait = 1;
  371. prvCamera.JPEGEncodeDone = 1;
  372. Timer_StartMS(prvCamera.CheckTimer, 1000, 1);
  373. return 0;
  374. }
  375. int luat_camera_stop(int id)
  376. {
  377. Timer_Stop(prvCamera.CheckTimer);
  378. DCMI_CaptureSwitch(0, 0, 0, 0, 0, NULL);
  379. if (prvCamera.DataCache)
  380. {
  381. free(prvCamera.DataCache);
  382. prvCamera.DataCache = NULL;
  383. }
  384. OS_DeInitBuffer(&prvCamera.FileBuffer);
  385. return 0;
  386. }
  387. int luat_camera_close(int id)
  388. {
  389. luat_camera_stop(id);
  390. GPIO_Iomux(GPIOD_01, 1);
  391. GPIO_Iomux(GPIOD_02, 1);
  392. GPIO_Iomux(GPIOD_03, 1);
  393. GPIO_Iomux(GPIOD_08, 1);
  394. GPIO_Iomux(GPIOD_09, 1);
  395. GPIO_Iomux(GPIOD_10, 1);
  396. GPIO_Iomux(GPIOD_11, 1);
  397. GPIO_Iomux(GPIOE_00, 1);
  398. GPIO_Iomux(GPIOE_01, 1);
  399. GPIO_Iomux(GPIOE_02, 1);
  400. GPIO_Iomux(GPIOA_05, 1);
  401. HWTimer_Stop(prvCamera.PWMID);
  402. luat_i2c_close(prvCamera.I2CID);
  403. }