Browse Source

expanded io imu and light HAL / interfaces

curiousmuch 6 years ago
parent
commit
970c9cc656
7 changed files with 186 additions and 384 deletions
  1. 72 124
      hal/io.c
  2. 54 222
      hal/lsm6ds3.c
  3. 8 5
      include/hal/io.h
  4. 11 0
      include/hal/lsm6ds3.h
  5. 19 0
      include/interface/light_interface.h
  6. 7 0
      interface/light_interface.c
  7. 15 33
      user/user_main.c

+ 72 - 124
hal/io.c

@@ -11,15 +11,28 @@
 *******************************************************************************/
 
 #include "hal/io.h"
+#include "hal/lsm6ds3.h"
+#include "log/esp_log.h"
+
+static const char* TAG = "io.c";
 
 // debounce timers
-static Encoder_Timer_t encoder_timer;
+static Encoder_t encoder;
+
+void Encoder_GPIO_Handler(void);
+static void switchA_debounce(void);
+static void switchB_debounce(void);
 
-void ICACHE_FLASH_ATTR Encoder_InitIO(uint32_t debounce_delay, switch_function_t
-    switch_a_cb, switch_function_t switch_b_cb, switch_function_t imu_cb)
+void ICACHE_FLASH_ATTR Encoder_InitIO(void)
 {
-    ETS_GPIO_INTR_DISABLE();
-    gpio_init();
+    ETS_GPIO_INTR_DISABLE();    // disable interrupts
+
+    gpio_init();                // magic function in SDK
+
+    encoder.debounce_delay = DEBOUNCEDELAY;
+    encoder.switch_a_cb = NULL;
+    encoder.switch_b_cb = NULL;
+    encoder.imu_cb = NULL; 
 
     // setup as GPIO pins
     PIN_FUNC_SELECT(LDO_SHUTDOWN_MUX, LDO_SHUTDOWN_FUNC);
@@ -30,29 +43,40 @@ void ICACHE_FLASH_ATTR Encoder_InitIO(uint32_t debounce_delay, switch_function_t
     // setup GPIO states
     gpio_output_set(BIT(LDO_SHUTDOWN), BIT(STATUS_LED), (BIT(LDO_SHUTDOWN)|BIT(STATUS_LED)), (BIT(SWITCH_A)|BIT(SWITCH_B)));
 
-    // attach interrupts to switchs
-    gpio_pin_intr_state_set(GPIO_ID_PIN(SWTICH_A), GPIO_PIN_INTR_ANYEDGE);
-    gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
+    // attach callback function for GPIO interrupts
+    ETS_GPIO_INTR_ATTACH(Encoder_GPIO_Handler, NULL);
 
-    os_timer_disarm(&encoder_timer.switch_a);
-    os_timer_disarm(&encoder_timer.switch_b);
+    // setup debounce SW timers
+    os_timer_disarm(&encoder.switch_a_timer);
+    os_timer_disarm(&encoder.switch_b_timer);
+    os_timer_setfn(&encoder.switch_a_timer, (os_timer_func_t *)switchA_debounce, NULL);
+    os_timer_setfn(&encoder.switch_b_timer, (os_timer_func_t *)switchB_debounce, NULL);
 
-    os_timer_setfn(&timerA, (os_timer_func_t *)switchA_debounce, NULL);
-    os_timer_setfn(&timerB, (os_timer_func_t *)switchB_debounce, NULL);
-    ETS_GPIO_INTR_ENABLE();
-    return;
 }
 
- void ICACHE_FLASH_ATTR Encoder_EnableAdaptiveSwitches(void)
+void ICACHE_FLASH_ATTR Encoder_SetSwitchCallback(switch_function_t switch_a_cb, switch_function_t switch_b_cb)
+{
+    encoder.switch_a_cb = switch_a_cb;
+    encoder.switch_b_cb = switch_b_cb;
+}
+
+void ICACHE_FLASH_ATTR Encoder_SetIMUCallback(imu_function_t imu_cb)
+{
+    encoder.imu_cb = imu_cb;
+}
+
+ void ICACHE_FLASH_ATTR Encoder_EnableIOInterrupts(void)
  {
-     gpio_pin_intr_state_set(GPIO_ID_PIN(SWTICH_A), GPIO_PIN_INTR_ANYEDGE);
+     ETS_GPIO_INTR_DISABLE();
+     gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
      gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
-
+     ETS_GPIO_INTR_ENABLE();
  }
 
- void ICACHE_FLASH_ATTR Encoder_DisableAdaptiveSwitches(void)
+ void ICACHE_FLASH_ATTR Encoder_DisableIOInterrupts(void)
  {
-     gpio_pin_intr_state_set(GPIO_ID_PIN(SWTICH_A), GPIO_PIN_INTR_DISABLE);
+     ETS_GPIO_INTR_DISABLE();
+     gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_DISABLE);
      gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_DISABLE);
  }
 
