From 0f26e38b84d68a3da9cfa64bd8599730f761c9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Lasersk=C3=B6ld?= Date: Sat, 14 Mar 2020 10:56:54 +0100 Subject: [PATCH] Fixed formatting --- software/.clang-format | 15 ++++ software/arduino/keyboard/io.h | 46 ++++++------ software/arduino/keyboard/keyboard.ino | 6 +- software/arduino/keyboard/keyboardmap.h | 41 +++++------ software/arduino/keyboard/keyboardstate.h | 14 ++-- .../arduino/keyboard/keyboardwritefunctions.h | 2 +- software/arduino/keyboard/keys.h | 72 +++++++++---------- software/arduino/keyboard/pins.h | 3 - software/arduino/sim/keyboard.cpp | 8 ++- software/arduino/sim/main.cpp | 15 ++-- software/arduino/sim/simio.h | 11 ++- 11 files changed, 122 insertions(+), 111 deletions(-) diff --git a/software/.clang-format b/software/.clang-format index 8eebeaf..c207d2d 100644 --- a/software/.clang-format +++ b/software/.clang-format @@ -1,2 +1,17 @@ BasedOnStyle: LLVM +IndentWidth: 4 +SortIncludes: true +AccessModifierOffset: -4 +AlwaysBreakTemplateDeclarations: true +AllowShortFunctionsOnASingleLine: None +AllowAllArgumentsOnNextLine: true +BinPackArguments: false +BinPackParameters: false +BreakBeforeBraces: Custom +BraceWrapping: + BeforeCatch: true + BeforeElse: true + +AlwaysBreakAfterReturnType: None +PenaltyReturnTypeOnItsOwnLine: 1000000 \ No newline at end of file diff --git a/software/arduino/keyboard/io.h b/software/arduino/keyboard/io.h index 210eda5..6a4449f 100644 --- a/software/arduino/keyboard/io.h +++ b/software/arduino/keyboard/io.h @@ -23,43 +23,39 @@ keyboard_state_t state; KeyboardMap keyboardMap(width, height, keyConfig1); - - void setup() { - for (auto pin : xPins) { - pinMode(pin, OUTPUT); - } + for (auto pin : xPins) { + pinMode(pin, OUTPUT); + } } //! Turn on one column pin and turn of the rest void changeColumnPin(size_t column) { - for (size_t i = 0; i < width; ++i) { - digitalWrite(xPins[i], i == column); - } + for (size_t i = 0; i < width; ++i) { + digitalWrite(xPins[i], i == column); + } } void readRowPins(keyboard_state_t &keyboardState, size_t x) { - for (size_t y = 0; y < height; ++y) { - int value = digitalRead(yPins[y]); - auto &storedKeyState = keyboardState.state(x, y); + for (size_t y = 0; y < height; ++y) { + int value = digitalRead(yPins[y]); + auto &storedKeyState = keyboardState.state(x, y); - if (storedKeyState != value) { - Serial.print("readRowPins: key is changed "); - Serial.print(x); - Serial.print(y); - keyboardMap.publishEvent(x, y, value); - storedKeyState = value; + if (storedKeyState != value) { + Serial.print("readRowPins: key is changed "); + Serial.print(x); + Serial.print(y); + keyboardMap.publishEvent(x, y, value); + storedKeyState = value; + } } - } } void loop() { - // Cycle through columns - for (size_t x = 0; x < width; ++x) { - changeColumnPin(x); + // Cycle through columns + for (size_t x = 0; x < width; ++x) { + changeColumnPin(x); - readRowPins(state, x); - } + readRowPins(state, x); + } } - - diff --git a/software/arduino/keyboard/keyboard.ino b/software/arduino/keyboard/keyboard.ino index 493c7d6..0f1ec4a 100644 --- a/software/arduino/keyboard/keyboard.ino +++ b/software/arduino/keyboard/keyboard.ino @@ -3,9 +3,9 @@ #include "io.h" void writeKeyRelease(char key) { - // Implement this + // Implement this } void writeKeyPress(char key) { - // Implement this -} \ No newline at end of file + // Implement this +} diff --git a/software/arduino/keyboard/keyboardmap.h b/software/arduino/keyboard/keyboardmap.h index ec1b651..e0be648 100644 --- a/software/arduino/keyboard/keyboardmap.h +++ b/software/arduino/keyboard/keyboardmap.h @@ -4,28 +4,29 @@ #include "keyboardwritefunctions.h" #include "keys.h" - //! A class for translating key coordinates to scancodes class KeyboardMap { - size_t _width; - size_t _height; - size_t _size; - const char *_translation; + size_t _width; + size_t _height; + size_t _size; + const char *_translation; public: - KeyboardMap(size_t width, size_t height, const char *values) - : _width(width), _height(height), _size(width * height), - _translation(values) {} - - void publishEvent(size_t x, size_t y, bool keyValue) { - size_t index = x + y * _width; - if (index < _size) { - auto out = _translation[index]; - if (keyValue) { - writeKeyPress(out); - } else { - writeKeyRelease(out); - } + KeyboardMap(size_t width, size_t height, const char *values) + : _width(width), _height(height), _size(width * height), + _translation(values) { } - } -}; \ No newline at end of file + + void publishEvent(size_t x, size_t y, bool keyValue) { + size_t index = x + y * _width; + if (index < _size) { + auto out = _translation[index]; + if (keyValue) { + writeKeyPress(out); + } + else { + writeKeyRelease(out); + } + } + } +}; diff --git a/software/arduino/keyboard/keyboardstate.h b/software/arduino/keyboard/keyboardstate.h index e05c749..7256e04 100644 --- a/software/arduino/keyboard/keyboardstate.h +++ b/software/arduino/keyboard/keyboardstate.h @@ -2,12 +2,16 @@ template class KeyboardState { - char _state[width * height] = {}; + char _state[width * height] = {}; public: - typedef char state_t; + typedef char state_t; - // Todo: Bounds check - state_t state(size_t x, size_t y) const { return _state[y * width + x]; } - state_t &state(size_t x, size_t y) { return _state[y * width + x]; } + // Todo: Bounds check + state_t state(size_t x, size_t y) const { + return _state[y * width + x]; + } + state_t &state(size_t x, size_t y) { + return _state[y * width + x]; + } }; diff --git a/software/arduino/keyboard/keyboardwritefunctions.h b/software/arduino/keyboard/keyboardwritefunctions.h index 88d6519..e3d54b7 100644 --- a/software/arduino/keyboard/keyboardwritefunctions.h +++ b/software/arduino/keyboard/keyboardwritefunctions.h @@ -1,4 +1,4 @@ #pragma once void writeKeyRelease(char key); -void writeKeyPress(char key); \ No newline at end of file +void writeKeyPress(char key); diff --git a/software/arduino/keyboard/keys.h b/software/arduino/keyboard/keys.h index 0813cf9..773379e 100644 --- a/software/arduino/keyboard/keys.h +++ b/software/arduino/keyboard/keys.h @@ -2,39 +2,39 @@ // Special keys: https://www.arduino.cc/en/Reference/KeyboardModifiers enum Keys { - LEFT_CTRL = 128, - LEFT_SHIFT = 129, - LEFT_ALT = 130, - LEFT_GUI = 131, - RIGHT_CTRL = 132, - RIGHT_SHIFT = 133, - RIGHT_ALT = 134, - RIGHT_GUI = 135, - UP_ARROW = 218, - DOWN_ARROW = 217, - LEFT_ARROW = 216, - RIGHT_ARROW = 215, - BACKSPACE = 178, - TAB = 179, - RETURN = 176, - ESC = 177, - INSERT = 209, - DELETE = 212, - PAGE_UP = 211, - PAGE_DOWN = 214, - HOME = 210, - END = 213, - CAPS_LOCK = 193, - F1 = 194, - F2 = 195, - F3 = 196, - F4 = 197, - F5 = 198, - F6 = 199, - F7 = 200, - F8 = 201, - F9 = 202, - F10 = 203, - F11 = 204, - F12 = 205, -}; \ No newline at end of file + LEFT_CTRL = 128, + LEFT_SHIFT = 129, + LEFT_ALT = 130, + LEFT_GUI = 131, + RIGHT_CTRL = 132, + RIGHT_SHIFT = 133, + RIGHT_ALT = 134, + RIGHT_GUI = 135, + UP_ARROW = 218, + DOWN_ARROW = 217, + LEFT_ARROW = 216, + RIGHT_ARROW = 215, + BACKSPACE = 178, + TAB = 179, + RETURN = 176, + ESC = 177, + INSERT = 209, + DELETE = 212, + PAGE_UP = 211, + PAGE_DOWN = 214, + HOME = 210, + END = 213, + CAPS_LOCK = 193, + F1 = 194, + F2 = 195, + F3 = 196, + F4 = 197, + F5 = 198, + F6 = 199, + F7 = 200, + F8 = 201, + F9 = 202, + F10 = 203, + F11 = 204, + F12 = 205, +}; diff --git a/software/arduino/keyboard/pins.h b/software/arduino/keyboard/pins.h index b5995c4..54791ca 100644 --- a/software/arduino/keyboard/pins.h +++ b/software/arduino/keyboard/pins.h @@ -12,6 +12,3 @@ constexpr size_t height = 5; const char xPins[width] = {1, 2, 3, 4, 5, 6}; const char yPins[height] = {8, 9, 10, 11, 12}; - - - diff --git a/software/arduino/sim/keyboard.cpp b/software/arduino/sim/keyboard.cpp index 55fe9ee..396e41d 100644 --- a/software/arduino/sim/keyboard.cpp +++ b/software/arduino/sim/keyboard.cpp @@ -8,12 +8,14 @@ #include using namespace std; +std::string scanCodeLookup(int code); // Defined in main + void writeKeyRelease(char key) { cout << "Key released " << key << endl; } void writeKeyPress(char key) { - cout << "keyboard.cpp: key pressed " << key << ", " << static_cast(key) << endl; + cout << "keyboard.cpp: key pressed " << key << ", " + << static_cast(key) << endl; + cout << "that is: " << scanCodeLookup(key) << endl; } - - diff --git a/software/arduino/sim/main.cpp b/software/arduino/sim/main.cpp index e45a179..de9fe13 100644 --- a/software/arduino/sim/main.cpp +++ b/software/arduino/sim/main.cpp @@ -8,12 +8,12 @@ #include "keys.h" #include "matgui/application.h" -#include "matgui/window.h" #include "matgui/button.h" +#include "matgui/window.h" // Simulator -#include "simio.h" #include "keyboardwritefunctions.h" +#include "simio.h" // The code #include "io.h" @@ -23,6 +23,10 @@ using namespace std; using namespace MatGui; +std::string scanCodeLookup(int code) { + return Application::GetKeyNameFromScancode(code); +} + int main(int argc, char **argv) { Application app(argc, argv); @@ -48,12 +52,7 @@ int main(int argc, char **argv) { window.addChild(std::move(layout)); } - window.frameUpdate.connect([]() { - loop(); - }); + window.frameUpdate.connect([]() { loop(); }); app.mainLoop(); } - - - diff --git a/software/arduino/sim/simio.h b/software/arduino/sim/simio.h index 5d2dd60..2225b9f 100644 --- a/software/arduino/sim/simio.h +++ b/software/arduino/sim/simio.h @@ -10,7 +10,7 @@ #include #define HIGH 0x1 -#define LOW 0x0 +#define LOW 0x0 #define INPUT 0x0 #define OUTPUT 0x1 @@ -25,9 +25,8 @@ static class SerialPort { public: - template - void print(const T& value); + void print(const T &value); } Serial; @@ -35,9 +34,8 @@ void digitalWrite(uint8_t pin, uint8_t value); int digitalRead(uint8_t pin); void pinMode(uint8_t pin, uint8_t mode); -template -inline void SerialPort::print(const T& value) - { +template +inline void SerialPort::print(const T &value) { std::cout << value << std::endl; } @@ -46,4 +44,3 @@ namespace simio { void setSimKey(size_t x, size_t y, uint8_t state); } -