Fixed formatting

This commit is contained in:
Mattias Lasersköld 2020-03-14 10:56:54 +01:00
parent e053a7b4b0
commit 0f26e38b84
11 changed files with 122 additions and 111 deletions

View File

@ -1,2 +1,17 @@
BasedOnStyle: LLVM 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

View File

@ -23,43 +23,39 @@ keyboard_state_t state;
KeyboardMap keyboardMap(width, height, keyConfig1); KeyboardMap keyboardMap(width, height, keyConfig1);
void setup() { void setup() {
for (auto pin : xPins) { for (auto pin : xPins) {
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
} }
} }
//! Turn on one column pin and turn of the rest //! Turn on one column pin and turn of the rest
void changeColumnPin(size_t column) { void changeColumnPin(size_t column) {
for (size_t i = 0; i < width; ++i) { for (size_t i = 0; i < width; ++i) {
digitalWrite(xPins[i], i == column); digitalWrite(xPins[i], i == column);
} }
} }
void readRowPins(keyboard_state_t &keyboardState, size_t x) { void readRowPins(keyboard_state_t &keyboardState, size_t x) {
for (size_t y = 0; y < height; ++y) { for (size_t y = 0; y < height; ++y) {
int value = digitalRead(yPins[y]); int value = digitalRead(yPins[y]);
auto &storedKeyState = keyboardState.state(x, y); auto &storedKeyState = keyboardState.state(x, y);
if (storedKeyState != value) { if (storedKeyState != value) {
Serial.print("readRowPins: key is changed "); Serial.print("readRowPins: key is changed ");
Serial.print(x); Serial.print(x);
Serial.print(y); Serial.print(y);
keyboardMap.publishEvent(x, y, value); keyboardMap.publishEvent(x, y, value);
storedKeyState = value; storedKeyState = value;
}
} }
}
} }
void loop() { void loop() {
// Cycle through columns // Cycle through columns
for (size_t x = 0; x < width; ++x) { for (size_t x = 0; x < width; ++x) {
changeColumnPin(x); changeColumnPin(x);
readRowPins(state, x); readRowPins(state, x);
} }
} }

View File

@ -3,9 +3,9 @@
#include "io.h" #include "io.h"
void writeKeyRelease(char key) { void writeKeyRelease(char key) {
// Implement this // Implement this
} }
void writeKeyPress(char key) { void writeKeyPress(char key) {
// Implement this // Implement this
} }

View File

@ -4,28 +4,29 @@
#include "keyboardwritefunctions.h" #include "keyboardwritefunctions.h"
#include "keys.h" #include "keys.h"
//! A class for translating key coordinates to scancodes //! A class for translating key coordinates to scancodes
class KeyboardMap { class KeyboardMap {
size_t _width; size_t _width;
size_t _height; size_t _height;
size_t _size; size_t _size;
const char *_translation; const char *_translation;
public: public:
KeyboardMap(size_t width, size_t height, const char *values) KeyboardMap(size_t width, size_t height, const char *values)
: _width(width), _height(height), _size(width * height), : _width(width), _height(height), _size(width * height),
_translation(values) {} _translation(values) {
}
void publishEvent(size_t x, size_t y, bool keyValue) {
size_t index = x + y * _width; void publishEvent(size_t x, size_t y, bool keyValue) {
if (index < _size) { size_t index = x + y * _width;
auto out = _translation[index]; if (index < _size) {
if (keyValue) { auto out = _translation[index];
writeKeyPress(out); if (keyValue) {
} else { writeKeyPress(out);
writeKeyRelease(out); }
} else {
writeKeyRelease(out);
}
}
} }
}
}; };

View File

@ -2,12 +2,16 @@
template <size_t width, size_t height> template <size_t width, size_t height>
class KeyboardState { class KeyboardState {
char _state[width * height] = {}; char _state[width * height] = {};
public: public:
typedef char state_t; typedef char state_t;
// Todo: Bounds check // 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) const {
state_t &state(size_t x, size_t y) { return _state[y * width + x]; } return _state[y * width + x];
}
state_t &state(size_t x, size_t y) {
return _state[y * width + x];
}
}; };

View File

