From 0bc0044f8094a94a1d52d35d72f597bcd635ec66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 30 Mar 2017 12:09:08 +0200 Subject: [PATCH] [apps] First version of keyboard hardware test Change-Id: Ic322575f22f1576eacf0068bc3aa09257e2ae3f4 --- apps/Makefile | 1 + apps/apps_container.cpp | 3 +- apps/apps_container.h | 2 + apps/hardware_test/Makefile | 7 ++ apps/hardware_test/app.cpp | 16 ++++ apps/hardware_test/app.h | 21 ++++++ apps/hardware_test/keyboard_controller.cpp | 31 ++++++++ apps/hardware_test/keyboard_controller.h | 24 ++++++ apps/hardware_test/keyboard_view.cpp | 88 ++++++++++++++++++++++ apps/hardware_test/keyboard_view.h | 32 ++++++++ 10 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 apps/hardware_test/Makefile create mode 100644 apps/hardware_test/app.cpp create mode 100644 apps/hardware_test/app.h create mode 100644 apps/hardware_test/keyboard_controller.cpp create mode 100644 apps/hardware_test/keyboard_controller.h create mode 100644 apps/hardware_test/keyboard_view.cpp create mode 100644 apps/hardware_test/keyboard_view.h diff --git a/apps/Makefile b/apps/Makefile index 35a4b1a80..07d798181 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,6 +1,7 @@ include apps/calculation/Makefile include apps/graph/Makefile include apps/home/Makefile +include apps/hardware_test/Makefile include apps/probability/Makefile include apps/regression/Makefile include apps/sequence/Makefile diff --git a/apps/apps_container.cpp b/apps/apps_container.cpp index 4ca1aac98..43fcd4843 100644 --- a/apps/apps_container.cpp +++ b/apps/apps_container.cpp @@ -14,6 +14,7 @@ AppsContainer::AppsContainer() : m_graphApp(this, &m_globalContext), m_probabilityApp(this), m_calculationApp(this, &m_globalContext), + m_hardwareTestApp(this), m_regressionApp(this), m_sequenceApp(this, &m_globalContext), m_settingsApp(this), @@ -34,7 +35,7 @@ App * AppsContainer::appAtIndex(int index) { &m_calculationApp, &m_graphApp, &m_sequenceApp, - &m_graphApp, + &m_hardwareTestApp, &m_statisticsApp, &m_probabilityApp, &m_regressionApp, diff --git a/apps/apps_container.h b/apps/apps_container.h index 24ab7e62d..467fee1f5 100644 --- a/apps/apps_container.h +++ b/apps/apps_container.h @@ -5,6 +5,7 @@ #include "graph/app.h" #include "probability/app.h" #include "calculation/app.h" +#include "hardware_test/app.h" #include "regression/app.h" #include "sequence/app.h" #include "settings/app.h" @@ -38,6 +39,7 @@ private: Graph::App m_graphApp; Probability::App m_probabilityApp; Calculation::App m_calculationApp; + HardwareTest::App m_hardwareTestApp; Regression::App m_regressionApp; Sequence::App m_sequenceApp; Settings::App m_settingsApp; diff --git a/apps/hardware_test/Makefile b/apps/hardware_test/Makefile new file mode 100644 index 000000000..8a26e2629 --- /dev/null +++ b/apps/hardware_test/Makefile @@ -0,0 +1,7 @@ +app_objs += $(addprefix apps/hardware_test/,\ + app.o\ + keyboard_controller.o\ + keyboard_view.o\ +) + + diff --git a/apps/hardware_test/app.cpp b/apps/hardware_test/app.cpp new file mode 100644 index 000000000..193b8a4da --- /dev/null +++ b/apps/hardware_test/app.cpp @@ -0,0 +1,16 @@ +#include "app.h" +#include "../apps_container.h" + +extern "C" { +#include +} + +namespace HardwareTest { + +App::App(AppsContainer * container) : + ::App(container, &m_keyboardController), + m_keyboardController(KeyboardController(&m_modalViewController)) +{ +} + +} diff --git a/apps/hardware_test/app.h b/apps/hardware_test/app.h new file mode 100644 index 000000000..6556b89f3 --- /dev/null +++ b/apps/hardware_test/app.h @@ -0,0 +1,21 @@ +#ifndef HARDWARE_TEST_APP_H +#define HARDWARE_TEST_APP_H + +#include +#include "keyboard_controller.h" + +class AppsContainer; + +namespace HardwareTest { + +class App : public ::App { +public: + App(AppsContainer * container); +private: + KeyboardController m_keyboardController; +}; + +} + +#endif + diff --git a/apps/hardware_test/keyboard_controller.cpp b/apps/hardware_test/keyboard_controller.cpp new file mode 100644 index 000000000..967bc8d1a --- /dev/null +++ b/apps/hardware_test/keyboard_controller.cpp @@ -0,0 +1,31 @@ +#include "keyboard_controller.h" +extern "C" { +#include +} + +namespace HardwareTest { + +KeyboardController::KeyboardController(Responder * parentResponder) : + ViewController(parentResponder), + m_view(KeyboardView()) +{ +} + +View * KeyboardController::view() { + return &m_view; +} + +bool KeyboardController::handleEvent(Ion::Events::Event event) { + if (event != Ion::Events::Event::PlainKey(m_view.testedKey())) { + m_view.setDefectiveKey(m_view.testedKey()); + } + m_view.setNextKey(); + return true; +} + +void KeyboardController::didBecomeFirstResponder() { + m_view.resetTestedKey(); +} + +} + diff --git a/apps/hardware_test/keyboard_controller.h b/apps/hardware_test/keyboard_controller.h new file mode 100644 index 000000000..7d477fa62 --- /dev/null +++ b/apps/hardware_test/keyboard_controller.h @@ -0,0 +1,24 @@ +#ifndef HARDWARE_TEST_KEYBOARD_CONTROLLER_H +#define HARDWARE_TEST_KEYBOARD_CONTROLLER_H + +#include +#include "keyboard_view.h" + +namespace HardwareTest { + +class KeyboardController : public ViewController { +public: + KeyboardController(Responder * parentResponder); + + View * view() override; + + bool handleEvent(Ion::Events::Event event) override; + void didBecomeFirstResponder() override; +private: + KeyboardView m_view; +}; + +} + +#endif + diff --git a/apps/hardware_test/keyboard_view.cpp b/apps/hardware_test/keyboard_view.cpp new file mode 100644 index 000000000..de4d69f0f --- /dev/null +++ b/apps/hardware_test/keyboard_view.cpp @@ -0,0 +1,88 @@ +#include "keyboard_view.h" + +namespace HardwareTest { + +KeyboardView::KeyboardView() : + m_testedKey(Ion::Keyboard::Key::A1) +{ + for (int i = 0; i < Ion::Keyboard::NumberOfKeys; i++) { + m_defectiveKey[i] = 0; + } +} + +Ion::Keyboard::Key KeyboardView::testedKey() const { + return m_testedKey; +} + +void KeyboardView::setDefectiveKey(Ion::Keyboard::Key key) { + m_defectiveKey[(int)key] = 1; +} + +void KeyboardView::setNextKey() { + m_testedKey = (Ion::Keyboard::Key)((int)m_testedKey+1); + int keyIndex = (int)m_testedKey; + if (keyIndex == 54) { + resetTestedKey(); + } + if ((keyIndex > 7 && keyIndex < 12) || keyIndex == 35 || keyIndex == 41 || keyIndex == 47 || keyIndex == 53) { + setNextKey(); + } + markRectAsDirty(bounds()); +} + +void KeyboardView::resetTestedKey() { + m_testedKey = Ion::Keyboard::Key::A1; + markRectAsDirty(bounds()); +} + + +void KeyboardView::drawRect(KDContext * ctx, KDRect rect) const { + ctx->fillRect(bounds(), KDColorWhite); + for (int i = 0; i < Ion::Keyboard::NumberOfKeys; i++) { + drawKey(i, ctx, rect); + } +} + +void KeyboardView::drawKey(int keyIndex, KDContext * ctx, KDRect rect) const { + KDColor color = keyIndex == (int)m_testedKey ? KDColorBlue: KDColorBlack; + if (keyIndex < (int)m_testedKey) { + color = m_defectiveKey[keyIndex] == 0 ? KDColorGreen : KDColorRed; + } + /* the key is on the cross */ + if (keyIndex < 4) { + KDCoordinate x = keyIndex == 1 || keyIndex == 2 ? k_margin + k_smallSquareSize : k_margin; + x = keyIndex == 3 ? x + 2*k_smallSquareSize : x; + KDCoordinate y = keyIndex == 0 || keyIndex == 3 ? k_margin + k_smallSquareSize : k_margin; + y = keyIndex == 2 ? y + 2*k_smallSquareSize : y; + ctx->fillRect(KDRect(x, y, k_smallSquareSize, k_smallSquareSize), color); + } + /* the key is a "OK" or "back" */ + if (keyIndex >= 4 && keyIndex < 6) { + KDCoordinate x = keyIndex == 4 ? 5*k_margin + 3*k_smallSquareSize + 2*k_bigRectWidth : 6*k_margin + 3*k_smallSquareSize + 2*k_bigRectWidth + k_bigSquareSize; + KDCoordinate y = 2*k_margin; + ctx->fillRect(KDRect(x, y, k_bigSquareSize, k_bigSquareSize), color); + } + /* the key is a "home" or "power" */ + if (keyIndex >= 6 && keyIndex < 8) { + KDCoordinate x = 3*k_margin + 3*k_smallSquareSize; + KDCoordinate y = keyIndex == 6 ? k_margin : 2*k_margin + k_bigRectHeight; + ctx->fillRect(KDRect(x, y, k_bigRectWidth, k_bigRectHeight), color); + } + /* the key is a small key as "shift", "alpha" ...*/ + if (keyIndex >= 12 && keyIndex < 30) { + int j = (keyIndex - 12)/6; + int i = (keyIndex - 12) - 6*j; + KDCoordinate x = (i+1)*k_margin + i*k_smallRectWidth; + KDCoordinate y = 2*k_bigRectHeight + (j+3)*k_margin + j*k_smallRectHeight; + ctx->fillRect(KDRect(x, y, k_smallRectWidth, k_smallRectHeight), color); + } + /* the key is a big key as "7", "Ans" ...*/ + if (keyIndex >= 30 && keyIndex != 35 && keyIndex != 41 && keyIndex != 47 && keyIndex != 53) { + int j = (keyIndex - 30)/6; + int i = (keyIndex - 30) - 6*j; + KDCoordinate x = (i+1)*k_margin + i*k_bigRectWidth; + KDCoordinate y = 2*k_bigRectHeight + 3*k_smallRectHeight + (j+6)*k_margin + j*k_bigRectHeight; + ctx->fillRect(KDRect(x, y, k_bigRectWidth, k_bigRectHeight), color); + } +} +} diff --git a/apps/hardware_test/keyboard_view.h b/apps/hardware_test/keyboard_view.h new file mode 100644 index 000000000..8cfdc89c6 --- /dev/null +++ b/apps/hardware_test/keyboard_view.h @@ -0,0 +1,32 @@ +#ifndef HARDWARE_TEST_KEYBOARD_VIEW_H +#define HARDWARE_TEST_KEYBOARD_VIEW_H + +#include + +namespace HardwareTest { + +class KeyboardView : public View { +public: + KeyboardView(); + Ion::Keyboard::Key testedKey() const; + void setDefectiveKey(Ion::Keyboard::Key key); + void setNextKey(); + void resetTestedKey(); + void drawRect(KDContext * ctx, KDRect rect) const override; +private: + void drawKey(int key, KDContext * ctx, KDRect rect) const; + constexpr static int k_margin = 4; + constexpr static int k_smallSquareSize = 8; + constexpr static int k_bigSquareSize = 14; + constexpr static int k_smallRectHeight = 8; + constexpr static int k_smallRectWidth = 16; + constexpr static int k_bigRectHeight = 14; + constexpr static int k_bigRectWidth = 20; + Ion::Keyboard::Key m_testedKey; + int m_defectiveKey[Ion::Keyboard::NumberOfKeys]; +}; + +} + +#endif +