@@ -61,11 +85,6 @@ void ICACHE_FLASH_ATTR Encoder_ShutDown(void)
     GPIO_OUTPUT_SET(LDO_SHUTDOWN, 0);
 }
 
-void ICACHE_FLASH_ATTR Encoder_Restart(void)
-{
-    return;
-}
-
 void ICACHE_FLASH_ATTR Encoder_GPIO_Handler(void)
 {
 	uint8_t i;
@@ -80,119 +99,50 @@ void ICACHE_FLASH_ATTR Encoder_GPIO_Handler(void)
 		{
 			gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_DISABLE);			// disable interrupt for pin
 			GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(i));			// clear interrupt status
-			#if MODULE_IO_DEBUG
-				os_printf("GPIO: Pin %d Interrupt\r\n", i);
-			#endif
+            ESP_LOGV(TAG, "Pin Interrupt %d", i);
 			switch(i)
 			{
-				case PBA: {
-					//os_printf("I'm here!\r\n");
-					if (sysCfg.module_state.connected == true) 								// if module is connected to Wi-Fi
-					{
-
-						uint8_t fifoFlags;
-						fifoFlags = lsm6ds3_fifo_getFlags();
-						os_printf("LSM6DS3: FIFO Flags %x\r\n", fifoFlags);
-						if (fifoFlags & (LSM6DS3_FIFO_OVER_RUN | LSM6DS3_FIFO_FULL ) )
-						{
-							os_printf("LSM6DS3: FIFO Full / Overrun\r\n");
-							lsm6ds3_reset();
-
-							os_printf("LSM6DS3: Reset IMU\r\n");
-							gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);		// re-enable pin interrupt
-
-							// shutdown LSM6DS3
-							// set error flag in health message
-						}
-						else if (fifoFlags & LSM6DS3_FIFO_THRESHOLD)
-						{
-							os_printf("LSM6DS3: FIFO Threshold\r\n");
-							publish_fifo_dump();
-							gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);		// re-enable pin interrupt
-
-						}
-						else if (fifoFlags & LSM6DS3_FIFO_EMPTY)
-						{
-							os_printf("LSM6DS3: FIFO Empty\r\n");
-							gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);		// re-enable pin interrupt
-
-						}
-						else
-						{
-							os_timer_arm(&pba_timer, DEBOUNCEDELAY, 0);	// arm SW timer
-						}
-
-					}
-					else if (sysCfg.module_state.connected == false && sysCfg.module_state.asleep == true)
-					{
-						sysCfg.module_state.asleep = false;									// indicate module is now awake
-						/*
-
-						 *	MODULE NEEDS TO WAKE FROM SLEEP HERE IF PLACED TO SLEEP BY WI-FI PROCESS
-
-						 */
-						lsm6ds3_read_motion();												// clear LSM6DS3 interrupt
-						start_wifi_process();												// start Wi-Fi process
-						gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);		// re-enable pin interrupt
-					}
-					else
-					{
-						//os_printf("WHY ARE WE HERE!!!!\r\n");
-						lsm6ds3_read_motion();
-						gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);		// re-enable pin interrupt
-
-					}
-					break;
+				case SWITCH_A:
+                {
+                    if (LSM6DS3_Get_Interrupts() != LSM6DS3_NO_INT)
+                    {
+                        if (encoder.imu_cb != NULL)
+                            encoder.imu_cb();
+                        gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);     // re-enable interrupts
+
+                    }
+                    else
+                    {
+                        if (encoder.switch_a_cb != NULL)
+                            encoder.switch_a_cb();
+                        os_timer_arm(&encoder.switch_a_timer, encoder.debounce_delay, 0);
+                    }
+        		    break;
 				}
