123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /* main.c -- MQTT client example
- *
- * Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com>
- * 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_state_t;
- static system_state_t system_state;
- 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_state.wifi_connected == false && system_state.motion_detector == true)
- {
- system_state.motion_detector = false;
- Encoder_DisableIOInterrupts();
- Encoder_DisableMotion();
- Light_AccessPointSearching();
- WiFi_Connect(WIFI_TIMEOUT);
- }
- // the LSM6DS3 retains and old interrupt that isn't cleared until the first time switch 1 is pressed.
- if (system_state.wifi_connected == true && system_state.motion_detector == false)
- {
- Encoder_EnableIOInterrupts();
- }
- break;
- }
- default:
- {
- ESP_LOGE(TAG, "Unknown IMU interrupt")
- }
- }
- }
- void ICACHE_FLASH_ATTR switch_a_cb(uint32_t gpio_states)
- {
- if (system_state.wifi_connected == false)
- {
- system_state.motion_detector = false;
- Encoder_DisableIOInterrupts();
- Encoder_DisableMotion();
- Light_AccessPointSearching();
- WiFi_Connect(WIFI_TIMEOUT);
- }
- if (system_state.ideasX_connected)
- {
- ESP_LOGD(TAG, "GPIO_STATUS: %x", gpio_states);
- IdeasX_Publish_Buttons(gpio_states);
- }
- }
- void ICACHE_FLASH_ATTR switch_b_cb(uint32_t gpio_states)
- {
- if (system_state.wifi_connected == false)
- {
- system_state.motion_detector = false;
- Encoder_DisableIOInterrupts();
- Encoder_DisableMotion();
- Light_AccessPointSearching();
- WiFi_Connect(WIFI_TIMEOUT);
- }
- if (system_state.ideasX_connected)
- {
- ESP_LOGD(TAG, "GPIO_STATUS: %x", gpio_states);
- IdeasX_Publish_Buttons(gpio_states);
- }
- }
- void ICACHE_FLASH_ATTR ideasX_success_cb(void)
- {
- Light_BrokerSuccess();
- system_state.ideasX_connected = true;
- Encoder_EnableIOInterrupts();
- IO_ActivateDebounceTimer();
- }
- void ICACHE_FLASH_ATTR ideasX_fail_cb(void)
- {
- Light_BrokerFailure();
- system_state.ideasX_connected = false;
- Encoder_DisableIOInterrupts();
- IO_DisableDebounceTimer();
- }
- void ICACHE_FLASH_ATTR wifi_success_cb(void)
- {
- system_state.wifi_connected = true;
- Light_AccessPointConnected();
- IdeasX_Connect();
- //IdeasX_Init();
- }
- void ICACHE_FLASH_ATTR wifi_fail_cb(wifi_failure_et failure_code)
- {
- system_state.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_state.motion_detector = true;
- Encoder_EnableMotion();
- Encoder_EnableIOInterrupts();
- }
- void ICACHE_FLASH_ATTR app_init(void)
- {
- system_state.wifi_connected = false;
- Light_AccessPointSearching();
- WiFi_Connect(WIFI_TIMEOUT);
- }
- void user_init(void)
- {
- //os_delay_us(1000000);
- 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_InitIMU();
- Encoder_DisableIOInterrupts();
- Encoder_EnableIMUI2CInterface(true);
- // IO_Shutdown(); // set the shutdown pin for testing
-
- 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);
- }
|