user_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* main.c -- MQTT client example
  2. *
  3. * Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "esp_common.h"
  31. #include "driver/uart.h"
  32. #include "driver/spi_interface.h"
  33. #include "gpio.h"
  34. #include "log/esp_log.h"
  35. #include "gdbstub/gdbstub.h"
  36. #include "hal/lsm6ds3.h"
  37. #include "hal/max17043.h"
  38. #include "hal/lps25hb.h"
  39. #include "hal/ws2812.h"
  40. #include "interface/wifi_interface.h"
  41. #include "interface/encoder_interface.h"
  42. #include "user_config.h"
  43. static const char* TAG = "main.c";
  44. typedef struct {
  45. bool wifi_connected;
  46. bool ideasX_connected;
  47. bool motion_detector;
  48. } system_state_t;
  49. static system_state_t system_state;
  50. void ICACHE_FLASH_ATTR imu_cb(IMU_Interrupt_et interrupt)
  51. {
  52. switch(interrupt)
  53. {
  54. case IMU_MOTION:
  55. {
  56. // the flag is hack because multiple significant motion interrupt are called
  57. if (system_state.wifi_connected == false && system_state.motion_detector == true)
  58. {
  59. system_state.motion_detector = false;
  60. Encoder_DisableIOInterrupts();
  61. Encoder_DisableMotion();
  62. Light_AccessPointSearching();
  63. WiFi_Connect(WIFI_TIMEOUT);
  64. }
  65. // the LSM6DS3 retains and old interrupt that isn't cleared until the first time switch 1 is pressed.
  66. if (system_state.wifi_connected == true && system_state.motion_detector == false)
  67. {
  68. Encoder_EnableIOInterrupts();
  69. }
  70. break;
  71. }
  72. default:
  73. {
  74. ESP_LOGE(TAG, "Unknown IMU interrupt")
  75. }
  76. }
  77. }
  78. void ICACHE_FLASH_ATTR switch_a_cb(uint32_t gpio_states)
  79. {
  80. if (system_state.wifi_connected == false)
  81. {
  82. system_state.motion_detector = false;
  83. Encoder_DisableIOInterrupts();
  84. Encoder_DisableMotion();
  85. Light_AccessPointSearching();
  86. WiFi_Connect(WIFI_TIMEOUT);
  87. }
  88. if (system_state.ideasX_connected)
  89. {
  90. ESP_LOGD(TAG, "GPIO_STATUS: %x", gpio_states);
  91. IdeasX_Publish_Buttons(gpio_states);
  92. }
  93. }
  94. void ICACHE_FLASH_ATTR switch_b_cb(uint32_t gpio_states)
  95. {
  96. if (system_state.wifi_connected == false)
  97. {
  98. system_state.motion_detector = false;
  99. Encoder_DisableIOInterrupts();
  100. Encoder_DisableMotion();
  101. Light_AccessPointSearching();
  102. WiFi_Connect(WIFI_TIMEOUT);
  103. }
  104. if (system_state.ideasX_connected)
  105. {
  106. ESP_LOGD(TAG, "GPIO_STATUS: %x", gpio_states);
  107. IdeasX_Publish_Buttons(gpio_states);
  108. }
  109. }
  110. void ICACHE_FLASH_ATTR ideasX_success_cb(void)
  111. {
  112. Light_BrokerSuccess();
  113. system_state.ideasX_connected = true;
  114. Encoder_EnableIOInterrupts();
  115. IO_ActivateDebounceTimer();
  116. }
  117. void ICACHE_FLASH_ATTR ideasX_fail_cb(void)
  118. {
  119. Light_BrokerFailure();
  120. system_state.ideasX_connected = false;
  121. Encoder_DisableIOInterrupts();
  122. IO_DisableDebounceTimer();
  123. }
  124. void ICACHE_FLASH_ATTR wifi_success_cb(void)
  125. {
  126. system_state.wifi_connected = true;
  127. Light_AccessPointConnected();
  128. IdeasX_Connect();
  129. //IdeasX_Init();
  130. }
  131. void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code)
  132. {
  133. system_state.wifi_connected = false;
  134. IdeasX_Disconnect();
  135. switch (failure_code)
  136. {
  137. case WIFI_AP_DISCONNECTED:
  138. {
  139. ESP_LOGI(TAG, "Wi-Fi connection lost");
  140. Light_AccessPointFailure();
  141. break;
  142. }
  143. case WIFI_AP_AUTHMODE_CHANGE:
  144. {
  145. ESP_LOGI(TAG, "Wi-Fi credential failure or change");
  146. Light_AccessPointFailure();
  147. break;
  148. }
  149. case WIFI_NO_STORED_APS_FAILURE:
  150. {
  151. ESP_LOGI(TAG, "no stored Wi-Fi credentials");
  152. Light_AccessPointNoCredentials();
  153. break;
  154. }
  155. case WIFI_NO_AVAILABLE_APS_FAILURE:
  156. {
  157. ESP_LOGI(TAG, "no Wi-Fi available");
  158. Light_AccessPointFailure();
  159. break;
  160. }
  161. case WIFI_TIMEOUT_FAILURE:
  162. {
  163. ESP_LOGI(TAG, "Wi-Fi timeout");
  164. Light_AccessPointFailure();
  165. break;
  166. }
  167. case WIFI_SCAN_FAILURE:
  168. {
  169. ESP_LOGE(TAG, "Wi-Fi scan failure");
  170. Light_SystemFailure();
  171. break;
  172. }
  173. default:
  174. {
  175. ESP_LOGW(TAG, "unknown Wi-Fi failure code");
  176. Light_SystemFailure();
  177. }
  178. }
  179. // re-enable motion detectors
  180. system_state.motion_detector = true;
  181. Encoder_EnableMotion();
  182. Encoder_EnableIOInterrupts();
  183. }
  184. void ICACHE_FLASH_ATTR app_init(void)
  185. {
  186. system_state.wifi_connected = false;
  187. Light_AccessPointSearching();
  188. WiFi_Connect(WIFI_TIMEOUT);
  189. }
  190. void user_init(void)
  191. {
  192. //os_delay_us(1000000);
  193. Encoder_InitUART(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1); // this needs to be abstracted
  194. os_printf("\n\n----------------------------------------------------------\n");
  195. os_printf("\tIdeasX Encoder - (curiousmuch.org)\n");
  196. os_printf("----------------------------------------------------------\n\n");
  197. // Intialize UART and Wi-Fi
  198. WiFi_Initialize();
  199. IdeasX_Init();
  200. Encoder_InitIO();
  201. Encoder_InitIMU();
  202. Encoder_DisableIOInterrupts();
  203. Encoder_EnableIMUI2CInterface(true);
  204. // IO_Shutdown(); // set the shutdown pin for testing
  205. WiFi_SetCallbacks(wifi_success_cb, wifi_fail_cb);
  206. IdeasX_SetStatusCallbacks(ideasX_success_cb, ideasX_fail_cb);
  207. Encoder_SetSwitchCallback(switch_a_cb, switch_b_cb);
  208. Encoder_DisableDebounceTimer();
  209. Encoder_SetIMUCallback(imu_cb);
  210. system_init_done_cb(app_init);
  211. }