Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
capsense.h@1:40e5ac1119a6, 2022-07-12 (annotated)
- Committer:
- fionalin
- Date:
- Tue Jul 12 13:02:25 2022 -0400
- Revision:
- 1:40e5ac1119a6
- Parent:
- 0:4e3ad938564e
- Child:
- 2:d9745be6c253
updated to macro format
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fionalin | 0:4e3ad938564e | 1 | #pragma once |
fionalin | 0:4e3ad938564e | 2 | |
fionalin | 0:4e3ad938564e | 3 | #include <mbed.h> |
fionalin | 0:4e3ad938564e | 4 | |
fionalin | 1:40e5ac1119a6 | 5 | /** |
fionalin | 1:40e5ac1119a6 | 6 | * Capacitive-sensing module. |
fionalin | 1:40e5ac1119a6 | 7 | * |
fionalin | 1:40e5ac1119a6 | 8 | * A capacitive sensor needs to be enabled and disabled periodically. |
fionalin | 1:40e5ac1119a6 | 9 | * When the capacitive sensor is enabled, the capacitor charges. |
fionalin | 1:40e5ac1119a6 | 10 | * During this period, try_measure should be called as often as |
fionalin | 1:40e5ac1119a6 | 11 | * possible. When try_measure returns true, the measurement is ready |
fionalin | 1:40e5ac1119a6 | 12 | * to be read. |
fionalin | 1:40e5ac1119a6 | 13 | * |
fionalin | 1:40e5ac1119a6 | 14 | * At some point, toggle needs to be called again to disable the |
fionalin | 1:40e5ac1119a6 | 15 | * capacitive sensor. This gives the capacitor time to discharge. |
fionalin | 1:40e5ac1119a6 | 16 | * Make sure to wait a bit before toggling the sensor back on; |
fionalin | 1:40e5ac1119a6 | 17 | * otherwise, the capacitor might have leftover charge. |
fionalin | 1:40e5ac1119a6 | 18 | */ |
fionalin | 0:4e3ad938564e | 19 | class CapSense { |
fionalin | 0:4e3ad938564e | 20 | public: |
fionalin | 0:4e3ad938564e | 21 | CapSense(PinName measure_pin, PinName square_pin); |
fionalin | 0:4e3ad938564e | 22 | |
fionalin | 0:4e3ad938564e | 23 | bool toggle(); |
fionalin | 0:4e3ad938564e | 24 | bool try_measure(); |
fionalin | 0:4e3ad938564e | 25 | long read_measurement(); |
fionalin | 0:4e3ad938564e | 26 | |
fionalin | 0:4e3ad938564e | 27 | private: |
fionalin | 0:4e3ad938564e | 28 | DigitalOut square_wave; |
fionalin | 0:4e3ad938564e | 29 | DigitalIn cap_sense_measure; |
fionalin | 0:4e3ad938564e | 30 | Timer timer; |
fionalin | 0:4e3ad938564e | 31 | long last_measurement; |
fionalin | 0:4e3ad938564e | 32 | bool measured_this_cycle; |
fionalin | 0:4e3ad938564e | 33 | }; |