Started creating a keyboard simulator

This commit is contained in:
Mattias Lasersköld 2020-03-03 21:50:24 +01:00
parent c4a3ddac69
commit f76fe8a787
15 changed files with 306 additions and 63 deletions

8
.gitignore vendored
View File

@ -1,2 +1,10 @@
*.sch-bak *.sch-bak
*.kicad_pcb-bak *.kicad_pcb-bak
*.o
*.d
software/arduino/.cproject
software/arduino/.project
software/arduino/.settings/
.vscode/

3
.gitmodules vendored
View File

@ -22,3 +22,6 @@
[submodule "hardware/lib/biacco42_promicro"] [submodule "hardware/lib/biacco42_promicro"]
path = hardware/lib/biacco42_promicro path = hardware/lib/biacco42_promicro
url = https://github.com/Biacco42/ProMicroKiCad.git url = https://github.com/Biacco42/ProMicroKiCad.git
[submodule "software/arduino/sim/matgui"]
path = software/arduino/sim/matgui
url = https://github.com/mls-m5/matgui

3
Makefile Normal file
View File

@ -0,0 +1,3 @@
all:
make -C software/arduino/keyboard

View File

@ -2,7 +2,7 @@
all: verify all: verify
verify: verify:
~/Prog/Program/arduino-1.8.9/arduino --verify keyboard.ino | sed "s/stepper:/stepper.ino:/g" ~/Prog/Program/arduino-1.8.9/arduino --verify keyboard.ino -v | sed "s/stepper:/stepper.ino:/g"
upload: upload:
~/Prog/Program/arduino-1.8.9/arduino --upload keyboard.ino | sed "s/stepper:/stepper.ino:/g" ~/Prog/Program/arduino-1.8.9/arduino --upload keyboard.ino | sed "s/stepper:/stepper.ino:/g"

View File

@ -0,0 +1,65 @@
/*
* io.h
*
* Created on: 3 mars 2020
* Author: Mattias Larsson Sköld
*/
#pragma once
#include "keyboardmap.h"
#include "keyboardstate.h"
#include "pins.h"
// Standad layer
const char *keyConfig1 = "§12345"
"\tQWERT"
"\177ASDFG"
"\129<ZXCV"
"\128\131\130 ";
typedef KeyboardState<width, height> keyboard_state_t;
keyboard_state_t state;
KeyboardMap keyboardMap(width, height, keyConfig1);
void setup() {
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);
}
}
void readRowPins(keyboard_state_t &keyboardState, size_t x) {
for (size_t y = 0; y < height; ++y) {
int value = digitalRead(yPins[y]);
auto &keyState = keyboardState.state(x, y);
if (keyState != value) {
Serial.print("readRowPins: key is changed ");
Serial.print(x);
Serial.print(y);
keyboardMap.publishEvent(x, y, keyState);
keyState = value;
}
}
}
void loop() {
// Cycle through columns
for (size_t x = 0; x < width; ++x) {
changeColumnPin(x);
readRowPins(state, x);
}
}

View File

