Skip to content

Commit

Permalink
🚧 Refactor TouchSensorKit
Browse files Browse the repository at this point in the history
Remove isRunning
Remove callbacks onStart and onEnd of Touch read
Rename init by initialize
UT - initialize touch_sensor_kit at SetUp
UT - registerOnSensor Touched/Released
Refactor call of setSensitivity for UT
Some refactor
Replace EventLoop by EventQueue
Rename readAtPosition by isTouched
Use std::map to store previous value for signal edge
Remove reset
Rename setSensitivityAtPosition by setSensitivity
Remove default_min_sensitivity_value
event_queue delay set as parameter
Format + Remove unused libs
Refactor CMakeLists
Rename start by enable and stop by disable
SetRefreshDelay for EventQueue

Co-Authored-By: YannL <yann.locatelli2@gmail.com>
  • Loading branch information
YannLocatelli committed Jan 13, 2023
1 parent a640beb commit c5ad5bf
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 142 deletions.
13 changes: 4 additions & 9 deletions libs/TouchSensorKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@ add_library(TouchSensorKit STATIC)

target_include_directories(TouchSensorKit
PUBLIC
include
include
)

target_sources(TouchSensorKit
PRIVATE
source/TouchSensorKit.cpp
source/TouchSensorKit.cpp
)

target_link_libraries(TouchSensorKit
mbed-os
IOKit
CoreI2C
CoreIOExpander
CoreQDAC
CoreEventQueue
CoreTouchSensor
)

