user_main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 motion_detector;
  47. } system_config_t;
  48. static system_config_t system_config;
  49. void ICACHE_FLASH_ATTR imu_cb(IMU_Interrupt_et interrupt)
  50. {
  51. switch(interrupt)
  52. {
  53. case IMU_MOTION:
  54. {
  55. system_config.motion_detector = false;
  56. if (system_config.wifi_connected == false)
  57. {
  58. Encoder_DisableIOInterrupts();
  59. Encoder_DisableMotion();
  60. Light_AccessPointSearching();
  61. WiFi_Connect(WIFI_TIMEOUT);
  62. }
  63. break;
  64. }
  65. default:
  66. {
  67. ESP_LOGE(TAG, "Unknown IMU interrupt")
  68. }
  69. }
  70. }
  71. void ICACHE_FLASH_ATTR wifi_success_cb(void)
  72. {
  73. system_config.wifi_connected = true;
  74. }
  75. void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code)
  76. {
  77. system_config.wifi_connected = false;
  78. switch (failure_code)
  79. {
  80. case WIFI_AP_DISCONNECTED:
  81. {
  82. ESP_LOGI(TAG, "Wi-Fi connection lost");
  83. Light_AccessPointFailure();
  84. break;
  85. }
  86. case WIFI_AP_AUTHMODE_CHANGE:
  87. {
  88. ESP_LOGI(TAG, "Wi-Fi credential failure or change");
  89. Light_AccessPointFailure();
  90. break;
  91. }
  92. case WIFI_NO_STORED_APS_FAILURE:
  93. {
  94. ESP_LOGI(TAG, "no stored Wi-Fi credentials");
  95. Light_AccessPointNoCredentials();
  96. break;
  97. }
  98. case WIFI_NO_AVAILABLE_APS_FAILURE:
  99. {
  100. ESP_LOGI(TAG, "no Wi-Fi available");
  101. Light_AccessPointFailure();
  102. break;
  103. }
  104. case WIFI_TIMEOUT_FAILURE:
  105. {
  106. ESP_LOGI(TAG, "Wi-Fi timeout");
  107. Light_AccessPointFailure();
  108. break;
  109. }
  110. case WIFI_SCAN_FAILURE:
  111. {
  112. ESP_LOGE(TAG, "Wi-Fi scan failure");
  113. Light_SystemFailure();
  114. break;
  115. }
  116. default:
  117. {
  118. ESP_LOGW(TAG, "unknown Wi-Fi failure code");
  119. Light_SystemFailure();
  120. }
  121. }
  122. // re-enable motion detectors
  123. system_config.motion_detector = true;
  124. Encoder_EnableMotion();
  125. Encoder_EnableIOInterrupts();
  126. }
  127. void ICACHE_FLASH_ATTR app_init(void)
  128. {
  129. system_config.wifi_connected = false;
  130. Light_AccessPointSearching();
  131. WiFi_Connect(WIFI_TIMEOUT);
  132. }
  133. void user_init(void)
  134. {
  135. uart_init(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1); // this needs to be abstracted
  136. os_printf("\n\n----------------------------------------------------------\n");
  137. os_printf("\tIdeasX Encoder - Tyler Berezowsky\n");
  138. os_printf("----------------------------------------------------------\n\n");
  139. // Intialize UART and Wi-Fi
  140. WiFi_Initialize();
  141. Encoder_InitIO();
  142. Encoder_DisableIOInterrupts();
  143. IO_Shutdown(); // set the shutdown pin for testing purporses
  144. WiFi_SetCallbacks(wifi_success_cb, wifi_fail_cb);
  145. //Encoder_SetSwitchCallback(switch_a_cb, switch_b_cb);
  146. Encoder_SetIMUCallback(imu_cb);
  147. system_init_done_cb(app_init);
  148. }