-				case PBB: {
-					if (sysCfg.module_state.connected == true)								// if module is connected to Wi-Fi
-					{
-						os_timer_arm(&pbb_timer, DEBOUNCEDELAY, 0); 						// arm SW timer
-					}
-					else if (sysCfg.module_state.searching == false && sysCfg.module_state.connected == false && sysCfg.module_state.asleep == true)
-					{
-						sysCfg.module_state.asleep = false;									// indicate module is now awake
-						/*
-
-						 *	MODULE NEEDS TO WAKE FROM SLEEP HERE IF PLACED TO SLEEP BY WI-FI PROCESS
-
-						 */
-						start_wifi_process(); 												// start Wi-Fi process
-						gpio_pin_intr_state_set(GPIO_ID_PIN(i), GPIO_PIN_INTR_ANYEDGE);		// re-enable pin interrupt
-					}
-					break;
+				case SWITCH_B:
+                {
+                    if (encoder.switch_b_cb != NULL)
+                        encoder.switch_b_cb();
+                    os_timer_arm(&encoder.switch_b_timer, encoder.debounce_delay, 0);
+                    break;
 				}
-				default: {
-					#if MODULE_IO_DEBUG
-						os_printf("Help! Unknown Interrupt: %d\r\n", gpio_status);
-					#endif
+				default:
+                {
+                    ESP_LOGW(TAG, "unexpected interrupt on pin %d", i);
 				}
 			}
 		}
 	}
 
-	/*
-
-		NOTE: This is slightly a hack. When the start_wifi_process function is called, it will disable I/O interrupts to insure
-		it is not called multiple times and schedule a Wi-Fi scan. The functions returns before the scan is started the following
-		line is called and therefore the LSM6DS3 is free to trigger the ISR multiple times on any motion. This line prevents this.
-		This could also be stopped by setting the INTS to INT2 when the first INT is called.
-	*/
-	if (sysCfg.module_state.searching == false)
-	{
-		ETS_GPIO_INTR_ENABLE();
-		os_printf("MODULE IO: I re-enabled interrupts..bitch.\r\n");
-	}
+    ETS_GPIO_INTR_ENABLE();
 }
 
 static void ICACHE_FLASH_ATTR switchA_debounce(void)
 {
 	ETS_GPIO_INTR_DISABLE();
 
-	uint32_t gpio_states = GPIO_REG_READ(GPIO_IN_ADDRESS);
-	//publish_button_state(gpio_states);
 	gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_A), GPIO_PIN_INTR_ANYEDGE);
-	os_timer_disarm(&timerA);
+	os_timer_disarm(&encoder.switch_a_timer);
 
 	ETS_GPIO_INTR_ENABLE();
 }
@@ -202,10 +152,8 @@ static void ICACHE_FLASH_ATTR switchB_debounce(void)
 {
 	ETS_GPIO_INTR_DISABLE();
 
-	uint32_t gpio_states = GPIO_REG_READ(GPIO_IN_ADDRESS);
-	//publish_button_state(gpio_states);
 	gpio_pin_intr_state_set(GPIO_ID_PIN(SWITCH_B), GPIO_PIN_INTR_ANYEDGE);
-	os_timer_disarm(&timerB);
+	os_timer_disarm(&encoder.switch_b_timer);
 
 	ETS_GPIO_INTR_ENABLE();
 }

+ 54 - 222
hal/lsm6ds3.c

@@ -140,225 +140,57 @@ LSM6DS3_Enable_I2C_Bridge(uint8_t enable)
     return Sensor_IO_Set_Register(LSM6DS3_MASTER_CONFIG, master_config);
 }
 
