luat_lib_dac.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. @module dac
  3. @summary 数模转换
  4. @version 1.0
  5. @date 2021.12.03
  6. @demo multimedia
  7. */
  8. #include "luat_base.h"
  9. #include "luat_dac.h"
  10. #include "luat_fs.h"
  11. #include "luat_malloc.h"
  12. #define LUAT_LOG_TAG "dac"
  13. #include "luat_log.h"
  14. /*
  15. 打开DAC通道,并配置参数
  16. @api dac.open(ch, freq, mode)
  17. @int 通道编号,例如0
  18. @int 输出频率,单位hz
  19. @int 模式,默认为0,预留
  20. @return true 成功返回true,否则返回false
  21. @return int 底层返回值,调试用
  22. if dac.open(0, 44000) then
  23. log.info("dac", "dac ch0 is opened")
  24. end
  25. */
  26. static int l_dac_open(lua_State *L) {
  27. int ch = luaL_checkinteger(L, 1);
  28. int freq = luaL_checkinteger(L, 2);
  29. int mode = luaL_optinteger(L, 3, 0);
  30. int ret = luat_dac_setup(ch, freq, mode);
  31. lua_pushboolean(L, ret == 0 ? 1 : 0);
  32. lua_pushinteger(L, ret);
  33. return 2;
  34. }
  35. /*
  36. 从指定DAC通道输出一段波形,或者单个值
  37. @api dac.write(ch, data)
  38. @int 通道编号,例如0
  39. @string 若输出固定值,可以填数值, 若输出波形,填string
  40. @return true 成功返回true,否则返回false
  41. @return int 底层返回值,调试用
  42. if dac.open(0, 44000) then
  43. log.info("dac", "dac ch0 is opened")
  44. dac.write(0, string.fromHex("ABCDABCD"))
  45. end
  46. dac.close(0)
  47. */
  48. static int l_dac_write(lua_State *L) {
  49. uint16_t* buff;
  50. size_t len;
  51. int ch;
  52. uint16_t value;
  53. ch = luaL_checkinteger(L, 1);
  54. if (lua_isinteger(L, 2)) {
  55. value = luaL_checkinteger(L, 2);
  56. buff = &value;
  57. len = 1;
  58. }
  59. else if (lua_isuserdata(L, 2)) {
  60. return 0; // TODO 支持zbuff
  61. }
  62. else if (lua_isstring(L, 2)) {
  63. buff = (uint16_t*)luaL_checklstring(L, 2, &len);
  64. }
  65. else {
  66. return 0;
  67. }
  68. int ret = luat_dac_write(ch, buff, len);
  69. lua_pushboolean(L, ret == 0 ? 1 : 0);
  70. lua_pushinteger(L, ret);
  71. return 2;
  72. }
  73. // /*
  74. // 从指定DAC通道输出一段波形,数据从文件读取
  75. // @api dac.writeFile(ch, fd)
  76. // @int 通道编号,例如0
  77. // @string 文件路径
  78. // @return true 成功返回true,否则返回false
  79. // @return int 底层返回值,调试用
  80. // if dac.open(0, 44000) then
  81. // log.info("dac", "dac ch0 is opened")
  82. // dac.writeFile(0, "/luadb/test.wav")
  83. // end
  84. // dac.close(0)
  85. // */
  86. // static int l_dac_write_file(lua_State *L) {
  87. // uint16_t* buff;
  88. // int ch;
  89. // size_t buff_size = 4096;
  90. // size_t len;
  91. // ch = luaL_checkinteger(L, 1);
  92. // const char* path = luaL_checkstring(L, 2);
  93. // FILE* fd = luat_fs_fopen(path, "rb");
  94. // if (fd == NULL) {
  95. // LLOGD("file not exist %s", path);
  96. // lua_pushboolean(L, 0);
  97. // return 1;
  98. // }
  99. // if (lua_isinteger(L, 3)) {
  100. // buff_size = luaL_checkinteger(L, 3);
  101. // }
  102. // buff = luat_heap_malloc(buff_size);
  103. // if (buff == NULL) {
  104. // luat_fs_fclose(fd);
  105. // LLOGW("buff size too big? %d", buff_size);
  106. // lua_pushboolean(L, 0);
  107. // return 1;
  108. // }
  109. // while (1) {
  110. // len = luat_fs_fread(buff, buff_size, 1, fd);
  111. // if (len == 0) {
  112. // break;
  113. // }
  114. // luat_dac_write(ch, buff, len);
  115. // }
  116. // luat_heap_free(buff);
  117. // luat_fs_fclose(fd);
  118. // lua_pushboolean(L, 1);
  119. // return 1;
  120. // }
  121. /*
  122. 关闭DAC通道
  123. @api dac.close(ch)
  124. @int 通道编号,例如0
  125. @return true 成功返回true,否则返回false
  126. @return int 底层返回值,调试用
  127. if dac.open(0, 44000) then
  128. log.info("dac", "dac ch0 is opened")
  129. dac.write(0, string.fromHex("ABCDABCD"))
  130. end
  131. dac.close(0)
  132. */
  133. static int l_dac_close(lua_State *L) {
  134. int ch = luaL_checkinteger(L, 1);
  135. int ret = luat_dac_close(ch);
  136. lua_pushboolean(L, ret == 0 ? 1 : 0);
  137. lua_pushinteger(L, ret);
  138. return 2;
  139. }
  140. #include "rotable2.h"
  141. static const rotable_Reg_t reg_dac[] =
  142. {
  143. { "open" , ROREG_FUNC(l_dac_open)},
  144. { "write" , ROREG_FUNC(l_dac_write)},
  145. // { "writeFile", ROREG_FUNC(l_dac_write_file)},
  146. { "close" , ROREG_FUNC(l_dac_close)},
  147. { NULL, ROREG_INT(0) }
  148. };
  149. LUAMOD_API int luaopen_dac( lua_State *L ) {
  150. luat_newlib2(L, reg_dac);
  151. return 1;
  152. }