From a133b89d99e2bcda90e08bcca61e1ebfa8a9f685 Mon Sep 17 00:00:00 2001 From: Mourad Latoundji Date: Thu, 21 Jul 2022 16:41:12 +0200 Subject: [PATCH] :sparkles: (drivers): Add CoreTouchSensor --- drivers/CMakeLists.txt | 1 + drivers/CoreTouchSensor/CMakeLists.txt | 32 +++++++++ .../CoreTouchSensor/include/CoreTouchSensor.h | 38 ++++++++++ .../source/CoreTouchSensor.cpp | 23 +++++++ .../tests/CoreTouchSensor_test.cpp | 69 +++++++++++++++++++ tests/unit/CMakeLists.txt | 1 + 6 files changed, 164 insertions(+) create mode 100644 drivers/CoreTouchSensor/CMakeLists.txt create mode 100644 drivers/CoreTouchSensor/include/CoreTouchSensor.h create mode 100644 drivers/CoreTouchSensor/source/CoreTouchSensor.cpp create mode 100644 drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index f3154b563c..c0436c0302 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -40,5 +40,6 @@ add_subdirectory(${DRIVERS_DIR}/CoreMotor) # Touch drivers add_subdirectory(${DRIVERS_DIR}/CoreIOExpander) add_subdirectory(${DRIVERS_DIR}/CoreQDAC) +add_subdirectory(${DRIVERS_DIR}/CoreTouchSensor) diff --git a/drivers/CoreTouchSensor/CMakeLists.txt b/drivers/CoreTouchSensor/CMakeLists.txt new file mode 100644 index 0000000000..abbe6e5108 --- /dev/null +++ b/drivers/CoreTouchSensor/CMakeLists.txt @@ -0,0 +1,32 @@ +# 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() diff --git a/drivers/CoreTouchSensor/include/CoreTouchSensor.h b/drivers/CoreTouchSensor/include/CoreTouchSensor.h new file mode 100644 index 0000000000..f21c93ba05 --- /dev/null +++ b/drivers/CoreTouchSensor/include/CoreTouchSensor.h @@ -0,0 +1,38 @@ +// 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: + explicit CoreTouchSensor(mbed::interface::DigitalIn &detect_pin, mbed::interface::DigitalOut &power_mode_pin, + interface::QDAC &dac, Channel channel) + : _detect_pin(detect_pin), _power_mode_pin(power_mode_pin), _sensitivity_pin({dac, channel}) + { + _detect_pin.mode(PinMode::PullUp); + _sensitivity_pin.dac.init(); + } + auto read() -> int final; + void setPowerMode(PowerMode power_mode) final; + void setSensitivity(uint16_t value, bool saved = false) final; + + private: + mbed::interface::DigitalIn &_detect_pin; + mbed::interface::DigitalOut &_power_mode_pin; + struct AnalogOut { + interface::QDAC &dac; + Channel channel; + }; + AnalogOut _sensitivity_pin; +}; +} // namespace leka diff --git a/drivers/CoreTouchSensor/source/CoreTouchSensor.cpp b/drivers/CoreTouchSensor/source/CoreTouchSensor.cpp new file mode 100644 index 0000000000..1043120d65 --- /dev/null +++ b/drivers/CoreTouchSensor/source/CoreTouchSensor.cpp @@ -0,0 +1,23 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "CoreTouchSensor.h" + +using namespace leka; + +auto CoreTouchSensor::read() -> int +{ + return _detect_pin.read(); +} + +void CoreTouchSensor::setPowerMode(PowerMode power_mode) +{ + auto pm = static_cast(power_mode); + _power_mode_pin.write(pm); +} + +void CoreTouchSensor::setSensitivity(uint16_t value, bool saved) +{ + _sensitivity_pin.dac.write(_sensitivity_pin.channel, value, saved); +} diff --git a/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp b/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp new file mode 100644 index 0000000000..cb4de5b4ae --- /dev/null +++ b/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp @@ -0,0 +1,69 @@ +// 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 "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 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 {}; + Channel channel {Channel::B}; + + CoreTouchSensor sensor {in, out, dac, channel}; +}; + +TEST_F(CoreTouchSensorTest, initializationDefault) +{ + EXPECT_CALL(dac, init).Times(1); + auto new_sensor = CoreTouchSensor {in, out, dac, channel}; + ASSERT_NE(&new_sensor, nullptr); +} + +TEST_F(CoreTouchSensorTest, read) +{ + auto expected_read = 0x01; + 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, setPowerMode) +{ + auto power_mode = PowerMode::low; + auto expected_value = static_cast(power_mode); + EXPECT_CALL(mockIOExpander, writePin(pin_number, expected_value)).Times(1); + sensor.setPowerMode(power_mode); +} + +TEST_F(CoreTouchSensorTest, setSensitivity) +{ + EXPECT_CALL(dac, write(channel, 0x0ABC, false)).Times(1); + auto value = 0x0ABC; + sensor.setSensitivity(value); +} diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 413fafea2e..dc6e874870 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -264,6 +264,7 @@ leka_register_unit_tests_for_driver(CoreRFIDReader) leka_register_unit_tests_for_driver(CoreSPI) leka_register_unit_tests_for_driver(CoreSTM32Hal) leka_register_unit_tests_for_driver(CoreTicker) +leka_register_unit_tests_for_driver(CoreTouchSensor) leka_register_unit_tests_for_driver(CoreVideo) # Register libraries