tcs34725.c 13 KB

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