Archived
1
0

move scanner instantiation from Row_ShiftRegs to sketch, update keybrd_4b_split_keyboard_with_shift_registers.ino

This commit is contained in:
wolfv6 2016-09-06 22:51:36 -06:00
parent ac04d25a36
commit cefecb62a3
6 changed files with 67 additions and 57 deletions

View File

@ -19,7 +19,7 @@ void Scanner_IOE::begin()
/* scan() strobes the row's strobePin and retuns state of port's input pins. /* scan() strobes the row's strobePin and retuns state of port's input pins.
Bitwise variables are 1 bit per key. Bitwise variables are 1 bit per key.
*/ */
uint8_t Scanner_IOE::scan(const uint8_t strobePin) read_pins_t Scanner_IOE::scan(const uint8_t strobePin)
{ {
uint8_t readState; //bitwise, 1 means key is pressed, 0 means released uint8_t readState; //bitwise, 1 means key is pressed, 0 means released

View File

@ -28,6 +28,6 @@ class Scanner_IOE : public ScannerInterface
refPortWrite(refPortWrite), refPortRead(refPortRead) {} refPortWrite(refPortWrite), refPortRead(refPortRead) {}
void init(const uint8_t strobePin); void init(const uint8_t strobePin);
void begin(); void begin();
uint8_t scan(const uint8_t strobePin); read_pins_t scan(const uint8_t strobePin);
}; };
#endif #endif

View File

@ -1,38 +1,50 @@
#include "Scanner_ShiftRegs74HC165.h" #include "Scanner_ShiftRegs74HC165.h"
//constructor Scanner_ShiftRegs74HC165::Scanner_ShiftRegs74HC165(const bool strobeOn, const uint8_t slaveSelect,
Scanner_ShiftRegs74HC165::Scanner_ShiftRegs74HC165(const uint8_t strobePin, uint8_t readPinCount) const uint8_t readPinCount)
: strobePin(strobePin), byte_count(ceil (float(readPinCount)/8)) : strobeOn(strobeOn), strobeOff(!strobeOn), slaveSelect(slaveSelect),
byte_count( ceil(float(readPinCount)/8) )
{ {
//configure controller to communicate with shift register matrix pinMode(slaveSelect, OUTPUT);
pinMode(strobePin, OUTPUT);
pinMode(SHIFT_LOAD, OUTPUT);
} }
/* init() is called once for each row from Row constructor.
Configure controller to communicate with shift register matrix.
*/
void Scanner_ShiftRegs74HC165::init(const uint8_t strobePin)
{
pinMode(strobePin, OUTPUT);
}
/* begin() should be called once from sketch setup().
*/
void Scanner_ShiftRegs74HC165::begin() void Scanner_ShiftRegs74HC165::begin()
{ {
//initialize shift register's shift/load pin //initialize shift register's shift/load pin
digitalWrite(SHIFT_LOAD, HIGH); digitalWrite(slaveSelect, HIGH);
} }
/* scan() strobes the row's strobePin and retuns state of the shift register's input pins. /* scan() strobes the row's strobePin and returns state of the shift register's input pins.
strobePin is Arduino pin number connected to this row
Bitwise variables are 1 bit per key. Bitwise variables are 1 bit per key.
*/ */
read_pins_t Scanner_ShiftRegs74HC165::scan() read_pins_t Scanner_ShiftRegs74HC165::scan(const uint8_t strobePin)
{ {
read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released read_pins_t readState = 0; //bitwise, 1 means key is pressed, 0 means released
//strobe row on //strobe row on
digitalWrite(strobePin, STROBE_ON); digitalWrite(strobePin, strobeOn);
delayMicroseconds(3); //time to stablize voltage //delayMicroseconds(3); //time to stablize voltage
delayMicroseconds(333); //todo
//read all the column pins //read all the column pins
digitalWrite(SHIFT_LOAD, LOW); //load parallel inputs to the register digitalWrite(slaveSelect, LOW); //load parallel inputs to the register
digitalWrite(SHIFT_LOAD, HIGH); //shift the data toward a serial output digitalWrite(slaveSelect, HIGH); //shift the data toward a serial output
SPI.transfer(&readState, byte_count); //SPI.transfer(&readState, byte_count);
SPI.transfer(&readState, 4);//todo
//strobe row off //strobe row off
digitalWrite(strobePin, STROBE_OFF); digitalWrite(strobePin, strobeOff);
return readState; return readState;
} }

View File

