Files @ a710d1e0fc2a
Branch filter:

Location: therm-ng/src/buttons.c

Ethan Zonca
Import from old therm: menus working
/*
 * 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 <math.h>


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();
	}
}