From 28ed640958cf01c3cf0dcea3bf9a5b58ecdc03f1 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 13 Dec 2019 08:51:23 -0800 Subject: [PATCH 1/2] Trying out software cap-sense --- libs/core/pinsDigital.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/libs/core/pinsDigital.cpp b/libs/core/pinsDigital.cpp index 1db3643b7..a4cc6444b 100644 --- a/libs/core/pinsDigital.cpp +++ b/libs/core/pinsDigital.cpp @@ -101,6 +101,27 @@ void onEvent(DigitalInOutPin pin, PinEvent event, Action body) { } } +/** + * Measure pin capacitance (via discharge time). + */ +//% +int readCapacitance(DigitalInOutPin pin) { + pin->setDigitalValue(1); + pin->getAnalogValue(); + sleep_us(5); + return pin->getAnalogValue(); + + /* + sleep_us(50); + auto t0 = current_time_us(); + int n = 0; + while (current_time_us() - t0 < 3000 && pin->getDigitalValue()) + n++; + int diff = current_time_us() - t0; + return diff; + */ +} + /** * Return the duration of a pulse in microseconds * @param name the pin which measures the pulse From 14028c9267002d4d8b1411649da16edd927d586c Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Thu, 23 Apr 2020 18:44:28 -0700 Subject: [PATCH 2/2] Fix cap sense values --- libs/core/pinsDigital.cpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/libs/core/pinsDigital.cpp b/libs/core/pinsDigital.cpp index a4cc6444b..b51af4756 100644 --- a/libs/core/pinsDigital.cpp +++ b/libs/core/pinsDigital.cpp @@ -102,24 +102,16 @@ void onEvent(DigitalInOutPin pin, PinEvent event, Action body) { } /** - * Measure pin capacitance (via discharge time). + * Measure pin capacitance. */ //% int readCapacitance(DigitalInOutPin pin) { + // on STM32F4 this requires 2M pulldown + // other chips to be tested pin->setDigitalValue(1); - pin->getAnalogValue(); - sleep_us(5); - return pin->getAnalogValue(); - - /* + pin->getDigitalValue(PullMode::None); sleep_us(50); - auto t0 = current_time_us(); - int n = 0; - while (current_time_us() - t0 < 3000 && pin->getDigitalValue()) - n++; - int diff = current_time_us() - t0; - return diff; - */ + return pin->getAnalogValue(); } /**