Fiona Lin / Mbed OS cap_sense

capsense.h

Committer:
fionalin
Date:
2022-07-12
Revision:
1:40e5ac1119a6
Parent:
0:4e3ad938564e
Child:
2:d9745be6c253

File content as of revision 1:40e5ac1119a6:

#pragma once

#include <mbed.h>

/**
 * Capacitive-sensing module.
 *
 * A capacitive sensor needs to be enabled and disabled periodically.
 * When the capacitive sensor is enabled, the capacitor charges.
 * During this period, try_measure should be called as often as
 * possible. When try_measure returns true, the measurement is ready
 * to be read.
 *
 * At some point, toggle needs to be called again to disable the
 * capacitive sensor. This gives the capacitor time to discharge.
 * Make sure to wait a bit before toggling the sensor back on;
 * otherwise, the capacitor might have leftover charge.
 */
class CapSense {
    public:
    CapSense(PinName measure_pin, PinName square_pin);

    bool toggle();
    bool try_measure();
    long read_measurement();
    
    private:
    DigitalOut square_wave;
    DigitalIn cap_sense_measure;
    Timer timer;
    long last_measurement;
    bool measured_this_cycle;
};