tcs34725.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // ***************************************************************************
  2. // TCS34725 module for ESP8266 with nodeMCU
  3. //
  4. // Written by K. Townsend (microBuilder.eu), Adapted for nodeMCU by Travis Howse (tjhowse gmail.com)
  5. //
  6. // BSD (see license.txt)
  7. // ***************************************************************************
  8. // Original header:
  9. /**************************************************************************/
  10. /*!
  11. @file tcs34725.c
  12. @author K. Townsend (microBuilder.eu)
  13. @ingroup Sensors
  14. @brief Driver for the TAOS TCS34725 I2C digital RGB/color sensor
  15. @license BSD (see license.txt)
  16. */
  17. /**************************************************************************/
  18. //#define NODE_DEBUG
  19. #include "module.h"
  20. #include "lauxlib.h"
  21. #include "platform.h"
  22. #include "user_interface.h"
  23. #include <math.h>
  24. // #define TCS34725_ADDRESS (0x29<<1)
  25. #define TCS34725_ADDRESS (0x29)
  26. #define TCS34725_BUS_ID (0x00) /* ?? Not sure what this is for . Nodemcu I2C bus ID? */
  27. #define TCS34725_READBIT (0x01)
  28. #define TCS34725_COMMAND_BIT (0x80)
  29. #define TCS34725_ENABLE (0x00)
  30. #define TCS34725_ENABLE_AIEN (0x10) /* RGBC Interrupt Enable */
  31. #define TCS34725_ENABLE_WEN (0x08) /* Wait enable - Writing 1 activates the wait timer */
  32. #define TCS34725_ENABLE_AEN (0x02) /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */
  33. #define TCS34725_ENABLE_PON (0x01) /* Power on - Writing 1 activates the internal oscillator, 0 disables it */
  34. #define TCS34725_ATIME (0x01) /* Integration time */
  35. #define TCS34725_WTIME (0x03) /* Wait time (if TCS34725_ENABLE_WEN is asserted) */
  36. #define TCS34725_WTIME_2_4MS (0xFF) /* WLONG0 = 2.4ms WLONG1 = 0.029s */
  37. #define TCS34725_WTIME_204MS (0xAB) /* WLONG0 = 204ms WLONG1 = 2.45s */
  38. #define TCS34725_WTIME_614MS (0x00) /* WLONG0 = 614ms WLONG1 = 7.4s */
  39. #define TCS34725_AILTL (0x04) /* Clear channel lower interrupt threshold */
  40. #define TCS34725_AILTH (0x05)
  41. #define TCS34725_AIHTL (0x06) /* Clear channel upper interrupt threshold */
  42. #define TCS34725_AIHTH (0x07)
  43. #define TCS34725_PERS (0x0C) /* Persistence register - basic SW filtering mechanism for interrupts */
  44. #define TCS34725_PERS_NONE (0b0000) /* Every RGBC cycle generates an interrupt */
  45. #define TCS34725_PERS_1_CYCLE (0b0001) /* 1 clean channel value outside threshold range generates an interrupt */
  46. #define TCS34725_PERS_2_CYCLE (0b0010) /* 2 clean channel values outside threshold range generates an interrupt */
  47. #define TCS34725_PERS_3_CYCLE (0b0011) /* 3 clean channel values outside threshold range generates an interrupt */
  48. #define TCS34725_PERS_5_CYCLE (0b0100) /* 5 clean channel values outside threshold range generates an interrupt */
  49. #define TCS34725_PERS_10_CYCLE (0b0101) /* 10 clean channel values outside threshold range generates an interrupt */
  50. #define TCS34725_PERS_15_CYCLE (0b0110) /* 15 clean channel values outside threshold range generates an interrupt */
  51. #define TCS34725_PERS_20_CYCLE (0b0111) /* 20 clean channel values outside threshold range generates an interrupt */
  52. #define TCS34725_PERS_25_CYCLE (0b1000) /* 25 clean channel values outside threshold range generates an interrupt */
  53. #define TCS34725_PERS_30_CYCLE (0b1001) /* 30 clean channel values outside threshold range generates an interrupt */
  54. #define TCS34725_PERS_35_CYCLE (0b1010) /* 35 clean channel values outside threshold range generates an interrupt */
  55. #define TCS34725_PERS_40_CYCLE (0b1011) /* 40 clean channel values outside threshold range generates an interrupt */
  56. #define TCS34725_PERS_45_CYCLE (0b1100) /* 45 clean channel values outside threshold range generates an interrupt */
  57. #define TCS34725_PERS_50_CYCLE (0b1101) /* 50 clean channel values outside threshold range generates an interrupt */
  58. #define TCS34725_PERS_55_CYCLE (0b1110) /* 55 clean channel values outside threshold range generates an interrupt */
  59. #define TCS34725_PERS_60_CYCLE (0b1111) /* 60 clean channel values outside threshold range generates an interrupt */
  60. #define TCS34725_CONFIG (0x0D)
  61. #define TCS34725_CONFIG_WLONG (0x02) /* Choose between short and long (12x) wait times via TCS34725_WTIME */
  62. #define TCS34725_CONTROL (0x0F) /* Set the gain level for the sensor */
  63. #define TCS34725_ID (0x12) /* 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 */
  64. #define TCS34725_STATUS (0x13)
  65. #define TCS34725_STATUS_AINT (0x10) /* RGBC Clean channel interrupt */
  66. #define TCS34725_STATUS_AVALID (0x01) /* Indicates that the RGBC channels have completed an integration cycle */
  67. #define TCS34725_CDATAL (0x14) /* Clear channel data */
  68. #define TCS34725_CDATAH (0x15)
  69. #define TCS34725_RDATAL (0x16) /* Red channel data */
  70. #define TCS34725_RDATAH (0x17)
  71. #define TCS34725_GDATAL (0x18) /* Green channel data */
  72. #define TCS34725_GDATAH (0x19)
  73. #define TCS34725_BDATAL (0x1A) /* Blue channel data */
  74. #define TCS34725_BDATAH (0x1B)
  75. #define TCS34725_EN_DELAY 30
  76. typedef enum
  77. {
  78. TCS34725_INTEGRATIONTIME_2_4MS = 0xFF, /**< 2.4ms - 1 cycle - Max Count: 1024 */
  79. TCS34725_INTEGRATIONTIME_24MS = 0xF6, /**< 24ms - 10 cycles - Max Count: 10240 */
  80. TCS34725_INTEGRATIONTIME_101MS = 0xD5, /**< 101ms - 42 cycles - Max Count: 43008 */
  81. TCS34725_INTEGRATIONTIME_154MS = 0xC0, /**< 154ms - 64 cycles - Max Count: 65535 */
  82. TCS34725_INTEGRATIONTIME_700MS = 0x00 /**< 700ms - 256 cycles - Max Count: 65535 */
  83. }
  84. tcs34725IntegrationTime_t;
  85. typedef enum
  86. {
  87. TCS34725_GAIN_1X = 0x00, /**< No gain */
  88. TCS34725_GAIN_4X = 0x01, /**< 2x gain */
  89. TCS34725_GAIN_16X = 0x02, /**< 16x gain */
  90. TCS34725_GAIN_60X = 0x03 /**< 60x gain */
  91. }
  92. tcs34725Gain_t;
  93. static void temp_setup_debug(int line, const char *str);
  94. int tcs34725Setup(lua_State* L);
  95. int tcs34725Enable(lua_State* L);
  96. int tcs34725Disable(lua_State* L);
  97. int tcs34725GetRawData(lua_State* L);
  98. int tcs34725LuaSetIntegrationTime(lua_State* L);
  99. uint8_t tcs34725SetIntegrationTime(tcs34725IntegrationTime_t it, lua_State* L);
  100. int tcs34725LuaSetGain(lua_State* L);
  101. uint8_t tcs34725SetGain(tcs34725Gain_t gain, lua_State* L);
  102. static bool _tcs34725Initialised = false;
  103. static int32_t _tcs34725SensorID = 0;
  104. static tcs34725Gain_t _tcs34725Gain = TCS34725_GAIN_1X;
  105. static tcs34725IntegrationTime_t _tcs34725IntegrationTime = TCS34725_INTEGRATIONTIME_2_4MS;
  106. os_timer_t tcs34725_timer; // timer for forced mode readout
  107. sint32_t cb_tcs_en;
  108. /**************************************************************************/
  109. /*!
  110. @brief Writes an 8 bit values over I2C
  111. */
  112. /**************************************************************************/
  113. uint8_t tcs34725Write8 (uint8_t reg, uint8_t value)
  114. {
  115. platform_i2c_send_start(TCS34725_BUS_ID);
  116. platform_i2c_send_address(TCS34725_BUS_ID, TCS34725_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  117. platform_i2c_send_byte(TCS34725_BUS_ID, TCS34725_COMMAND_BIT | reg );
  118. platform_i2c_send_byte(TCS34725_BUS_ID, value);
  119. platform_i2c_send_stop(TCS34725_BUS_ID);
  120. return 0;
  121. }
  122. /**************************************************************************/
  123. /*!
  124. @brief Reads a 8 bit values over I2C
  125. */
  126. /**************************************************************************/
  127. uint8_t tcs34725Read8(uint8_t reg)
  128. {
  129. uint8_t value;
  130. platform_i2c_send_start(TCS34725_BUS_ID);
  131. platform_i2c_send_address(TCS34725_BUS_ID, TCS34725_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER);
  132. platform_i2c_send_byte(TCS34725_BUS_ID, TCS34725_COMMAND_BIT | reg);
  133. platform_i2c_send_stop(TCS34725_BUS_ID);
  134. platform_i2c_send_start(TCS34725_BUS_ID);
  135. platform_i2c_send_address(TCS34725_BUS_ID, TCS34725_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER);
  136. value = platform_i2c_recv_byte(TCS34725_BUS_ID, 0);
  137. platform_i2c_send_stop(TCS34725_BUS_ID);
  138. return value;
  139. }
  140. /**************************************************************************/
  141. /*!
  142. @brief Reads a 16 bit values over I2C
  143. */
  144. /**************************************************************************/
  145. uint16_t tcs34725Read16(uint8_t reg)
  146. {
  147. uint8_t low = tcs34725Read8(reg);
  148. uint8_t high = tcs34725Read8(++reg);
  149. return (high << 8) | low;
  150. }
  151. /**************************************************************************/
  152. /*!
  153. @brief Finishes enabling the device
  154. */
  155. /**************************************************************************/
  156. uint8_t tcs34725EnableDone()
  157. {
  158. dbg_printf("Enable finished\n");
  159. lua_State *L = lua_getstate();
  160. os_timer_disarm (&tcs34725_timer);
  161. tcs34725Write8(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
  162. /* Ready to go ... set the initialised flag */
  163. _tcs34725Initialised = true;
  164. /* This needs to take place after the initialisation flag! */
  165. tcs34725SetIntegrationTime(TCS34725_INTEGRATIONTIME_2_4MS, L);
  166. tcs34725SetGain(TCS34725_GAIN_60X, L);
  167. lua_rawgeti(L, LUA_REGISTRYINDEX, cb_tcs_en); // Get the callback to call
  168. luaL_unref(L, LUA_REGISTRYINDEX, cb_tcs_en); // Unregister the callback to avoid leak
  169. cb_tcs_en = LUA_NOREF;
  170. luaL_pcallx(L, 0, 0);
  171. return 0;
  172. }
  173. /**************************************************************************/
  174. /*!
  175. @brief Enables the device
  176. */
  177. /**************************************************************************/
  178. int tcs34725Enable(lua_State* L)
  179. {
  180. dbg_printf("Enable begun\n");
  181. if (lua_isfunction(L, 1)) {
  182. if (cb_tcs_en != LUA_NOREF) {
  183. luaL_unref(L, LUA_REGISTRYINDEX, cb_tcs_en);
  184. }
  185. lua_pushvalue(L, 1);
  186. cb_tcs_en = luaL_ref(L, LUA_REGISTRYINDEX);
  187. } else {
  188. return luaL_error(L, "Enable argument must be a function.");
  189. }
  190. tcs34725Write8(TCS34725_ENABLE, TCS34725_ENABLE_PON);
  191. // Start a timer to wait TCS34725_EN_DELAY before calling tcs34725EnableDone
  192. os_timer_disarm (&tcs34725_timer);
  193. os_timer_setfn (&tcs34725_timer, (os_timer_func_t *)tcs34725EnableDone, NULL);
  194. os_timer_arm (&tcs34725_timer, TCS34725_EN_DELAY, 0); // trigger callback when readout is ready
  195. return 0;
  196. }
  197. /**************************************************************************/
  198. /*!
  199. @brief Disables the device (putting it in lower power sleep mode)
  200. */
  201. /**************************************************************************/
  202. int tcs34725Disable(lua_State* L)
  203. {
  204. /* Turn the device off to save power */
  205. uint8_t reg = 0;
  206. reg = tcs34725Read8(TCS34725_ENABLE);
  207. tcs34725Write8(TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
  208. _tcs34725Initialised = false;
  209. return 0;
  210. }
  211. /**************************************************************************/
  212. /*!
  213. @brief Initialises the I2C block
  214. */
  215. /**************************************************************************/
  216. int tcs34725Setup(lua_State* L)
  217. {
  218. uint8_t id = 0;
  219. /* Make sure we have the right IC (0x44 = TCS34725 and TCS34721) */
  220. id = tcs34725Read8(TCS34725_ID);
  221. dbg_printf("id: %x\n",id);
  222. if (id != 0x44) {
  223. return luaL_error(L, "No TCS34725 found.");
  224. }
  225. lua_pushinteger(L, 1);
  226. return 1;
  227. }
  228. /**************************************************************************/
  229. /*!
  230. @brief Sets the integration time to the specified value
  231. */
  232. /**************************************************************************/
  233. int tcs34725LuaSetIntegrationTime(lua_State* L)
  234. {
  235. tcs34725IntegrationTime_t it = luaL_checkinteger(L, 1);
  236. return tcs34725SetIntegrationTime(it,L);
  237. }
  238. /**************************************************************************/
  239. /*!
  240. @brief Sets the integration time to the specified value
  241. */
  242. /**************************************************************************/
  243. uint8_t tcs34725SetIntegrationTime(tcs34725IntegrationTime_t it, lua_State* L)
  244. {
  245. if (!_tcs34725Initialised)
  246. {
  247. tcs34725Setup(L);
  248. }
  249. tcs34725Write8(TCS34725_ATIME, it);
  250. _tcs34725IntegrationTime = it;
  251. return 0;
  252. }
  253. /**************************************************************************/
  254. /*!
  255. @brief Sets gain to the specified value from Lua
  256. */
  257. /**************************************************************************/
  258. int tcs34725LuaSetGain(lua_State* L)
  259. {
  260. tcs34725Gain_t gain = luaL_checkinteger(L, 1);
  261. return tcs34725SetGain(gain,L);
  262. }
  263. /**************************************************************************/
  264. /*!
  265. @brief Sets gain to the specified value
  266. */
  267. /**************************************************************************/
  268. uint8_t tcs34725SetGain(tcs34725Gain_t gain, lua_State* L)
  269. {
  270. if (!_tcs34725Initialised)
  271. {
  272. return luaL_error(L, "TCS34725 not initialised.");
  273. }
  274. tcs34725Write8(TCS34725_CONTROL, gain);
  275. _tcs34725Gain = gain;
  276. return 0;
  277. }
  278. /**************************************************************************/
  279. /*!
  280. @brief Reads the raw red, green, blue and clear channel values
  281. */
  282. /**************************************************************************/
  283. int tcs34725GetRawData(lua_State* L)
  284. {
  285. uint16_t r;
  286. uint16_t g;
  287. uint16_t b;
  288. uint16_t c;
  289. if (!_tcs34725Initialised)
  290. {
  291. return luaL_error(L, "TCS34725 not initialised.");
  292. }
  293. c = tcs34725Read16(TCS34725_CDATAL);
  294. r = tcs34725Read16(TCS34725_RDATAL);
  295. g = tcs34725Read16(TCS34725_GDATAL);
  296. b = tcs34725Read16(TCS34725_BDATAL);
  297. lua_pushinteger(L, c);
  298. lua_pushinteger(L, r);
  299. lua_pushinteger(L, g);
  300. lua_pushinteger(L, b);
  301. return 4;
  302. }
  303. LROT_BEGIN(tcs34725, NULL, 0)
  304. LROT_FUNCENTRY( setup, tcs34725Setup )
  305. LROT_FUNCENTRY( enable, tcs34725Enable )
  306. LROT_FUNCENTRY( disable, tcs34725Disable )
  307. LROT_FUNCENTRY( raw, tcs34725GetRawData )
  308. LROT_FUNCENTRY( setGain, tcs34725LuaSetGain )
  309. LROT_FUNCENTRY( setIntegrationTime, tcs34725LuaSetIntegrationTime )
  310. LROT_END(tcs34725, NULL, 0)
  311. NODEMCU_MODULE(TCS34725, "tcs34725", tcs34725, NULL);