@ -1,27 +1,6 @@
#include "keyboardmap.h"
#include "keyboardstate.h"
#include <array>
namespace {
const char *keyConfig1 = "§12345" #include "io.h"
"\tQWERT"
"\177ASDFG"
"\129<ZXCV"
"\128\131\130 ";
constexpr size_t width = 6;
constexpr size_t height = 5;
const std::array<char, width> xPins = {};
const std::array<char, height> yPins = {};
typedef KeyboardState<width, height> keyboard_state_t;
keyboard_state_t state;
KeyboardMap keyboardMap(width, height, keyConfig1);
} // namespace
void writeKeyRelease(char key) { void writeKeyRelease(char key) {
// Implement this // Implement this
@ -30,38 +9,3 @@ void writeKeyRelease(char key) {
void writeKeyPress(char key) { void writeKeyPress(char key) {
// Implement this // Implement this
} }
void setup() {
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 < xPins.size(); ++i) {
digitalWrite(xPins[i], i == column);
}
}
void readRowPins(keyboard_state_t &keyboardState, size_t x) {
for (size_t y = 0; y < yPins.size(); ++y) {
int value = digitalRead(yPins[y]);
auto &keyState = keyboardState.state(x, y);
if (keyState != value) {
keyboardMap.publishEvent(x, y, keyState);
Serial.print("key is changed");
keyState = value;
}
}
}
void loop() {
// Cycle through columns
for (size_t x = 0; x < width; ++x) {
changeColumnPin(x);
readRowPins(state, x);
}
}

View File

@ -1,10 +1,8 @@
#pragma once #pragma once
#include <array>
template <size_t width, size_t height> template <size_t width, size_t height>
class KeyboardState { class KeyboardState {
std::array<char, width*height> _state = {}; char _state[width * height] = {};
public: public:
typedef char state_t; typedef char state_t;

View File

@ -0,0 +1,17 @@
/*
* pins.h
*
* Created on: 3 mars 2020
* Author: Mattias Larsson Sköld
*/
#pragma once
constexpr size_t width = 6;
constexpr size_t height = 5;
const char xPins[width] = {1, 2, 3, 4, 5, 6};
const char yPins[height] = {8, 9, 10, 11, 12};

View File

@ -0,0 +1,18 @@
# using matmake
# https://github.com/mls-m5/matmake
config +=
debug
c++14
includes +=
../keyboard
sysincludes +=
matgui/include
sim.src = *.cpp
sim.src += matgui/src/*.cpp
sim.libs += -lGL -lSDL2 -lSDL2_image

View File

@ -0,0 +1,19 @@
/*
* keyboard.cpp
*
* Created on: 3 mars 2020
* Author: Mattias Larsson Sköld
*/
#include <iostream>
using namespace std;
void writeKeyRelease(char key) {
cout << "Key released " << key << endl;
}
void writeKeyPress(char key) {
cout << "keyboard.cpp: key pressed " << key << ", " << static_cast<unsigned>(key) << endl;
}

View File

@ -0,0 +1,59 @@
/*
* main.cpp
*
* Created on: 3 mars 2020
* Author: Mattias Larsson Sköld
*/
#include "keys.h"
#include "matgui/application.h"
#include "matgui/window.h"
#include "matgui/button.h"
// Simulator
#include "simio.h"
#include "keyboardwritefunctions.h"
// The code
#include "io.h"
#include <iostream>
using namespace std;
using namespace MatGui;
int main(int argc, char **argv) {
Application app(argc, argv);
Window window("keyboard simulator");
setup();
for (size_t y = 0; y < height; ++y) {
auto layout = make_unique<LinearLayout>();
layout->orientation(LayoutOrientation::LAYOUT_HORIZONTAL);
for (size_t x = 0; x < width; ++x) {
auto button = make_unique<Button>("hej");
button->pointerDown.connect([x, y](Button::PointerArgument arg) {
cout << "clicked " << x << ", " << y << endl;
simio::setSimKey(x, y, true);
});
button->pointerUp.connect([x, y](Button::PointerArgument arg) {
cout << "main.cpp: released " << x << ", " << y << endl;
simio::setSimKey(x, y, false);
});
layout->addChild(std::move(button));
}
window.addChild(std::move(layout));
}
window.frameUpdate.connect([]() {
loop();
});
app.mainLoop();
}

@ -0,0 +1 @@
Subproject commit 1b6ba9f2d5b2954975eac090895a32e5c3a51027

BIN
software/arduino/sim/sim Executable file

Binary file not shown.

View File

@ -0,0 +1,59 @@
/*
* simio.cpp
*
* Created on: 3 mars 2020
* Author: Mattias Larsson Sköld
*/
#include "simio.h"
#include <array>
#include "pins.h"
#include <algorithm>
using namespace simio;
namespace {
std::array<uint8_t, 255> pinState = {};
std::array<uint8_t, width * height> simKeyState = {};
auto getSimKey(size_t x, size_t y) {
return simKeyState.at(x + y * width);
}
} // namespace
void digitalWrite(uint8_t pin, uint8_t value) {
pinState.at(pin) = value;
}
int digitalRead(uint8_t ypin) {
auto found = std::find(yPins, yPins + height, ypin);
if (found == yPins + height) {
throw "not a valid input pin";
}
size_t y = found - yPins;
for (int x = 0; x < width; ++x) {
if (getSimKey(x, y) * pinState.at(xPins[x])) {
return true;
}
}
return false;
}
void pinMode(uint8_t pin, uint8_t mode) {
// Does not make so much sense with
}
namespace simio {
void setSimKey(size_t x, size_t y, uint8_t state) {
simKeyState.at(x + y * width) = state;
}
}

View File

@ -0,0 +1,49 @@
/*
* simio.h
*
* Created on: 3 mars 2020
* Author: Mattias Larsson Sköld
*/
#pragma once
#include <iostream>
#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x0
#define OUTPUT 0x1
#define INPUT_PULLUP 0x2
#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
#define TWO_PI 6.283185307179586476925286766559
#define DEG_TO_RAD 0.017453292519943295769236907684886
#define RAD_TO_DEG 57.295779513082320876798154814105
#define EULER 2.718281828459045235360287471352
static class SerialPort {
public:
template <class T>
void print(const T& value);
} Serial;
void digitalWrite(uint8_t pin, uint8_t value);
int digitalRead(uint8_t pin);
void pinMode(uint8_t pin, uint8_t mode);
template<class T>
inline void SerialPort::print(const T& value)
{
std::cout << value << std::endl;
}
namespace simio {
void setSimKey(size_t x, size_t y, uint8_t state);
}