Skip to content

Commit 577792f

Browse files
authored
Merge pull request #32 from stevenewald/echavemann/capsense-driver
Capacitive Touch Controller
2 parents 5f846fe + 04d93c2 commit 577792f

File tree

11 files changed

+209
-5
lines changed

11 files changed

+209
-5
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include "drivers/driver_enums.hpp"
4+
#include "drivers/gpio_pin.hpp"
5+
#include "drivers/gpio_pin_event.hpp"
6+
#include "microbit_v2.h"
7+
#include "nrf_gpio.h"
8+
#include "scheduler/user_callback_storage.hpp"
9+
#include "util.hpp"
10+
11+
namespace edge::drivers {
12+
13+
class CapsenseController {
14+
GPIOPin touch_logo{TOUCH_LOGO, GPIOConfiguration::IN_NORES};
15+
16+
UserCallbackStorage subscriptions;
17+
18+
bool touched;
19+
20+
bool prev_touched;
21+
22+
uint32_t time_test_started;
23+
24+
GPIOPinEvent event;
25+
26+
CapsenseController();
27+
~CapsenseController() = default;
28+
29+
public:
30+
CapsenseController(const CapsenseController&) = delete;
31+
CapsenseController(CapsenseController&&) = delete;
32+
CapsenseController& operator=(const CapsenseController&) = delete;
33+
CapsenseController& operator=(CapsenseController&&) = delete;
34+
35+
static CapsenseController& get();
36+
37+
void subscribe_captouch_press(ProcessCallbackPtr callback, uint8_t process_id);
38+
39+
bool get_captouch_pressed();
40+
41+
void handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin);
42+
43+
void start_capacitive_test();
44+
};
45+
46+
} // namespace edge::drivers

include/drivers/driver_enums.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "hal/hal_enums.hpp"
34
#include "util.hpp"
45

56
namespace edge::drivers {
@@ -9,9 +10,14 @@ enum class DriverCommand {
910
BUTTONS = 2,
1011
TERMINAL_OUTPUT = 3,
1112
TIMER_CANCEL = 4,
13+
CAPTOUCH = 5,
1214
};
1315

14-
enum class DriverSubscribe { NOTIFY_BUTTON_PRESS = 0, TIMER_START = 1 };
16+
enum class DriverSubscribe {
17+
NOTIFY_BUTTON_PRESS = 0,
18+
TIMER_START = 1,
19+
NOTIFY_CAPTOUCH = 2
20+
};
1521

1622
enum class GPIOConfiguration { OUT, IN_NORES, IN_PDR, IN_PUR };
1723

@@ -24,4 +30,7 @@ struct subscribe_callback {
2430
int arg2;
2531
};
2632

33+
aidan::PinPullMode to_pin_pull_mode(drivers::GPIOConfiguration config);
34+
drivers::ButtonState to_cap_sense_state(nrf_gpio_pin_sense_t sense);
35+
2736
} // namespace edge::drivers

include/drivers/gpio_pin_event.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ class GPIOPinEvent {
1414
) :
1515
pin(pin)
1616
{
17-
// TODO: add resistance
1817
aidan::GPIOEventController::get().set_gpio_callback(
19-
pin, aidan::PinPullMode::PUR, callback
18+
pin, to_pin_pull_mode(resistance), callback
2019
);
2120
}
2221

include/userlib/syscalls.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ void get_button_pressed(
2121
void (*callback)(drivers::ButtonType, drivers::ButtonState)
2222
);
2323

24+
bool get_captouch_pressed();
25+
26+
void subscribe_captouch_pressed(void (*callback)(drivers::ButtonState));
27+
2428
void send_ipc(const char* name, uint32_t message);
2529

2630
void subscribe_ipc(const char* name, void (*callback)(int message));

src/drivers/captouch_controller.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "drivers/captouch_controller.hpp"
2+
3+
#include "config.hpp"
4+
#include "microbit_v2.h"
5+
#include "nrf_gpio.h"
6+
7+
namespace edge::drivers {
8+
9+
CapsenseController::CapsenseController() :
10+
touched(false), prev_touched(false),
11+
event{
12+
TOUCH_LOGO, GPIOConfiguration::IN_NORES,
13+
aidan::GPIOEventController::GPIOEventCallback::create<
14+
CapsenseController, &CapsenseController::handle_gpio_interrupt>(*this)
15+
}
16+
{
17+
touch_logo.clear();
18+
nrf_gpio_cfg(
19+
TOUCH_LOGO, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT,
20+
NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE
21+
);
22+
};
23+
24+
CapsenseController& CapsenseController::get()
25+
{
26+
static CapsenseController capsenseController;
27+
return capsenseController;
28+
}
29+
30+
void CapsenseController::subscribe_captouch_press(
31+
ProcessCallbackPtr callback, uint8_t process_id
32+
)
33+
{
34+
subscriptions.set_callback(process_id, callback);
35+
}
36+
37+
bool CapsenseController::get_captouch_pressed()
38+
{
39+
return touched;
40+
}
41+
42+
void CapsenseController::handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin)
43+
{
44+
if (sense == NRF_GPIO_PIN_SENSE_LOW) {
45+
touched = true;
46+
}
47+
else if (sense == NRF_GPIO_PIN_SENSE_HIGH) {
48+
touched = false;
49+
}
50+
if ((prev_touched != touched)) {
51+
for (int process_id = 0; process_id < MAX_PROCESSES; ++process_id) {
52+
if (subscriptions.has_callback(process_id)) {
53+
subscriptions.call_callback(
54+
process_id, static_cast<int>(to_cap_sense_state(sense))
55+
);
56+
}
57+
}
58+
}
59+
prev_touched = touched;
60+
}
61+
62+
} // namespace edge::drivers

