diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/inc/adc.h b/inc/adc.h --- a/inc/adc.h +++ b/inc/adc.h @@ -20,6 +20,7 @@ enum _adc_reading_id }; uint16_t get_viout_counts(void); +uint16_t get_vgiout_counts(void); uint16_t get_vbat_counts(void); /* USER CODE END Includes */ diff --git a/inc/buttons.h b/inc/buttons.h new file mode 100644 --- /dev/null +++ b/inc/buttons.h @@ -0,0 +1,21 @@ +/* + * buttons.h + * + * Created on: Oct 22, 2016 + * Author: Nicholas Orlando + */ + +#ifndef BUTTONS_H_ +#define BUTTONS_H_ + +enum _btn_state_id +{ + NOT_PRESSED = 0, + PRESSED = 1, + HELD_SHORT = 2, + HELD_LONG = 3, +}; + +void freaking_debounce(void); + +#endif /* BUTTONS_H_ */ diff --git a/inc/gpio.h b/inc/gpio.h --- a/inc/gpio.h +++ b/inc/gpio.h @@ -16,8 +16,12 @@ #define SW_A_Pin GPIO_PIN_6 #define SW_A_GPIO_Port GPIOA + +// Define button push #define SW_BTN_Pin GPIO_PIN_8 #define SW_BTN_GPIO_Port GPIOA +#define SW_BTN SW_BTN_GPIO_Port , SW_BTN_Pin + #define SW_C_Pin GPIO_PIN_9 #define SW_C_GPIO_Port GPIOA #define SW_D_Pin GPIO_PIN_10 diff --git a/inc/interrupts.h b/inc/interrupts.h --- a/inc/interrupts.h +++ b/inc/interrupts.h @@ -14,4 +14,7 @@ void SDADC1_IRQHandler(void); void SDADC2_IRQHandler(void); + + + #endif diff --git a/src/adc.c b/src/adc.c --- a/src/adc.c +++ b/src/adc.c @@ -29,6 +29,11 @@ uint16_t get_viout_counts(void) return adc_readings[ADC_CHAN_VIOUT]; } +uint16_t get_vgiout_counts(void) +{ + return adc_readings[ADC_CHAN_GAIN_VIOUT]; +} + uint16_t get_vbat_counts(void) { return adc_readings[ADC_CHAN_VBAT]; diff --git a/src/buttons.c b/src/buttons.c new file mode 100644 --- /dev/null +++ b/src/buttons.c @@ -0,0 +1,76 @@ +/* + * buttons.c + * + * Created on: Oct 22, 2016 + * Author: Nicholas Orlando + */ + +#include "stm32f3xx_hal.h" +#include "stm32f3xx.h" +#include "gpio.h" +#include "ssd1306.h" +#include "buttons.h" +#include + + +uint32_t last_button_debounce_time = 0; // the system time at which the button values were last checked +uint32_t debounce_resolution = 5; // period in which to check pin state in milliseconds +float filter_fraction = 0.2; // a number between 0 and 1, which adjusts the heaviness of the filter. + // 0 = never changes and 1 = no filter at all. +uint8_t l_thresh = 20; //lower threshold for when then button is considered pressed vs not pressed. +uint8_t u_thresh = 80; //upper threshold for when then button is considered pressed vs not pressed. + +uint8_t temp_counter = 0; // temporary counter for testing purposes + +float sw_btn_avg = 0; // some sort of running average +uint8_t sw_btn_state = NOT_PRESSED; // the state of the pin after filtering +uint8_t sw_btn_old_state = NOT_PRESSED; // the state of the pin after filtering + +void freaking_debounce(void) +{ + if(HAL_GetTick() - last_button_debounce_time > debounce_resolution) + { + char buffer[256]; // needed for writing stuff to screen + + //averaging to filter button presses + if(HAL_GPIO_ReadPin(SW_BTN) == GPIO_PIN_RESET) + { + sw_btn_avg = sw_btn_avg + filter_fraction * (100 - sw_btn_avg); + } + else + { + sw_btn_avg = sw_btn_avg + filter_fraction * (0 - sw_btn_avg); + } + + // check to see if the btn average value has crossed a threshold + if(sw_btn_avg < l_thresh) + { + sw_btn_state = NOT_PRESSED; +// snprintf(buffer, 256, "NOT_PRESSED"); +// ssd1306_drawstring(buffer, 1, 0); + } + if(sw_btn_avg > u_thresh) + { + sw_btn_state = PRESSED; +// snprintf(buffer, 256, "PRESSED "); +// ssd1306_drawstring(buffer, 1, 0); + } + + // do something when state has changed + if((sw_btn_state == PRESSED) && (sw_btn_old_state == NOT_PRESSED)) + { + temp_counter++; + snprintf(buffer, 256, "%i", temp_counter); + ssd1306_drawstring(buffer, 1, 0); + } + + +// snprintf(buffer, 256, "sw-btn-avg: %.1f", sw_btn_avg); +// ssd1306_drawstring(buffer, 2, 0); + + // save previous button states + sw_btn_old_state = sw_btn_state; + + last_button_debounce_time = HAL_GetTick(); + } +} diff --git a/src/flash.c b/src/flash.c --- a/src/flash.c +++ b/src/flash.c @@ -14,8 +14,6 @@ settings_t settings; // Initialize flash and restore settings void flash_init(void) { -// settings.values.can_id = CAN_ID_DEFAULT; -// settings.values.tx_period = CAN_TRANSMISSION_PERIOD_DEFAULT; flash_restoresettings(); } @@ -67,6 +65,11 @@ void flash_restoresettings(void) settings.data[readctr] = eeprom_emulation[readctr]; } } + // No data in flash! Set defaults here + else + { + settings.values.can_id = 22; + } } diff --git a/src/interrupts.c b/src/interrupts.c --- a/src/interrupts.c +++ b/src/interrupts.c @@ -6,8 +6,8 @@ #include "stm32f3xx.h" #include "interrupts.h" +#include "gpio.h" -#include "gpio.h" // Systick interrupt void SysTick_Handler(void) @@ -49,12 +49,17 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPI { case SW_BTN_Pin: { - if(HAL_GetTick() > last_button_press + 100) - { - HAL_GPIO_TogglePin(LED_RED); - HAL_GPIO_TogglePin(GATE_DRIVE); - last_button_press = HAL_GetTick(); - } +// if(HAL_GetTick() > last_button_press + 100) +// { +// HAL_GPIO_TogglePin(LED_RED); +// HAL_GPIO_TogglePin(GATE_DRIVE); +// last_button_press = HAL_GetTick(); +// } } break; } } + + + + + diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,8 @@ #include "stdio.h" #include "adc.h" #include "dma.h" +#include "interrupts.h" +#include "buttons.h" int main(void) { @@ -27,35 +29,45 @@ int main(void) ssd1306_clearscreen(); adc_init(); - // ssd1306_drawstring(const char *dataPtr, unsigned char row, unsigned char xPos) - ssd1306_drawstring("[ ProtoFuse ]", 0, 0); - ssd1306_drawstring("HW v1.0 SW v0.1", 1, 0); - - uint16_t temp_counter = 0; - - flash_init(); watchdog_init(); + //just some example code for getting flash values +// flash_getsettings()->values.can_id = 67; +// +// if(flash_getsettings()->values.can_id == 12); + +// ssd1306_drawstring(const char *dataPtr, unsigned char row, unsigned char xPos) +// ssd1306_drawstring("[ ProtoFuse ]", 0, 0); + + float temp_counter = 0; + // Software timers - uint32_t last_blink_time = HAL_GetTick(); + uint32_t last_screen_update_time = HAL_GetTick(); while (1) { + // function that checks all the buttons + freaking_debounce(); + // Grab and transmit data - if(HAL_GetTick() - last_blink_time > 100) + if(HAL_GetTick() - last_screen_update_time > 100) { char buffer[256]; - // added stdio.h to fix implicit declaration error - // changed battery_adc_count from int32_t to int - snprintf(buffer, 256, "Counter: %u", temp_counter); - ssd1306_drawstring(buffer, 2, 0); - snprintf(buffer, 256, "i count: %u", get_viout_counts()); - ssd1306_drawstring(buffer, 3, 0); -// HAL_GPIO_TogglePin(LED_RED); - temp_counter++; - last_blink_time = HAL_GetTick(); + snprintf(buffer, 256, "Counter: %.1f", temp_counter); + ssd1306_drawstring(buffer, 0, 0); +// snprintf(buffer, 256, "VBATT CNTS: %u", get_vbat_counts()); +// ssd1306_drawstring(buffer, 1, 0); +// snprintf(buffer, 256, "IADC CNTS: %u", get_viout_counts()); +// ssd1306_drawstring(buffer, 2, 0); +// snprintf(buffer, 256, "GIADC CNTS: %u", get_vgiout_counts()); +// ssd1306_drawstring(buffer, 3, 0); + + + + temp_counter = temp_counter + 0.1; + last_screen_update_time = HAL_GetTick(); } watchdog_feed();