user_main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_config_t;
  49. static system_config_t system_config;
  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_config.wifi_connected == false && system_config.motion_detector == true)
  58. {
  59. system_config.motion_detector = false;
  60. Encoder_DisableIOInterrupts();
  61. Encoder_DisableMotion();
  62. Light_AccessPointSearching();
  63. WiFi_Connect(WIFI_TIMEOUT);
  64. }
  65. break;
  66. }
  67. default:
  68. {
  69. ESP_LOGE(TAG, "Unknown IMU interrupt")
  70. }
  71. }
  72. }
  73. void ICACHE_FLASH_ATTR switch_a_cb(void)
  74. {
  75. if (system_config.wifi_connected == false)
  76. {
  77. system_config.motion_detector = false;
  78. Encoder_DisableIOInterrupts();
  79. Encoder_DisableMotion();
  80. Light_AccessPointSearching();
  81. WiFi_Connect(WIFI_TIMEOUT);
  82. }
  83. }
  84. void ICACHE_FLASH_ATTR switch_b_cb(void)
  85. {
  86. if (system_config.wifi_connected == false)
  87. {
  88. system_config.motion_detector = false;
  89. Encoder_DisableIOInterrupts();
  90. Encoder_DisableMotion();
  91. Light_AccessPointSearching();
  92. WiFi_Connect(WIFI_TIMEOUT);
  93. }
  94. }
  95. void ICACHE_FLASH_ATTR ideasX_success_cb(void)
  96. {
  97. Light_BrokerSuccess();
  98. }
  99. void ICACHE_FLASH_ATTR ideasX_fail_cb(void)
  100. {
  101. Light_BrokerFailure();
  102. }
  103. void ICACHE_FLASH_ATTR wifi_success_cb(void)
  104. {
  105. system_config.wifi_connected = true;
  106. Light_AccessPointConnected();
  107. IdeasX_Connect();
  108. //IdeasX_Init();
  109. }
  110. void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code)
  111. {
  112. system_config.wifi_connected = false;
  113. IdeasX_Disconnect();
  114. switch (failure_code)
  115. {
  116. case WIFI_AP_DISCONNECTED:
  117. {
  118. ESP_LOGI(TAG, "Wi-Fi connection lost");
  119. Light_AccessPointFailure();
  120. break;
  121. }
  122. case WIFI_AP_AUTHMODE_CHANGE:
  123. {
  124. ESP_LOGI(TAG, "Wi-Fi credential failure or change");
  125. Light_AccessPointFailure();
  126. break;
  127. }
  128. case WIFI_NO_STORED_APS_FAILURE:
  129. {
  130. ESP_LOGI(TAG, "no stored Wi-Fi credentials");
  131. Light_AccessPointNoCredentials();
  132. break;
  133. }
  134. case WIFI_NO_AVAILABLE_APS_FAILURE:
  135. {
  136. ESP_LOGI(TAG, "no Wi-Fi available");
  137. Light_AccessPointFailure();
  138. break;
  139. }
  140. case WIFI_TIMEOUT_FAILURE:
  141. {
  142. ESP_LOGI(TAG, "Wi-Fi timeout");
  143. Light_AccessPointFailure();
  144. break;
  145. }
  146. case WIFI_SCAN_FAILURE:
  147. {
  148. ESP_LOGE(TAG, "Wi-Fi scan failure");
  149. Light_SystemFailure();
  150. break;
  151. }
  152. default:
  153. {
  154. ESP_LOGW(TAG, "unknown Wi-Fi failure code");
  155. Light_SystemFailure();
  156. }
  157. }
  158. // re-enable motion detectors
  159. system_config.motion_detector = true;
  160. Encoder_EnableMotion();
  161. Encoder_EnableIOInterrupts();
  162. }
  163. void ICACHE_FLASH_ATTR app_init(void)
  164. {
  165. system_config.wifi_connected = false;
  166. Light_AccessPointSearching();
  167. WiFi_Connect(WIFI_TIMEOUT);
  168. }
  169. void user_init(void)
  170. {
  171. Encoder_InitUART(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1); // this needs to be abstracted
  172. os_printf("\n\n----------------------------------------------------------\n");
  173. os_printf("\tIdeasX Encoder - (curiousmuch.org)\n");
  174. os_printf("----------------------------------------------------------\n\n");
  175. // Intialize UART and Wi-Fi
  176. WiFi_Initialize();
  177. IdeasX_Init();
  178. Encoder_InitIO();
  179. Encoder_DisableIOInterrupts();
  180. //IO_Shutdown(); // set the shutdown pin for testing purporses
  181. WiFi_SetCallbacks(wifi_success_cb, wifi_fail_cb);
  182. IdeasX_SetStatusCallbacks(ideasX_success_cb, ideasX_fail_cb);
  183. Encoder_SetSwitchCallback(switch_a_cb, switch_b_cb);
  184. Encoder_DisableDebounceTimer();
  185. Encoder_SetIMUCallback(imu_cb);
  186. system_init_done_cb(app_init);
  187. }