user_sensor.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_humiture.c
  5. *
  6. * Description: humiture demo's function realization
  7. *
  8. * Modification history:
  9. * 2014/5/1, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "ets_sys.h"
  12. #include "osapi.h"
  13. #include "os_type.h"
  14. #include "user_interface.h"
  15. #if SENSOR_DEVICE
  16. #include "user_sensor.h"
  17. LOCAL struct keys_param keys;
  18. LOCAL struct single_key_param *single_key[SENSOR_KEY_NUM];
  19. LOCAL os_timer_t sensor_sleep_timer;
  20. LOCAL os_timer_t link_led_timer;
  21. LOCAL uint8 link_led_level = 0;
  22. LOCAL uint32 link_start_time;
  23. #if HUMITURE_SUB_DEVICE
  24. #include "driver/i2c_master.h"
  25. #define MVH3004_Addr 0x88
  26. LOCAL uint8 humiture_data[4];
  27. /******************************************************************************
  28. * FunctionName : user_mvh3004_burst_read
  29. * Description : burst read mvh3004's internal data
  30. * Parameters : uint8 addr - mvh3004's address
  31. * uint8 *pData - data point to put read data
  32. * uint16 len - read length
  33. * Returns : bool - true or false
  34. *******************************************************************************/
  35. LOCAL bool ICACHE_FLASH_ATTR
  36. user_mvh3004_burst_read(uint8 addr, uint8 *pData, uint16 len)
  37. {
  38. uint8 ack;
  39. uint16 i;
  40. i2c_master_start();
  41. i2c_master_writeByte(addr);
  42. ack = i2c_master_getAck();
  43. if (ack) {
  44. os_printf("addr not ack when tx write cmd \n");
  45. i2c_master_stop();
  46. return false;
  47. }
  48. i2c_master_stop();
  49. i2c_master_wait(40000);
  50. i2c_master_start();
  51. i2c_master_writeByte(addr + 1);
  52. ack = i2c_master_getAck();
  53. if (ack) {
  54. os_printf("addr not ack when tx write cmd \n");
  55. i2c_master_stop();
  56. return false;
  57. }
  58. for (i = 0; i < len; i++) {
  59. pData[i] = i2c_master_readByte();
  60. i2c_master_setAck((i == (len - 1)) ? 1 : 0);
  61. }
  62. i2c_master_stop();
  63. return true;
  64. }
  65. /******************************************************************************
  66. * FunctionName : user_mvh3004_read_th
  67. * Description : read mvh3004's humiture data
  68. * Parameters : uint8 *data - where data to put
  69. * Returns : bool - ture or false
  70. *******************************************************************************/
  71. bool ICACHE_FLASH_ATTR
  72. user_mvh3004_read_th(uint8 *data)
  73. {
  74. return user_mvh3004_burst_read(MVH3004_Addr, data, 4);
  75. }
  76. /******************************************************************************
  77. * FunctionName : user_mvh3004_init
  78. * Description : init mvh3004, mainly i2c master gpio
  79. * Parameters : none
  80. * Returns : none
  81. *******************************************************************************/
  82. void ICACHE_FLASH_ATTR
  83. user_mvh3004_init(void)
  84. {
  85. i2c_master_gpio_init();
  86. }
  87. uint8 *ICACHE_FLASH_ATTR
  88. user_mvh3004_get_poweron_th(void)
  89. {
  90. return humiture_data;
  91. }
  92. #endif
  93. /******************************************************************************
  94. * FunctionName : user_humiture_long_press
  95. * Description : humiture key's function, needed to be installed
  96. * Parameters : none
  97. * Returns : none
  98. *******************************************************************************/
  99. LOCAL void ICACHE_FLASH_ATTR
  100. user_sensor_long_press(void)
  101. {
  102. user_esp_platform_set_active(0);
  103. system_restore();
  104. system_restart();
  105. }
  106. LOCAL void ICACHE_FLASH_ATTR
  107. user_link_led_init(void)
  108. {
  109. PIN_FUNC_SELECT(SENSOR_LINK_LED_IO_MUX, SENSOR_LINK_LED_IO_FUNC);
  110. PIN_FUNC_SELECT(SENSOR_UNUSED_LED_IO_MUX, SENSOR_UNUSED_LED_IO_FUNC);
  111. GPIO_OUTPUT_SET(GPIO_ID_PIN(SENSOR_UNUSED_LED_IO_NUM), 0);
  112. }
  113. void ICACHE_FLASH_ATTR
  114. user_link_led_output(uint8 level)
  115. {
  116. GPIO_OUTPUT_SET(GPIO_ID_PIN(SENSOR_LINK_LED_IO_NUM), level);
  117. }
  118. LOCAL void ICACHE_FLASH_ATTR
  119. user_link_led_timer_cb(void)
  120. {
  121. link_led_level = (~link_led_level) & 0x01;
  122. GPIO_OUTPUT_SET(GPIO_ID_PIN(SENSOR_LINK_LED_IO_NUM), link_led_level);
  123. }
  124. void ICACHE_FLASH_ATTR
  125. user_link_led_timer_init(void)
  126. {
  127. link_start_time = system_get_time();
  128. os_timer_disarm(&link_led_timer);
  129. os_timer_setfn(&link_led_timer, (os_timer_func_t *)user_link_led_timer_cb, NULL);
  130. os_timer_arm(&link_led_timer, 50, 1);
  131. link_led_level = 0;
  132. GPIO_OUTPUT_SET(GPIO_ID_PIN(SENSOR_LINK_LED_IO_NUM), link_led_level);
  133. }
  134. void ICACHE_FLASH_ATTR
  135. user_link_led_timer_done(void)
  136. {
  137. os_timer_disarm(&link_led_timer);
  138. GPIO_OUTPUT_SET(GPIO_ID_PIN(SENSOR_LINK_LED_IO_NUM), 0);
  139. }
  140. void ICACHE_FLASH_ATTR
  141. user_sensor_deep_sleep_enter(void)
  142. {
  143. system_deep_sleep(SENSOR_DEEP_SLEEP_TIME > link_start_time \
  144. ? SENSOR_DEEP_SLEEP_TIME - link_start_time : 30000000);
  145. }
  146. void ICACHE_FLASH_ATTR
  147. user_sensor_deep_sleep_disable(void)
  148. {
  149. os_timer_disarm(&sensor_sleep_timer);
  150. }
  151. void ICACHE_FLASH_ATTR
  152. user_sensor_deep_sleep_init(uint32 time)
  153. {
  154. os_timer_disarm(&sensor_sleep_timer);
  155. os_timer_setfn(&sensor_sleep_timer, (os_timer_func_t *)user_sensor_deep_sleep_enter, NULL);
  156. os_timer_arm(&sensor_sleep_timer, time, 0);
  157. }
  158. /******************************************************************************
  159. * FunctionName : user_humiture_init
  160. * Description : init humiture function, include key and mvh3004
  161. * Parameters : none
  162. * Returns : none
  163. *******************************************************************************/
  164. void ICACHE_FLASH_ATTR
  165. user_sensor_init(uint8 active)
  166. {
  167. user_link_led_init();
  168. wifi_status_led_install(SENSOR_WIFI_LED_IO_NUM, SENSOR_WIFI_LED_IO_MUX, SENSOR_WIFI_LED_IO_FUNC);
  169. if (wifi_get_opmode() != SOFTAP_MODE) {
  170. single_key[0] = key_init_single(SENSOR_KEY_IO_NUM, SENSOR_KEY_IO_MUX, SENSOR_KEY_IO_FUNC,
  171. user_sensor_long_press, NULL);
  172. keys.key_num = SENSOR_KEY_NUM;
  173. keys.single_key = single_key;
  174. key_init(&keys);
  175. if (GPIO_INPUT_GET(GPIO_ID_PIN(SENSOR_KEY_IO_NUM)) == 0) {
  176. user_sensor_long_press();
  177. }
  178. }
  179. #if HUMITURE_SUB_DEVICE
  180. user_mvh3004_init();
  181. user_mvh3004_read_th(humiture_data);
  182. #endif
  183. #ifdef SENSOR_DEEP_SLEEP
  184. if (wifi_get_opmode() != STATIONAP_MODE) {
  185. if (active == 1) {
  186. user_sensor_deep_sleep_init(SENSOR_DEEP_SLEEP_TIME / 1000 );
  187. } else {
  188. user_sensor_deep_sleep_init(SENSOR_DEEP_SLEEP_TIME / 1000 / 3 * 2);
  189. }
  190. }
  191. #endif
  192. }
  193. #endif