@ -5,6 +5,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <config_keybrd.h> #include <config_keybrd.h>
#include <SPI.h> #include <SPI.h>
#include <ScannerInterface.h>
#include <PortWriteInterface.h> #include <PortWriteInterface.h>
#include <PortReadInterface.h> #include <PortReadInterface.h>
@ -17,31 +18,32 @@ Shift register parallel input pins have 10k pull-up resistors powered
Orient diodes with cathode (banded end) towards the write pins (row) Orient diodes with cathode (banded end) towards the write pins (row)
Controller's MISO pin is connected to shift register's complementary serial output (/QH) pin Controller's MISO pin is connected to shift register's complementary serial output (/QH) pin
Use these two lines in the sketch: Use these two lines in the sketch:
const bool Scanner_ShiftRegs74HC165::STROBE_ON = LOW; const bool Scanner_ShiftRegs74HC165::strobeOn = LOW;
const bool Scanner_ShiftRegs74HC165::STROBE_OFF = HIGH; const bool Scanner_ShiftRegs74HC165::strobeOff = HIGH;
For active high: For active high:
Shift register parallel input pins have 10k pull-down resistors grounded Shift register parallel input pins have 10k pull-down resistors grounded
Orient diodes with cathode (banded end) towards the read pins. Orient diodes with cathode (banded end) towards the read pins.
Controller's MISO pin is connected to shift register's serial output (QH) pin Controller's MISO pin is connected to shift register's serial output (QH) pin
Use these two lines in the sketch: Use these two lines in the sketch:
const bool Scanner_ShiftRegs74HC165::STROBE_ON = HIGH; const bool Scanner_ShiftRegs74HC165::strobeOn = HIGH;
const bool Scanner_ShiftRegs74HC165::STROBE_OFF = LOW; const bool Scanner_ShiftRegs74HC165::strobeOff = LOW;
In addition, each row needs to be connected to a strobe pin from the controller. In addition, each row needs to be connected to a strobe pin from the controller.
*/ */
class Scanner_ShiftRegs74HC165 class Scanner_ShiftRegs74HC165 : public ScannerInterface
{ {
private: private:
static const uint8_t SHIFT_LOAD; //controller's pin number that is const bool strobeOn; //logic level of strobe on, active state HIGH or LOW
// connected to shift register's SHIFT_LOAD pin const bool strobeOff; //logic level of strobe off, complement of active state
static const bool STROBE_ON; //logic level of strobe on, active state HIGH or LOW const uint8_t slaveSelect; //controller's pin number that is
static const bool STROBE_OFF; //logic level of strobe off, complement of active state // connected to shift register's SHIFT-LOAD pin
const uint8_t strobePin; //Arduino pin number connected to this row
const uint8_t byte_count; //number of bytes to read from shift registers const uint8_t byte_count; //number of bytes to read from shift registers
public: public:
Scanner_ShiftRegs74HC165(const uint8_t strobePin, const uint8_t readPinCount); Scanner_ShiftRegs74HC165(const bool strobeOn, const uint8_t slaveSelect,
virtual read_pins_t scan(); const uint8_t readPinCount);
void init(const uint8_t strobePin);
void begin(); void begin();
virtual read_pins_t scan(const uint8_t strobePin);
}; };
#endif #endif

View File

