-
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.
🔀 Merge branch 'mmyster/feature/add-core-touch-sensor' into develop
- Loading branch information
Showing
8 changed files
with
254 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,30 @@ | ||
# Leka - LekaOS | ||
# Copyright 2022 APF France handicap | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
add_library(CoreTouchSensor STATIC) | ||
|
||
target_include_directories(CoreTouchSensor | ||
PUBLIC | ||
include | ||
) | ||
|
||
target_sources(CoreTouchSensor | ||
PRIVATE | ||
source/CoreTouchSensor.cpp | ||
) | ||
|
||
target_link_libraries(CoreTouchSensor | ||
PRIVATE | ||
mbed-os | ||
CoreI2C | ||
CoreIOExpander | ||
CoreQDAC | ||
IOKit | ||
) | ||
|
||
if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests") | ||
leka_unit_tests_sources( | ||
tests/CoreTouchSensor_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,48 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "drivers/interfaces/InterfaceDigitalIn.h" | ||
#include "drivers/interfaces/InterfaceDigitalOut.h" | ||
|
||
#include "interface/drivers/QDAC.h" | ||
#include "interface/drivers/TouchSensor.h" | ||
|
||
namespace leka { | ||
|
||
class CoreTouchSensor : public interface::TouchSensor | ||
{ | ||
public: | ||
static constexpr auto default_max_sensitivity_value = uint16_t {0x0FFF}; | ||
static constexpr auto default_min_sensitivity_value = uint16_t {0x0000}; | ||
|
||
explicit CoreTouchSensor(mbed::interface::DigitalIn &detect_pin, mbed::interface::DigitalOut &power_mode_pin, | ||
interface::QDAC &dac, uint8_t channel) | ||
: _detect_pin(detect_pin), _power_mode_pin(power_mode_pin), _sensitivity_pin({dac, channel}) {}; | ||
void init() final; | ||
auto read() -> bool final; | ||
void reset() final; | ||
void setSensitivity(uint16_t value) final; | ||
|
||
private: | ||
enum class PowerMode : uint8_t | ||
{ | ||
low = 0, | ||
normal = 1 | ||
}; | ||
void setPowerMode(PowerMode power_mode); | ||
|
||
mbed::interface::DigitalIn &_detect_pin; | ||
mbed::interface::DigitalOut &_power_mode_pin; | ||
struct AnalogOut { | ||
interface::QDAC &dac; | ||
uint8_t channel; | ||
}; | ||
AnalogOut _sensitivity_pin; | ||
|
||
bool _state {}; | ||
}; | ||
|
||
} // 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,47 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "CoreTouchSensor.h" | ||
|
||
#include "rtos/ThisThread.h" | ||
|
||
#include "MathUtils.h" | ||
|
||
using namespace leka; | ||
using namespace std::chrono_literals; | ||
|
||
void CoreTouchSensor::init() | ||
{ | ||
_detect_pin.mode(PinMode::PullUp); | ||
setPowerMode(PowerMode::normal); | ||
_sensitivity_pin.dac.init(); | ||
} | ||
|
||
auto CoreTouchSensor::read() -> bool | ||
{ | ||
_state = (1 == _detect_pin.read()); | ||
return _state; | ||
} | ||
|
||
void CoreTouchSensor::reset() | ||
{ | ||
setPowerMode(PowerMode::low); | ||
rtos::ThisThread::sleep_for(110ms); | ||
setPowerMode(PowerMode::normal); | ||
rtos::ThisThread::sleep_for(1ms); | ||
} | ||
|
||
void CoreTouchSensor::setSensitivity(uint16_t value) | ||
{ | ||
auto inverted_value = | ||
utils::math::map<uint16_t, uint16_t>(value, default_min_sensitivity_value, default_max_sensitivity_value, | ||
default_max_sensitivity_value, default_min_sensitivity_value); | ||
_sensitivity_pin.dac.write(_sensitivity_pin.channel, inverted_value); | ||
} | ||
|
||
void CoreTouchSensor::setPowerMode(PowerMode power_mode) | ||
{ | ||
auto pm = static_cast<uint8_t>(power_mode); | ||
_power_mode_pin.write(pm); | ||
} |
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,84 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "CoreTouchSensor.h" | ||
|
||
#include "IOKit/DigitalIn.h" | ||
#include "IOKit/DigitalOut.h" | ||
#include "MathUtils.h" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "mocks/leka/CoreI2C.h" | ||
#include "mocks/leka/CoreQDAC.h" | ||
#include "mocks/leka/IOExpander.h" | ||
|
||
using namespace leka; | ||
|
||
using ::testing::Args; | ||
using ::testing::DoAll; | ||
using ::testing::ElementsAre; | ||
using ::testing::Return; | ||
using ::testing::SetArrayArgument; | ||
|
||
class CoreTouchSensorTest : public ::testing::Test | ||
{ | ||
protected: | ||
// void SetUp() override {} | ||
// void TearDown() override {} | ||
|
||
mock::IOExpander<uint16_t> mockIOExpander {}; | ||
uint8_t pin_number {0x0001}; | ||
|
||
leka::io::expanded::DigitalIn<> in {mockIOExpander, pin_number}; | ||
leka::io::expanded::DigitalOut<> out {mockIOExpander, pin_number}; | ||
mock::CoreQDAC dac {}; | ||
uint8_t channel {0x01}; | ||
|
||
CoreTouchSensor sensor {in, out, dac, channel}; | ||
}; | ||
|
||
TEST_F(CoreTouchSensorTest, initializationDefault) | ||
{ | ||
auto new_sensor = CoreTouchSensor {in, out, dac, channel}; | ||
ASSERT_NE(&new_sensor, nullptr); | ||
} | ||
|
||
TEST_F(CoreTouchSensorTest, init) | ||
{ | ||
EXPECT_CALL(dac, init).Times(1); | ||
|
||
sensor.init(); | ||
} | ||
|
||
TEST_F(CoreTouchSensorTest, read) | ||
{ | ||
auto expected_read = bool {false}; | ||
EXPECT_CALL(mockIOExpander, readPin(pin_number)).Times(1).WillOnce(Return(expected_read)); | ||
auto actual_read = sensor.read(); | ||
|
||
EXPECT_EQ(actual_read, expected_read); | ||
} | ||
|
||
TEST_F(CoreTouchSensorTest, reset) | ||
{ | ||
auto expected_value = uint8_t {}; | ||
|
||
expected_value = 0x00; | ||
EXPECT_CALL(mockIOExpander, writePin(pin_number, expected_value)).Times(1); | ||
|
||
expected_value = 0x01; | ||
EXPECT_CALL(mockIOExpander, writePin(pin_number, expected_value)).Times(1); | ||
|
||
sensor.reset(); | ||
} | ||
|
||
TEST_F(CoreTouchSensorTest, setSensitivity) | ||
{ | ||
auto value = 0x0ABC; | ||
auto expected_value = utils::math::map<uint16_t, uint16_t>( | ||
value, CoreTouchSensor::default_min_sensitivity_value, CoreTouchSensor::default_max_sensitivity_value, | ||
CoreTouchSensor::default_max_sensitivity_value, CoreTouchSensor::default_min_sensitivity_value); | ||
EXPECT_CALL(dac, write(channel, expected_value)).Times(1); | ||
sensor.setSensitivity(value); | ||
} |
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,22 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
namespace leka::interface { | ||
|
||
class TouchSensor | ||
{ | ||
public: | ||
virtual ~TouchSensor() = default; | ||
|
||
virtual void init() = 0; | ||
virtual auto read() -> bool = 0; | ||
virtual void reset() = 0; | ||
virtual void setSensitivity(uint16_t value) = 0; | ||
}; | ||
|
||
} // namespace leka::interface |
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,21 @@ | ||
// Leka - LekaOS | ||
// Copyright 2022 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "gmock/gmock.h" | ||
#include "interface/drivers/TouchSensor.h" | ||
|
||
namespace leka::mock { | ||
|
||
class CoreTouchSensor : public interface::TouchSensor | ||
{ | ||
public: | ||
MOCK_METHOD(void, init, (), (override)); | ||
MOCK_METHOD(bool, read, (), (override)); | ||
MOCK_METHOD(void, reset, (), (override)); | ||
MOCK_METHOD(void, setSensitivity, (uint16_t), (override)); | ||
}; | ||
|
||
} // namespace leka::mock |