ow.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Module for interfacing with the OneWire interface
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include "driver/onewire.h"
  6. // Lua: ow.setup( id )
  7. static int ow_setup( lua_State *L )
  8. {
  9. unsigned id = luaL_checkinteger( L, 1 );
  10. if(id==0)
  11. return luaL_error( L, "no 1-wire for D0" );
  12. MOD_CHECK_ID( ow, id );
  13. onewire_init( id );
  14. return 0;
  15. }
  16. // Lua: r = ow.reset( id )
  17. static int ow_reset( lua_State *L )
  18. {
  19. unsigned id = luaL_checkinteger( L, 1 );
  20. MOD_CHECK_ID( ow, id );
  21. lua_pushinteger( L, onewire_reset(id) );
  22. return 1;
  23. }
  24. // Lua: ow.skip( id )
  25. static int ow_skip( lua_State *L )
  26. {
  27. unsigned id = luaL_checkinteger( L, 1 );
  28. MOD_CHECK_ID( ow, id );
  29. onewire_skip(id);
  30. return 0;
  31. }
  32. // Lua: ow.select( id, buf[8])
  33. static int ow_select( lua_State *L )
  34. {
  35. uint8_t rom[8];
  36. size_t datalen;
  37. int numdata, i;
  38. unsigned id = luaL_checkinteger( L, 1 );
  39. const char *pdata;
  40. MOD_CHECK_ID( ow, id );
  41. if( lua_istable( L, 2 ) )
  42. {
  43. datalen = lua_objlen( L, 2 );
  44. if (datalen!=8)
  45. return luaL_error( L, "wrong arg range" );
  46. for( i = 0; i < datalen; i ++ )
  47. {
  48. lua_rawgeti( L, 2, i + 1 );
  49. numdata = ( int )luaL_checkinteger( L, -1 );
  50. lua_pop( L, 1 );
  51. if( numdata > 255 )
  52. return luaL_error( L, "wrong arg range" );
  53. rom[i] = (uint8_t)numdata;
  54. }
  55. }
  56. else
  57. {
  58. pdata = luaL_checklstring( L, 2, &datalen );
  59. if (datalen!=8)
  60. return luaL_error( L, "wrong arg range" );
  61. for( i = 0; i < datalen; i ++ ){
  62. rom[i] = pdata[i];
  63. }
  64. }
  65. onewire_select(id, rom);
  66. return 0;
  67. }
  68. // Lua: ow.write( id, v, power)
  69. static int ow_write( lua_State *L )
  70. {
  71. int power = 0;
  72. unsigned id = luaL_checkinteger( L, 1 );
  73. MOD_CHECK_ID( ow, id );
  74. int v = (int)luaL_checkinteger( L, 2 );
  75. if( v > 255 )
  76. return luaL_error( L, "wrong arg range" );
  77. if(lua_isnumber(L, 3))
  78. power = lua_tointeger(L, 3);
  79. if(power!=0)
  80. power = 1;
  81. onewire_write((uint8_t)id, (uint8_t)v, (uint8_t)power);
  82. return 0;
  83. }
  84. // Lua: ow.write_bytes( id, buf, power)
  85. static int ow_write_bytes( lua_State *L )
  86. {
  87. int power = 0;
  88. size_t datalen;
  89. unsigned id = luaL_checkinteger( L, 1 );
  90. MOD_CHECK_ID( ow, id );
  91. const uint8_t *pdata = luaL_checklstring( L, 2, &datalen );
  92. if(lua_isnumber(L, 3))
  93. power = lua_tointeger(L, 3);
  94. if(power!=0)
  95. power = 1;
  96. onewire_write_bytes((uint8_t)id, pdata, (uint16_t)datalen, (uint8_t)power);
  97. return 0;
  98. }
  99. // Lua: r = ow.read( id )
  100. static int ow_read( lua_State *L )
  101. {
  102. unsigned id = luaL_checkinteger( L, 1 );
  103. MOD_CHECK_ID( ow, id );
  104. lua_pushinteger( L, onewire_read(id) );
  105. return 1;
  106. }
  107. // Lua: r = ow.read_bytes( id, size )
  108. static int ow_read_bytes( lua_State *L )
  109. {
  110. unsigned id = luaL_checkinteger( L, 1 );
  111. MOD_CHECK_ID( ow, id );
  112. u32 size = ( u32 )luaL_checkinteger( L, 2 );
  113. if( size == 0 )
  114. return 0;
  115. luaL_Buffer b;
  116. luaL_buffinit( L, &b );
  117. char *p = luaL_prepbuffer(&b);
  118. onewire_read_bytes(id, (uint8_t *)p, size);
  119. luaL_addsize(&b, size);
  120. luaL_pushresult( &b );
  121. return 1;
  122. }
  123. // Lua: ow.depower( id )
  124. static int ow_depower( lua_State *L )
  125. {
  126. unsigned id = luaL_checkinteger( L, 1 );
  127. MOD_CHECK_ID( ow, id );
  128. onewire_depower(id);
  129. return 0;
  130. }
  131. #if ONEWIRE_SEARCH
  132. // Clear the search state so that if will start from the beginning again.
  133. // Lua: ow.reset_search( id )
  134. static int ow_reset_search( lua_State *L )
  135. {
  136. unsigned id = luaL_checkinteger( L, 1 );
  137. MOD_CHECK_ID( ow, id );
  138. onewire_reset_search(id);
  139. return 0;
  140. }
  141. // Setup the search to find the device type 'family_code' on the next call
  142. // to search(*newAddr) if it is present.
  143. // Lua: ow.target_search( id, family_code)
  144. static int ow_target_search( lua_State *L )
  145. {
  146. unsigned id = luaL_checkinteger( L, 1 );
  147. MOD_CHECK_ID( ow, id );
  148. int code = (int)luaL_checkinteger( L, 2 );
  149. if( code > 255 )
  150. return luaL_error( L, "wrong arg range" );
  151. onewire_target_search((uint8_t)id, (uint8_t)code);
  152. return 0;
  153. }
  154. // Look for the next device. Returns 1 if a new address has been
  155. // returned. A zero might mean that the bus is shorted, there are
  156. // no devices, or you have already retrieved all of them. It
  157. // might be a good idea to check the CRC to make sure you didn't
  158. // get garbage. The order is deterministic. You will always get
  159. // the same devices in the same order.
  160. // Lua: r = ow.search( id )
  161. static int ow_search( lua_State *L )
  162. {
  163. unsigned id = luaL_checkinteger( L, 1 );
  164. MOD_CHECK_ID( ow, id );
  165. luaL_Buffer b;
  166. luaL_buffinit( L, &b );
  167. char *p = luaL_prepbuffer(&b);
  168. if(onewire_search(id, (uint8_t *)p)){
  169. luaL_addsize(&b, 8);
  170. luaL_pushresult( &b );
  171. } else {
  172. luaL_pushresult(&b); /* close buffer */
  173. lua_pop(L,1);
  174. lua_pushnil(L);
  175. }
  176. return 1;
  177. }
  178. #endif
  179. #if ONEWIRE_CRC
  180. // uint8_t onewire_crc8(const uint8_t *addr, uint8_t len);
  181. // Lua: r = ow.crc8( buf )
  182. static int ow_crc8( lua_State *L )
  183. {
  184. size_t datalen;
  185. const uint8_t *pdata = luaL_checklstring( L, 1, &datalen );
  186. if(datalen > 255)
  187. return luaL_error( L, "wrong arg range" );
  188. lua_pushinteger( L, onewire_crc8(pdata, (uint8_t)datalen) );
  189. return 1;
  190. }
  191. #if ONEWIRE_CRC16
  192. // bool onewire_check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc);
  193. // Lua: b = ow.check_crc16( buf, inverted_crc0, inverted_crc1, crc )
  194. static int ow_check_crc16( lua_State *L )
  195. {
  196. size_t datalen;
  197. uint8_t inverted_crc[2];
  198. const uint8_t *pdata = luaL_checklstring( L, 1, &datalen );
  199. if(datalen > 65535)
  200. return luaL_error( L, "wrong arg range" );
  201. int crc = 0;
  202. crc = luaL_checkinteger( L, 2 );
  203. if(datalen > 255)
  204. return luaL_error( L, "wrong arg range" );
  205. inverted_crc[0] = (uint8_t)crc;
  206. crc = luaL_checkinteger( L, 3 );
  207. if(datalen > 255)
  208. return luaL_error( L, "wrong arg range" );
  209. inverted_crc[1] = (uint8_t)crc;
  210. crc = 0;
  211. if(lua_isnumber(L, 4))
  212. crc = lua_tointeger(L, 4);
  213. if(crc > 65535)
  214. return luaL_error( L, "wrong arg range" );
  215. lua_pushboolean( L, onewire_check_crc16(pdata, (uint16_t)datalen, inverted_crc, (uint16_t)crc) );
  216. return 1;
  217. }
  218. // uint16_t onewire_crc16(const uint8_t* input, uint16_t len, uint16_t crc);
  219. // Lua: r = ow.crc16( buf, crc )
  220. static int ow_crc16( lua_State *L )
  221. {
  222. size_t datalen;
  223. const uint8_t *pdata = luaL_checklstring( L, 1, &datalen );
  224. if(datalen > 65535)
  225. return luaL_error( L, "wrong arg range" );
  226. int crc = 0;
  227. if(lua_isnumber(L, 2))
  228. crc = lua_tointeger(L, 2);
  229. if(crc > 65535)
  230. return luaL_error( L, "wrong arg range" );
  231. lua_pushinteger( L, onewire_crc16(pdata, (uint16_t)datalen, (uint16_t)crc) );
  232. return 1;
  233. }
  234. #endif
  235. #endif
  236. // Module function map
  237. static const LUA_REG_TYPE ow_map[] = {
  238. { LSTRKEY( "setup" ), LFUNCVAL( ow_setup ) },
  239. { LSTRKEY( "reset" ), LFUNCVAL( ow_reset ) },
  240. { LSTRKEY( "skip" ), LFUNCVAL( ow_skip ) },
  241. { LSTRKEY( "select" ), LFUNCVAL( ow_select ) },
  242. { LSTRKEY( "write" ), LFUNCVAL( ow_write ) },
  243. { LSTRKEY( "write_bytes" ), LFUNCVAL( ow_write_bytes ) },
  244. { LSTRKEY( "read" ), LFUNCVAL( ow_read ) },
  245. { LSTRKEY( "read_bytes" ), LFUNCVAL( ow_read_bytes ) },
  246. { LSTRKEY( "depower" ), LFUNCVAL( ow_depower ) },
  247. #if ONEWIRE_SEARCH
  248. { LSTRKEY( "reset_search" ), LFUNCVAL( ow_reset_search ) },
  249. { LSTRKEY( "target_search" ), LFUNCVAL( ow_target_search ) },
  250. { LSTRKEY( "search" ), LFUNCVAL( ow_search ) },
  251. #endif
  252. #if ONEWIRE_CRC
  253. { LSTRKEY( "crc8" ), LFUNCVAL( ow_crc8 ) },
  254. #if ONEWIRE_CRC16
  255. { LSTRKEY( "check_crc16" ), LFUNCVAL( ow_check_crc16 ) },
  256. { LSTRKEY( "crc16" ), LFUNCVAL( ow_crc16 ) },
  257. #endif
  258. #endif
  259. { LNILKEY, LNILVAL }
  260. };
  261. NODEMCU_MODULE(OW, "ow", ow_map, NULL);