-// /******************************************************************************
-//  * FunctionName : lsm6ds3_xl_config
-//  * Description  : Setup LSM6DS3 registers for accel. sample and other cool nonsense.
-//  				  Note: 20ms is required from boot for the sensor to load some magic before you
-//  				  can play.
-//  * Parameters   : bool en_hp_mode: Enable or Disable high performance mode. NOTE: HP mode is
-//  				  is required for LP filter data and certain sampling rates.
-//  				  odr_xl: Output data rate for XL. See .h file for available rates.
-//  				  uint8_t scale_xl: Range of senstiviity selection. See .h file for available rates.
-//  				  uint8_t bw_xl: Anti-aliasing LP filter selection. See .h file and datasheet.
-//  * Returns      : bool 1: success 0: failed
-// *******************************************************************************/
-// bool ICACHE_FLASH_ATTR
-// lsm6ds3_xl_config(bool en_hp_mode, uint8_t odr_xl, uint8_t scale_xl, uint8_t bw_xl)
-// {
-//     uint8_t ctrl6_c = spi_rx8_address(HSPI, LSM6DS3_CTRL6_C);
-//     uint8_t ctrl1_xl = odr_xl | scale_xl | bw_xl;
-//
-//     if(en_hp_mode)
-//     ctrl6_c &= ~LSM6DS3_XL_HM_MODE;
-//     else
-//     ctrl6_c |= LSM6DS3_XL_HM_MODE;
-//     spi_tx8_address(HSPI, LSM6DS3_CTRL6_C, ctrl6_c);
-//     spi_tx8_address(HSPI, LSM6DS3_CTRL1_XL, ctrl1_xl);
-//     if ( (spi_rx8_address(HSPI, LSM6DS3_CTRL1_XL) == ctrl1_xl) & (spi_rx8_address(HSPI, LSM6DS3_CTRL6_C) == ctrl6_c) )
-//     return 1;
-//     else
-//     return 0;
-// }
-// /******************************************************************************
-//  * FunctionName : lsm6ds3_g_config
-//  * Description  : Setup LSM6DS3 registers for accel. sample and other cool nonsense.
-//  				  Note: 20ms is required from boot for the sensor to load some magic before you
-//  				  can play.
-//  * Parameters   : bool en_hp_mode: Enable or Disable high performance mode. NOTE: HP mode is
-//  				  is required for LP filter data and certain sampling rates.
-//  				  odr_xl: Output data rate for XL. See .h file for available rates.
-//  				  uint8_t scale_xl: Range of senstiviity selection. See .h file for available rates.
-//  				  uint8_t bw_xl: Anti-aliasing LP filter selection. See .h file and datasheet.
-//  * Returns      : bool 1: success 0: failed
-// *******************************************************************************/
-// bool ICACHE_FLASH_ATTR
-// lsm6ds3_g_config(bool en_hp_mode, bool hp_g_en, bool hp_g_rst,
-//     uint8_t odr_g, uint8_t scale_g, uint8_t hpcf_g)
-// {
-//     uint8_t ctrl7_g;
-//     uint8_t ctrl2_g = odr_g | scale_g;
-//
-//     if (en_hp_mode)
-//     ctrl7_g = LSM6DS3_G_HM_MODE;
-//     else
-//     ctrl7_g &= ~LSM6DS3_G_HM_MODE;
-//     if (hp_g_en)
-//     ctrl7_g |= LSM6DS3_HP_G_EN | hpcf_g;
-//     else
-//     ctrl7_g &= ~LSM6DS3_HP_G_EN;
-//     if (hp_g_rst)
-//     ctrl7_g |= LSM6DS3_HP_G_RST;
-//     else
-//     ctrl7_g &= ~LSM6DS3_HP_G_RST;
-//
-//     spi_tx8_address(HSPI, LSM6DS3_CTRL2_G, ctrl2_g);
-//     spi_tx8_address(HSPI, LSM6DS3_CTRL7_G, ctrl7_g);
-//     if ( (spi_rx8_address(HSPI, LSM6DS3_CTRL2_G) == ctrl2_g) & (spi_rx8_address(HSPI, LSM6DS3_CTRL7_G) == ctrl7_g))
-//     return 1;
-//     else
-//     return 0;
-// }
-// /******************************************************************************
-//  * FunctionName : lsm6ds3_fifo_config
-//  * Description  : Setup LSM6DS3 registers for FIFO. FIFO is automatically placed into continous mode.
-//  				  Decimation is set to zero, and the ODR of the FIFO is set to match that max(XL_ODR, G_ODR)
-//  				  INT1 is automatically enabled / disabled for FIFO overflow and threshold.
-//  				  If FIFO is enable it will automatically disable the significant motion ISR.
-//  * Parameters   : bool enable: Enable or Disable FIFO
-//  				  threshold: Set the threshold level of the FIFO
-//  * Returns      : bool 1: success 0: failed
-// *******************************************************************************/
-// bool ICACHE_FLASH_ATTR
-// lsm6ds3_fifo_config(uint16_t threshold)
-// {
-//     uint8_t ctrl1_xl, ctrl2_g, max_odr, fifo_ctrl3;
-//
-//     // read ODR rates
-//     ctrl1_xl = spi_rx8_address(HSPI, LSM6DS3_CTRL1_XL) & 0xF0;			// get ODR bits and clear the rest
-//     ctrl2_g = spi_rx8_address(HSPI, LSM6DS3_CTRL2_G) & 0xF0;			// get ODR bits and clear the rest
-//     os_printf("CTRL1_XL, CTRL2_G: %x, %x\r\n", ctrl1_xl, ctrl2_g);
-//     if (!(ctrl1_xl || ctrl2_g))
-//     {
-//     // disable FIFO
-//     os_printf("LSM6DS3: XL and G disabled. FIFO disabled.\r\n");
-//     lsm6ds3_set_register(LSM6DS3_FIFO_CTRL5, 0x00);
-//     lsm6ds3_set_register(LSM6DS3_INT1_CTRL, 0x00); 				// disable INT1 for overflow and threshold
-//
-//     }
-//     else
-//     {
-//
-//     // set Decimation Factor to 1 if sensor is active
-//     //lsm6ds3_set_register(FIFO_CTRL3, 0b00001001);
-//
-//     // set threshold
-//     lsm6ds3_set_register(LSM6DS3_FIFO_CTRL1, (uint8_t)(0xFF & threshold));
-//     lsm6ds3_set_register(LSM6DS3_FIFO_CTRL2, (uint8_t)(0xFF & (threshold >> 8)));
-//
-//     // set decimation factor to one if sensor ODR is not zero
-//     fifo_ctrl3 = 0;
-//     if (ctrl1_xl & 0xF0)
-//     fifo_ctrl3 = BIT0;
-//     if(ctrl2_g & 0xF0)
-//     fifo_ctrl3 |= BIT3;
-//     lsm6ds3_set_register(LSM6DS3_FIFO_CTRL3, fifo_ctrl3);
-//     os_printf("FIFO_CTRL3: %x\r\n", fifo_ctrl3);
-//
-//     // determine max ODR
-//     if ( (ctrl1_xl) >= (ctrl2_g) )
-//     max_odr = ctrl1_xl;
-//     else
-//     max_odr = ctrl2_g;
-//     max_odr = max_odr >> 1; 									// format for fifo register
-//     lsm6ds3_set_register(LSM6DS3_FIFO_CTRL5, max_odr | 0x06);	// set FIFO ODR and mode to continously
-//     lsm6ds3_set_register(LSM6DS3_INT1_CTRL, BIT3); 				// set INT1 for overflow and threshold
-//     }
-//     return 1;
-// }
-//
-// bool ICACHE_FLASH_ATTR lsm6ds3_reset(void)
-// {
-//     lsm6ds3_set_register(LSM6DS3_CTRL3_C, BIT0);
-//     os_delay_us(30000);
-//     return !(bool)(spi_rx8_address(HSPI, LSM6DS3_CTRL3_C) & BIT7);
-// }
-//
-// uint16_t ICACHE_FLASH_ATTR lsm6ds3_fifo_dump(uint16_t *buf, uint16_t buf_len, uint16_t bufPos)
-// {
-//     uint16_t samples = (uint16_t)spi_rx8_address(HSPI, LSM6DS3_FIFO_STATUS1);
-//     samples |= (((uint16_t)spi_rx8_address(HSPI, LSM6DS3_FIFO_STATUS2) & 0x0F) << 8);
-//
-//     // read and store into buff in multiples of 3
-//     if (samples <=  (buf_len - bufPos) && samples != 0)
-//     {
-//     uint16_t i;
-//     for(i=bufPos; i<samples+bufPos; i++)
-//     {
-//     buf[i] = spi_rx16_address(HSPI, LSM6DS3_FIFO_DATA_OUT_L);
-//     }
-//     return samples;
-//     }
-//     else
-//     return 0;
-//
-// }
-//
-//
-// uint8_t ICACHE_FLASH_ATTR lsm6ds3_fifo_getFlags(void)
-// {
-//     /*	There is (2) possible sources of interrupts caused by the IMU
-//     that the ESP8266 actually cares about.
-//     1) The significant motion interrupt
-//     2) The FIFO interrupts
-//
-//     LSM6DS3_FIFO_EMPTY
-//     LSM6DS3_FIFO_FULL
-//     LSM6DS3_FIFO_OVER_RUN
-//     LSM6DS3_FIFO_THRESHOLD
-//     */
-//     return (uint8_t)spi_rx8_address(HSPI, LSM6DS3_FIFO_STATUS2);
-// }
-//
-// bool ICACHE_FLASH_ATTR lsm6ds3_read_motion(void)
-// {
-//     uint8_t wake_up_src;
-//     wake_up_src = SPIread(LSM6DS3_WAKE_UP_SRC);
-//     //if (wake_up_src)
-//     {
-//     os_printf("WAKE_UP_SRC: %x\r\n", wake_up_src);
-//     os_printf("Direction ");
-//     if (wake_up_src & BIT2)
-//     os_printf("x");
-//     if (wake_up_src & BIT1)
-//     os_printf("y");
-//     if (wake_up_src & BIT0)
-//     os_printf("z");
-//
-//     os_printf("\r\n");
-//     }
-//     return wake_up_src & BIT3;
-// }
-//
-// bool ICACHE_FLASH_ATTR lsm6ds3_motion_enable(void)
-// {
-//     SPIwrite(LSM6DS3_CTRL2_G, LSM6DS3_ODR_G_POWERDOWN);								// disable gyro
-//     SPIwrite(LSM6DS3_CTRL1_XL, (LSM6DS3_ODR_XL_13Hz + LSM6DS3_FS_XL_2G));					// 208Hz 2g scale
-//     SPIwrite(LSM6DS3_TAP_CFG, LSM6DS3_LIR); 										// enable interuppt latch and slope filter
-//     SPIwrite(LSM6DS3_WAKE_UP_THS, 0x01); 									// set thres 32/64*2G = 1G
-//     SPIwrite(LSM6DS3_WAKE_UP_DUR, 0x02); 									// zero duration
-//     SPIwrite(LSM6DS3_MD1_CFG, LSM6DS3_INT1_WU);										// set interrupt for INTX (1 is connected to PBA)
-//     SPIwrite(LSM6DS3_CTRL3_C, LSM6DS3_H_LACTIVE);                                   // set int lines active low
-//     os_printf("MOTION PROCESS: Setup IMU for motion detection\r\n");
-//
-//     return 1;
-// }
-//
-// bool ICACHE_FLASH_ATTR lsm6ds3_motion_disable(void)
-// {
-//     lsm6ds3_reset();
-//     SPIwrite(LSM6DS3_CTRL2_G, LSM6DS3_ODR_G_POWERDOWN); 							// disable gyro
-//     SPIwrite(LSM6DS3_CTRL1_XL, LSM6DS3_ODR_XL_POWERDOWN); 							// disable XL
-//     SPIwrite(LSM6DS3_CTRL3_C, LSM6DS3_H_LACTIVE);
-//     return 1;
-// }
-//
-// #if LSM6DS3_DEBUG
-// void ICACHE_FLASH_ATTR
-// lsm6ds3_xl_print(void)
-// {
-//     os_printf("XL_X: %d\r\nXL_Y: %d\r\n XL_Z: %d\r\n",
-//     (SPIread(LSM6DS3_OUTX_H_XL)<<8)+SPIread(LSM6DS3_OUTX_L_XL), (SPIread(LSM6DS3_OUTY_H_XL)<<8)+SPIread(LSM6DS3_OUTY_L_XL),
-//     (SPIread(LSM6DS3_OUTZ_H_XL)<<8)+SPIread(LSM6DS3_OUTX_L_XL));
-//     return;
-// }
-// #endif
+void ICACHE_FLASH_ATTR LSM6DS3_Reset(void)
+{
+    Sensor_IO_Set_Register(LSM6DS3_CTRL3_C, BIT0);
+    os_delay_us(30000);
+}
+
+void ICACHE_FLASH_ATTR LSM6DS3_EnableMotion(void)
+{
+	Sensor_IO_Set_Register(LSM6DS3_CTRL2_G, LSM6DS3_ODR_G_POWERDOWN);						// disable gyro
+	Sensor_IO_Set_Register(LSM6DS3_CTRL1_XL, (LSM6DS3_ODR_XL_13Hz + LSM6DS3_FS_XL_2G));		// 208Hz 2g scale
+	Sensor_IO_Set_Register(LSM6DS3_TAP_CFG, LSM6DS3_LIR); 									// enable interuppt latch and slope filter
+	Sensor_IO_Set_Register(LSM6DS3_WAKE_UP_THS, 0x01); 									   // set thres 32/64*2G = 1G
+	Sensor_IO_Set_Register(LSM6DS3_WAKE_UP_DUR, 0x02); 									   // zero duration
+	Sensor_IO_Set_Register(LSM6DS3_MD1_CFG, LSM6DS3_INT1_WU);								// set interrupt for INTX (1 is connected to PBA)
+	Sensor_IO_Set_Register(LSM6DS3_CTRL3_C, LSM6DS3_H_LACTIVE);                             // set int lines active low
+
+}
+
+void ICACHE_FLASH_ATTR LSM6DS3_DisableMotion(void)
+{
+	LSM6DS3_Reset();
+	Sensor_IO_Set_Register(LSM6DS3_CTRL2_G, LSM6DS3_ODR_G_POWERDOWN);      // disable gyro
+	Sensor_IO_Set_Register(LSM6DS3_CTRL1_XL, LSM6DS3_ODR_XL_POWERDOWN);    // disable XL
+	Sensor_IO_Set_Register(LSM6DS3_CTRL3_C, LSM6DS3_H_LACTIVE);
+}
+
+// determine if there has been a FIFO or significant motion interrupt
+LSM6DS3_Interrupt_et ICACHE_FLASH_ATTR LSM6DS3_Get_Interrupts(void)
+{
+    uint32_t fifo_status2, wake_up_src;
+    Sensor_IO_Read(LSM6DS3_FIFO_STATUS2, 1, &fifo_status2);
+    Sensor_IO_Read(LSM6DS3_WAKE_UP_SRC, 1, &wake_up_src);
+
+
+    if (wake_up_src)
+    {
+        ESP_LOGV(TAG, "significant motion interrupt");
+        return LSM6DS3_SIGN_MOTION_INT;
+    }
+    else if (fifo_status2 & LSM6DS3_FIFO_OVER_RUN)
+    {
+        ESP_LOGV(TAG, "FIFO overflow interrupt");
+        return LSM6DS3_FIFO_OVERFLOW_INT;
+    }
+    else if (fifo_status2 & LSM6DS3_FIFO_FULL)
+    {
+        ESP_LOGV(TAG, "FIFO full interrupt");
+        return LSM6DS3_FIFO_FULL_INT;
+    }
+    else
+    {
+        return LSM6DS3_NO_INT;
+    }
+}

