Touch code cleanup, stop scanning when no touch.

No need to change voltages on touch panel when no touch is detected. This should reduce noise a bit.
This commit is contained in:
Jared Boone 2015-09-02 18:48:38 -07:00
parent f1ca3fe5bb
commit 9a3454d695

View File

@ -79,43 +79,48 @@ static touch::Frame touch_frame;
static uint32_t touch_debounce = 0; static uint32_t touch_debounce = 0;
static uint32_t touch_debounce_mask = (1U << 1) - 1; static uint32_t touch_debounce_mask = (1U << 1) - 1;
static bool touch_stable = false; static bool touch_detected = false;
static bool touch_cycle = false;
static bool touch_update() { static bool touch_update() {
const auto samples = touch::adc::get(); const auto samples = touch::adc::get();
const auto current_phase = touch_pins_configs[touch_phase]; const auto current_phase = touch_pins_configs[touch_phase];
if( current_phase == portapack::IO::TouchPinsConfig::WaitTouch ) { switch(current_phase) {
/* Debounce touches. */ case portapack::IO::TouchPinsConfig::WaitTouch:
const bool touch_raw = (samples.yp < touch::touch_threshold) && (samples.yn < touch::touch_threshold); {
touch_debounce = (touch_debounce << 1) | (touch_raw ? 1U : 0U); /* Debounce touches. */
touch_stable = ((touch_debounce & touch_debounce_mask) == touch_debounce_mask); const bool touch_raw = (samples.yp < touch::touch_threshold) && (samples.yn < touch::touch_threshold);
} else { touch_debounce = (touch_debounce << 1) | (touch_raw ? 1U : 0U);
if( touch_stable ) { touch_detected = ((touch_debounce & touch_debounce_mask) == touch_debounce_mask);
switch(current_phase) { if( !touch_detected && !touch_cycle ) {
case portapack::IO::TouchPinsConfig::SensePressure: return false;
temp_frame.pressure += samples;
break;
case portapack::IO::TouchPinsConfig::SenseX:
temp_frame.x += samples;
break;
case portapack::IO::TouchPinsConfig::SenseY:
temp_frame.y += samples;
break;
default:
break;
} }
} }
break;
case portapack::IO::TouchPinsConfig::SensePressure:
temp_frame.pressure += samples;
break;
case portapack::IO::TouchPinsConfig::SenseX:
temp_frame.x += samples;
break;
case portapack::IO::TouchPinsConfig::SenseY:
temp_frame.y += samples;
break;
default:
break;
} }
touch_phase++; touch_phase++;
if( touch_phase >= touch_pins_configs.size() ) { if( touch_phase >= touch_pins_configs.size() ) {
/* New iteration, calculate values and flag touch event */ /* New iteration, calculate values and flag touch event */
touch_phase = 0; touch_phase = 0;
temp_frame.touch = touch_stable; temp_frame.touch = touch_detected;
touch_cycle = touch_detected;
touch_frame = temp_frame; touch_frame = temp_frame;
temp_frame = {}; temp_frame = {};
return true; return true;