src/drivers/driver_commands.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "drivers/driver_commands.hpp"
22

33
#include "drivers/button_driver.hpp"
4+
#include "drivers/captouch_controller.hpp"
45
#include "drivers/driver_enums.hpp"
56
#include "drivers/led_matrix_controller.hpp"
67
#include "drivers/virtual_timer_controller.hpp"
@@ -25,6 +26,8 @@ etl::optional<int> handle_command(DriverCommand type, int arg1, int arg2, int ar
2526
return ButtonController::get().get_button_pressed(
2627
static_cast<ButtonType>(arg1)
2728
);
29+
case DriverCommand::CAPTOUCH:
30+
return CapsenseController::get().get_captouch_pressed();
2831
case DriverCommand::TIMER_CANCEL:
2932
VirtualTimerController::get().virtual_timer_cancel(
3033
static_cast<uint32_t>(arg1)
@@ -49,6 +52,9 @@ etl::optional<int> handle_subscribe(
4952
return VirtualTimerController::get().virtual_timer_start(
5053
static_cast<uint32_t>(arg1), callback, 0, arg2
5154
);
55+
case DriverSubscribe::NOTIFY_CAPTOUCH:
56+
CapsenseController::get().subscribe_captouch_press(callback, process_id);
57+
break;
5258
}
5359
return etl::nullopt;
5460
}

src/drivers/driver_enums.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "drivers/driver_enums.hpp"
2+
3+
#include "hal/hal_enums.hpp"
4+
#include "util.hpp"
5+
6+
namespace edge::drivers {
7+
aidan::PinPullMode to_pin_pull_mode(GPIOConfiguration configuration)
8+
{
9+
switch (configuration) {
10+
case GPIOConfiguration::IN_NORES:
11+
return aidan::PinPullMode::NONE;
12+
case GPIOConfiguration::IN_PDR:
13+
return aidan::PinPullMode::PDR;
14+
case GPIOConfiguration::IN_PUR:
15+
return aidan::PinPullMode::PUR;
16+
default:
17+
panic("Unexpected GPIO Configuration conversion to PinPullMode.");
18+
}
19+
}
20+
21+
drivers::ButtonState to_cap_sense_state(nrf_gpio_pin_sense_t sense)
22+
{
23+
if (sense == NRF_GPIO_PIN_SENSE_LOW) {
24+
return drivers::ButtonState::DOWN;
25+
}
26+
else if (sense == NRF_GPIO_PIN_SENSE_HIGH) {
27+
return drivers::ButtonState::UP;
28+
}
29+
else {
30+
panic("Unexpected nrf_gpio_pin_sense");
31+
}
32+
}
33+
34+
} // namespace edge::drivers

src/entrypoint.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
extern void ipc_part1(void);
1010
extern void ipc_part2(void);
1111
extern void exception_task(void);
12+
extern void captouch_task(void);
1213

1314
int main(void)
1415
{
1516
printf("Starting EdgeOS\n");
1617

1718
edge::FaultHandler::get();
1819

20+
edge::scheduler.add_task(captouch_task);
1921
edge::scheduler.add_task(exception_task);
2022

2123
edge::scheduler.add_task(ipc_part1);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "drivers/driver_enums.hpp"
2+
#include "hal/gpio_wrapper.hpp"
3+
#include "nrf_delay.h"
4+
#include "userlib/syscalls.hpp"
5+
6+
void callback(edge::drivers::ButtonState a)
7+
{
8+
if (a == edge::drivers::ButtonState::UP) {
9+
edge::userlib::debug_print("CALLBACK: Finger lifted.\n");
10+
}
11+
else {
12+
edge::userlib::debug_print("CALLBACK: Finger pressed.\n");
13+
}
14+
}
15+
16+
void captouch_task(void)
17+
{
18+
edge::userlib::subscribe_captouch_pressed(callback);
19+
while (1) {
20+
if (edge::userlib::get_captouch_pressed()) {
21+
// edge::userlib::debug_print("pressed\n");
22+
}
23+
else {
24+
// edge::userlib::debug_print("not pressed\n");
25+
}
26+
edge::userlib::yield();
27+
}
28+
}

src/user_programs/user_program_exception.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ void callback(uint32_t id)
66
{
77
static bool b = false;
88
edge::userlib::set_led(2, 2, b);
9-
b=!b;
9+
b = !b;
1010
}
1111

1212
void callback2(uint32_t id)
1313
{
1414
static bool b = false;
1515
edge::userlib::set_led(2, 2, b);
16-
b=!b;
16+
b = !b;
1717
}
1818

1919
void exception_task(void)

0 commit comments

Comments
 (0)