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@2:d9745be6c253, 2022-08-04 (annotated)
- Committer:
- fionalin
- Date:
- Thu Aug 04 15:14:29 2022 -0400
- Revision:
- 2:d9745be6c253
- Parent:
- 1:40e5ac1119a6
hehe forgot to commit
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 | 2:d9745be6c253 | 23 | void start(); |
fionalin | 2:d9745be6c253 | 24 | void reset(); |
fionalin | 0:4e3ad938564e | 25 | bool try_measure(); |
fionalin | 2:d9745be6c253 | 26 | uint32_t read_measurement(); |
fionalin | 0:4e3ad938564e | 27 | |
fionalin | 0:4e3ad938564e | 28 | private: |
fionalin | 0:4e3ad938564e | 29 | DigitalOut square_wave; |
fionalin | 0:4e3ad938564e | 30 | DigitalIn cap_sense_measure; |
fionalin | 0:4e3ad938564e | 31 | Timer timer; |
fionalin | 2:d9745be6c253 | 32 | uint32_t last_measurement; |
fionalin | 0:4e3ad938564e | 33 | bool measured_this_cycle; |
fionalin | 0:4e3ad938564e | 34 | }; |