/* main.c -- MQTT client example * * Copyright (c) 2014-2015, Tuan PM * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Redis nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "esp_common.h" #include "driver/uart.h" #include "driver/spi_interface.h" #include "gpio.h" #include "log/esp_log.h" #include "gdbstub/gdbstub.h" #include "hal/lsm6ds3.h" #include "hal/max17043.h" #include "hal/lps25hb.h" #include "hal/ws2812.h" #include "interface/wifi_interface.h" #include "interface/encoder_interface.h" #include "user_config.h" static const char* TAG = "main.c"; typedef struct { bool wifi_connected; bool ideasX_connected; bool motion_detector; } system_config_t; static system_config_t system_config; void ICACHE_FLASH_ATTR imu_cb(IMU_Interrupt_et interrupt) { switch(interrupt) { case IMU_MOTION: { // the flag is hack because multiple significant motion interrupt are called if (system_config.wifi_connected == false && system_config.motion_detector == true) { system_config.motion_detector = false; Encoder_DisableIOInterrupts(); Encoder_DisableMotion(); Light_AccessPointSearching(); WiFi_Connect(WIFI_TIMEOUT); } break; } default: { ESP_LOGE(TAG, "Unknown IMU interrupt") } } } void ICACHE_FLASH_ATTR switch_a_cb(void) { if (system_config.wifi_connected == false) { system_config.motion_detector = false; Encoder_DisableIOInterrupts(); Encoder_DisableMotion(); Light_AccessPointSearching(); WiFi_Connect(WIFI_TIMEOUT); } } void ICACHE_FLASH_ATTR switch_b_cb(void) { if (system_config.wifi_connected == false) { system_config.motion_detector = false; Encoder_DisableIOInterrupts(); Encoder_DisableMotion(); Light_AccessPointSearching(); WiFi_Connect(WIFI_TIMEOUT); } } void ICACHE_FLASH_ATTR ideasX_success_cb(void) { Light_BrokerSuccess(); } void ICACHE_FLASH_ATTR ideasX_fail_cb(void) { Light_BrokerFailure(); } void ICACHE_FLASH_ATTR wifi_success_cb(void) { system_config.wifi_connected = true; Light_AccessPointConnected(); IdeasX_Connect(); //IdeasX_Init(); } void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code) { system_config.wifi_connected = false; IdeasX_Disconnect(); switch (failure_code) { case WIFI_AP_DISCONNECTED: { ESP_LOGI(TAG, "Wi-Fi connection lost"); Light_AccessPointFailure(); break; } case WIFI_AP_AUTHMODE_CHANGE: { ESP_LOGI(TAG, "Wi-Fi credential failure or change"); Light_AccessPointFailure(); break; } case WIFI_NO_STORED_APS_FAILURE: { ESP_LOGI(TAG, "no stored Wi-Fi credentials"); Light_AccessPointNoCredentials(); break; } case WIFI_NO_AVAILABLE_APS_FAILURE: { ESP_LOGI(TAG, "no Wi-Fi available"); Light_AccessPointFailure(); break; } case WIFI_TIMEOUT_FAILURE: { ESP_LOGI(TAG, "Wi-Fi timeout"); Light_AccessPointFailure(); break; } case WIFI_SCAN_FAILURE: { ESP_LOGE(TAG, "Wi-Fi scan failure"); Light_SystemFailure(); break; } default: { ESP_LOGW(TAG, "unknown Wi-Fi failure code"); Light_SystemFailure(); } } // re-enable motion detectors system_config.motion_detector = true; Encoder_EnableMotion(); Encoder_EnableIOInterrupts(); } void ICACHE_FLASH_ATTR app_init(void) { system_config.wifi_connected = false; Light_AccessPointSearching(); WiFi_Connect(WIFI_TIMEOUT); } void user_init(void) { Encoder_InitUART(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1); // this needs to be abstracted os_printf("\n\n----------------------------------------------------------\n"); os_printf("\tIdeasX Encoder - (curiousmuch.org)\n"); os_printf("----------------------------------------------------------\n\n"); // Intialize UART and Wi-Fi WiFi_Initialize(); IdeasX_Init(); Encoder_InitIO(); Encoder_DisableIOInterrupts(); //IO_Shutdown(); // set the shutdown pin for testing purporses WiFi_SetCallbacks(wifi_success_cb, wifi_fail_cb); IdeasX_SetStatusCallbacks(ideasX_success_cb, ideasX_fail_cb); Encoder_SetSwitchCallback(switch_a_cb, switch_b_cb); Encoder_DisableDebounceTimer(); Encoder_SetIMUCallback(imu_cb); system_init_done_cb(app_init); }