From 8cd19522694fffe532b011e9739e1d36e8db7fd2 Mon Sep 17 00:00:00 2001 From: Frank Adams <33405607+thedalles77@users.noreply.github.com> Date: Fri, 1 Oct 2021 18:06:19 -0700 Subject: [PATCH] Add files via upload --- .../PowerBook520/520_FPC_Keyboard.txt | 79 +++ .../PowerBook520/Matrix_Decoder_3p2_520.ino | 372 +++++++++++++ Example_Keyboards/PowerBook520/PB520.ino | 516 ++++++++++++++++++ 3 files changed, 967 insertions(+) create mode 100644 Example_Keyboards/PowerBook520/520_FPC_Keyboard.txt create mode 100644 Example_Keyboards/PowerBook520/Matrix_Decoder_3p2_520.ino create mode 100644 Example_Keyboards/PowerBook520/PB520.ino diff --git a/Example_Keyboards/PowerBook520/520_FPC_Keyboard.txt b/Example_Keyboards/PowerBook520/520_FPC_Keyboard.txt new file mode 100644 index 0000000..f546d30 --- /dev/null +++ b/Example_Keyboards/PowerBook520/520_FPC_Keyboard.txt @@ -0,0 +1,79 @@ +Cntrl-L 10 15 +Shift-L 10 16 +Shift-R 10 25 +Alt-L 10 18 +Alt-R +GUI 10 19 +Fn +A 2 24 +B 24 11 +C 4 22 +D 4 23 +E 21 4 +F 5 24 +G 6 20 +H 9 24 +I 9 21 +J 6 24 +K 6 21 +L 6 23 +M 7 22 +N 6 22 +O 5 13 +P 7 21 +Q 3 21 +R 5 21 +S 3 24 +T 14 7 +U 11 23 +V 22 5 +W 21 1 +X 1 22 +Y 7 20 +Z 3 22 +` 6 14 +1 9 14 +2 9 20 +3 9 13 +4 6 12 +5 7 12 +6 8 12 +7 8 14 +8 8 20 +9 8 13 +0 4 20 +- 5 20 += 20 11 +Back Space 9 23 +Esc 12 9 +F1 2 12 +F2 3 12 +F3 1 12 +F4 4 12 +F5 12 5 +F6 11 12 +F7 2 14 +F8 3 14 +F9 14 1 +F10 14 4 +F11 5 14 +F12 11 14 +Insert +Delete 10 26 +Arrow-Right 5 23 +Arrow-Left 2 23 +Arrow-Up 1 23 +Arrow-Down 1 24 +Menu +/ 8 24 +. 3 23 +, 7 24 +; 23 7 +' 8 23 +Enter 11 13 +[ 7 13 +] 8 21 +\ 6 13 +Caps-Lock 10 17 +Tab 21 2 +Space 11 22 diff --git a/Example_Keyboards/PowerBook520/Matrix_Decoder_3p2_520.ino b/Example_Keyboards/PowerBook520/Matrix_Decoder_3p2_520.ino new file mode 100644 index 0000000..39f01ad --- /dev/null +++ b/Example_Keyboards/PowerBook520/Matrix_Decoder_3p2_520.ino @@ -0,0 +1,372 @@ +/* Copyright 2020 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. +*/ +// It will determine how a laptop keyboard matrix is wired using a Teensy 3.2 on an FPC daughterboard. +// Open an editor on a computer and load or create a file that lists every key +// on the laptop keyboard that will be tested. Connect the FPC cable of the test keyboard +// to the Teensy/FPC board. Connect a USB cable from the Teensy to the computer. +// Wait a few seconds for the computer to see the Teensy as a keyboard. If numbers are reported on the screen +// before any keys are pressed, these pin numbers are shorted together and must be fixed. +// Press each key one by one on the test keyboard as listed on the editor screen. When a key +// is pressed on the test keyboard, the program detects which two pins on the FPC connector +// were connected. Those two pin numbers are sent over USB (separated by a TAB) and displayed +// on the editor. After sending the numbers, a DOWN ARROW is sent over USB to prepare for +// the next key. Once all keys on the test keyboard have been pressed, the file in +// the editor can be saved to create a row-column matrix. +// +// If your keyboard has diodes, you must pay attention to the order of the two pins that are reported by the Teensy. The code performs +// a bottom up test first, followed by a top down test so that one of the two tests will forward bias the diode. +// The first pin reported over USB is the cathode side and the second pin is the anode side. The diode direction must be taken into +// account when programming the TMK or Teensyduino keyboard routine. +// +// Revision History +// Rev 1.00 - Nov 18, 2018 - Original Release +// Rev 1.1 - April 19, 2020 - Use min_pin in bottom up loop +// +// Load an array with the Teensy 3.2 I/O numbers that correspond to FPC pins 1 thru 34. + int con_pin[] = {1, 2, 4, 3, 7, 6, 9, 8, 11, 10, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 28, 29, 30, 31, 32}; +// +// Define maximum and minimum pin numbers that will be tested. +// max_pin is usually set to the FPC connector size. min_pin is usually set to 1. The routine will start testing at pin 1 and go up to the max pin size. +// The max and min pin values can be adjusted to exclude testing the FPC traces at the edges if they are reported as shorted. An example would be if pin 1 +// and pin 34 are both grounds. They will be reported as tied together but they are not needed by the key matrix. In this case, set the +// min_pin to 2 and the max_pin to 33. +// +int max_pin = 26; // the keyboard FPC connector pin count. If set to 34, unsolder the LED or the code won't work +int min_pin = 1; // the first pin to be tested on the FPC connector (usually pin 1) +// +// load the key codes used in sending usb numbers, tab, and down arrow +int key_1 = KEY_1; +int key_2 = KEY_2; +int key_3 = KEY_3; +int key_4 = KEY_4; +int key_5 = KEY_5; +int key_6 = KEY_6; +int key_7 = KEY_7; +int key_8 = KEY_8; +int key_9 = KEY_9; +int key_0 = KEY_0; +int key_tab = KEY_TAB; +int key_down = KEY_DOWN; +// +// Function to set a pin as an input with a pullup so it's high unless grounded by a key press +void go_z(int pin) +{ + pinMode(pin, INPUT_PULLUP); + digitalWrite(pin, HIGH); +} + +// Function to set a pin as an output and drive it to a logic low (0 volts) +void go_0(int pin) +{ + pinMode(pin, OUTPUT); + digitalWrite(pin, LOW); +} + +// Function to send numbers over USB for display on an editor +void usb_num(int num) // the numbers 0 thru 33 are sent over usb as 1 thru 34 +{ + switch (num) { + case 0: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + break; + case 1: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + break; + case 2: + Keyboard.set_key1(key_3); + Keyboard.send_now(); + break; + case 3: + Keyboard.set_key1(key_4); + Keyboard.send_now(); + break; + case 4: + Keyboard.set_key1(key_5); + Keyboard.send_now(); + break; + case 5: + Keyboard.set_key1(key_6); + Keyboard.send_now(); + break; + case 6: + Keyboard.set_key1(key_7); + Keyboard.send_now(); + break; + case 7: + Keyboard.set_key1(key_8); + Keyboard.send_now(); + break; + case 8: + Keyboard.set_key1(key_9); + Keyboard.send_now(); + break; + case 9: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_0); + Keyboard.send_now(); + break; + case 10: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(50); + Keyboard.set_key1(0); + Keyboard.send_now(); + delay(50); + Keyboard.set_key2(key_1); + Keyboard.send_now(); + break; + case 11: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_2); + Keyboard.send_now(); + break; + case 12: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_3); + Keyboard.send_now(); + break; + case 13: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_4); + Keyboard.send_now(); + break; + case 14: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_5); + Keyboard.send_now(); + break; + case 15: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_6); + Keyboard.send_now(); + break; + case 16: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_7); + Keyboard.send_now(); + break; + case 17: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_8); + Keyboard.send_now(); + break; + case 18: + Keyboard.set_key1(key_1); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_9); + Keyboard.send_now(); + break; + case 19: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_0); + Keyboard.send_now(); + break; + case 20: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_1); + Keyboard.send_now(); + break; + case 21: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(50); + Keyboard.set_key1(0); + Keyboard.send_now(); + delay(50); + Keyboard.set_key2(key_2); + Keyboard.send_now(); + break; + case 22: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_3); + Keyboard.send_now(); + break; + case 23: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_4); + Keyboard.send_now(); + break; + case 24: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_5); + Keyboard.send_now(); + break; + case 25: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_6); + Keyboard.send_now(); + break; + case 26: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_7); + Keyboard.send_now(); + break; + case 27: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_8); + Keyboard.send_now(); + break; + case 28: + Keyboard.set_key1(key_2); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_9); + Keyboard.send_now(); + break; + case 29: + Keyboard.set_key1(key_3); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_0); + Keyboard.send_now(); + break; + case 30: + Keyboard.set_key1(key_3); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_1); + Keyboard.send_now(); + break; + case 31: + Keyboard.set_key1(key_3); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_2); + Keyboard.send_now(); + break; + case 32: + Keyboard.set_key1(key_3); + Keyboard.send_now(); + delay(50); + Keyboard.set_key1(0); + Keyboard.send_now(); + delay(50); + Keyboard.set_key2(key_3); + Keyboard.send_now(); + break; + case 33: + Keyboard.set_key1(key_3); + Keyboard.send_now(); + delay(20); + Keyboard.set_key2(key_4); + Keyboard.send_now(); + break; + } + delay(20); + Keyboard.set_key1(0); // clear out the key slots + Keyboard.set_key2(0); + Keyboard.send_now(); + delay(20); + Keyboard.set_key1(key_tab); // Tab over to position for next number + Keyboard.send_now(); + delay(20); + Keyboard.set_key1(0); // clear out the tab from the slot + Keyboard.send_now(); + delay(20); +} + +// Function to send a down arrow over usb to position for the next key +void down_arrow(void) { + Keyboard.set_key1(key_down); // send a down arrow + Keyboard.send_now(); + delay(20); + Keyboard.set_key1(0); // release the down arrow + Keyboard.send_now(); +} + +// --------------------------------------------------Setup----------------------------------- +void setup() { + for (int k = 0; k < max_pin; k++) { // loop thru all connector pins + go_z(con_pin[k]); // set each pin as an input with a pullup + } + delay(15000); // Wait for the host to connect to the Teensy as a keyboard. If 2 pins are shorted, + // you want the host to be ready to receive the pin numbers. +} +// +// -------------------------------------------Main Loop-------------------------------------- +// +void loop() { +// +// ***********Bottom up Test************ +// + for (int i=min_pin-1; imin_pin-1; p--) { // outer loop pin + go_0(con_pin[p]); // make the outer loop pin an output and send this pin low + for (int r=p-1; r>=min_pin-1; r--) { // inner loop pin + delayMicroseconds(10); // give time to let the signals settle out + if (!digitalRead(con_pin[r])) { // check for connection between inner and outer pins + usb_num(p); // send outer loop pin number over usb + usb_num(r); // send inner loop pin number over usb + down_arrow(); // send a down arrow over usb + while(!digitalRead(con_pin[r])) { // wait until key is released + ; + } + } + } + go_z(con_pin[p]); // return the outer loop pin to float with pullup + } +// +delay(25); // overall keyboard scan rate is about 30 milliseconds +// +} diff --git a/Example_Keyboards/PowerBook520/PB520.ino b/Example_Keyboards/PowerBook520/PB520.ino new file mode 100644 index 0000000..3416d3c --- /dev/null +++ b/Example_Keyboards/PowerBook520/PB520.ino @@ -0,0 +1,516 @@ + +/* Copyright 2018 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 an Apple PowerBook 100 Laptop Keyboard Controller using a Teensy 3.2 on +// two 14 pin FPC connector breakout boards with bottom contacts. +// This routine uses the Teensyduino "Micro-Manager Method" to send Normal and Modifier +// keys over USB. Multi-media keys are sent with keyboard press and release functions. +// Description of Teensyduino keyboard functions is at www.pjrc.com/teensy/td_keyboard.html +// +// Revision History +// Initial Release August 31, 2021 +// +#define MODIFIERKEY_FN 0x8f // give Fn key a HID code +#define CAPS_LED 13 // Teensy LED shows Caps-Lock +#define ADB_DATA_PIN 5 // TB bidirectional data pin +// +const byte rows_max = 15; // sets the number of rows in the matrix +const byte cols_max = 11; // 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_F3,KEY_F1,KEY_F2,KEY_F4,KEY_F5,KEY_4,KEY_5,KEY_6,KEY_ESC,0,KEY_F6}, + {0,0,0,0,KEY_O,KEY_BACKSLASH,KEY_LEFT_BRACE,KEY_9,KEY_3,0,KEY_ENTER}, + {KEY_F9,KEY_F7,KEY_F8,KEY_F10,KEY_F11,KEY_TILDE,KEY_T,KEY_7,KEY_1,0,KEY_F12}, + {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_CAPS_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,KEY_0,KEY_MINUS,KEY_G,KEY_Y,KEY_8,KEY_2,0,KEY_EQUAL}, + {KEY_W,KEY_TAB,KEY_Q,KEY_E,KEY_R,KEY_K,KEY_P,KEY_RIGHT_BRACE,KEY_I,0,0}, + {KEY_X,0,KEY_Z,KEY_C,KEY_V,KEY_N,KEY_M,0,0,0,KEY_SPACE}, + {KEY_UP,KEY_LEFT,KEY_PERIOD,KEY_D,KEY_RIGHT,KEY_L,KEY_SEMICOLON,KEY_QUOTE,KEY_BACKSPACE,0,KEY_U}, + {KEY_DOWN,KEY_A,KEY_S,0,KEY_F,KEY_J,KEY_COMMA,KEY_SLASH,KEY_H,0,KEY_B}, + {0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,KEY_DELETE,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,MODIFIERKEY_CTRL,0}, + {0,0,0,0,0,0,0,0,0,MODIFIERKEY_SHIFT,0}, + {0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0,0,0,MODIFIERKEY_ALT,0}, + {0,0,0,0,0,0,0,0,0,MODIFIERKEY_GUI,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,0,0,0,0,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] = { + {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,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,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,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 # 12,13,14,15,16,17,18,19,20,21,22,23,24,25,26 +// Teensy I/O # 16,17,18,19,20,21,22,23,29,30,31,32,15 +int Row_IO[rows_max] = {14,15,16,17,18,19,20,21,22,23,28,29,30,31,32}; // Teensy LC I/O numbers for rows +// +// Column FPC pin # 01,02,03,04,05,06,07,08,09,10,11 +// Teensy I/O # 1,2,3,4,6,7,8,9,10,11,12,14 +int Col_IO[cols_max] = {1,2,4,3,7,6,9,8,11,10,12}; // 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 = 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 = 0; +int mod_ctrl_r = 0; +int mod_alt = 0; +int mod_alt_r = 0; +int mod_gui = 0; +// +// TB Constants +int ADB_TIMEOUT = 10000; +// TB Functions +static void send_0bit() { + digitalWrite(ADB_DATA_PIN, LOW); + delayMicroseconds(65); + digitalWrite(ADB_DATA_PIN, HIGH); + delayMicroseconds(35); +} + +static void send_1bit() { + digitalWrite(ADB_DATA_PIN, LOW); + delayMicroseconds(35); + digitalWrite(ADB_DATA_PIN, HIGH); + delayMicroseconds(65); +} + +static void attn() { + //delay(1); + digitalWrite(ADB_DATA_PIN, LOW); + delayMicroseconds(800-35); + send_0bit(); +} + +void send_talk_command() { + //attn + pinMode(ADB_DATA_PIN, OUTPUT); + attn(); + //address (3) 0011 + //0 + send_0bit(); + send_0bit(); + send_1bit(); + send_1bit(); + //talk 11 + send_1bit(); + send_1bit(); + //register 00 + send_0bit(); + send_0bit(); + //stop bit + send_0bit(); + //start to stop time + delayMicroseconds(140); + +} +int receive_data_packet() { + pinMode(ADB_DATA_PIN, INPUT); + int data_time = 0; + bool command_stop = false; + int adb_data = 0; + //begin reading ADB datapin + + while(digitalRead(ADB_DATA_PIN)!=LOW && data_time < ADB_TIMEOUT){ + //wait for beginning of start bit + data_time+=1; + delayMicroseconds(1); + } + + while(digitalRead(ADB_DATA_PIN)!=HIGH && data_time < ADB_TIMEOUT){ + //wait for end of start bit + data_time+=1; + delayMicroseconds(1); + } + + while(digitalRead(ADB_DATA_PIN)!=LOW && data_time < ADB_TIMEOUT){ + //wait for beginning of first data bit + data_time+=1; + delayMicroseconds(1); + } + + //data packet + data_time = 0; + while(!command_stop && data_time < ADB_TIMEOUT) { + int low = 0; + int high = 0; + bool bit_stop = false; + while(!bit_stop && data_time < ADB_TIMEOUT) { + + int adb_stream = digitalRead(ADB_DATA_PIN); + if(adb_stream == LOW) low+=1; + if(adb_stream == HIGH) high+=1; + if(adb_stream == LOW && high > 0) bit_stop = true; + if(high > 240){ + bit_stop = true; + break; + } + data_time+=1; + delayMicroseconds(1); + } + if(high > 240){ + command_stop = true; + } + //append the new data bit to adb_data + if(high > low && !command_stop) adb_data = (adb_data << 1) + 1; + if(low > high && !command_stop) adb_data = adb_data << 1; + + } + //stop bit + return adb_data; +} +/*data bits: + * [c][x][x][x][x][x][x][x][c2][y][y][y][y][y][y][y] +*/ +void parse_data(int data) { + if(data !=0) { + int mousex = data & 0b0000000001111111; + int mousey = (data >> 8) & 0b01111111; + //the mouse value is + //7 bit two's complement + if((mousey & 0b1000000) != 0) { + mousey = ((~mousey + 1) & 0b01111111) * -1; + } + if((mousex & 0b1000000) != 0) { + mousex = ((~mousex + 1) & 0b01111111) * -1; + } + Mouse.move(mousex, mousey); + Mouse.move(mousex, mousey); + + int mouseClick = data & 0b1000000000000000; + + if(mouseClick == 0) { + Mouse.set_buttons(1,0,0); + } + else if(mouseClick != 0) { + Mouse.set_buttons(0,0,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_SHIFT) { + mod_shift = m_key; + } + else if (m_key == MODIFIERKEY_RIGHT_SHIFT) { + mod_shift_r = m_key; + } + else if (m_key == MODIFIERKEY_CTRL) { + mod_ctrl = m_key; + } + else if (m_key == MODIFIERKEY_RIGHT_CTRL) { + mod_ctrl_r = m_key; + } + else if (m_key == MODIFIERKEY_ALT) { + mod_alt = 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_SHIFT) { + mod_shift = 0; + } + else if (m_key == MODIFIERKEY_RIGHT_SHIFT) { + mod_shift_r = 0; + } + else if (m_key == MODIFIERKEY_CTRL) { + mod_ctrl = 0; + } + else if (m_key == MODIFIERKEY_RIGHT_CTRL) { + mod_ctrl_r = 0; + } + else if (m_key == MODIFIERKEY_ALT) { + mod_alt = 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 | mod_shift_r | mod_ctrl | mod_ctrl_r | mod_alt | 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 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 + load_slot(normal[x][y]); //update first available slot with normal key name + 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 + clear_slot(normal[x][y]); //clear the slot that contains the normal key name + send_normals(); // send all slots over USB including the key that was just released + } + } + } +// **************end of normal and media key section +// + } + go_z(Row_IO[x]); // De-activate Row (send it to hi-z) + } +// +// **********keyboard scan complete +// +// ************TB*********************************** + send_talk_command(); + parse_data(receive_data_packet()); +// ***********end of TB****************************** +// +// 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 +}