Skip to content

Commit

Permalink
🔀 Merge branch 'mmyster/feature/add-core-touch-sensor' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Sep 5, 2022
2 parents f66b540 + 14245cc commit 5d6fb17
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)


30 changes: 30 additions & 0 deletions drivers/CoreTouchSensor/CMakeLists.txt
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()
48 changes: 48 additions & 0 deletions drivers/CoreTouchSensor/include/CoreTouchSensor.h
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
47 changes: 47 additions & 0 deletions drivers/CoreTouchSensor/source/CoreTouchSensor.cpp
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);
}
84 changes: 84 additions & 0 deletions drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp
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);
}
22 changes: 22 additions & 0 deletions include/interface/drivers/TouchSensor.h
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
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/mocks/mocks/leka/CoreTouchSensor.h
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

0 comments on commit 5d6fb17

Please sign in to comment.