Skip to content

Commit 01b780e

Browse files
committed
V1.0.7 Add SAMD1 and other tidy ups
1 parent 2d38873 commit 01b780e

File tree

11 files changed

+175
-88
lines changed

11 files changed

+175
-88
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# PS2KeyAdvanced
22
## Arduino PS2 Keyboard FULL keyboard protocol support and full keys to integer coding
3-
**V1.0.6** January 2020 - Fix typos, correct keyboard reset status improve library.properties
3+
**V1.0.7** February 2020 - Add notes for porting to other platforms
4+
5+
V1.0.6 January 2020 - Fix typos, correct keyboard reset status improve library.properties
46
and additional reduction for easier platform handling
57

68
V1.0.4 August 2018 - Minor reduction in available() method to remove redundant extra safety checks on buffers
@@ -15,8 +17,10 @@ For other versions that just read the keycodes for all keyboard types or allow y
1517

1618
### Platforms
1719

18-
Arduino AVR - tested on Uno and Mega 2560
19-
Arduino SAM - tested on DUE
20+
- Arduino AVR - tested on Uno and Mega 2560 (Users have tested on Teensy 2.0 and Teensy++ 2.0)
21+
- Arduino SAM - tested on DUE
22+
23+
See later for porting to other platforms
2024

2125
### Test Environment
2226

@@ -44,20 +48,21 @@ For other versions that just read the keycodes for all keyboard types or allow y
4448
2. When using DUE or other boards with 3V3 I/O you MUST use a level translator FET or IC like Texas Instruments TXS0102 or similar as most keyboards not only operate at 5V but the two wire communications are pulled up by a resistor to 5V at the keyboard end.
4549

4650
### Introduction
47-
4851
After looking round for suitable libraries I found most were lacking in functionality and high in code and data footprint, so I created a series of PS2 Keyboard libraries. This is the second which fully supports the PS2 Keyboard Protocol, even allowing you control of keyboard LEDs (some have 4 LEDs) and changing settings..
4952

5053
The PS2 Keyboard interface is still needed for systems that have no USB and even if you have USB, you want it left for other uses.
5154

5255
The PS2 Keyboard interface is a Bi-directional two wire interface with a clock line and a data line which you connect to your Arduino (see above), the keyboard protocol has many nuances all of which are used in the other libraries of this series. this library allows you to access the keycodes sent from a keyboard into its small buffer and read out the codes with simple methods.
5356

5457
Returns any keypress as 16 bit integer, which includes a coded value for the key along with status for
58+
5559
- Make/Break
5660
- CTRL, SHIFT, CAPS, ALT, GUI, ALT-GR Status
5761
- Alphanumeric/keyboard Function
5862
- 8 bit key code (defined in public header)
5963

60-
Fully featured PS2 keyboard library to provide
64+
Fully featured PS2 keyboard library to provide
65+
6166
- All keys have a keycode (ESC, A-Z and 0-9 as ASCII equivalents)
6267
- All function (F1 to F24), multimedia and movement keys supported
6368
- Parity checking of data sent/received
@@ -74,20 +79,17 @@ Returns any keypress as 16 bit integer, which includes a coded value for the key
7479
- Handles NUM/SCROLL internally
7580

7681
### Installation
77-
7882
Performed by standard zip file library inclusion into Arduino IDE
79-
8083
### Examples
81-
8284
This library has THREE examples, from simplest to most complec -
8385

8486
- SimpleTest that uses the serial port to output the converted codes received on every keystroke and auto-repeat.
8587
- advcodetest that uses serial port and some of the keys pressed to send commands back to the keyboard to see the responses for things like Reset keyboard, Read ID, change Typematic rate (auto-repeat).
8688
- KeyToLCD - Example that will allow you to display keyboard actions on LCD connected to Arduino and allow cursor movements to move the cursor on LCD, whilst also displaying strings for keys like ESC, TAB, F1 to F12
8789

88-
90+
## Porting to different boards or architectures
91+
See document Porting.md in extras folder for hints to assist in this process
8992
### Contributor and Author Details
90-
9193
Author Paul Carpenter, PC Services
9294

9395
Web Site http://www.pcserviceselectronics.co.uk

examples/SimpleTest/SimpleTest.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ uint16_t c;
139139
PS2KeyAdvanced keyboard;
140140

141141

142-
void setup()
142+
void setup( )
143143
{
144144
// Configure the keyboard library
145145
keyboard.begin( DATAPIN, IRQPIN );
@@ -148,12 +148,12 @@ Serial.println( "PS2 Advanced Key Simple Test:" );
148148
}
149149

