luat_lib_camera.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. @module camera
  3. @summary 摄像头
  4. @version 1.0
  5. @date 2022.01.11
  6. @demo camera
  7. */
  8. #include "luat_base.h"
  9. #include "luat_camera.h"
  10. #include "luat_msgbus.h"
  11. #include "luat_fs.h"
  12. #include "luat_malloc.h"
  13. #include "luat_uart.h"
  14. #include "luat_zbuff.h"
  15. #define LUAT_LOG_TAG "camera"
  16. #include "luat_log.h"
  17. #define MAX_DEVICE_COUNT 2
  18. typedef struct luat_camera_cb {
  19. int scanned;
  20. } luat_camera_cb_t;
  21. static luat_camera_cb_t camera_cbs[MAX_DEVICE_COUNT];
  22. int l_camera_handler(lua_State *L, void* ptr) {
  23. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  24. lua_pop(L, 1);
  25. int camera_id = msg->arg1;
  26. if (camera_cbs[camera_id].scanned) {
  27. lua_geti(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  28. if (lua_isfunction(L, -1)) {
  29. lua_pushinteger(L, camera_id);
  30. if (msg->ptr)
  31. {
  32. lua_pushlstring(L, (char *)msg->ptr,msg->arg2);
  33. }
  34. else if (msg->arg2 > 1)
  35. {
  36. lua_pushinteger(L, msg->arg2);
  37. }
  38. else
  39. {
  40. lua_pushboolean(L, msg->arg2);
  41. }
  42. lua_call(L, 2, 0);
  43. }
  44. }
  45. lua_pushinteger(L, 0);
  46. return 1;
  47. }
  48. /*
  49. 初始化摄像头
  50. @api camera.init(InitReg)
  51. @table InitReg camera初始化命令 见demo/camera/AIR105 注意:如扫码 camera初始化时需设置为灰度输出
  52. @return int camera_id
  53. @usage
  54. camera_id = camera.init(GC032A_InitReg)--屏幕输出rgb图像
  55. --初始化后需要start才开始输出/扫码
  56. camera.start(camera_id)--开始指定的camera
  57. */
  58. static int l_camera_init(lua_State *L){
  59. luat_camera_conf_t conf = {0};
  60. conf.lcd_conf = luat_lcd_get_default();
  61. if (lua_istable(L, 1)) {
  62. lua_pushliteral(L, "zbar_scan");
  63. lua_gettable(L, 1);
  64. if (lua_isinteger(L, -1)) {
  65. conf.zbar_scan = luaL_checkinteger(L, -1);
  66. }
  67. lua_pop(L, 1);
  68. lua_pushliteral(L, "draw_lcd");
  69. lua_gettable(L, 1);
  70. if (lua_isinteger(L, -1)) {
  71. conf.draw_lcd = luaL_checkinteger(L, -1);
  72. }
  73. lua_pop(L, 1);
  74. lua_pushliteral(L, "i2c_id");
  75. lua_gettable(L, 1);
  76. if (lua_isinteger(L, -1)) {
  77. conf.i2c_id = luaL_checkinteger(L, -1);
  78. }
  79. lua_pop(L, 1);
  80. lua_pushliteral(L, "i2c_addr");
  81. lua_gettable(L, 1);
  82. if (lua_isinteger(L, -1)) {
  83. conf.i2c_addr = luaL_checkinteger(L, -1);
  84. }
  85. lua_pop(L, 1);
  86. lua_pushliteral(L, "pwm_id");
  87. lua_gettable(L, 1);
  88. if (lua_isinteger(L, -1)) {
  89. conf.pwm_id = luaL_checkinteger(L, -1);
  90. }
  91. lua_pop(L, 1);
  92. lua_pushliteral(L, "pwm_period");
  93. lua_gettable(L, 1);
  94. if (lua_isinteger(L, -1)) {
  95. conf.pwm_period = luaL_checkinteger(L, -1);
  96. }
  97. lua_pop(L, 1);
  98. lua_pushliteral(L, "pwm_pulse");
  99. lua_gettable(L, 1);
  100. if (lua_isinteger(L, -1)) {
  101. conf.pwm_pulse = luaL_checkinteger(L, -1);
  102. }
  103. lua_pop(L, 1);
  104. lua_pushliteral(L, "sensor_width");
  105. lua_gettable(L, 1);
  106. if (lua_isinteger(L, -1)) {
  107. conf.sensor_width = luaL_checkinteger(L, -1);
  108. }
  109. lua_pop(L, 1);
  110. lua_pushliteral(L, "sensor_height");
  111. lua_gettable(L, 1);
  112. if (lua_isinteger(L, -1)) {
  113. conf.sensor_height = luaL_checkinteger(L, -1);
  114. }
  115. lua_pop(L, 1);
  116. lua_pushliteral(L, "color_bit");
  117. lua_gettable(L, 1);
  118. if (lua_isinteger(L, -1)) {
  119. conf.color_bit = luaL_checkinteger(L, -1);
  120. }
  121. lua_pop(L, 1);
  122. lua_pushliteral(L, "id_reg");
  123. lua_gettable(L, 1);
  124. if (lua_isinteger(L, -1)) {
  125. conf.id_reg = luaL_checkinteger(L, -1);
  126. }
  127. lua_pop(L, 1);
  128. lua_pushliteral(L, "id_value");
  129. lua_gettable(L, 1);
  130. if (lua_isinteger(L, -1)) {
  131. conf.id_value = luaL_checkinteger(L, -1);
  132. }
  133. lua_pop(L, 1);
  134. lua_pushliteral(L, "init_cmd");
  135. lua_gettable(L, 1);
  136. if (lua_istable(L, -1)) {
  137. conf.init_cmd_size = lua_rawlen(L, -1);
  138. conf.init_cmd = luat_heap_malloc(conf.init_cmd_size * sizeof(uint8_t));
  139. for (size_t i = 1; i <= conf.init_cmd_size; i++){
  140. lua_geti(L, -1, i);
  141. conf.init_cmd[i-1] = luaL_checkinteger(L, -1);
  142. lua_pop(L, 1);
  143. }
  144. }else if(lua_isstring(L, -1)){
  145. int len,cmd;
  146. const char *fail_name = luaL_checklstring(L, -1, &len);
  147. FILE* fd = luat_fs_fopen(fail_name, "rb");
  148. conf.init_cmd_size = 0;
  149. if (fd){
  150. #define INITCMD_BUFF_SIZE 128
  151. char init_cmd_buff[INITCMD_BUFF_SIZE] ;
  152. conf.init_cmd = luat_heap_malloc(sizeof(uint8_t));
  153. while (1) {
  154. memset(init_cmd_buff, 0, INITCMD_BUFF_SIZE);
  155. int readline_len = luat_fs_readline(init_cmd_buff, INITCMD_BUFF_SIZE-1, fd);
  156. if (readline_len < 1)
  157. break;
  158. if (memcmp(init_cmd_buff, "#", 1)==0){
  159. continue;
  160. }
  161. char *token = strtok(init_cmd_buff, ",");
  162. if (sscanf(token,"%x",&cmd) < 1){
  163. continue;
  164. }
  165. conf.init_cmd_size = conf.init_cmd_size + 1;
  166. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  167. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  168. while( token != NULL ) {
  169. token = strtok(NULL, ",");
  170. if (sscanf(token,"%x",&cmd) < 1){
  171. break;
  172. }
  173. conf.init_cmd_size = conf.init_cmd_size + 1;
  174. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  175. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  176. }
  177. }
  178. conf.init_cmd[conf.init_cmd_size]= 0;
  179. luat_fs_fclose(fd);
  180. }else{
  181. LLOGE("init_cmd fail open error");
  182. }
  183. }
  184. lua_pop(L, 1);
  185. }
  186. lua_pushinteger(L, luat_camera_init(&conf));
  187. return 1;
  188. }
  189. /*
  190. 注册摄像头事件回调
  191. @api camera.on(id, event, func)
  192. @int camera id, camera 0写0, camera 1写1
  193. @string 事件名称
  194. @function 回调方法
  195. @return nil 无返回值
  196. @usage
  197. camera.on(0, "scanned", function(id, str)
  198. print(id, str)
  199. end)
  200. */
  201. static int l_camera_on(lua_State *L) {
  202. int camera_id = luaL_checkinteger(L, 1);
  203. const char* event = luaL_checkstring(L, 2);
  204. if (!strcmp("scanned", event)) {
  205. if (camera_cbs[camera_id].scanned != 0) {
  206. luaL_unref(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  207. camera_cbs[camera_id].scanned = 0;
  208. }
  209. if (lua_isfunction(L, 3)) {
  210. lua_pushvalue(L, 3);
  211. camera_cbs[camera_id].scanned = luaL_ref(L, LUA_REGISTRYINDEX);
  212. }
  213. }
  214. return 0;
  215. }
  216. /**
  217. 开始指定的camera
  218. @api camera.start(id)
  219. @int camera id,例如0
  220. @return boolean 成功返回true,否则返回false
  221. @usage
  222. camera.start(0)
  223. */
  224. static int l_camera_start(lua_State *L) {
  225. int id = luaL_checkinteger(L, 1);
  226. lua_pushboolean(L, luat_camera_start(id) == 0 ? 1 : 0);
  227. return 1;
  228. }
  229. /**
  230. 停止指定的camera
  231. @api camera.stop(id)
  232. @int camera id,例如0
  233. @return boolean 成功返回true,否则返回false
  234. @usage
  235. camera.stop(0)
  236. */
  237. static int l_camera_stop(lua_State *L) {
  238. int id = luaL_checkinteger(L, 1);
  239. lua_pushboolean(L, luat_camera_stop(id) == 0 ? 1 : 0);
  240. return 1;
  241. }
  242. /**
  243. 关闭指定的camera,释放相应的IO资源
  244. @api camera.close(id)
  245. @int camera id,例如0
  246. @return boolean 成功返回true,否则返回false
  247. @usage
  248. camera.close(0)
  249. */
  250. static int l_camera_close(lua_State *L) {
  251. int id = luaL_checkinteger(L, 1);
  252. lua_pushboolean(L, luat_camera_close(id) == 0 ? 1 : 0);
  253. return 1;
  254. }
  255. LUAT_WEAK int luat_camera_capture(int id, uint8_t quality, const char *path) {
  256. LLOGD("not support yet");
  257. return -1;
  258. }
  259. LUAT_WEAK int luat_camera_get_raw_start(int id, int w, int h, uint8_t *data, uint32_t max_len) {
  260. LLOGD("not support yet");
  261. return -1;
  262. }
  263. LUAT_WEAK int luat_camera_get_raw_again(int id) {
  264. LLOGD("not support yet");
  265. return -1;
  266. }
  267. LUAT_WEAK luat_camera_video(int id, int w, int h, uint8_t uart_id) {
  268. LLOGD("not support yet");
  269. return -1;
  270. }
  271. /**
  272. camera拍照
  273. @api camera.capture(id, y_diff, save_path, quality)
  274. @int camera id,例如0
  275. @int y_diff,Y分量校准量,0~255,越大越亮,根据实际情况修改,GC032A测试下来需要填0
  276. @string save_path,文件保存路径,空则写在上次路径里,默认是/capture.jpg
  277. @int quality, jpeg压缩质量,1最差,占用空间小,3最高,占用空间最大而且费时间,默认1
  278. @return boolean 成功返回true,否则返回false
  279. @usage
  280. camera.capture(0)
  281. */
  282. static int l_camera_capture(lua_State *L) {
  283. int id = luaL_checkinteger(L, 1);
  284. const char* save_path = luaL_checkstring(L, 2);
  285. int quality = luaL_optinteger(L, 3, 1);
  286. luat_camera_capture(id, quality, save_path);
  287. return 0;
  288. }
  289. /**
  290. camera输出视频流到USB
  291. @api camera.video(id, w, h, out_path)
  292. @int camera id,例如0
  293. @int 宽度
  294. @int 高度
  295. @int 输出路径,目前只能用虚拟串口0
  296. @return boolean 成功返回true,否则返回false
  297. @usage
  298. camera.video(0, 320, 240, uart.VUART_0)
  299. */
  300. static int l_camera_video(lua_State *L) {
  301. int id = luaL_checkinteger(L, 1);
  302. int w = luaL_optinteger(L, 2, 320);
  303. int h = luaL_optinteger(L, 3, 240);
  304. int param = luaL_optinteger(L, 4, LUAT_VUART_ID_0);
  305. luat_camera_video(id, w, h, param);
  306. return 0;
  307. }
  308. /**
  309. 启动camera输出原始数据到用户的zbuff缓存区,输出1fps后会停止,并通过camera.on设置的回调函数回调接收到的长度,如果需要再次输出,请调用camera.getRaw
  310. @api camera.startRaw(id, w, h, buff)
  311. @int camera id,例如0
  312. @int 宽度
  313. @int 高度
  314. @zbuff 用于存放数据的缓存区,大小必须不小于w X h X 2 byte
  315. @return boolean 成功返回true,否则返回false
  316. @usage
  317. camera.startRaw(0, 320, 240, buff)
  318. */
  319. static int l_camera_start_raw(lua_State *L) {
  320. int id = luaL_checkinteger(L, 1);
  321. int w = luaL_optinteger(L, 2, 320);
  322. int h = luaL_optinteger(L, 3, 240);
  323. luat_zbuff_t *buff = luaL_checkudata(L, 4, LUAT_ZBUFF_TYPE);
  324. luat_camera_get_raw_start(id, w, h, buff->addr, buff->len);
  325. return 0;
  326. }
  327. /**
  328. 再次启动camera输出原始数据到用户的zbuff缓存区,输出1fps后会停止,并通过camera.on设置的回调函数回调接收到的长度,如果需要再次输出,请继续调用本API
  329. @api camera.getRaw(id)
  330. @int camera id,例如0
  331. @return boolean 成功返回true,否则返回false
  332. @usage
  333. camera.getRaw(0)
  334. */
  335. static int l_camera_get_raw(lua_State *L) {
  336. int id = luaL_checkinteger(L, 1);
  337. luat_camera_get_raw_again(id);
  338. return 0;
  339. }
  340. #include "rotable2.h"
  341. static const rotable_Reg_t reg_camera[] =
  342. {
  343. { "init" , ROREG_FUNC(l_camera_init )},
  344. { "start" , ROREG_FUNC(l_camera_start )},
  345. { "stop" , ROREG_FUNC(l_camera_stop)},
  346. { "capture", ROREG_FUNC(l_camera_capture)},
  347. { "video", ROREG_FUNC(l_camera_video)},
  348. { "startRaw", ROREG_FUNC(l_camera_start_raw)},
  349. { "getRaw", ROREG_FUNC(l_camera_get_raw)},
  350. { "close", ROREG_FUNC(l_camera_close)},
  351. { "on", ROREG_FUNC(l_camera_on)},
  352. { NULL, {}}
  353. };
  354. LUAMOD_API int luaopen_camera( lua_State *L ) {
  355. luat_newlib2(L, reg_camera);
  356. return 1;
  357. }