From e37fa96ab3c268d017c390dad28fa022c82c7b0d Mon Sep 17 00:00:00 2001 From: Mourad Latoundji Date: Tue, 28 Jun 2022 12:51:50 +0200 Subject: [PATCH] :sparkles: (drivers): Add CoreTouchSensor --- drivers/CMakeLists.txt | 1 + .../CoreDACTouch/tests/CoreDACTouch_test.cpp | 1 - drivers/CoreTouchSensor/CMakeLists.txt | 31 +++++++++ .../CoreTouchSensor/include/CoreTouchSensor.h | 36 ++++++++++ .../source/CoreTouchSensor.cpp | 7 ++ .../tests/CoreTouchSensor_test.cpp | 65 +++++++++++++++++++ 6 files changed, 140 insertions(+), 1 deletion(-) 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 98301c8ad1..5dc4368bd1 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -41,5 +41,6 @@ add_subdirectory(${DRIVERS_DIR}/FastLED) # Touch drivers add_subdirectory(${DRIVERS_DIR}/CoreIOExpander) add_subdirectory(${DRIVERS_DIR}/CoreDACTouch) +add_subdirectory(${DRIVERS_DIR}/CoreTouchSensor) diff --git a/drivers/CoreDACTouch/tests/CoreDACTouch_test.cpp b/drivers/CoreDACTouch/tests/CoreDACTouch_test.cpp index aa58c53d45..d351dfaa4e 100644 --- a/drivers/CoreDACTouch/tests/CoreDACTouch_test.cpp +++ b/drivers/CoreDACTouch/tests/CoreDACTouch_test.cpp @@ -17,7 +17,6 @@ using ::testing::Return; using ::testing::SetArrayArgument; class CoreDACTouchTest : public ::testing::Test - { protected: // void SetUp() override {} diff --git a/drivers/CoreTouchSensor/CMakeLists.txt b/drivers/CoreTouchSensor/CMakeLists.txt new file mode 100644 index 0000000000..a8781a75b7 --- /dev/null +++ b/drivers/CoreTouchSensor/CMakeLists.txt @@ -0,0 +1,31 @@ +# 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 + mbed-os + CoreI2C + CoreIOExpander + CoreDACTouch + 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..f48e2cef9d --- /dev/null +++ b/drivers/CoreTouchSensor/include/CoreTouchSensor.h @@ -0,0 +1,36 @@ +// 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/DACTouch.h" + +namespace leka { + +class CoreTouchSensor +{ + public: + explicit CoreTouchSensor(mbed::interface::DigitalIn &detect_pin, mbed::interface::DigitalOut &power_mode_pin, + interface::DACTouch &sensitivity_dac, uint8_t channel) + : _detect_pin(detect_pin), + _power_mode_pin(power_mode_pin), + _sensitivity_dac(sensitivity_dac), + _channel(channel) {}; + auto read() -> int { return _detect_pin.read(); } + void setPowerMode(int power_mode) { _power_mode_pin.write(power_mode); } + void adjustSensitivity(std::array value) + { + _sensitivity_dac.writeToSpecificInputRegister(_channel, value); + } + + private: + mbed::interface::DigitalIn &_detect_pin; + mbed::interface::DigitalOut &_power_mode_pin; + interface::DACTouch &_sensitivity_dac; + uint8_t _channel; +}; +} // namespace leka diff --git a/drivers/CoreTouchSensor/source/CoreTouchSensor.cpp b/drivers/CoreTouchSensor/source/CoreTouchSensor.cpp new file mode 100644 index 0000000000..33abbfb98a --- /dev/null +++ b/drivers/CoreTouchSensor/source/CoreTouchSensor.cpp @@ -0,0 +1,7 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "CoreTouchSensor.h" diff --git a/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp b/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp new file mode 100644 index 0000000000..6a746f8737 --- /dev/null +++ b/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp @@ -0,0 +1,65 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "CoreTouchSensor.h" + +#include "CoreDACTouch.h" +#include "CoreIOExpander.h" +#include "DigitalOut.h" +#include "IOKit/DigitalIn.h" +#include "IOKit/DigitalOut.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "mocks/leka/CoreI2C.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 {} + + const uint8_t i2c_address {0xC0}; + mock::CoreI2C mocki2c; + CoreDACTouchMCP4728 dac {mocki2c, i2c_address}; + uint8_t channel {1}; + + mbed::DigitalOut io_expander_reset {PinName::SENSOR_PROXIMITY_MUX_RESET, 0}; + CoreIOExpanderMCP23017 io_expander {mocki2c, io_expander_reset}; + uint8_t pin_number {0x0001}; + + leka::io::expanded::DigitalIn<> input {io_expander, pin_number}; + leka::io::expanded::DigitalOut<> power_mode {io_expander, pin_number}; + + CoreTouchSensor sensor {input, power_mode, dac, channel}; +}; + +TEST_F(CoreTouchSensorTest, initializationDefault) +{ + auto new_sensor = CoreTouchSensor {input, power_mode, dac, channel}; + ASSERT_NE(&new_sensor, nullptr); +} + +TEST_F(CoreTouchSensorTest, read) +{ + sensor.read(); +} + +TEST_F(CoreTouchSensorTest, setPowerMode) +{ + sensor.setPowerMode(0x01); +} + +TEST_F(CoreTouchSensorTest, adjustSensitivity) +{ + auto value = std::array {0x0A, 0xBC}; + sensor.adjust_sensitivity(channel, value); +}