From 230d843457ed47315be155d6616cb1127a4ec9b5 Mon Sep 17 00:00:00 2001 From: Jean Jacques Avril Date: Tue, 28 Sep 2021 23:02:49 +0200 Subject: [PATCH] Keyboard class is almost done --- .gitignore | 5 ++ .vscode/extensions.json | 7 +++ .vscode/settings.json | 55 +++++++++++++++++++++ include/README | 39 +++++++++++++++ lib/README | 46 ++++++++++++++++++ platformio.ini | 17 +++++++ src/Keyboard.cpp | 103 ++++++++++++++++++++++++++++++++++++++++ src/Keyboard.h | 23 +++++++++ src/main.cpp | 26 ++++++++++ test/README | 11 +++++ 10 files changed, 332 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/Keyboard.cpp create mode 100644 src/Keyboard.h create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..67112e1 --- /dev/null +++ b/.vscode/settings.json @@ -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" +} \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -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 diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -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 +#include + +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 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..26851cc --- /dev/null +++ b/platformio.ini @@ -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 diff --git a/src/Keyboard.cpp b/src/Keyboard.cpp new file mode 100644 index 0000000..5956450 --- /dev/null +++ b/src/Keyboard.cpp @@ -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()); +} \ No newline at end of file diff --git a/src/Keyboard.h b/src/Keyboard.h new file mode 100644 index 0000000..628563b --- /dev/null +++ b/src/Keyboard.h @@ -0,0 +1,23 @@ +#ifndef KEYBOARD_CLASS +#define KEYBOARD_CLASS +#include "PCF8574.h" +#include +class Keyboard +{ +private: + unsigned long timeElapsed; + std::queue 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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c84874e --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,26 @@ + +#include +#include +#include "Keyboard.h" + +#define PIN_WIRE_SDA D3 +#define PIN_WIRE_SCL D4 +#include +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"); +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -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