luat_lib_camera.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. @module camera
  3. @summary 摄像头
  4. @version 1.0
  5. @date 2022.01.11
  6. */
  7. #include "luat_base.h"
  8. #include "luat_camera.h"
  9. #include "luat_msgbus.h"
  10. #define LUAT_LOG_TAG "camera"
  11. #include "luat_log.h"
  12. #define MAX_DEVICE_COUNT 2
  13. typedef struct luat_camera_cb {
  14. int scanned;
  15. } luat_camera_cb_t;
  16. static luat_camera_cb_t camera_cbs[MAX_DEVICE_COUNT];
  17. int l_camera_handler(lua_State *L, void* ptr) {
  18. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  19. lua_pop(L, 1);
  20. int camera_id = msg->arg1;
  21. if (camera_cbs[camera_id].scanned) {
  22. lua_geti(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  23. if (lua_isfunction(L, -1)) {
  24. lua_pushinteger(L, camera_id);
  25. if (msg->ptr)
  26. {
  27. lua_pushlstring(L, (char *)msg->ptr,msg->arg2);
  28. }
  29. else
  30. {
  31. lua_pushboolean(L, msg->arg2);
  32. }
  33. lua_call(L, 2, 0);
  34. }
  35. }
  36. lua_pushinteger(L, 0);
  37. return 1;
  38. }
  39. /*
  40. 初始化摄像头
  41. @api camera.init(InitReg)
  42. @table InitReg camera初始化命令 见demo/camera/AIR105 注意:如扫码 camera初始化时需设置为灰度输出
  43. @return int camera_id
  44. @usage
  45. camera_id = camera.init(GC032A_InitReg)--屏幕输出rgb图像
  46. --初始化后需要start才开始输出/扫码
  47. camera.start(camera_id)--开始指定的camera
  48. */
  49. static int l_camera_init(lua_State *L){
  50. luat_camera_conf_t conf = {0};
  51. conf.lcd_conf = luat_lcd_get_default();
  52. if (lua_istable(L, 1)) {
  53. lua_pushliteral(L, "zbar_scan");
  54. lua_gettable(L, 1);
  55. if (lua_isinteger(L, -1)) {
  56. conf.zbar_scan = luaL_checkinteger(L, -1);
  57. }
  58. lua_pop(L, 1);
  59. lua_pushliteral(L, "draw_lcd");
  60. lua_gettable(L, 1);
  61. if (lua_isinteger(L, -1)) {
  62. conf.draw_lcd = luaL_checkinteger(L, -1);
  63. }
  64. lua_pop(L, 1);
  65. lua_pushliteral(L, "i2c_id");
  66. lua_gettable(L, 1);
  67. if (lua_isinteger(L, -1)) {
  68. conf.i2c_id = luaL_checkinteger(L, -1);
  69. }
  70. lua_pop(L, 1);
  71. lua_pushliteral(L, "i2c_addr");
  72. lua_gettable(L, 1);
  73. if (lua_isinteger(L, -1)) {
  74. conf.i2c_addr = luaL_checkinteger(L, -1);
  75. }
  76. lua_pop(L, 1);
  77. lua_pushliteral(L, "pwm_id");
  78. lua_gettable(L, 1);
  79. if (lua_isinteger(L, -1)) {
  80. conf.pwm_id = luaL_checkinteger(L, -1);
  81. }
  82. lua_pop(L, 1);
  83. lua_pushliteral(L, "pwm_period");
  84. lua_gettable(L, 1);
  85. if (lua_isinteger(L, -1)) {
  86. conf.pwm_period = luaL_checkinteger(L, -1);
  87. }
  88. lua_pop(L, 1);
  89. lua_pushliteral(L, "pwm_pulse");
  90. lua_gettable(L, 1);
  91. if (lua_isinteger(L, -1)) {
  92. conf.pwm_pulse = luaL_checkinteger(L, -1);
  93. }
  94. lua_pop(L, 1);
  95. lua_pushliteral(L, "sensor_width");
  96. lua_gettable(L, 1);
  97. if (lua_isinteger(L, -1)) {
  98. conf.sensor_width = luaL_checkinteger(L, -1);
  99. }
  100. lua_pop(L, 1);
  101. lua_pushliteral(L, "sensor_height");
  102. lua_gettable(L, 1);
  103. if (lua_isinteger(L, -1)) {
  104. conf.sensor_height = luaL_checkinteger(L, -1);
  105. }
  106. lua_pop(L, 1);
  107. lua_pushliteral(L, "color_bit");
  108. lua_gettable(L, 1);
  109. if (lua_isinteger(L, -1)) {
  110. conf.color_bit = luaL_checkinteger(L, -1);
  111. }
  112. lua_pop(L, 1);
  113. lua_pushliteral(L, "id_reg");
  114. lua_gettable(L, 1);
  115. if (lua_isinteger(L, -1)) {
  116. conf.id_reg = luaL_checkinteger(L, -1);
  117. }
  118. lua_pop(L, 1);
  119. lua_pushliteral(L, "id_value");
  120. lua_gettable(L, 1);
  121. if (lua_isinteger(L, -1)) {
  122. conf.id_value = luaL_checkinteger(L, -1);
  123. }
  124. lua_pop(L, 1);
  125. lua_pushliteral(L, "init_cmd");
  126. lua_gettable(L, 1);
  127. if (lua_istable(L, -1)) {
  128. conf.init_cmd_size = lua_rawlen(L, -1);
  129. conf.init_cmd = luat_heap_malloc(conf.init_cmd_size * sizeof(uint8_t));
  130. for (size_t i = 1; i <= conf.init_cmd_size; i++){
  131. lua_geti(L, -1, i);
  132. conf.init_cmd[i-1] = luaL_checkinteger(L, -1);
  133. lua_pop(L, 1);
  134. }
  135. }
  136. lua_pop(L, 1);
  137. }
  138. lua_pushinteger(L, luat_camera_init(&conf));
  139. return 1;
  140. }
  141. /*
  142. 注册摄像头事件回调
  143. @api camera.on(id, event, func)
  144. @int camera id, camera 0写0, camera 1写1
  145. @string 事件名称
  146. @function 回调方法
  147. @return nil 无返回值
  148. @usage
  149. camera.on(0, "scanned", function(id, str)
  150. print(id, str)
  151. end)
  152. */
  153. static int l_camera_on(lua_State *L) {
  154. int camera_id = luaL_checkinteger(L, 1);
  155. const char* event = luaL_checkstring(L, 2);
  156. if (!strcmp("scanned", event)) {
  157. if (camera_cbs[camera_id].scanned != 0) {
  158. luaL_unref(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  159. camera_cbs[camera_id].scanned = 0;
  160. }
  161. if (lua_isfunction(L, 3)) {
  162. lua_pushvalue(L, 3);
  163. camera_cbs[camera_id].scanned = luaL_ref(L, LUA_REGISTRYINDEX);
  164. }
  165. }
  166. return 0;
  167. }
  168. /**
  169. 开始指定的camera
  170. @api camera.start(id)
  171. @int camera id,例如0
  172. @return boolean 成功返回true,否则返回false
  173. @usage
  174. camera.start(0)
  175. */
  176. static int l_camera_start(lua_State *L) {
  177. int id = luaL_checkinteger(L, 1);
  178. lua_pushboolean(L, luat_camera_start(id) == 0 ? 1 : 0);
  179. return 1;
  180. }
  181. /**
  182. 停止指定的camera
  183. @api camera.stop(id)
  184. @int camera id,例如0
  185. @return boolean 成功返回true,否则返回false
  186. @usage
  187. camera.stop(0)
  188. */
  189. static int l_camera_stop(lua_State *L) {
  190. int id = luaL_checkinteger(L, 1);
  191. lua_pushboolean(L, luat_camera_stop(id) == 0 ? 1 : 0);
  192. return 1;
  193. }
  194. /**
  195. 关闭指定的camera,释放相应的IO资源
  196. @api camera.close(id)
  197. @int camera id,例如0
  198. @return boolean 成功返回true,否则返回false
  199. @usage
  200. camera.close(0)
  201. */
  202. static int l_camera_close(lua_State *L) {
  203. int id = luaL_checkinteger(L, 1);
  204. lua_pushboolean(L, luat_camera_close(id) == 0 ? 1 : 0);
  205. return 1;
  206. }
  207. LUAT_WEAK luat_camera_capture(int id, uint8_t quality, const char *path) {
  208. LLOGD("not support yet");
  209. return -1;
  210. }
  211. /**
  212. camera拍照
  213. @api camera.capture(id, y_diff, save_path, quality)
  214. @int camera id,例如0
  215. @int y_diff,Y分量校准量,0~255,越大越亮,根据实际情况修改,GC032A测试下来需要填0
  216. @string save_path,文件保存路径,空则写在上次路径里,默认是/capture.jpg
  217. @int quality, jpeg压缩质量,1最差,占用空间小,3最高,占用空间最大而且费时间,默认1
  218. @return boolean 成功返回true,否则返回false
  219. @usage
  220. camera.capture(0)
  221. */
  222. static int l_camera_capture(lua_State *L) {
  223. int id = luaL_checkinteger(L, 1);
  224. const char* save_path = luaL_checkstring(L, 2);
  225. int quality = luaL_optinteger(L, 3, 1);
  226. luat_camera_capture(id, quality, save_path);
  227. return 0;
  228. }
  229. #include "rotable2.h"
  230. static const rotable_Reg_t reg_camera[] =
  231. {
  232. { "init" , ROREG_FUNC(l_camera_init )},
  233. { "start" , ROREG_FUNC(l_camera_start )},
  234. { "stop" , ROREG_FUNC(l_camera_stop)},
  235. { "capture", ROREG_FUNC(l_camera_capture)},
  236. { "close", ROREG_FUNC(l_camera_close)},
  237. { "on", ROREG_FUNC(l_camera_on)},
  238. { NULL, {}}
  239. };
  240. LUAMOD_API int luaopen_camera( lua_State *L ) {
  241. luat_newlib2(L, reg_camera);
  242. return 1;
  243. }