@ -13,9 +13,9 @@ Using smaller types on a 32-bit uC (Teensy LC) would accomplish nothing.
For Scanner_ShiftRegs74HC165, Scanner_ShiftRegs74HC165::readPinCount For Scanner_ShiftRegs74HC165, Scanner_ShiftRegs74HC165::readPinCount
For Scanner_IOE, cover the last 1 bit in Scanner_IOE::strobePin For Scanner_IOE, cover the last 1 bit in Scanner_IOE::strobePin
*/ */
typedef uint8_t read_pins_t; //typedef uint8_t read_pins_t;
//typedef uint16_t read_pins_t; //typedef uint16_t read_pins_t;
//typedef uint32_t read_pins_t; typedef uint32_t read_pins_t;
/* SAMPLE_COUNT_MACRO is used in Debouncer_Samples.h /* SAMPLE_COUNT_MACRO is used in Debouncer_Samples.h
SAMPLE_COUNT_MACRO = 4 is very reliable for a keyboard. SAMPLE_COUNT_MACRO = 4 is very reliable for a keyboard.

View File

@ -4,10 +4,10 @@ Tested on Teensy LC and two 74HC165 shift registers.
The right matrix has 2 shift registers daisy chained. The right matrix has 2 shift registers daisy chained.
Layout Layout Layout Layout
| Left |**0** | Right |**0**|**1**|**2**|**3**|**4**|**5**|**6**|**7**| | Left |**0**| | Right |**0**|**1**|**2**|**3**|**4**|**5**|**6**|
|:-----:|-----| |:-----:|-----|-----|-----|-----|-----|-----|-----|-----| |:-----:|-----| |:-----:|-----|-----|-----|-----|-----|-----|-----|
| **0** | x | | **0** | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | **0** | x | | **0** | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| **1** | y | | **1** | a | b | c | d | e | f | g | h | | **1** | y | | **1** | a | b | c | d | e | f | g |
*/ */
// ################## GLOBAL ################### // ################## GLOBAL ###################
// ================= INCLUDES ================== // ================= INCLUDES ==================
@ -15,11 +15,12 @@ The right matrix has 2 shift registers daisy chained.
#include <Code_Sc.h> #include <Code_Sc.h>
//Left matrix //Left matrix
#include <Row_uC.h> #include <Scanner_uC.h>
#include <Row.h>
//Right matrix //Right matrix
#include <SPI.h> #include <SPI.h>//needed?? todo
#include <Row_ShiftRegisters.h> #include <Scanner_ShiftRegs74HC165.h>
// =============== CONFIGURATION =============== // =============== CONFIGURATION ===============
ScanDelay scanDelay(9000); ScanDelay scanDelay(9000);
@ -45,42 +46,37 @@ Code_Sc s_5(KEY_5);
Code_Sc s_6(KEY_6); Code_Sc s_6(KEY_6);
// =============== LEFT MATRIX ================= // =============== LEFT MATRIX =================
//set left matrix for active low uint8_t readPins_L[] = {14};
const bool Scanner_uC::STROBE_ON = LOW; uint8_t readPinCount_L = sizeof(readPins_L)/sizeof(*readPins_L);
const bool Scanner_uC::STROBE_OFF = HIGH;
//column pin Scanner_uC scanner_L(LOW, readPins_L, readPinCount_L); //active LOW
uint8_t readPins[] = {14};
uint8_t READ_PIN_COUNT = sizeof(readPins)/sizeof(*readPins);
//rows //rows
Key* ptrsKeys_L0[] = { &s_x }; Key* ptrsKeys_L0[] = { &s_x };
Row_uC row_L0(0, readPins, READ_PIN_COUNT, ptrsKeys_L0); uint8_t KEY_COUNT_L0 = sizeof(ptrsKeys_L0)/sizeof(*ptrsKeys_L0);
Row row_L0(scanner_L, 0, ptrsKeys_L0, KEY_COUNT_L0);
Key* ptrsKeys_L1[] = { &s_y }; Key* ptrsKeys_L1[] = { &s_y };
Row_uC row_L1(1, readPins, READ_PIN_COUNT, ptrsKeys_L1); uint8_t KEY_COUNT_L1 = sizeof(ptrsKeys_L1)/sizeof(*ptrsKeys_L1);
Row row_L1(scanner_L, 1, ptrsKeys_L1, KEY_COUNT_L1);
// =============== RIGHT MATRIX ================ // =============== RIGHT MATRIX ================
//set matrix to active high //use slaveSelect pin SS (Arduino pin 10) for fastest scan
const bool Scanner_ShiftRegs74HC165::STROBE_ON = HIGH; Scanner_ShiftRegs74HC165 scanner_R(HIGH, SS, 14); //active HIGH
const bool Scanner_ShiftRegs74HC165::STROBE_OFF = LOW;
//chip select pin
const uint8_t Scanner_ShiftRegs74HC165::SHIFT_LOAD = 10;
//rows //rows
Key* ptrsKeys_R0[] = { &s_6, &s_5, &s_4, &s_3, //shift regiser on right Key* ptrsKeys_R0[] = { &s_6, &s_5, &s_4, &s_3, //shift register on right
&s_c, &s_d, &s_e, &s_f, &s_c, &s_d, &s_e, &s_f,
&s_2, &s_1, &s_0, &s_g, //shift regiser on left &s_2, &s_1, &s_0, &s_g, //shift register on left
&s_a, &s_b }; //unused input pins are grounded &s_a, &s_b }; //unused input pins are grounded
Row_ShiftRegisters row_R0(0, sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0), ptrsKeys_R0); Row row_R0(scanner_R, 0, ptrsKeys_R0, sizeof(ptrsKeys_R0)/sizeof(*ptrsKeys_R0));
// ################### MAIN #################### // ################### MAIN ####################
void setup() void setup()
{ {
Keyboard.begin(); Keyboard.begin();
SPI.begin(); SPI.begin();//todo move to begin()
row_R0.begin(); scanner_R.begin();
} }
void loop() void loop()