+ 8 - 5
include/hal/io.h

@@ -19,16 +19,19 @@
 #define SWITCH_B_FUNC       FUNC_GPIO2
 #define LDO_SHUTDOWN_FUNC   FUNC_GPIO4
 
+#define DEBOUNCEDELAY 15    // ms
+
 typedef void (* switch_function_t)(void);
+typedef void (* imu_function_t)(void);
 
 typedef struct {
-    os_timer_t switch_a;
-    os_timer_t switch_b;
+    os_timer_t switch_a_timer;
+    os_timer_t switch_b_timer;
     switch_function_t switch_a_cb;
     switch_function_t switch_b_cb;
-    switch_function_t imu_cb;
-    uint32_t debounce_delay; 
-} Encoder_Timer_t;
+    imu_function_t imu_cb;
+    uint32_t debounce_delay;
+} Encoder_t;
 
 
 void ICACHE_FLASH_ATTR Encoder_InitIO(void);

+ 11 - 0
include/hal/lsm6ds3.h

@@ -259,11 +259,22 @@ void lsm6ds3_xl_print(void);
 #define LSM6DS3_OUT_MAG_RAW_Z_H				0x6B
 /*------------------------------------------------------------------------------*/
 
+typedef enum {
+    LSM6DS3_NO_INT = 0,
+    LSM6DS3_FIFO_FULL_INT,
+    LSM6DS3_FIFO_OVERFLOW_INT,
+    LSM6DS3_SIGN_MOTION_INT
+}   LSM6DS3_Interrupt_et;
+
 /*------------------------------------------------------------------------------*/
 // Function Prototypes
 /*------------------------------------------------------------------------------*/
 int32_t LSM6DS3_Enable_I2C_Bridge(uint8_t enable);
 int32_t LSM6DS3_Enable_I2C_Pullups(uint8_t enable);
