Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxb committed Nov 2, 2019
0 parents commit 4dac1fe
Show file tree
Hide file tree
Showing 19 changed files with 1,020 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.pio
Empty file added README
Empty file.
Empty file added cpp/README
Empty file.
39 changes: 39 additions & 0 deletions cpp/include/README
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions cpp/lib/README
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions cpp/lib/color_utils/color_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <Arduino.h>

uint16_t rgb_to_12bit(float r, float g, float b) {
return ((uint16_t)(r*15) << 8) | ((uint16_t)(g*15) << 4) | (uint16_t)(b*15);
}

1 change: 1 addition & 0 deletions cpp/lib/color_utils/color_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uint16_t rgb_to_12bit(float r, float g, float b);
110 changes: 110 additions & 0 deletions cpp/lib/keyboard/keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <Arduino.h>
#include "keyboard.h"
//#include <Wire.h>

// this is slightly inefficient because not all 5 rows have 13 keys each and we could have fit escape in there
unsigned long keys_down[5 * 13 + 1];
// this is inefficient because we only need one bit per key
byte keys_pressed[5 * 13 + 1];

#define KEY_REPEAT 200

/*
void requestEvent() {
Wire.write(keys, 8);
for (int i=0; i<8; ++i) {
keys[i] = 0;
}
}
*/

void keyboard_init() {
pinMode(R0, OUTPUT);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
pinMode(R4, OUTPUT);

pinMode(C0, INPUT_PULLDOWN);
pinMode(C1, INPUT_PULLDOWN);
pinMode(C2, INPUT_PULLDOWN);
pinMode(C3, INPUT_PULLDOWN);
pinMode(C4, INPUT_PULLDOWN);
pinMode(C5, INPUT_PULLDOWN);
pinMode(C6, INPUT_PULLDOWN);
pinMode(C7, INPUT_PULLDOWN);
pinMode(C8, INPUT_PULLDOWN);
pinMode(C9, INPUT_PULLDOWN);
pinMode(C10, INPUT_PULLDOWN);
pinMode(C11, INPUT_PULLDOWN);
pinMode(C12, INPUT_PULLDOWN);

pinMode(ESC, INPUT_PULLDOWN);

//Wire.begin();
//Wire.begin(8);
//Wire.onRequest(requestEvent);
}


void key_press(uint8_t index, unsigned long time) {
keys_down[index] = time;
keys_pressed[index] = 1;
}

void key_up(uint8_t index) {
keys_down[index] = 0;
}

void check_key(uint8_t index, byte value) {
if (value == HIGH) {
unsigned long time = millis();

// TODO: we probably don't even need this check if we only have presses and no keydown events as time - 0 will also be >= KEY_REPEAT
if (keys_down[index]) {
// ignore unless it has been long enough to repeat
if (time - keys_down[index] >= KEY_REPEAT) {
key_press(index, time);
}
}
else {
// wasn't down before, is now so it is pressed
// TODO: do we want keydown events too?
key_press(index, time);
}
}
else {
// TODO: we don't technically have to check this until we have keyup events
if (keys_down[index]) {
key_up(index);
}
}
}

void keyboard_poll() {
for (int r=0; r<5; r++) {
// TODO: this can be optimised a lot

digitalWrite(R0, (r == 0) ? HIGH : LOW);
digitalWrite(R1, (r == 1) ? HIGH : LOW);
digitalWrite(R2, (r == 2) ? HIGH : LOW);
digitalWrite(R3, (r == 3) ? HIGH : LOW);
digitalWrite(R4, (r == 4) ? HIGH : LOW);

check_key(r*13 + 0, digitalRead(C0));
check_key(r*13 + 1, digitalRead(C1));
check_key(r*13 + 2, digitalRead(C2));
check_key(r*13 + 3, digitalRead(C3));
check_key(r*13 + 4, digitalRead(C4));
check_key(r*13 + 5, digitalRead(C5));
check_key(r*13 + 6, digitalRead(C6));
check_key(r*13 + 7, digitalRead(C7));
check_key(r*13 + 8, digitalRead(C8));
check_key(r*13 + 9, digitalRead(C9));
check_key(r*13 + 10, digitalRead(C10));
check_key(r*13 + 11, digitalRead(C11));
check_key(r*13 + 12, digitalRead(C12));
}

check_key(5*13, digitalRead(ESC));
}
25 changes: 25 additions & 0 deletions cpp/lib/keyboard/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define R0 PC11
#define R1 PC12
#define R2 PC13
#define R3 PC14
#define R4 PC15
#define C0 PF1
#define C1 PC0
#define C2 PC1
#define C3 PC2
#define C4 PC3
#define C5 PA0
#define C6 PA1
#define C7 PA2
#define C8 PA3
#define C9 PA4
#define C10 PA5
#define C11 PA6
#define C12 PA7
#define ESC PF0

extern unsigned long keys_down[5 * 13 + 1];
extern byte keys_pressed[5 * 13 + 1];

void keyboard_init();
void keyboard_poll();
Loading

0 comments on commit 4dac1fe

Please sign in to comment.