From d98bdf7c033f3f35586249d61e2f17a537e9afef Mon Sep 17 00:00:00 2001 From: Mourad Latoundji Date: Thu, 21 Jul 2022 13:49:03 +0200 Subject: [PATCH] :art: (spike): Update lk_touch_sensor_kit --- .../tests/CoreTouchSensor_test.cpp | 8 ++ libs/TouchSensorKit/include/TouchSensorKit.h | 2 +- spikes/lk_touch_sensor_kit/CMakeLists.txt | 5 ++ spikes/lk_touch_sensor_kit/Leds.h | 70 +++++++++++++++++ spikes/lk_touch_sensor_kit/main.cpp | 75 +++++++++++++++++-- 5 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 spikes/lk_touch_sensor_kit/Leds.h diff --git a/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp b/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp index 1ad745d36a..cb4de5b4ae 100644 --- a/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp +++ b/drivers/CoreTouchSensor/tests/CoreTouchSensor_test.cpp @@ -4,6 +4,14 @@ #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; diff --git a/libs/TouchSensorKit/include/TouchSensorKit.h b/libs/TouchSensorKit/include/TouchSensorKit.h index 445fab74ee..de4d8208d0 100644 --- a/libs/TouchSensorKit/include/TouchSensorKit.h +++ b/libs/TouchSensorKit/include/TouchSensorKit.h @@ -6,7 +6,7 @@ #include "CoreTouchSensor.h" #include "LogKit.h" -#include "external/TouchSensorSystem.h" +#include "internal/TouchSensorSystem.h" namespace leka { diff --git a/spikes/lk_touch_sensor_kit/CMakeLists.txt b/spikes/lk_touch_sensor_kit/CMakeLists.txt index 8340e1f659..576e50ac23 100644 --- a/spikes/lk_touch_sensor_kit/CMakeLists.txt +++ b/spikes/lk_touch_sensor_kit/CMakeLists.txt @@ -16,6 +16,11 @@ target_sources(spike_lk_touch_sensor_kit target_link_libraries(spike_lk_touch_sensor_kit TouchSensorKit + CoreEventFlags + CoreLED + CoreSPI + EventLoopKit + LedKit ) target_link_custom_leka_targets(spike_lk_touch_sensor_kit) diff --git a/spikes/lk_touch_sensor_kit/Leds.h b/spikes/lk_touch_sensor_kit/Leds.h new file mode 100644 index 0000000000..556136d0e4 --- /dev/null +++ b/spikes/lk_touch_sensor_kit/Leds.h @@ -0,0 +1,70 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "PinNames.h" + +#include "drivers/BufferedSerial.h" +#include "drivers/HighResClock.h" +#include "rtos/ThisThread.h" + +#include "CoreEventFlags.h" +#include "CoreLED.h" +#include "CoreSPI.h" +#include "EventLoopKit.h" +#include "HelloWorld.h" +#include "LedKit.h" +#include "LogKit.h" + +using namespace leka; +using namespace std::chrono; + +namespace leds { + +namespace internal { + + namespace ears { + + auto spi = CoreSPI {LED_EARS_SPI_MOSI, NC, LED_EARS_SPI_SCK}; + constexpr auto size = 2; + + } // namespace ears + + namespace belt { + + auto spi = CoreSPI {LED_BELT_SPI_MOSI, NC, LED_BELT_SPI_SCK}; + constexpr auto size = 20; + + } // namespace belt + + namespace animations { + + auto event_loop = EventLoopKit {}; + + } // namespace animations + +} // namespace internal + +auto ears = CoreLED {internal::ears::spi}; +auto belt = CoreLED {internal::belt::spi}; + +void turnOn() +{ + ears.setColor(RGB::pure_blue); + belt.setColor(RGB::pure_blue); + ears.show(); + belt.show(); +} +void turnOff() +{ + ears.setColor(RGB::black); + belt.setColor(RGB::black); + ears.show(); + belt.show(); +} + +auto ledkit = LedKit {leds::internal::animations::event_loop, leds::ears, leds::belt}; + +} // namespace leds diff --git a/spikes/lk_touch_sensor_kit/main.cpp b/spikes/lk_touch_sensor_kit/main.cpp index 601b3cb7ce..0c70a9a1bf 100644 --- a/spikes/lk_touch_sensor_kit/main.cpp +++ b/spikes/lk_touch_sensor_kit/main.cpp @@ -6,12 +6,68 @@ #include "rtos/ThisThread.h" #include "HelloWorld.h" +#include "Leds.h" #include "LogKit.h" #include "TouchSensorKit.h" using namespace leka; using namespace std::chrono; +namespace TouchLed { +struct Ear { + Position position; + uint8_t index; +}; + +auto ears = std::array {Ear {Position::ear_left, 1}, Ear {Position::ear_right, 0}}; + +struct Belt { + Position position; + uint8_t start_index; + uint8_t end_index; +}; + +auto belt = std::array {Belt {Position::belt_left_back, 16, 18}, Belt {Position::belt_left_front, 10, 13}, + Belt {Position::belt_right_back, 6, 9}, Belt {Position::belt_right_front, 1, 3}}; + +void setColorEar(Ear ear, RGB color) +{ + leds::ears.setColorAtIndex(ear.index, color); + leds::ears.show(); +} + +void setColorBelt(Belt belt, RGB color) +{ + leds::belt.setColorRange(belt.start_index, belt.end_index, color); + leds::belt.show(); +} +} // namespace TouchLed + +auto touch_sensor_kit = TouchSensorKit(); + +void printState() +{ + log_info("Ear left touched: %s", touch_sensor_kit.isTouched(Position::ear_left) ? "true" : "false"); + log_info("Ear right touched: %s", touch_sensor_kit.isTouched(Position::ear_right) ? "true" : "false"); + log_info("Belt left front touched: %s", touch_sensor_kit.isTouched(Position::belt_left_front) ? "true" : "false"); + log_info("Belt left back touched: %s", touch_sensor_kit.isTouched(Position::belt_left_back) ? "true" : "false"); + log_info("Belt right front touched: %s", touch_sensor_kit.isTouched(Position::belt_right_front) ? "true" : "false"); + log_info("Belt right back touched: %s", touch_sensor_kit.isTouched(Position::belt_left_back) ? "true" : "false"); + log_info("\n\n"); +} + +void updateLeds() +{ + for (TouchLed::Ear touchled: TouchLed::ears) { + touch_sensor_kit.isTouched(touchled.position) ? TouchLed::setColorEar(touchled, RGB::pure_blue) + : TouchLed::setColorEar(touchled, RGB::black); + } + for (TouchLed::Belt touchled: TouchLed::belt) { + touch_sensor_kit.isTouched(touchled.position) ? TouchLed::setColorBelt(touchled, RGB::pure_blue) + : TouchLed::setColorBelt(touchled, RGB::black); + } +} + auto main() -> int { logger::init(); @@ -21,8 +77,7 @@ auto main() -> int log_info("Hello, World!\n\n"); - auto touch_sensor_kit = TouchSensorKit(); - touch_sensor_kit.setup(); + touch_sensor_kit.init(); auto start = rtos::Kernel::Clock::now(); @@ -31,14 +86,22 @@ auto main() -> int auto t = rtos::Kernel::Clock::now() - start; log_info("A message from your board %s --> \"%s\" at %i s\n", MBED_CONF_APP_TARGET_NAME, hello.world, int(t.count() / 1000)); - touch_sensor_kit.adjustSensitivity(0, true); + for (Position position: positions) { + touch_sensor_kit.setSensitivity(position, 0x000F, true); + } rtos::ThisThread::sleep_for(2s); + auto updateLeds_func = [&]() { updateLeds(); }; + auto printState_func = [&]() { printState(); }; + + touch_sensor_kit.resetByPowerMode(); + // touch_sensor_kit.registerOnSensorTouched(printState_func); + + rtos::ThisThread::sleep_for(500ms); + while (true) { - touch_sensor_kit.updateState(); - touch_sensor_kit.printState(); + updateLeds(); touch_sensor_kit.resetByPowerMode(); - log_info("\n\n"); rtos::ThisThread::sleep_for(1s); } }