Skip to content

Commit

Permalink
Macro Keypad (#7)
Browse files Browse the repository at this point in the history
* initial

* Fix code style issues with clang_format

* Final fixes and bindings

* Add final buttons

---------

Co-authored-by: Lint Action <lint-action@samuelmeuli.com>
  • Loading branch information
having11 and lint-action authored Apr 21, 2024
1 parent 136d875 commit 870a912
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 0 deletions.
5 changes: 5 additions & 0 deletions macro-keypad/macro-keypad/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions macro-keypad/macro-keypad/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
53 changes: 53 additions & 0 deletions macro-keypad/macro-keypad/include/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <Arduino.h>

constexpr uint8_t ColCount = 3;
constexpr uint8_t RowCount = 5;

namespace Pins {
constexpr uint8_t Led = 27;

static const uint8_t ColumnPins[ColCount] = {
0,
1,
2,
};
static const uint8_t RowPins[RowCount] = {
18, 19, 20, 21, 22,
};
} // namespace Pins

namespace Keys {
constexpr uint16_t DebounceDelay = 50;

enum class GamepadButton {
None = -1,
ButtonA = 0,
ButtonB = 1,
ButtonX = 2,
ButtonY = 3,
ButtonBumperLeft = 4,
ButtonBumperRight = 5,
ButtonBack = 6,
ButtonStart = 7,
ButtonLeftStick = 8,
ButtonRightStick = 9,
ButtonVirtual0 = 15,
ButtonVirtual1 = 16,
ButtonVirtual2 = 17,
ButtonVirtual3 = 18,
ButtonVirtual4 = 19,
ButtonVirtual5 = 20
};
} // namespace Keys

typedef struct {
uint8_t currentState;
uint8_t lastSteadyState = 0;
uint8_t lastFlickerState = 0;
Keys::GamepadButton button = Keys::GamepadButton::None;
uint32_t timestamp = 0;
} KeypadKey_t;

static KeypadKey_t keys[RowCount][ColCount];
39 changes: 39 additions & 0 deletions macro-keypad/macro-keypad/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 macro-keypad/macro-keypad/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
18 changes: 18 additions & 0 deletions macro-keypad/macro-keypad/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; 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:pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
board_build.core = earlephilhower
board_build.mcu = rp2040
board_build.f_cpu = 133000000L
upload_port = E:
115 changes: 115 additions & 0 deletions macro-keypad/macro-keypad/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <Arduino.h>
#include <Joystick.h>

#include "Constants.h"

using namespace Keys;

// Col 0 starts on the left
// Row 0 starts at the top

// Row 4 column 1 is not connected to anything

static GamepadButton keyMapping[RowCount][ColCount] = {
/* Row 0 */ {
GamepadButton::ButtonVirtual0,
GamepadButton::ButtonVirtual1,
GamepadButton::ButtonVirtual2,
},
/* Row 1 */
{
GamepadButton::ButtonX,
GamepadButton::ButtonB,
GamepadButton::ButtonA,
},
/* Row 2 */
{
GamepadButton::ButtonY,
GamepadButton::ButtonVirtual4,
GamepadButton::ButtonVirtual5,
},
/* Row 3 */
{
GamepadButton::ButtonBumperLeft,
GamepadButton::ButtonBumperRight,
GamepadButton::ButtonLeftStick,
},
/* Row 4 */
{
GamepadButton::ButtonRightStick,
GamepadButton::ButtonRightStick,
GamepadButton::ButtonVirtual3,
},
};

static Joystick_ gamepad;

static void configureKeys(void);
static void processKey(uint8_t, uint8_t);

void setup() {
pinMode(Pins::Led, OUTPUT);
digitalWriteFast(Pins::Led, HIGH);

for (uint8_t i = 0; i < ColCount; i++) {
pinMode(Pins::ColumnPins[i], OUTPUT);
digitalWriteFast(Pins::ColumnPins[i], LOW);
}

for (uint8_t i = 0; i < RowCount; i++) {
pinMode(Pins::RowPins[i], INPUT_PULLDOWN);
}

configureKeys();

gamepad.begin();
}

void loop() {
for (uint8_t col = 0; col < ColCount; col++) {
digitalWriteFast(Pins::ColumnPins[col], HIGH);
for (uint8_t row = 0; row < RowCount; row++) {
processKey(row, col);
}
digitalWriteFast(Pins::ColumnPins[col], LOW);
}
}

void configureKeys() {
for (uint8_t row = 0; row < RowCount; row++) {
for (uint8_t col = 0; col < ColCount; col++) {
keys[row][col] = {.currentState = 0,
.lastSteadyState = 0,
.lastFlickerState = 0,
.button = keyMapping[row][col],
.timestamp = 0};
}
}
}

void processKey(uint8_t row, uint8_t col) {
auto *key = &keys[row][col];

if (key->button == GamepadButton::None) {
return;
}

key->currentState = digitalRead(Pins::RowPins[row]);

if (key->currentState != key->lastFlickerState) {
key->timestamp = millis();
key->lastFlickerState = key->currentState;
}

if ((millis() - key->timestamp) > DebounceDelay) {
if (key->lastSteadyState && !key->currentState) {
// Released
gamepad.setButton(static_cast<uint8_t>(key->button), 0);
} else if (!key->lastSteadyState && key->currentState) {
// Pressed
gamepad.setButton(static_cast<uint8_t>(key->button), 1);
}

key->lastSteadyState = key->currentState;
}
}
11 changes: 11 additions & 0 deletions macro-keypad/macro-keypad/test/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

This directory is intended for PlatformIO Test Runner 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/en/latest/advanced/unit-testing/index.html

0 comments on commit 870a912

Please sign in to comment.