Keyboard class is almost done
This commit is contained in:
commit
230d843457
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
7
.vscode/extensions.json
vendored
Normal file
7
.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
]
|
||||
}
|
55
.vscode/settings.json
vendored
Normal file
55
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"sstream": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"set": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"string": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ranges": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
},
|
||||
"C_Cpp.errorSquiggles": "Disabled"
|
||||
}
|
39
include/README
Normal file
39
include/README
Normal file
@ -0,0 +1,39 @@
|
||||
|
||||
This directory is intended for project header files.
|
||||
|
||||
A header file is a file containing C declarations and macro definitions
|
||||
to be shared between several project source files. You request the use of a
|
||||
header file in your project source file (C, C++, etc) located in `src` folder
|
||||
by including it, with the C preprocessing directive `#include'.
|
||||
|
||||
```src/main.c
|
||||
|
||||
#include "header.h"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Including a header file produces the same results as copying the header file
|
||||
into each source file that needs it. Such copying would be time-consuming
|
||||
and error-prone. With a header file, the related declarations appear
|
||||
in only one place. If they need to be changed, they can be changed in one
|
||||
place, and programs that include the header file will automatically use the
|
||||
new version when next recompiled. The header file eliminates the labor of
|
||||
finding and changing all the copies as well as the risk that a failure to
|
||||
find one copy will result in inconsistencies within a program.
|
||||
|
||||
In C, the usual convention is to give header files names that end with `.h'.
|
||||
It is most portable to use only letters, digits, dashes, and underscores in
|
||||
header file names, and at most one dot.
|
||||
|
||||
Read more about using header files in official GCC documentation:
|
||||
|
||||
* Include Syntax
|
||||
* Include Operation
|
||||
* Once-Only Headers
|
||||
* Computed Includes
|
||||
|
||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
46
lib/README
Normal file
46
lib/README
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into executable file.
|
||||
|
||||
The source code of each library should be placed in a an own separate directory
|
||||
("lib/your_library_name/[here are source files]").
|
||||
|
||||
For example, see a structure of the following two libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
and a contents of `src/main.c`:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
17
platformio.ini
Normal file
17
platformio.ini
Normal file
@ -0,0 +1,17 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
xreef/PCF8574 library@^2.2.1
|
||||
marcoschwartz/LiquidCrystal_I2C@^1.1.4
|
103
src/Keyboard.cpp
Normal file
103
src/Keyboard.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
#include "Keyboard.h"
|
||||
//#define DEBUG
|
||||
#define PIN_WIRE_SDA D3
|
||||
#define PIN_WIRE_SCL D4
|
||||
Keyboard::Keyboard(uint8_t debounce)
|
||||
{
|
||||
this->debounce = debounce;
|
||||
}
|
||||
void Keyboard::begin(TwoWire *databus)
|
||||
{
|
||||
pcf8574 = new PCF8574(databus, 0x27, PIN_WIRE_SDA, PIN_WIRE_SCL);
|
||||
pcf8574->pinMode(0, OUTPUT);
|
||||
for (int i = 1; i < 8; i++)
|
||||
{
|
||||
if (i < 5)
|
||||
pcf8574->pinMode(i, INPUT_PULLUP);
|
||||
else
|
||||
pcf8574->pinMode(i, OUTPUT);
|
||||
}
|
||||
|
||||
Serial.print("Init pcf8574...");
|
||||
if (pcf8574->begin())
|
||||
Serial.println("OK");
|
||||
else
|
||||
Serial.println("KO");
|
||||
|
||||
pcf8574->digitalWrite(1, HIGH);
|
||||
for (int i = 5; i < 8; i++)
|
||||
pcf8574->digitalWrite(i, HIGH);
|
||||
}
|
||||
Keyboard::~Keyboard()
|
||||
{
|
||||
if (pcf8574 != nullptr)
|
||||
delete pcf8574;
|
||||
}
|
||||
void Keyboard::scan()
|
||||
{
|
||||
if (millis() < timeElapsed + this->debounce)
|
||||
return 0;
|
||||
uint8_t key = 0;
|
||||
for (int i = 7; i > 4; i--) // Columns
|
||||
{
|
||||
pcf8574->digitalWrite(i, LOW);
|
||||
for (int j = 1; j < 5; j++)
|
||||
{ // Rows
|
||||
key++;
|
||||
uint8_t val = pcf8574->digitalRead(j);
|
||||
#ifdef DEBUG
|
||||
Serial.println("i=" + String(i) + " j=" + String(j) + " v=" + String(val));
|
||||
delay(500);
|
||||
#endif
|
||||
if (val == 0)
|
||||
{
|
||||
timeElapsed = millis();
|
||||
this->lastKey = key;
|
||||
this->buffer.push(mapChr(key));
|
||||
}
|
||||
}
|
||||
pcf8574->digitalWrite(i, HIGH);
|
||||
}
|
||||
}
|
||||
char Keyboard::mapChr(uint8_t key)
|
||||
{
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case 1:
|
||||
return '1';
|
||||
case 2:
|
||||
return '4';
|
||||
case 3:
|
||||
return '7';
|
||||
case 4:
|
||||
return 'O';
|
||||
case 5:
|
||||
return '5';
|
||||
case 6:
|
||||
return '1';
|
||||
case 7:
|
||||
return '8';
|
||||
case 8:
|
||||
return '0';
|
||||
case 9:
|
||||
return '3';
|
||||
case 10:
|
||||
return '6';
|
||||
case 11:
|
||||
return '9';
|
||||
case 12:
|
||||
return 'X';
|
||||
|
||||
default:
|
||||
return 'F';
|
||||
}
|
||||
}
|
||||
char Keyboard::getChr()
|
||||
{
|
||||
return this->getChr(this->lastKey);
|
||||
}
|
||||
|
||||
bool Keyboard::available(){
|
||||
return (!this->buffer.empty());
|
||||
}
|
23
src/Keyboard.h
Normal file
23
src/Keyboard.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef KEYBOARD_CLASS
|
||||
#define KEYBOARD_CLASS
|
||||
#include "PCF8574.h"
|
||||
#include <bits/stdc++.h>
|
||||
class Keyboard
|
||||
{
|
||||
private:
|
||||
unsigned long timeElapsed;
|
||||
std::queue<char> buffer;
|
||||
uint8_t debounce;
|
||||
PCF8574* pcf8574;
|
||||
int lastKey;
|
||||
/* data */
|
||||
public:
|
||||
Keyboard(uint8_t debounce);
|
||||
~Keyboard();
|
||||
void scan();
|
||||
void begin(TwoWire *databus);
|
||||
static char getChr(uint8_t key);
|
||||
char getChr();
|
||||
bool available();
|
||||
};
|
||||
#endif
|
26
src/main.cpp
Normal file
26
src/main.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include "Keyboard.h"
|
||||
|
||||
#define PIN_WIRE_SDA D3
|
||||
#define PIN_WIRE_SCL D4
|
||||
#include <LiquidCrystal_I2C.h>
|
||||
TwoWire databus;
|
||||
Keyboard keyboard(200);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.print("Starting");
|
||||
delay(500);
|
||||
keyboard.begin(&databus);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
char key = keyboard.getKey();
|
||||
if(key!=0)
|
||||
Serial.println("Key " + String(key) + "was pressed");
|
||||
}
|
11
test/README
Normal file
11
test/README
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
This directory is intended for PlatformIO Unit Testing and project tests.
|
||||
|
||||
Unit Testing is a software testing method by which individual units of
|
||||
source code, sets of one or more MCU program modules together with associated
|
||||
control data, usage procedures, and operating procedures, are tested to
|
||||
determine whether they are fit for use. Unit testing finds problems early
|
||||
in the development cycle.
|
||||
|
||||
More information about PlatformIO Unit Testing:
|
||||
- https://docs.platformio.org/page/plus/unit-testing.html
|
Loading…
x
Reference in New Issue
Block a user