#include "pots.h" #include "hardware/adc.h" #define SAMPLES 16 #define HYSTERESIS 24 /* in 12-bit counts: 0.75 of a 7-bit step past the step's centre */ static int value[2], raw[2]; void pots_init(void){ adc_init(); adc_gpio_init(26); /* also turns off the digital input and pulls */ adc_gpio_init(27); pots_update(); value[0] = raw[0] >> 5; value[1] = raw[1] >> 5; } void pots_update(void){ int i, k; for(i = 0; i < 2; i++){ int sum = 0, centre, d; adc_select_input((uint)i); for(k = 0; k < SAMPLES; k++) sum += adc_read(); raw[i] = sum / SAMPLES; centre = value[i] * 32 + 16; d = raw[i] - centre; if(d > 16 + HYSTERESIS || d < -16 - HYSTERESIS) value[i] = raw[i] >> 5; } } int pot_value(int pot){ return value[pot]; } int pot_raw(int pot){ return raw[pot]; }