Skip to content

Commit

Permalink
🎨 (libs): Update TouchSensorKit
Browse files Browse the repository at this point in the history
  • Loading branch information
MMyster committed Aug 1, 2022
1 parent 405d88c commit 8f04ee0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 89 deletions.
6 changes: 0 additions & 6 deletions libs/TouchSensorKit/include/TouchSensorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#pragma once

#include "LogKit.h"
#include "external/TouchSensorSystem.h"
#include "interface/drivers/TouchSensor.h"
#include "interface/libs/EventLoop.h"

Expand Down Expand Up @@ -52,10 +50,6 @@ class TouchSensorKit
void resetAtPosition(Position position);
void setSensitivityAtPosition(Position position, uint16_t value, bool saved = false);

static constexpr std::array<Position, kNumberOfSensors> _positions {
Position::ear_left, Position::ear_right, Position::belt_left_back,
Position::belt_left_front, Position::belt_right_back, Position::belt_right_front};

interface::EventLoop &_event_loop;

interface::TouchSensor &_ear_left;
Expand Down
68 changes: 0 additions & 68 deletions libs/TouchSensorKit/include/external/TouchSensorSystem.h

This file was deleted.

19 changes: 16 additions & 3 deletions libs/TouchSensorKit/source/TouchSensorKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "TouchSensorKit.h"
#include <array>

#include "rtos/ThisThread.h"

Expand All @@ -18,6 +19,13 @@ void TouchSensorKit::init()
_belt_left_front.init();
_belt_right_back.init();
_belt_right_front.init();

_ear_left.setSensitivity(default_max_sensitivity_value, true);
_ear_right.setSensitivity(default_max_sensitivity_value, true);
_belt_left_back.setSensitivity(default_max_sensitivity_value, true);
_belt_left_front.setSensitivity(default_max_sensitivity_value, true);
_belt_right_back.setSensitivity(default_max_sensitivity_value, true);
_belt_right_front.setSensitivity(default_max_sensitivity_value, true);
}

void TouchSensorKit::start()
Expand All @@ -28,9 +36,13 @@ void TouchSensorKit::start()

void TouchSensorKit::run()
{
auto const 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};
auto state = std::array<bool, kNumberOfSensors> {};

while (true) {
for (Position position: _positions) {
for (Position position: positions) {
auto previousState = state.at(static_cast<size_t>(position));
auto currentState = readAtPosition(position);
state.at(static_cast<size_t>(position)) = currentState;
Expand All @@ -50,12 +62,12 @@ void TouchSensorKit::stop()
_event_loop.stop();
}

void TouchSensorKit::registerOnSensorTouched(std::function<void(Position)> const &on_sensor_touched_callback)
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(Position)> const &on_sensor_released_callback)
void TouchSensorKit::registerOnSensorReleased(std::function<void(const Position)> const &on_sensor_released_callback)
{
_on_sensor_released_callback = on_sensor_released_callback;
}
Expand All @@ -82,6 +94,7 @@ auto TouchSensorKit::readAtPosition(Position position) -> bool
return _belt_right_front.read();
break;
default:
return false;
break;
}
}
Expand Down
19 changes: 7 additions & 12 deletions libs/TouchSensorKit/tests/TouchSensorKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ TEST_F(TouchSensorTest, init)
EXPECT_CALL(mock_belt_right_front, init).Times(1);
EXPECT_CALL(mock_belt_right_back, init).Times(1);

EXPECT_CALL(mock_ear_left, setSensitivity).Times(1);
EXPECT_CALL(mock_ear_right, setSensitivity).Times(1);
EXPECT_CALL(mock_belt_left_front, setSensitivity).Times(1);
EXPECT_CALL(mock_belt_left_back, setSensitivity).Times(1);
EXPECT_CALL(mock_belt_right_front, setSensitivity).Times(1);
EXPECT_CALL(mock_belt_right_back, setSensitivity).Times(1);

touch_sensor_kit.init();
}

Expand All @@ -66,18 +73,6 @@ TEST_F(TouchSensorTest, stop)
{
touch_sensor_kit.stop();
}
TEST_F(TouchSensorTest, calibrate)
{
const auto default_max_sensitivity_value = uint16_t {0x0FF0};
const auto default_min_sensitivity_value = uint16_t {0x000F};
const auto step = uint16_t {10};

const auto count = static_cast<uint16_t>((default_max_sensitivity_value - default_min_sensitivity_value) / step);

EXPECT_CALL(mock_ear_left, read).Times(count + 1);
EXPECT_CALL(mock_ear_left, setSensitivity).Times(count + 3);
touch_sensor_kit.calibrate(Position::ear_left);
}

TEST_F(TouchSensorTest, registerOnSensorTouched)
{
Expand Down

0 comments on commit 8f04ee0

Please sign in to comment.