Skip to content

Commit

Permalink
✨ (drivers): Add CoreTouchSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
MMyster committed Jul 22, 2022
1 parent f841cd5 commit a133b89
Show file tree
Hide file tree
Showing 6 changed files with 164 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)


32 changes: 32 additions & 0 deletions drivers/CoreTouchSensor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions drivers/CoreTouchSensor/include/CoreTouchSensor.h
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions drivers/CoreTouchSensor/source/CoreTouchSensor.cpp
Original file line number Diff line number Diff line change
@@ -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<uint8_t>(power_mode);
_power_mode_pin.write(pm);
}

void CoreTouchSensor::setSensitivity(uint16_t value, bool saved)
{
_sensitivity_pin.dac.write(_sensitivity_pin.channel, value, saved);
}
69 changes: 69 additions & 0 deletions drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp
Original file line number Diff line number Diff line change
@@ -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<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 {};
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<uint8_t>(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);
}
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

0 comments on commit a133b89

Please sign in to comment.