luat_lib_camera.c 12 KB

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