if (${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")

if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")
leka_unit_tests_sources(
tests/TouchSensorKit_test.cpp
)

endif()
42 changes: 19 additions & 23 deletions libs/TouchSensorKit/include/TouchSensorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,45 @@

#pragma once

#include <map>

#include "CoreEventQueue.h"
#include "Position.h"
#include "interface/drivers/TouchSensor.h"
#include "interface/libs/EventLoop.h"

namespace leka {

class TouchSensorKit
{
public:
static constexpr auto kNumberOfSensors = uint8_t {6};

explicit TouchSensorKit(interface::EventLoop &event_loop, interface::TouchSensor &ear_left,
interface::TouchSensor &ear_right, interface::TouchSensor &belt_left_back,
interface::TouchSensor &belt_left_front, interface::TouchSensor &belt_right_back,
interface::TouchSensor &belt_right_front)
: _event_loop(event_loop),
_ear_left(ear_left),
explicit TouchSensorKit(interface::TouchSensor &ear_left, interface::TouchSensor &ear_right,
interface::TouchSensor &belt_left_back, interface::TouchSensor &belt_left_front,
interface::TouchSensor &belt_right_back, interface::TouchSensor &belt_right_front)
: _ear_left(ear_left),
_ear_right(ear_right),
_belt_left_back(belt_left_back),
_belt_left_front(belt_left_front),
_belt_right_back(belt_right_back),
_belt_right_front(belt_right_front) {};

void init();
void start();
void stop();
auto isRunning() -> bool;
void initialize();

void setRefreshDelay(std::chrono::milliseconds delay);
void enable();
void disable();

void registerOnStartRead(std::function<void()> const &on_start_read_callback);
void registerOnSensorTouched(std::function<void(const Position)> const &on_sensor_touched_callback);
void registerOnSensorReleased(std::function<void(const Position)> const &on_sensor_released_callback);
void registerOnEndRead(std::function<void()> const &on_end_read_callback);

auto isTouched(Position position) -> bool;

private:
void run();

auto readAtPosition(Position position) -> bool;
void resetAtPosition(Position position);
void setSensitivityAtPosition(Position position, uint16_t value);
void setSensitivity(Position position, float value);

interface::EventLoop &_event_loop;
bool _running = false;
CoreEventQueue _event_queue {};
std::chrono::milliseconds _refresh_delay {100};

interface::TouchSensor &_ear_left;
interface::TouchSensor &_ear_right;
Expand All @@ -55,11 +52,10 @@ class TouchSensorKit
interface::TouchSensor &_belt_right_front;

static constexpr auto default_max_sensitivity_value = float {1.F};
static constexpr auto default_min_sensitivity_value = float {0.6F};

std::function<void()> _on_start_read_callback {};
std::map<Position, bool> _previous_is_touched {};

std::function<void(const Position)> _on_sensor_touched_callback {};
std::function<void(const Position)> _on_sensor_released_callback {};
std::function<void()> _on_end_read_callback {};
};
} // namespace leka
104 changes: 30 additions & 74 deletions libs/TouchSensorKit/source/TouchSensorKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using namespace leka;
using namespace std::chrono_literals;

void TouchSensorKit::init()
void TouchSensorKit::initialize()
{
_ear_left.init();
_ear_right.init();
Expand All @@ -19,63 +19,50 @@ void TouchSensorKit::init()
_belt_right_back.init();
_belt_right_front.init();

_ear_left.setSensitivity(default_max_sensitivity_value);
_ear_right.setSensitivity(default_max_sensitivity_value);
_belt_left_back.setSensitivity(default_max_sensitivity_value);
_belt_left_front.setSensitivity(default_max_sensitivity_value);
_belt_right_back.setSensitivity(default_max_sensitivity_value);
_belt_right_front.setSensitivity(default_max_sensitivity_value);
setSensitivity(Position::ear_left, default_max_sensitivity_value);
setSensitivity(Position::ear_right, default_max_sensitivity_value);
setSensitivity(Position::belt_left_back, default_max_sensitivity_value);
setSensitivity(Position::belt_left_front, default_max_sensitivity_value);
setSensitivity(Position::belt_right_back, default_max_sensitivity_value);
setSensitivity(Position::belt_right_front, default_max_sensitivity_value);

_event_loop.registerCallback([this] { run(); });
_event_queue.dispatch_forever();
}

void TouchSensorKit::start()
void TouchSensorKit::setRefreshDelay(std::chrono::milliseconds delay)
{
stop();
_running = true;
_event_loop.start();
_refresh_delay = delay;
}

void TouchSensorKit::stop()
void TouchSensorKit::enable()
{
_event_loop.stop();
disable();
_event_queue.call_every(_refresh_delay, [this] { run(); });
}

void TouchSensorKit::disable()
{
_event_queue.cancelLastCall();
}

void TouchSensorKit::run()
{
auto constexpr positions =
std::array<Position, 6> {Position::ear_left, Position::ear_right, Position::belt_left_back,
Position::belt_left_front, Position::belt_right_back, Position::belt_right_front};
bool state = false;
while (isRunning()) {
if (_on_start_read_callback != nullptr) {
_on_start_read_callback();
}
for (Position position: positions) {
state = readAtPosition(position);
if (state && _on_sensor_touched_callback != nullptr) {
_on_sensor_touched_callback(position);
}
if (!state && _on_sensor_released_callback != nullptr) {
_on_sensor_released_callback(position);
}
std::to_array<Position>({Position::ear_left, Position::ear_right, Position::belt_left_back,
Position::belt_left_front, Position::belt_right_back, Position::belt_right_front});

for (Position position: positions) {
auto is_touched = isTouched(position);
if (is_touched && !_previous_is_touched[position] && _on_sensor_touched_callback != nullptr) {
_on_sensor_touched_callback(position);
}
if (_on_end_read_callback != nullptr) {
_on_end_read_callback();
if (!is_touched && _previous_is_touched[position] && _on_sensor_released_callback != nullptr) {
_on_sensor_released_callback(position);
}
_previous_is_touched[position] = is_touched;
}
}

auto TouchSensorKit::isRunning() -> bool
{
return _running;
}

void TouchSensorKit::registerOnStartRead(std::function<void()> const &on_start_read_callback)
{
_on_start_read_callback = on_start_read_callback;
}

void TouchSensorKit::registerOnSensorTouched(std::function<void(const Position)> const &on_sensor_touched_callback)
{
_on_sensor_touched_callback = on_sensor_touched_callback;
Expand All @@ -86,12 +73,7 @@ void TouchSensorKit::registerOnSensorReleased(std::function<void(const Position)
_on_sensor_released_callback = on_sensor_released_callback;
}

void TouchSensorKit::registerOnEndRead(std::function<void()> const &on_end_read_callback)
{
_on_end_read_callback = on_end_read_callback;
}

auto TouchSensorKit::readAtPosition(Position position) -> bool
auto TouchSensorKit::isTouched(Position position) -> bool
{
auto read = bool {};
switch (position) {
Expand Down Expand Up @@ -119,33 +101,7 @@ auto TouchSensorKit::readAtPosition(Position position) -> bool
return read;
}

void TouchSensorKit::resetAtPosition(Position position)
{
switch (position) {
case Position::ear_left:
_ear_left.reset();
break;
case Position::ear_right:
_ear_right.reset();
break;
case Position::belt_left_back:
_belt_left_back.reset();
break;
case Position::belt_left_front:
_belt_left_front.reset();
break;
case Position::belt_right_back:
_belt_right_back.reset();
break;
case Position::belt_right_front:
_belt_right_front.reset();
break;
default:
break;
}
}

void TouchSensorKit::setSensitivityAtPosition(Position position, uint16_t value)
void TouchSensorKit::setSensitivity(Position position, float value)
{
switch (position) {
case Position::ear_left:
Expand Down
Loading

0 comments on commit c5ad5bf

Please sign in to comment.