+void LSM6DS3_Reset(void);
+void LSM6DS3_EnableMotion(void);
+void LSM6DS3_DisableMotion(void);
+LSM6DS3_Interrupt_et LSM6DS3_Get_Interrupts(void);
 /*------------------------------------------------------------------------------*/
 
 #endif /* INCLUDE_DRIVER_LSM6DS3_H_ */

+ 19 - 0
include/interface/light_interface.h

@@ -0,0 +1,19 @@
+#ifndef LIGHT_INTERFACE_H
+#define LIGHT_INTERFACE_H
+
+#include "esp_common.h"
+#include "log/esp_log.h"
+#include "hal/ws2812.h"
+
+#define StatusLED_AccessPointFailure()          WS2812_SetColor(WS2812_RED, WS2812_PULSE)
+#define StatusLED_SystemFailure()               WS2812_SetColor(WS2812_RED, WS2812_SOLID)
+#define StatusLED_AccessPointSearching()        WS2812_SetColor(WS2812_GREEN, WS2812_PULSE)
+#define StatusLED_AccessPointNoCredentials()    WS2812_SetColor(WS2812_GREEN, WS2812_SOLID)
+#define StatusLED_BrokerFailure()               WS2812_SetColor(WS2812_BLUE, WS2812_PULSE)
+#define StatusLED_BrokerSuccess()               WS2812_SetColor(WS2812_BLUE, WS2812_SOLID)
+#define StatusLED_AdaptiveIMU()                 WS2812_SetColor(WS2812_MAGENTA, WS2812_SOLID)
+#define StatusLED_AdaptiveBar()                 WS2812_SetColor(WS2812_ORANGE, WS2812_SOLID)
+
+void StatusLED_Shutdown(void);
+
+#endif

