Skip to content

Commit

Permalink
Merge pull request #32 from stevenewald/echavemann/capsense-driver
Browse files Browse the repository at this point in the history
Capacitive Touch Controller
  • Loading branch information
stevenewald authored Dec 10, 2024
2 parents 5f846fe + 04d93c2 commit 577792f
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 5 deletions.
46 changes: 46 additions & 0 deletions include/drivers/captouch_controller.hpp
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
11 changes: 10 additions & 1 deletion include/drivers/driver_enums.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "hal/hal_enums.hpp"
#include "util.hpp"

namespace edge::drivers {
Expand All @@ -9,9 +10,14 @@ enum class DriverCommand {
BUTTONS = 2,
TERMINAL_OUTPUT = 3,
TIMER_CANCEL = 4,
CAPTOUCH = 5,
};

enum class DriverSubscribe { NOTIFY_BUTTON_PRESS = 0, TIMER_START = 1 };
enum class DriverSubscribe {
NOTIFY_BUTTON_PRESS = 0,
TIMER_START = 1,
NOTIFY_CAPTOUCH = 2
};

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

Expand All @@ -24,4 +30,7 @@ struct subscribe_callback {
int arg2;
};

aidan::PinPullMode to_pin_pull_mode(drivers::GPIOConfiguration config);
drivers::ButtonState to_cap_sense_state(nrf_gpio_pin_sense_t sense);

} // namespace edge::drivers
3 changes: 1 addition & 2 deletions include/drivers/gpio_pin_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ class GPIOPinEvent {
) :
pin(pin)
{
// TODO: add resistance
aidan::GPIOEventController::get().set_gpio_callback(
pin, aidan::PinPullMode::PUR, callback
pin, to_pin_pull_mode(resistance), callback
);
}

Expand Down
4 changes: 4 additions & 0 deletions include/userlib/syscalls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ void get_button_pressed(
void (*callback)(drivers::ButtonType, drivers::ButtonState)
);

bool get_captouch_pressed();

void subscribe_captouch_pressed(void (*callback)(drivers::ButtonState));

void send_ipc(const char* name, uint32_t message);

void subscribe_ipc(const char* name, void (*callback)(int message));
Expand Down
62 changes: 62 additions & 0 deletions src/drivers/captouch_controller.cpp
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
6 changes: 6 additions & 0 deletions src/drivers/driver_commands.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "drivers/driver_commands.hpp"

#include "drivers/button_driver.hpp"
#include "drivers/captouch_controller.hpp"
#include "drivers/driver_enums.hpp"
#include "drivers/led_matrix_controller.hpp"
#include "drivers/virtual_timer_controller.hpp"
Expand All @@ -25,6 +26,8 @@ etl::optional<int> handle_command(DriverCommand type, int arg1, int arg2, int ar
return ButtonController::get().get_button_pressed(
static_cast<ButtonType>(arg1)
);
case DriverCommand::CAPTOUCH:
return CapsenseController::get().get_captouch_pressed();
case DriverCommand::TIMER_CANCEL:
VirtualTimerController::get().virtual_timer_cancel(
static_cast<uint32_t>(arg1)
Expand All @@ -49,6 +52,9 @@ etl::optional<int> handle_subscribe(
return VirtualTimerController::get().virtual_timer_start(
static_cast<uint32_t>(arg1), callback, 0, arg2
);
case DriverSubscribe::NOTIFY_CAPTOUCH:
CapsenseController::get().subscribe_captouch_press(callback, process_id);
break;
}
return etl::nullopt;
}
Expand Down
34 changes: 34 additions & 0 deletions src/drivers/driver_enums.cpp
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
2 changes: 2 additions & 0 deletions src/entrypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
extern void ipc_part1(void);
extern void ipc_part2(void);
extern void exception_task(void);
extern void captouch_task(void);

int main(void)
{
printf("Starting EdgeOS\n");

edge::FaultHandler::get();

edge::scheduler.add_task(captouch_task);
edge::scheduler.add_task(exception_task);

edge::scheduler.add_task(ipc_part1);
Expand Down
28 changes: 28 additions & 0 deletions src/user_programs/user_program_captouch.cpp
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();
}
}
4 changes: 2 additions & 2 deletions src/user_programs/user_program_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ void callback(uint32_t id)
{
static bool b = false;
edge::userlib::set_led(2, 2, b);
b=!b;
b = !b;
}

void callback2(uint32_t id)
{
static bool b = false;
edge::userlib::set_led(2, 2, b);
b=!b;
b = !b;
}

void exception_task(void)
Expand Down
14 changes: 14 additions & 0 deletions src/userlib/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ bool USER_CODE get_button_pressed(drivers::ButtonType button_type)
RETURN_REGISTER(r0);
}

bool USER_CODE get_captouch_pressed()
{
SET_REGISTER(r0, (int)drivers::DriverCommand::CAPTOUCH);
TRIGGER_SVC(SystemCallType::COMMAND);
RETURN_REGISTER(r0);
}

void USER_CODE subscribe_captouch_pressed(void (*callback)(drivers::ButtonState))
{
SET_REGISTER(r0, (int)drivers::DriverSubscribe::NOTIFY_CAPTOUCH);
SET_REGISTER(r1, (int)callback);
TRIGGER_SVC(SystemCallType::SUBSCRIBE);
}

int USER_CODE get_time_us()
{
SET_REGISTER(r0, (int)drivers::DriverCommand::GET_TIME);
Expand Down

0 comments on commit 577792f

Please sign in to comment.