150150

151-
void loop()
151+
void loop( )
152152
{
153-
if( keyboard.available() )
153+
if( keyboard.available( ) )
154154
{
155155
// read the next key
156-
c = keyboard.read();
156+
c = keyboard.read( );
157157
if( c > 0 )
158158
{
159159
Serial.print( "Value " );

examples/advcodetest/advcodetest.ino

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
Clock pin from PS2 keyboard MUST be connected to an interrupt
5656
pin, these vary with the different types of Arduino
5757
58-
PS2KeyAdvanced requires both pins specified for begin()
58+
PS2KeyAdvanced requires both pins specified for begin( )
5959
6060
keyboard.begin( data_pin, irq_pin );
6161
@@ -151,27 +151,36 @@ uint8_t repeats = 0;
151151
PS2KeyAdvanced keyboard;
152152

153153

154-
void setup()
154+
void setup( )
155155
{
156156
Serial.begin( 115200 );
157157
Serial.println( "PS2 Advanced Key - Advanced Test:" );
158158

159159
// Configure the keyboard library
160160
keyboard.begin( DATAPIN, IRQPIN );
161-
keyboard.echo(); // ping keyboard to see if there
161+
keyboard.echo( ); // ping keyboard to see if there
162162
delay( 6 );
163-
c = keyboard.read();
164-
if( (c & 0xFF) == PS2_KEY_ECHO )
165-
Serial.println( "Keyboard OK.." );
163+
c = keyboard.read( );
164+
if( (c & 0xFF) == PS2_KEY_ECHO
165+
|| (c & 0xFF) == PS2_KEY_BAT )
166+
Serial.println( "Keyboard OK.." ); // Response was Echo or power up
167+
else
168+
if( ( c & 0xFF ) == 0 )
169+
Serial.println( "Keyboard Not Found" );
170+
else
171+
{
172+
Serial.print( "Invalid Code received of " );
173+
Serial.println( c, HEX );
174+
}
166175
}
167176

168177

169-
void loop()
178+
void loop( )
170179
{
171-
if( keyboard.available() )
180+
if( keyboard.available( ) )
172181
{
173182
// read the next key
174-
c = keyboard.read();
183+
c = keyboard.read( );
175184
if( ( c & 0xFF ) > 0 )
176185
Serial.println( c , HEX );
177186
/* now do something with keys entered results on serial monitor */
@@ -180,24 +189,24 @@ if( keyboard.available() )
180189
{
181190
case PS2_KEY_R:
182191
Serial.println( "Reset" );
183-
keyboard.resetKey(); // Reset keyboard
192+
keyboard.resetKey( ); // Reset keyboard
184193
break;
185194
case PS2_KEY_S:
186195
Serial.println( "Get Scancode set in use" );
187-
keyboard.getScanCodeSet(); // Get which scan code set
196+
keyboard.getScanCodeSet( ); // Get which scan code set
188197
break;
189198
case PS2_KEY_G:
190199
Serial.print( "Get current lock status = " );
191-
c = keyboard.getLock(); // Get current lock status
200+
c = keyboard.getLock( ); // Get current lock status
192201
Serial.println( c, HEX );
193202
break;
194203
case PS2_KEY_I:
195204
Serial.println( "Read ID code" );
196-
keyboard.readID(); // Get which scan code set
205+
keyboard.readID( ); // Get which scan code set
197206
break;
198207
case PS2_KEY_E:
199208
Serial.println( "Echo" );
200-
keyboard.echo(); // Get which scan code set
209+
keyboard.echo( );
201210
break;
202211
case PS2_KEY_T:
203212
Serial.println( "Typematic Rate" );
@@ -213,7 +222,7 @@ if( keyboard.available() )
213222
Serial.print( "No Repeat Makes for CTRL... " );
214223
repeats ^= 1;
215224
Serial.println( repeats );
216-
keyboard.setNoRepeat( repeats ); // Set repeat modet
225+
keyboard.setNoRepeat( repeats ); // Set repeat mode
217226
break;
218227
}
219228
}

extra/PS2 Keyboard.pdf

364 KB
Binary file not shown.

extra/Porting.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# PS2KeyAdvanced - Porting to new boards or Architectures
2+
This document assumes you have some low level understanding of your board, Arduino library structures and the compiler for your board.
3+
4+
The majority of assists you require are in PS2KeyAdvanced.h the main header file for the library which has some architecture specific defines.
5+
6+
- PS2_SUPPORTED
7+
- PS2_REQUIRES_PROGMEM
8+
- PS2_CLEAR_PENDING_IRQ
9+
10+
This are to cope with AVR Harvard architecture and AVR and SAM issue of not clearing past events on Interrupt pin before attachInterrupt enables interrupts
11+
those causing false interrupt events.
12+
## General Rules
13+
To add another board type you need to make some changes
14+
1. You need to determine what the define is for your architecture, as AVR uses ARDUINO_ARCH_AVR so from verbose compiling output you need to find the part in
15+
compiling output for YOUR BOARD that should be **-DARDUINO_ARCH_xxxx**. Where the "xxxx" is the board architecture. You need this for making later changes.
16+
2. In PS2KeyAdvanced.h you need to add a specific test for your board after the AVR and SAM checks (Note PS2_SUPPORTED **must** be included in your test otherwise a compilation error will occur.) like
17+
~~~
18+
#if defined( ARDUINO_ARCH_xxxx )
19+
#define PS2_SUPPORTED 1
20+
#define PS2_REQUIRES_PROGMEM 1
21+
#endif
22+
~~~
23+
3. Change library.properties to add your architecture to the comma separated list for your architecture, normally this is the "xxxx" for ARCH_ARDUINO_xxxx
24+
### PS2_SUPPORTED
25+
This flag is set to indicate we have a supported board, this flag stops a compiler error being forced.
26+
27+
To enable for your architecture add a line in the '#if' for your architecture as follows
28+
~~~
29+
#define PS2_SUPPORTED 1
30+
~~~
31+
### PS2_REQUIRES_PROGMEM
32+
This determines that to have constants in Flash memory not RAM on AVR boards the PROGMEM functions have to be used.
33+
34+
To enable for your architecture add a line in the '#if' for your architecture as follows
35+
~~~
36+
#define PS2_REQUIRES_PROGMEM 1
37+
~~~
38+
### PS2_CLEAR_PENDING_IRQ
39+
When sending data to the keyboard, interrupts have to be turned off and back on again, on AVR and SAM architecture any changes on the clock pin cause extra interrupts
40+
when attachInterrupt is used to start bit timing interrupts again, on other architectures this does not normally happen so we need to change how interrupt events
41+
we see for a valid byte sending. This does not affect receiving data just the sending.
42+
43+
This defines adds an extra bit clock event interrupt step for AVR and SAM
44+
45+
To enable for your architecture add a line in the '#if' for your architecture as follows
46+
~~~
47+
#define PS2_CLEAR_PENDING_IRQ 1
48+
~~~
49+
## Support of changes
50+
If you can test **ALL** functionality working, you are welcome to do a pull request from your github fork of this library. If meets our coding
51+
guidelines and you can show it is has been working it will include in next release.
52+
53+
Please note whilst we can give assistance, we probably do not have your board or maybe not the time to incorporate full support for you.

extra/readme.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Written by Paul Carpenter, PC Services <[email protected]>
44
Created September 2014
55
Updated January 2016
6+
Updated February 2020
67

78
This is for a LATIN style keyboard.
89

@@ -41,9 +42,10 @@
4142

4243
extras folder
4344
codes.txt The literals (constants) you can use to match codes returned
45+
PS2 Keyboard.pdf Copy of Website explaining PS2 Protocol
4446
readme.txt this file
4547
websites.txt Other websites about PS2 keyboard and UTF-8 character encoding
46-
Iamges folder containg images for a single NUMLOCK keypress and what
48+
Images folder containing images for a single NUMLOCK keypress and what
4749
happens on the wires.
4850

4951
src folder
@@ -175,5 +177,5 @@ For LEDs command use the following defines added together to set them
175177

176178
Paul Carpenter
177179
PC Services
178-
January 2016
180+
February 2020
179181
http://www.pcserviceselectronics.co.uk

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name=PS2KeyAdvanced
2-
version=1.0.6
2+
version=1.0.7
33
author=Paul Carpenter <[email protected]>
44
maintainer=Paul Carpenter <[email protected]>
55
sentence=PS2 keyboard FULL control and ALL keys processing, as well as LED control.
66
paragraph=Provides ability to convert long key stroke code sequences to a single integer, for all keys ANY Latin keyboard, even multimedia and 24 Function key keyboards.
77
category=Other
88
url=https://github.com/techpaul/PS2KeyAdvanced.git
9-
architectures=avr,sam
9+
architectures=avr,sam,samd1
1010
includes=PS2KeyAdvanced.h
1111

0 commit comments

Comments
 (0)