+ 7 - 0
interface/light_interface.c

@@ -0,0 +1,7 @@
+#include "light_interface.h"
+
+void StatusLED_Shutdown(void)
+{
+    WS2812_SetColor(WS2812_RED, WS2812_SOLID);
+    os_delay()
+}

+ 15 - 33
user/user_main.c

@@ -68,29 +68,12 @@ static const char* TAG = "main.c";
 //     }
 // }
 
-#define DEBOUNCEDELAY 		10 		// ms
-
-#define SHDN_MUX 	PERIPHS_IO_MUX_GPIO4_U	// GPIO 4
-#define PBA_MUX	 	PERIPHS_IO_MUX_GPIO5_U	// GPIO 5
-#define PBB_MUX  	PERIPHS_IO_MUX_GPIO2_U	// GPIO 2
-#define STATLED_MUX	PERIPHS_IO_MUX_GPIO2_U	// GPIO 0
-
-#define SHDN_FUNC		FUNC_GPIO4
-#define PBA_FUNC 		FUNC_GPIO5
-#define PBB_FUNC 		FUNC_GPIO2
-#define STATLED_FUNC 	FUNC_GPIO0
-
-#define SHDN 		4
-#define PBA 		5
-#define PBB 		2
-#define STATLED		0
-
  void ICACHE_FLASH_ATTR app_init(void)
 {
     uart_init(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1);
-    ESP_LOGI(TAG, "Starting LSM6DS3 Demo\n");
-    ESP_LOGI(TAG, "WiFi Testing \n");
 
+
+    ESP_LOGI(TAG, "LSM6DS3, MAX17043, and LPS25HB Testing...");
     //while(true)
     {
         LSM6DS3_Enable_I2C_Bridge(1);
@@ -106,6 +89,7 @@ static const char* TAG = "main.c";
         system_soft_wdt_feed();
     }
 
+    ESP_LOGI(TAG, "WS2812B Testing...");
     //while(true)
     {
         ESP_LOGI(TAG, "Setting WS2812B");
@@ -116,20 +100,18 @@ static const char* TAG = "main.c";
 
     }
 
-  // wifi_set_opmode(STATION_MODE);
-  // wifi_station_ap_number_set(MAX_APS);
-  // wifi_station_set_auto_connect(true);
-  //
-  // wifi_station_get_ap_info()
-  // wifi_station_ap_change()
-  // wifi_staiton_get_current_ap_id()
-  // wifi_set_sleep_type(MODEM_SLEEP_T);
-  // wifi_enable_gpio_wakeup()
-  //
-  // if (wifi_station_get_config())
-  //   os_printf("zero wifi_config\n");
-  // else
-  //   os_printf("there is a stored config");
+
+    ESP_LOGI(TAG, "Encoder IO Testing...")
+    {
+        Encoder_InitIO();
+        //Encoder_SetSwitchCallback(os_printf("SWITCH_A"), os_printf("SWITCH_B"));
+        //Encoder_SetIMUCallback(os_printf("IMU"));
+        LSM6DS3_EnableMotion();
+        Encoder_EnableIOInterrupts();
+
+    }
+
+
 }
 
 void user_init(void)