@ -2,39 +2,39 @@
// Special keys: https://www.arduino.cc/en/Reference/KeyboardModifiers // Special keys: https://www.arduino.cc/en/Reference/KeyboardModifiers
enum Keys { enum Keys {
LEFT_CTRL = 128, LEFT_CTRL = 128,
LEFT_SHIFT = 129, LEFT_SHIFT = 129,
LEFT_ALT = 130, LEFT_ALT = 130,
LEFT_GUI = 131, LEFT_GUI = 131,
RIGHT_CTRL = 132, RIGHT_CTRL = 132,
RIGHT_SHIFT = 133, RIGHT_SHIFT = 133,
RIGHT_ALT = 134, RIGHT_ALT = 134,
RIGHT_GUI = 135, RIGHT_GUI = 135,
UP_ARROW = 218, UP_ARROW = 218,
DOWN_ARROW = 217, DOWN_ARROW = 217,
LEFT_ARROW = 216, LEFT_ARROW = 216,
RIGHT_ARROW = 215, RIGHT_ARROW = 215,
BACKSPACE = 178, BACKSPACE = 178,
TAB = 179, TAB = 179,
RETURN = 176, RETURN = 176,
ESC = 177, ESC = 177,
INSERT = 209, INSERT = 209,
DELETE = 212, DELETE = 212,
PAGE_UP = 211, PAGE_UP = 211,
PAGE_DOWN = 214, PAGE_DOWN = 214,
HOME = 210, HOME = 210,
END = 213, END = 213,
CAPS_LOCK = 193, CAPS_LOCK = 193,
F1 = 194, F1 = 194,
F2 = 195, F2 = 195,
F3 = 196, F3 = 196,
F4 = 197, F4 = 197,
F5 = 198, F5 = 198,
F6 = 199, F6 = 199,
F7 = 200, F7 = 200,
F8 = 201, F8 = 201,
F9 = 202, F9 = 202,
F10 = 203, F10 = 203,
F11 = 204, F11 = 204,
F12 = 205, F12 = 205,
}; };

View File

@ -12,6 +12,3 @@ constexpr size_t height = 5;
const char xPins[width] = {1, 2, 3, 4, 5, 6}; const char xPins[width] = {1, 2, 3, 4, 5, 6};
const char yPins[height] = {8, 9, 10, 11, 12}; const char yPins[height] = {8, 9, 10, 11, 12};

View File

@ -8,12 +8,14 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
std::string scanCodeLookup(int code); // Defined in main
void writeKeyRelease(char key) { void writeKeyRelease(char key) {
cout << "Key released " << key << endl; cout << "Key released " << key << endl;
} }
void writeKeyPress(char key) { void writeKeyPress(char key) {
cout << "keyboard.cpp: key pressed " << key << ", " << static_cast<unsigned>(key) << endl; cout << "keyboard.cpp: key pressed " << key << ", "
<< static_cast<unsigned>(key) << endl;
cout << "that is: " << scanCodeLookup(key) << endl;
} }

View File

@ -8,12 +8,12 @@
#include "keys.h" #include "keys.h"
#include "matgui/application.h" #include "matgui/application.h"
#include "matgui/window.h"
#include "matgui/button.h" #include "matgui/button.h"
#include "matgui/window.h"
// Simulator // Simulator
#include "simio.h"
#include "keyboardwritefunctions.h" #include "keyboardwritefunctions.h"
#include "simio.h"
// The code // The code
#include "io.h" #include "io.h"
@ -23,6 +23,10 @@
using namespace std; using namespace std;
using namespace MatGui; using namespace MatGui;
std::string scanCodeLookup(int code) {
return Application::GetKeyNameFromScancode(code);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
Application app(argc, argv); Application app(argc, argv);
@ -48,12 +52,7 @@ int main(int argc, char **argv) {
window.addChild(std::move(layout)); window.addChild(std::move(layout));
} }
window.frameUpdate.connect([]() { window.frameUpdate.connect([]() { loop(); });
loop();
});
app.mainLoop(); app.mainLoop();
} }

View File

@ -10,7 +10,7 @@
#include <iostream> #include <iostream>
#define HIGH 0x1 #define HIGH 0x1
#define LOW 0x0 #define LOW 0x0
#define INPUT 0x0 #define INPUT 0x0
#define OUTPUT 0x1 #define OUTPUT 0x1
@ -25,9 +25,8 @@
static class SerialPort { static class SerialPort {
public: public:
template <class T> template <class T>
void print(const T& value); void print(const T &value);
} Serial; } Serial;
@ -35,9 +34,8 @@ void digitalWrite(uint8_t pin, uint8_t value);
int digitalRead(uint8_t pin); int digitalRead(uint8_t pin);
void pinMode(uint8_t pin, uint8_t mode); void pinMode(uint8_t pin, uint8_t mode);
template<class T> template <class T>
inline void SerialPort::print(const T& value) inline void SerialPort::print(const T &value) {
{
std::cout << value << std::endl; std::cout << value << std::endl;
} }
@ -46,4 +44,3 @@ namespace simio {
void setSimKey(size_t x, size_t y, uint8_t state); void setSimKey(size_t x, size_t y, uint8_t state);
} }