From 2431659b2bc9b9a927134bb8091f1b1415e865a2 Mon Sep 17 00:00:00 2001 From: wolfv6 Date: Tue, 5 Jul 2016 15:45:58 -0600 Subject: [PATCH] delete RowDelay, replace with scanDelay in loop() --- doc/keybrd_library_developer_guide.md | 4 ++- .../keybrd_shift_register.ino | 5 ++- src/Row_IOE.cpp | 1 - src/Row_IOE.h | 2 -- src/Row_ShiftRegisters.cpp | 1 - src/Row_ShiftRegisters.h | 2 -- src/Row_uC.cpp | 1 - src/Row_uC.h | 2 -- src/{RowDelay.cpp => ScanDelay.cpp} | 4 +-- src/{RowDelay.h => ScanDelay.h} | 31 +++++++++---------- 10 files changed, 24 insertions(+), 29 deletions(-) rename src/{RowDelay.cpp => ScanDelay.cpp} (51%) rename src/{RowDelay.h => ScanDelay.h} (60%) diff --git a/doc/keybrd_library_developer_guide.md b/doc/keybrd_library_developer_guide.md index 0746696..a5b4cd1 100644 --- a/doc/keybrd_library_developer_guide.md +++ b/doc/keybrd_library_developer_guide.md @@ -94,6 +94,7 @@ Keybrd library class inheritance diagram | Debouncer_4Samples + ScanDelay LayerStateInterface | @@ -244,7 +245,6 @@ Refer to it like a table of contents while reading the keybrd library. ``` loop() for each row RowBase::process() - RowBase::wait() delay time for debounce RowScanner_PinsArray::scan() strobe row on for each readPin set rowState bit @@ -257,6 +257,8 @@ Refer to it like a table of contents while reading the keybrd library. if rising edge Key_*::press() scanCode->press() Code_*::press() Keyboard.press(scancode) + scanDelay.delay(); + ``` ## The Arduino libraries diff --git a/examples/keybrd_shift_register/keybrd_shift_register.ino b/examples/keybrd_shift_register/keybrd_shift_register.ino index e4960b9..16bea9d 100644 --- a/examples/keybrd_shift_register/keybrd_shift_register.ino +++ b/examples/keybrd_shift_register/keybrd_shift_register.ino @@ -16,6 +16,7 @@ Layout // ################## GLOBAL ################### // ================= INCLUDES ================== #include +#include //Codes #include @@ -26,7 +27,7 @@ Layout #include // =============== CONFIGURATION =============== -const unsigned int RowDelay::DELAY_MICROSECONDS = 0; //500 +ScanDelay scanDelay(9000); const bool RowScanner_PinsArray::ACTIVE_HIGH = 0; //left matrix is active low const uint8_t RowScanner_SPIShiftRegisters::SHIFT_LOAD = 10; @@ -163,6 +164,8 @@ void loop() row_R0.process(); row_R1.process(); + scanDelay.delay(); + //delay(100); //Keyboard.println(""); //debug.print_microseconds_per_scan(); diff --git a/src/Row_IOE.cpp b/src/Row_IOE.cpp index 0c893bd..f5a2005 100644 --- a/src/Row_IOE.cpp +++ b/src/Row_IOE.cpp @@ -7,7 +7,6 @@ void Row_IOE::process() read_pins_mask_t rowEnd; //1 bit marks positioned after last key of row read_pins_t debouncedChanged; //1 means debounced changed - rowDelay.delay(); rowState = scanner.scan(rowEnd); debouncedChanged = debouncer.debounce(rowState, debounced); pressRelease(rowEnd, debouncedChanged); diff --git a/src/Row_IOE.h b/src/Row_IOE.h index 2670bb5..d2e7c85 100644 --- a/src/Row_IOE.h +++ b/src/Row_IOE.h @@ -2,7 +2,6 @@ #define ROWIOE_H #include -#include #include #include @@ -35,7 +34,6 @@ Number of pins in colPort0 should equal number of keys in ptrsKeys_0[] array. class Row_IOE : public RowBase { private: - RowDelay rowDelay; RowScanner_PinsBitwise scanner; Debouncer_4Samples debouncer; public: diff --git a/src/Row_ShiftRegisters.cpp b/src/Row_ShiftRegisters.cpp index e7fcd79..a0dc898 100644 --- a/src/Row_ShiftRegisters.cpp +++ b/src/Row_ShiftRegisters.cpp @@ -7,7 +7,6 @@ void Row_ShiftRegisters::process() read_pins_mask_t rowEnd; //1 bit marks positioned after last key of row read_pins_t debouncedChanged; //1 means debounced changed - rowDelay.delay(); rowState = scanner.scan(rowEnd); debouncedChanged = debouncer.debounce(rowState, debounced); pressRelease(rowEnd, debouncedChanged); diff --git a/src/Row_ShiftRegisters.h b/src/Row_ShiftRegisters.h index 3cb7441..5521897 100644 --- a/src/Row_ShiftRegisters.h +++ b/src/Row_ShiftRegisters.h @@ -2,7 +2,6 @@ #define ROW_SHIFTREGISTERS_H #include -#include #include #include //#include @@ -28,7 +27,6 @@ Number of pins in colPort0 should equal number of keys in ptrsKeys_0[] array. class Row_ShiftRegisters : public RowBase { private: - RowDelay rowDelay; RowScanner_SPIShiftRegisters scanner; Debouncer_4Samples debouncer; //Debouncer_Not debouncer; //passed test diff --git a/src/Row_uC.cpp b/src/Row_uC.cpp index da08286..a147f06 100644 --- a/src/Row_uC.cpp +++ b/src/Row_uC.cpp @@ -10,7 +10,6 @@ void Row_uC::process() read_pins_mask_t rowEnd; //1 bit marks positioned after last key of row read_pins_t debouncedChanged; //1 means debounced changed - rowDelay.delay(); rowState = scanner.scan(rowEnd); debouncedChanged = debouncer.debounce(rowState, debounced); pressRelease(rowEnd, debouncedChanged); diff --git a/src/Row_uC.h b/src/Row_uC.h index 9af0f15..3c2f625 100644 --- a/src/Row_uC.h +++ b/src/Row_uC.h @@ -2,7 +2,6 @@ #define ROW_H #include -#include #include #include @@ -30,7 +29,6 @@ Number of colPins should equal number of keys in ptrsKeys_0[] array. class Row_uC : public RowBase { private: - RowDelay rowDelay; RowScanner_PinsArray scanner; Debouncer_4Samples debouncer; public: diff --git a/src/RowDelay.cpp b/src/ScanDelay.cpp similarity index 51% rename from src/RowDelay.cpp rename to src/ScanDelay.cpp index 3d40c39..e46c713 100644 --- a/src/RowDelay.cpp +++ b/src/ScanDelay.cpp @@ -1,6 +1,6 @@ -#include "RowDelay.h" +#include "ScanDelay.h" -void RowDelay::delay() +void ScanDelay::delay() { delayMicroseconds(DELAY_MICROSECONDS); } diff --git a/src/RowDelay.h b/src/ScanDelay.h similarity index 60% rename from src/RowDelay.h rename to src/ScanDelay.h index 1b39120..2828eaa 100644 --- a/src/RowDelay.h +++ b/src/ScanDelay.h @@ -1,18 +1,16 @@ -#ifndef ROWDELAY_H -#define ROWDELAY_H +#ifndef SCANDELAY_H +#define SCANDELAY_H #include #include -/* RowDelay() delay's scan to give switches time to debounce. -For fastest response time, RowDelay() should be placed before scan() or after pressRelease() - (RowDelay() between scan and send would unnecessarily delay send). +/* ScanDelay() delay's scan to give switches time to debounce. DELAY_MICROSECONDS explained ---------------------------- A keyboard with a faster scan rate responds faster. Follow these step to tune DELAY_MICROSECONDS for maximum scan rate for a given SAMPLE_COUNT: -Initialize DELAY_MICROSECONDS in your sketch: - const unsigned int RowDelay::DELAY_MICROSECONDS = 1000; +Intantiate DELAY_MICROSECONDS in your sketch: + ScanDelay scanDelay(1000); Add this to the sketch's loop() function: debug.print_microseconds_per_scan(); Compile and load the sketch into the microcontroller; microseconds_per_scan is printed every second. @@ -24,22 +22,23 @@ Cherry MX specifies 5msec bounce time http://www.cherrycorp.com/english/switches hasu measured Cherry MX bounce times .3ms to 1.4ms http://geekhack.org/index.php?topic=42385.0 Tactile switch MJTP series bounce 10 ms http://www.apem.com/files/apem/brochures/MJTP_6MM.pdf +The largest allowable DELAY_MICROSECONDS is 65535 (65.535 ms). + Avoid sampling the switch input at a rate synchronous to events in the environment that might create periodic EMI. For instance, 50 and 60 Hz. -The largest allowable DELAY_MICROSECONDS is 65535 (.065535 seconds). +Polling I2C may slow the scan rate enough so that no additional delay is needed +and scanDelay(0) can be omitted from the loop(). -Polling I2C may slow the scan rate enough so that no additional delay is needed: - const unsigned int RowDelay::DELAY_MICROSECONDS = 0; - -DELAY_MICROSECONDS is static so multiple row types can share it. -For example, primary and secondary matrices would share the same DELAY_MICROSECONDS. +Could put delay directly in loop(), but then it's harder to finding this documentation. + delay(5); */ -class RowDelay +class ScanDelay { private: - static const unsigned int DELAY_MICROSECONDS; //delay between each Row scan for debouncing - public: + const unsigned int DELAY_MICROSECONDS; //delay each loop() for debouncing + public: + ScanDelay(const unsigned int DELAY_MICROSECONDS): DELAY_MICROSECONDS(DELAY_MICROSECONDS) {} void delay(); }; #endif