First arduino code

This commit is contained in:
Mattias Lasersköld 2020-02-23 14:48:15 +01:00
parent 56c23df471
commit 4176eac3cd
3 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,11 @@
all: verify
verify:
~/Prog/Program/arduino-1.8.9/arduino --verify keyboard.ino | sed "s/stepper:/stepper.ino:/g"
upload:
~/Prog/Program/arduino-1.8.9/arduino --upload keyboard.ino | sed "s/stepper:/stepper.ino:/g"
#test: testing.cpp stepper.ino
# c++ testing.cpp -std=c++14 -o test -g -pthread

View File

@ -0,0 +1,50 @@
#include "keyboardstate.h"
#include <array>
namespace {
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;
} // namespace
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) {
// Todo: Handle this
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

@ -0,0 +1,15 @@
#pragma once
#include <array>
template <size_t width, size_t height>
class KeyboardState {
std::array<char, width*height> _state = {};
public:
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]; }
};