-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from stevenewald/echavemann/capsense-driver
Capacitive Touch Controller
- Loading branch information
Showing
11 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#include "drivers/driver_enums.hpp" | ||
#include "drivers/gpio_pin.hpp" | ||
#include "drivers/gpio_pin_event.hpp" | ||
#include "microbit_v2.h" | ||
#include "nrf_gpio.h" | ||
#include "scheduler/user_callback_storage.hpp" | ||
#include "util.hpp" | ||
|
||
namespace edge::drivers { | ||
|
||
class CapsenseController { | ||
GPIOPin touch_logo{TOUCH_LOGO, GPIOConfiguration::IN_NORES}; | ||
|
||
UserCallbackStorage subscriptions; | ||
|
||
bool touched; | ||
|
||
bool prev_touched; | ||
|
||
uint32_t time_test_started; | ||
|
||
GPIOPinEvent event; | ||
|
||
CapsenseController(); | ||
~CapsenseController() = default; | ||
|
||
public: | ||
CapsenseController(const CapsenseController&) = delete; | ||
CapsenseController(CapsenseController&&) = delete; | ||
CapsenseController& operator=(const CapsenseController&) = delete; | ||
CapsenseController& operator=(CapsenseController&&) = delete; | ||
|
||
static CapsenseController& get(); | ||
|
||
void subscribe_captouch_press(ProcessCallbackPtr callback, uint8_t process_id); | ||
|
||
bool get_captouch_pressed(); | ||
|
||
void handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin); | ||
|
||
void start_capacitive_test(); | ||
}; | ||
|
||
} // namespace edge::drivers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "drivers/captouch_controller.hpp" | ||
|
||
#include "config.hpp" | ||
#include "microbit_v2.h" | ||
#include "nrf_gpio.h" | ||
|
||
namespace edge::drivers { | ||
|
||
CapsenseController::CapsenseController() : | ||
touched(false), prev_touched(false), | ||
event{ | ||
TOUCH_LOGO, GPIOConfiguration::IN_NORES, | ||
aidan::GPIOEventController::GPIOEventCallback::create< | ||
CapsenseController, &CapsenseController::handle_gpio_interrupt>(*this) | ||
} | ||
{ | ||
touch_logo.clear(); | ||
nrf_gpio_cfg( | ||
TOUCH_LOGO, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, | ||
NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE | ||
); | ||
}; | ||
|
||
CapsenseController& CapsenseController::get() | ||
{ | ||
static CapsenseController capsenseController; | ||
return capsenseController; | ||
} | ||
|
||
void CapsenseController::subscribe_captouch_press( | ||
ProcessCallbackPtr callback, uint8_t process_id | ||
) | ||
{ | ||
subscriptions.set_callback(process_id, callback); | ||
} | ||
|
||
bool CapsenseController::get_captouch_pressed() | ||
{ | ||
return touched; | ||
} | ||
|
||
void CapsenseController::handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin) | ||
{ | ||
if (sense == NRF_GPIO_PIN_SENSE_LOW) { | ||
touched = true; | ||
} | ||
else if (sense == NRF_GPIO_PIN_SENSE_HIGH) { | ||
touched = false; | ||
} | ||
if ((prev_touched != touched)) { | ||
for (int process_id = 0; process_id < MAX_PROCESSES; ++process_id) { | ||
if (subscriptions.has_callback(process_id)) { | ||
subscriptions.call_callback( | ||
process_id, static_cast<int>(to_cap_sense_state(sense)) | ||
); | ||
} | ||
} | ||
} | ||
prev_touched = touched; | ||
} | ||
|
||
} // namespace edge::drivers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "drivers/driver_enums.hpp" | ||
|
||
#include "hal/hal_enums.hpp" | ||
#include "util.hpp" | ||
|
||
namespace edge::drivers { | ||
aidan::PinPullMode to_pin_pull_mode(GPIOConfiguration configuration) | ||
{ | ||
switch (configuration) { | ||
case GPIOConfiguration::IN_NORES: | ||
return aidan::PinPullMode::NONE; | ||
case GPIOConfiguration::IN_PDR: | ||
return aidan::PinPullMode::PDR; | ||
case GPIOConfiguration::IN_PUR: | ||
return aidan::PinPullMode::PUR; | ||
default: | ||
panic("Unexpected GPIO Configuration conversion to PinPullMode."); | ||
} | ||
} | ||
|
||
drivers::ButtonState to_cap_sense_state(nrf_gpio_pin_sense_t sense) | ||
{ | ||
if (sense == NRF_GPIO_PIN_SENSE_LOW) { | ||
return drivers::ButtonState::DOWN; | ||
} | ||
else if (sense == NRF_GPIO_PIN_SENSE_HIGH) { | ||
return drivers::ButtonState::UP; | ||
} | ||
else { | ||
panic("Unexpected nrf_gpio_pin_sense"); | ||
} | ||
} | ||
|
||
} // namespace edge::drivers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "drivers/driver_enums.hpp" | ||
#include "hal/gpio_wrapper.hpp" | ||
#include "nrf_delay.h" | ||
#include "userlib/syscalls.hpp" | ||
|
||
void callback(edge::drivers::ButtonState a) | ||
{ | ||
if (a == edge::drivers::ButtonState::UP) { | ||
edge::userlib::debug_print("CALLBACK: Finger lifted.\n"); | ||
} | ||
else { | ||
edge::userlib::debug_print("CALLBACK: Finger pressed.\n"); | ||
} | ||
} | ||
|
||
void captouch_task(void) | ||
{ | ||
edge::userlib::subscribe_captouch_pressed(callback); | ||
while (1) { | ||
if (edge::userlib::get_captouch_pressed()) { | ||
// edge::userlib::debug_print("pressed\n"); | ||
} | ||
else { | ||
// edge::userlib::debug_print("not pressed\n"); | ||
} | ||
edge::userlib::yield(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters