Add files via upload

This commit is contained in:
Frank Adams 2019-11-30 09:49:27 -08:00 committed by GitHub
parent ac3c99cd5e
commit 7f7e6a7408
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 490 additions and 0 deletions

View file

@ -0,0 +1,405 @@
/* Copyright 2019 Frank Adams
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This software implements a Sony Vaio P Laptop USB Keyboard Controller using a Teensy 3.2
//
// Revision History
// Initial Release Nov 30, 2019
//
#define MODIFIERKEY_FN 0x8f // give Fn key a fake HID code
#define CAPS_LED 13 // Teensy LED on IO#13 shows Caps-Lock
//
const byte rows_max = 16; // sets the number of rows in the matrix
const byte cols_max = 8; // sets the number of columns in the matrix
//
// Load the normal key matrix with the Teensyduino key names
// described at www.pjrc.com/teensy/td_keyboard.html
// A zero indicates no normal key at that location.
int normal[rows_max][cols_max] = {
{KEY_RIGHT,KEY_LEFT,KEY_DOWN,0,0,0,0,KEY_UP},
{0,0,KEY_BACKSLASH,KEY_INSERT,KEY_DELETE,KEY_BACKSPACE,0,KEY_ENTER},
{KEY_QUOTE,0,0,KEY_NUM_LOCK,KEY_PRINTSCREEN,KEY_EQUAL,0,KEY_RIGHT_BRACE},
{KEY_L,KEY_COMMA,KEY_PERIOD,KEY_F10,KEY_F11,KEY_9,KEY_0,KEY_O},
{KEY_J,KEY_M,KEY_B,KEY_F8,KEY_F9,KEY_8,KEY_U,KEY_I},
{KEY_H,KEY_N,KEY_K,KEY_F7,KEY_6,KEY_7,KEY_Y,KEY_G},
{KEY_F,KEY_V,KEY_C,KEY_F5,KEY_F6,KEY_5,KEY_R,KEY_T},
{KEY_D,KEY_X,KEY_A,KEY_F3,KEY_F4,KEY_3,KEY_4,KEY_E},
{KEY_W,KEY_Z,KEY_S,KEY_F1,KEY_F2,KEY_1,KEY_2,KEY_Q},
{0,KEY_MENU,KEY_SLASH,KEY_F12,KEY_MINUS,KEY_P,KEY_LEFT_BRACE,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{KEY_SPACE,0,0,KEY_ESC,KEY_TILDE,KEY_TAB,KEY_CAPS_LOCK,KEY_SEMICOLON},
{0,0,0,0,0,0,0,0}
};
// Load the numlock key matrix with key names at the correct row-column location.
// This matrix is the same as the normal matrix except for the number pad keys
// A zero indicates no numlock key at that location.
int numlock[rows_max][cols_max] = {
{KEY_RIGHT,KEY_LEFT,KEY_DOWN,0,0,0,0,KEY_UP},
{0,0,KEY_BACKSLASH,KEY_INSERT,KEY_DELETE,KEY_BACKSPACE,0,KEY_ENTER},
{KEY_QUOTE,0,0,KEY_NUM_LOCK,KEY_PRINTSCREEN,KEY_EQUAL,0,KEY_RIGHT_BRACE},
{KEYPAD_3,KEY_COMMA,KEYPAD_PERIOD,KEY_F10,KEY_F11,KEYPAD_9,KEYPAD_SLASH,KEYPAD_6},
{KEYPAD_1,KEYPAD_0,KEY_B,KEY_F8,KEY_F9,KEYPAD_8,KEYPAD_4,KEYPAD_5},
{KEY_H,KEY_N,KEYPAD_2,KEY_F7,KEY_6,KEYPAD_7,KEY_Y,KEY_G},
{KEY_F,KEY_V,KEY_C,KEY_F5,KEY_F6,KEY_5,KEY_R,KEY_T},
{KEY_D,KEY_X,KEY_A,KEY_F3,KEY_F4,KEY_3,KEY_4,KEY_E},
{KEY_W,KEY_Z,KEY_S,KEY_F1,KEY_F2,KEY_1,KEY_2,KEY_Q},
{0,KEY_MENU,KEYPAD_PLUS,KEY_F12,KEY_MINUS,KEYPAD_ASTERIX,KEYPAD_ENTER,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{KEY_SPACE,0,0,KEY_ESC,KEY_TILDE,KEY_TAB,KEY_CAPS_LOCK,KEYPAD_MINUS},
{0,0,0,0,0,0,0,0}
};
// Load the modifier key matrix with key names at the correct row-column location.
// A zero indicates no modifier key at that location.
int modifier[rows_max][cols_max] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{MODIFIERKEY_RIGHT_SHIFT,0,0,0,0,MODIFIERKEY_LEFT_SHIFT,0,0},
{0,0,0,0,MODIFIERKEY_FN,0,0,0},
{0,MODIFIERKEY_RIGHT_ALT,0,0,0,0,0,MODIFIERKEY_LEFT_ALT},
{0,0,0,0,0,0,MODIFIERKEY_GUI,0},
{0,0,0,0,0,0,0,0},
{0,0,MODIFIERKEY_RIGHT_CTRL,MODIFIERKEY_LEFT_CTRL,0,0,0,0}
};
// Load the media key matrix with Fn key names at the correct row-column location.
// A zero indicates no media key at that location.
int media[rows_max][cols_max] = {
{KEY_END,KEY_HOME,KEY_PAGE_DOWN,0,0,0,0,KEY_PAGE_UP},
{0,0,0,KEY_PAUSE,0,0,0,0},
{0,0,0,KEY_SCROLL_LOCK,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,KEY_MEDIA_VOLUME_DEC,KEY_MEDIA_VOLUME_INC,0,0,0},
{0,0,0,0,KEY_MEDIA_MUTE,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
// Initialize the old_key matrix with one's.
// 1 = key not pressed, 0 = key is pressed
boolean old_key[rows_max][cols_max] = {
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1}
};
//
// Define the Teensy 3.2 I/O numbers (translated from the FPC pin #)
// Row FPC pin # 01,02,03,04,05,06,07,08,09,18,19,20,21,22,23,24
// Teensy I/O # 23,00,22,01,21,02,20,03,19,08,33,09,26,10,27,11
int Row_IO[rows_max] = {23,0,22,1,21,2,20,3,19,8,33,9,26,10,27,11}; // Teensy 3.2 I/O numbers for rows
//
// Column FPC pin # 10,11,12,13,14,15,16,17
// Teensy I/O # 04,18,05,17,06,24,07,25
int Col_IO[cols_max] = {4,18,5,17,6,24,7,25}; // Teensy 3.2 I/O numbers for columns
// Declare variables that will be used by functions
boolean slots_full = LOW; // Goes high when slots 1 thru 6 contain normal keys
// slot 1 thru slot 6 hold the normal key values to be sent over USB.
int slot1 = 0; //value of 0 means the slot is empty and can be used.
int slot2 = 0;
int slot3 = 0;
int slot4 = 0;
int slot5 = 0;
int slot6 = 0;
//
int mod_shift_l = 0; // These variables are sent over USB as modifier keys.
int mod_shift_r = 0; // Each is either set to 0 or MODIFIER_ ...
int mod_ctrl_l = 0;
int mod_ctrl_r = 0;
int mod_alt_l = 0;
int mod_alt_r = 0;
int mod_gui = 0;
//
// Function to load the key name into the first available slot
void load_slot(int key) {
if (!slot1) {
slot1 = key;
}
else if (!slot2) {
slot2 = key;
}
else if (!slot3) {
slot3 = key;
}
else if (!slot4) {
slot4 = key;
}
else if (!slot5) {
slot5 = key;
}
else if (!slot6) {
slot6 = key;
}
if (!slot1 || !slot2 || !slot3 || !slot4 || !slot5 || !slot6) {
slots_full = LOW; // slots are not full
}
else {
slots_full = HIGH; // slots are full
}
}
//
// Function to clear the slot that contains the key name
void clear_slot(int key) {
if (slot1 == key) {
slot1 = 0;
}
else if (slot2 == key) {
slot2 = 0;
}
else if (slot3 == key) {
slot3 = 0;
}
else if (slot4 == key) {
slot4 = 0;
}
else if (slot5 == key) {
slot5 = 0;
}
else if (slot6 == key) {
slot6 = 0;
}
if (!slot1 || !slot2 || !slot3 || !slot4 || !slot5 || !slot6) {
slots_full = LOW; // slots are not full
}
else {
slots_full = HIGH; // slots are full
}
}
//
// Function to load the modifier key name into the appropriate mod variable
void load_mod(int m_key) {
if (m_key == MODIFIERKEY_LEFT_SHIFT) {
mod_shift_l = m_key;
}
else if (m_key == MODIFIERKEY_RIGHT_SHIFT) {
mod_shift_r = m_key;
}
else if (m_key == MODIFIERKEY_LEFT_CTRL) {
mod_ctrl_l = m_key;
}
else if (m_key == MODIFIERKEY_RIGHT_CTRL) {
mod_ctrl_r = m_key;
}
else if (m_key == MODIFIERKEY_LEFT_ALT) {
mod_alt_l = m_key;
}
else if (m_key == MODIFIERKEY_RIGHT_ALT) {
mod_alt_r = m_key;
}
else if (m_key == MODIFIERKEY_GUI) {
mod_gui = m_key;
}
}
//
// Function to load 0 into the appropriate mod variable
void clear_mod(int m_key) {
if (m_key == MODIFIERKEY_LEFT_SHIFT) {
mod_shift_l = 0;
}
else if (m_key == MODIFIERKEY_RIGHT_SHIFT) {
mod_shift_r = 0;
}
else if (m_key == MODIFIERKEY_LEFT_CTRL) {
mod_ctrl_l = 0;
}
else if (m_key == MODIFIERKEY_RIGHT_CTRL) {
mod_ctrl_r = 0;
}
else if (m_key == MODIFIERKEY_LEFT_ALT) {
mod_alt_l = 0;
}
else if (m_key == MODIFIERKEY_RIGHT_ALT) {
mod_alt_r = 0;
}
else if (m_key == MODIFIERKEY_GUI) {
mod_gui = 0;
}
}
//
// Function to send the modifier keys over usb
void send_mod() {
Keyboard.set_modifier(mod_shift_l | mod_shift_r | mod_ctrl_l | mod_ctrl_r | mod_alt_l | mod_alt_r | mod_gui);
Keyboard.send_now();
}
//
// Function to send the normal keys in the 6 slots over usb
void send_normals() {
Keyboard.set_key1(slot1);
Keyboard.set_key2(slot2);
Keyboard.set_key3(slot3);
Keyboard.set_key4(slot4);
Keyboard.set_key5(slot5);
Keyboard.set_key6(slot6);
Keyboard.send_now();
}
//
// Function to set a pin to high impedance (acts like open drain output)
void go_z(int pin)
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
//
// Function to set a pin as an input with a pullup
void go_pu(int pin)
{
pinMode(pin, INPUT_PULLUP);
digitalWrite(pin, HIGH);
}
//
// Function to send a pin to a logic low
void go_0(int pin)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
//
// Function to send a pin to a logic high
void go_1(int pin)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
//
//----------------------------------Setup-------------------------------------------
void setup() {
for (int a = 0; a < cols_max; a++) { // loop thru all column pins
go_pu(Col_IO[a]); // set each column pin as an input with a pullup
}
//
for (int b = 0; b < rows_max; b++) { // loop thru all row pins
go_z(Row_IO[b]); // set each row pin as a floating output
}
}
//
boolean Fn_pressed = HIGH; // Initialize Fn key to HIGH = "not pressed"
extern volatile uint8_t keyboard_leds; // 8 bits sent from Pi to Teensy that give keyboard LED status. Caps lock is bit D1.
//
//---------------------------------Main Loop---------------------------------------------
//
void loop() {
// Scan keyboard matrix with an outer loop that drives each row low and an inner loop that reads every column (with pull ups).
// The routine looks at each key's present state (by reading the column input pin) and also the previous state from the last scan
// that was 30msec ago. The status of a key that was just pressed or just released is sent over USB and the state is saved in the old_key matrix.
// The keyboard keys will read as logic low if they are pressed (negative logic).
// The old_key matrix also uses negative logic (low=pressed).
//
for (int x = 0; x < rows_max; x++) { // loop thru the rows
go_0(Row_IO[x]); // Activate Row (send it low)
delayMicroseconds(10); // give the row time to go low and settle out
for (int y = 0; y < cols_max; y++) { // loop thru the columns
// **********Modifier keys including the Fn special case
if (modifier[x][y] != 0) { // check if modifier key exists at this location in the array (a non-zero value)
if (!digitalRead(Col_IO[y]) && (old_key[x][y])) { // Read column to see if key is low (pressed) and was previously not pressed
if (modifier[x][y] != MODIFIERKEY_FN) { // Exclude Fn modifier key
load_mod(modifier[x][y]); // function reads which modifier key is pressed and loads it into the appropriate mod_... variable
send_mod(); // function sends the state of all modifier keys over usb including the one that just got pressed
old_key[x][y] = LOW; // Save state of key as "pressed"
}
else {
Fn_pressed = LOW; // Fn status variable is active low
old_key[x][y] = LOW; // old_key state is "pressed" (active low)
}
}
else if (digitalRead(Col_IO[y]) && (!old_key[x][y])) { //check if key is not pressed and was previously pressed
if (modifier[x][y] != MODIFIERKEY_FN) { // Exclude Fn modifier key
clear_mod(modifier[x][y]); // function reads which modifier key was released and loads 0 into the appropriate mod_... variable
send_mod(); // function sends all mod's over usb including the one that just released
old_key[x][y] = HIGH; // Save state of key as "not pressed"
}
else {
Fn_pressed = HIGH; // Fn is no longer active
old_key[x][y] = HIGH; // old_key state is "not pressed"
}
}
}
// ***********end of modifier section
//
// ***********Normal keys, num lock keys and media keys in this section
else if ((normal[x][y] != 0) || (media[x][y] != 0)) { // check if normal or media key exists at this location in the array
if (!digitalRead(Col_IO[y]) && (old_key[x][y]) && (!slots_full)) { // check if key pressed and not previously pressed and slots not full
old_key[x][y] = LOW; // Save state of key as "pressed"
if (Fn_pressed) { // Fn_pressed is active low so it is not pressed and normal key needs to be sent
if (keyboard_leds & 1) { // test if Num Lock is turned on
load_slot(numlock[x][y]); //update first available slot with key name from numlock matrix
send_normals(); // send all slots over USB including the key that just got pressed
}
else {
load_slot(normal[x][y]); //update first available slot with key name from normal matrix
send_normals(); // send all slots over USB including the key that just got pressed
}
}
else if (media[x][y] != 0) { // Fn is pressed so send media if a key exists in the matrix
Keyboard.press(media[x][y]); // media key is sent using keyboard press function per PJRC
delay(5); // delay 5 milliseconds before releasing to make sure it gets sent over USB
Keyboard.release(media[x][y]); // send media key release
}
}
else if (digitalRead(Col_IO[y]) && (!old_key[x][y])) { //check if key is not pressed, but was previously pressed
old_key[x][y] = HIGH; // Save state of key as "not pressed"
if (Fn_pressed) { // Fn is not pressed
if (keyboard_leds & 1) { // test if Num Lock is turned on
clear_slot(numlock[x][y]); //clear slot with key name from numlock matrix
send_normals(); // send all slots over USB including the key that just got released
}
else {
clear_slot(normal[x][y]); //clear slot with key name from normal matrix
send_normals(); // send all slots over USB including the key that just got released
}
}
}
}
// **************end of normal, num lock, and media key section
//
}
go_z(Row_IO[x]); // De-activate Row (send it to hi-z)
}
//
// **********keyboard scan complete
//
// Turn on the LED on the Teensy for Caps Lock based on bit 1 in the keyboard_leds variable controlled by the USB host computer
//
if (keyboard_leds & 1<<1) { // mask off all bits but D1 and test if set
go_1(CAPS_LED); // turn on the LED
}
else {
go_0(CAPS_LED); // turn off the LED
}
//
delay(25); // The overall keyboard scanning rate is about 30ms
}

View file

@ -0,0 +1,85 @@
Keyboard 1 Keyboard 2
MODIFIERKEY_LEFT_CTRL 13 24 13 24
MODIFIERKEY_RIGHT_CTRL 12 24 12 24
MODIFIERKEY_LEFT_SHIFT 15 19 15 19
MODIFIERKEY_RIGHT_SHIFT 19 10 10 19
MODIFIERKEY_LEFT_ALT 17 21 17 21
MODIFIERKEY_RIGHT_ALT 21 11 11 21
MODIFIERKEY_GUI 16 22 22 16
MODIFIERKEY_LEFT_FN 14 20 14 20
MODIFIERKEY_RIGHT_FN 14 20 14 20
KEY_A 12 8 12 8
KEY_B 5 12 5 12
KEY_C 7 12 7 12
KEY_D 10 8 8 10
KEY_E 17 8 8 17
KEY_F 7 10 7 10
KEY_G 6 17 6 17
KEY_H 10 6 6 10
KEY_I 5 17 17 5
KEY_J 5 10 5 10
KEY_K 6 12 6 12
KEY_L 4 10 10 4
KEY_M 5 11 11 5
KEY_N 11 6 11 6
KEY_O 4 17 4 17
KEY_P 15 18 15 18
KEY_Q 9 17 9 17
KEY_R 7 16 16 7
KEY_S 9 12 9 12
KEY_T 7 17 7 17
KEY_U 5 16 5 16
KEY_V 7 11 7 11
KEY_W 9 10 9 10
KEY_X 8 11 8 11
KEY_Y 6 16 16 6
KEY_Z 9 11 11 9
KEY_TILDE 14 23 23 14
KEY_1 9 15 15 9
KEY_2 9 16 9 16
KEY_3 8 15 8 15
KEY_4 8 16 8 16
KEY_5 7 15 15 7
KEY_6 6 14 6 14
KEY_7 6 15 15 6
KEY_8 15 5 15 5
KEY_9 4 15 4 15
KEY_0 4 16 4 16
KEY_MINUS 14 18 14 18
KEY_EQUAL 3 15 3 15
KEY_BACKSPACE 15 2 15 2
KEY_ESC 13 23 13 23
KEY_F1 13 9 9 13
KEY_F2 9 14 9 14
KEY_F3 8 13 8 13
KEY_F4 8 14 8 14
KEY_F5 7 13 13 7
KEY_F6 7 14 7 14
KEY_F7 13 6 6 13
KEY_F8 5 13 5 13
KEY_F9 5 14 5 14
KEY_F10 13 4 4 13
KEY_F11 4 14 4 14
KEY_F12 13 18 13 18
KEY_INSERT 2 13 13 2
KEY_PRINT_SCREEN 3 14 3 14
KEY_DELETE 2 14 2 14
KEY_RIGHT 10 1 1 10
KEY_LEFT 1 11 1 11
KEY_UP 1 17 1 17
KEY_DOWN 1 12 12 1
KEY_MENU 11 18 11 18
KEY_SLASH 12 18 12 18
KEY_PERIOD 4 12 4 12
KEY_COMMA 4 11 4 11
KEY_SEMICOLON 17 23 17 23
KEY_QUOTE 3 10 3 10
KEY_ENTER 2 17 2 17
KEY_LEFT_BRACE 16 18 16 18
KEY_RIGHT_BRACE 3 17 3 17
KEY_BACKSLASH 2 12 2 12
KEY_CAPS_LOCK 16 23 16 23
KEY_TAB 15 23 15 23
KEY_SPACE 10 23 10 23
KEY_NUM_LOCK 13 3 3 13