2016-07-18 02:03:03 +00:00
|
|
|
/* keybrd_2_single-layer.ino
|
2016-05-09 14:05:08 +00:00
|
|
|
|
|
|
|
This sketch:
|
2016-07-18 02:03:03 +00:00
|
|
|
is firmware for a simple 1-layer keyboard
|
2016-09-17 05:45:12 +00:00
|
|
|
runs on two rows and two columns of a breadboard keyboard
|
2016-05-09 14:05:08 +00:00
|
|
|
|
|
|
|
This layout table shows how keys are arranged on the keyboard:
|
|
|
|
|
|
|
|
| Layout | **0** | **1** |
|
|
|
|
|:------:|-------|-------|
|
2016-09-17 05:45:12 +00:00
|
|
|
| **0** | 1 | 2 |
|
|
|
|
| **1** | a | b |
|
2016-05-09 14:05:08 +00:00
|
|
|
|
|
|
|
The layout's row and column numbers are in the headers.
|
|
|
|
Each cell in the table's body represents a key.
|
|
|
|
|
2016-07-18 02:26:00 +00:00
|
|
|
The following sketch is annotated with a walk-through narrative enclosed in comment blocks.
|
2016-09-17 05:45:12 +00:00
|
|
|
Each comment block explains one or two lines of code after the comnent.
|
2016-05-09 14:05:08 +00:00
|
|
|
|
2016-07-18 02:03:03 +00:00
|
|
|
keybrd objects are instantiated under the "GLOBAL" heading.
|
|
|
|
The keyboard runs at the end of the sketch, under the "MAIN" heading.
|
2016-05-09 14:05:08 +00:00
|
|
|
*/
|
|
|
|
// ################## GLOBAL ###################
|
2016-07-18 02:26:00 +00:00
|
|
|
/* ================= INCLUDES ==================
|
2016-05-09 14:05:08 +00:00
|
|
|
All the includes in this sketch are to keybrd library classes.
|
|
|
|
*/
|
|
|
|
#include <Code_Sc.h>
|
2016-09-17 05:45:12 +00:00
|
|
|
#include <Row.h>
|
|
|
|
#include <Scanner_uC.h>
|
|
|
|
#include <ScanDelay.h>
|
2016-05-09 14:05:08 +00:00
|
|
|
|
2016-07-18 02:03:03 +00:00
|
|
|
/* ============ SPEED CONFIGURATION ============
|
2016-05-09 14:05:08 +00:00
|
|
|
Keyboard switches are made of moving contacts.
|
|
|
|
When the contacts close, they bounce apart one or more times before making steady contact.
|
2016-07-18 02:03:03 +00:00
|
|
|
ScanDelay gives the switches time to debounce.
|
2016-09-17 05:45:12 +00:00
|
|
|
ScanDelay specifies microsecond between matrix scans.
|
2016-05-09 14:05:08 +00:00
|
|
|
*/
|
2016-07-18 02:03:03 +00:00
|
|
|
ScanDelay scanDelay(9000);
|
2016-05-09 14:05:08 +00:00
|
|
|
|
2016-09-17 05:45:12 +00:00
|
|
|
/* ================== SCANNER ==================
|
|
|
|
Microcontroller pins 14 and 15 are connected to the matrix columns.
|
2016-05-09 14:05:08 +00:00
|
|
|
sizeof() is used to compute the number of array elements.
|
2016-09-17 05:45:12 +00:00
|
|
|
This eliminates the risk of a programmer forgetting to update a count
|
2016-07-18 02:26:00 +00:00
|
|
|
after adding or removing an element from the array.
|
2016-05-09 14:05:08 +00:00
|
|
|
*/
|
2016-07-18 02:03:03 +00:00
|
|
|
uint8_t readPins[] = {14, 15};
|
2016-09-17 05:45:12 +00:00
|
|
|
uint8_t readPinCount = sizeof(readPins)/sizeof(*readPins);
|
|
|
|
|
|
|
|
/*
|
|
|
|
The first parameter of the scanner constructor defines the logic level for the strobes.
|
|
|
|
"Active low" means that if a switch is pressed (active), the read pin is low.
|
|
|
|
The scanner uses readPins, readPinCount to read the colums.
|
|
|
|
*/
|
|
|
|
Scanner_uC scanner(LOW, readPins, readPinCount);
|
|
|
|
|
|
|
|
/* HOW SCANNER OBJECTS WORK
|
|
|
|
The scanner object strobes a row.
|
|
|
|
If a key is pressed, the LOW strobe pulls that readPin LOW.
|
|
|
|
Then the scanner reads its readPins.
|
|
|
|
*/
|
2016-05-09 14:05:08 +00:00
|
|
|
|
2016-07-18 02:03:03 +00:00
|
|
|
/* =================== CODES ===================
|
|
|
|
Four Codes are instantiated, one for each key in the layout.
|
|
|
|
The Code object names in this sketch start with a "s_" prefix.
|
2016-05-09 14:05:08 +00:00
|
|
|
|
|
|
|
The Code_Sc constructor takes one scancode ("Sc" means "scancode").
|
2016-07-18 02:03:03 +00:00
|
|
|
When Code_Sc is pressed, it sends the scancode.
|
2016-05-09 14:05:08 +00:00
|
|
|
*/
|
|
|
|
Code_Sc s_a(KEY_A);
|
|
|
|
Code_Sc s_b(KEY_B);
|
2016-09-17 05:45:12 +00:00
|
|
|
|
|
|
|
Code_Sc s_1(KEY_1);
|
|
|
|
Code_Sc s_2(KEY_2);
|
2016-05-09 14:05:08 +00:00
|
|
|
|
2016-07-18 02:03:03 +00:00
|
|
|
/* =================== ROWS ====================
|
2016-07-18 02:26:00 +00:00
|
|
|
Here we pack Code objects into Row objects.
|
2016-05-09 14:05:08 +00:00
|
|
|
The Row objects names in this sketch start with a "row_" followed by a row number.
|
|
|
|
|
2016-09-17 05:45:12 +00:00
|
|
|
Row constructor has four parameters:
|
|
|
|
1) scanner
|
|
|
|
2) strobePin connected to the row.
|
|
|
|
3) ptrsKeys[] containing all the Code objects of the row, one Code object per key.
|
|
|
|
4) the number of keys in the row.
|
2016-05-09 14:05:08 +00:00
|
|
|
*/
|
2016-09-17 05:45:12 +00:00
|
|
|
Key* ptrsKeys_0[] = { &s_1, &s_2 };
|
|
|
|
uint8_t keyCount_0 = sizeof(ptrsKeys_0)/sizeof(*ptrsKeys_0);
|
|
|
|
Row row_0(scanner, 0, ptrsKeys_0, keyCount_0);
|
2016-05-09 14:05:08 +00:00
|
|
|
|
2016-09-17 05:45:12 +00:00
|
|
|
Key* ptrsKeys_1[] = { &s_a, &s_b };
|
|
|
|
uint8_t keyCount_1 = sizeof(ptrsKeys_1)/sizeof(*ptrsKeys_1);
|
|
|
|
Row row_1(scanner, 1, ptrsKeys_1, keyCount_1);
|
2016-05-09 14:05:08 +00:00
|
|
|
|
2016-07-18 02:03:03 +00:00
|
|
|
/* ################### MAIN ####################
|
|
|
|
setup() is used to initialize the keyboard firmware. Keyboard.begin() should be called once.
|
2016-05-09 14:05:08 +00:00
|
|
|
*/
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Keyboard.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-07-18 02:03:03 +00:00
|
|
|
loop() continually scans the matrix, one row at a time.
|
2016-07-18 02:26:00 +00:00
|
|
|
Each row object strobes its strobePin and reads the readPins.
|
2016-07-18 02:03:03 +00:00
|
|
|
And when a key press is detected, the row sends the key's scancode.
|
|
|
|
|
|
|
|
scanDelay creates time intervals between matrix scans.
|
2016-09-17 05:45:12 +00:00
|
|
|
The delay is needed so that the debouncer is not overwelmed.
|
2016-05-09 14:05:08 +00:00
|
|
|
*/
|
|
|
|
void loop()
|
|
|
|
{
|
2016-07-18 02:03:03 +00:00
|
|
|
row_0.process();
|
|
|
|
row_1.process();
|
|
|
|
scanDelay.delay();
|
2016-05-09 14:05:08 +00:00
|
|
|
}
|