-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: YannL <[email protected]>
- Loading branch information
1 parent
416e9b1
commit 32e6d8a
Showing
7 changed files
with
416 additions
and
0 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
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,27 @@ | ||
# Leka - LekaOS | ||
# Copyright 2022 APF France handicap | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
add_library(TouchSensorKit STATIC) | ||
|
||
target_include_directories(TouchSensorKit | ||
PUBLIC | ||
include | ||
) | ||
|
||
target_sources(TouchSensorKit | ||
PRIVATE | ||
source/TouchSensorKit.cpp | ||
) | ||
|
||
target_link_libraries(TouchSensorKit | ||
mbed-os | ||
CoreEventQueue | ||
CoreTouchSensor | ||
) | ||
|
||
if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests") | ||
leka_unit_tests_sources( | ||
tests/TouchSensorKit_test.cpp | ||
) | ||
endif() |
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,21 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
namespace leka { | ||
|
||
enum class Position : uint8_t | ||
{ | ||
ear_left = 0, | ||
ear_right = 1, | ||
belt_left_back = 2, | ||
belt_left_front = 3, | ||
belt_right_back = 4, | ||
belt_right_front = 5, | ||
}; | ||
|
||
} |
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 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <map> | ||
|
||
#include "CoreEventQueue.h" | ||
#include "Position.h" | ||
#include "interface/drivers/TouchSensor.h" | ||
|
||
namespace leka { | ||
|
||
class TouchSensorKit | ||
{ | ||
public: | ||
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 initialize(); | ||
|
||
void setRefreshDelay(std::chrono::milliseconds delay); | ||
void enable(); | ||
void disable(); | ||
|
||
void registerOnSensorTouched(std::function<void(const Position)> const &on_sensor_touched_callback); | ||
void registerOnSensorReleased(std::function<void(const Position)> const &on_sensor_released_callback); | ||
|
||
auto isTouched(Position position) -> bool; | ||
|
||
private: | ||
void run(); | ||
|
||
void setSensitivity(Position position, float value); | ||
|
||
CoreEventQueue _event_queue {}; | ||
std::chrono::milliseconds _refresh_delay {100}; | ||
int _event_id {}; | ||
|
||
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; | ||
|
||
static constexpr auto default_max_sensitivity_value = float {1.F}; | ||
|
||
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 {}; | ||
}; | ||
} // namespace leka |
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,128 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "TouchSensorKit.h" | ||
#include <array> | ||
|
||
#include "rtos/ThisThread.h" | ||
|
||
using namespace leka; | ||
using namespace std::chrono_literals; | ||
|
||
void TouchSensorKit::initialize() | ||
{ | ||
_ear_left.init(); | ||
_ear_right.init(); | ||
_belt_left_back.init(); | ||
_belt_left_front.init(); | ||
_belt_right_back.init(); | ||
_belt_right_front.init(); | ||
|
||
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_queue.dispatch_forever(); | ||
} | ||
|
||
void TouchSensorKit::setRefreshDelay(std::chrono::milliseconds delay) | ||
{ | ||
_refresh_delay = delay; | ||
} | ||
|
||
void TouchSensorKit::enable() | ||
{ | ||
disable(); | ||
_event_id = _event_queue.call_every(_refresh_delay, [this] { run(); }); | ||
} | ||
|
||
void TouchSensorKit::disable() | ||
{ | ||
_event_queue.cancel(_event_id); | ||
} | ||
|
||
void TouchSensorKit::run() | ||
{ | ||
auto constexpr positions = | ||
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 (!is_touched && _previous_is_touched[position] && _on_sensor_released_callback != nullptr) { | ||
_on_sensor_released_callback(position); | ||
} | ||
_previous_is_touched[position] = is_touched; | ||
} | ||
} | ||
|
||
void TouchSensorKit::registerOnSensorTouched(std::function<void(const Position)> const &on_sensor_touched_callback) | ||
{ | ||
_on_sensor_touched_callback = on_sensor_touched_callback; | ||
} | ||
|
||
void TouchSensorKit::registerOnSensorReleased(std::function<void(const Position)> const &on_sensor_released_callback) | ||
{ | ||
_on_sensor_released_callback = on_sensor_released_callback; | ||
} | ||
|
||
auto TouchSensorKit::isTouched(Position position) -> bool | ||
{ | ||
auto read = bool {}; | ||
switch (position) { | ||
case Position::ear_left: | ||
read = _ear_left.read(); | ||
break; | ||
case Position::ear_right: | ||
read = _ear_right.read(); | ||
break; | ||
case Position::belt_left_back: | ||
read = _belt_left_back.read(); | ||
break; | ||
case Position::belt_left_front: | ||
read = _belt_left_front.read(); | ||
break; | ||
case Position::belt_right_back: | ||
read = _belt_right_back.read(); | ||
break; | ||
case Position::belt_right_front: | ||
read = _belt_right_front.read(); | ||
break; | ||
default: | ||
break; | ||
} | ||
return read; | ||
} | ||
|
||
void TouchSensorKit::setSensitivity(Position position, float value) | ||
{ | ||
switch (position) { | ||
case Position::ear_left: | ||
_ear_left.setSensitivity(value); | ||
break; | ||
case Position::ear_right: | ||
_ear_right.setSensitivity(value); | ||
break; | ||
case Position::belt_left_back: | ||
_belt_left_back.setSensitivity(value); | ||
break; | ||
case Position::belt_left_front: | ||
_belt_left_front.setSensitivity(value); | ||
break; | ||
case Position::belt_right_back: | ||
_belt_right_back.setSensitivity(value); | ||
break; | ||
case Position::belt_right_front: | ||
_belt_right_front.setSensitivity(value); | ||
break; | ||
default: | ||
break; | ||
} | ||
} |
Oops, something went wrong.