Keyboard Driver Update (Perixx,removed debug code)

This commit is contained in:
Sergei Panarin 2023-04-06 13:43:46 +02:00
parent 2a29261b59
commit 59ef65c3d8
1 changed files with 599 additions and 686 deletions

View File

@ -1,4 +1,4 @@
/* SmartyKit 1 - PS/2 keyboard driver v.1.1
/* SmartyKit 1 - PS/2 keyboard driver v.1.2 (2023)
* (with automatic mode if no PS/2 keyboard is connected)
* http://www.smartykit.io/
* Copyright (C) 2020, Sergey Panarin <sergey@smartykit.io>
@ -15,9 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <PS2KeyAdvanced.h>
//#define _SMARTY_DEBUG_
#include "PS2KeyAdvanced.h"
// Keyboard Driver pins connections:
// D0 & D1 reserved for Serial connection
@ -43,10 +41,9 @@ static boolean autoCommandSent = false; // run pre-set command only once after r
#define LORATE_CLOCK_PIN A4
#define LORATE_CLOCK_LED_PIN A5
//10 milliseconds = 100 times per second (100 Hz)
// 100 milliseconds = 10 times per second (10 Hz)
#define LORATE_CLOCK_PERIOD_MILLIS 100
// Uncomment any of following defines to include those mappings
// or comment to exclude
// #define GERMAN
@ -161,9 +158,9 @@ static boolean autoCommandSent = false; // run pre-set command only once after r
#define PS2_thorn 254 // þ
#define PS2_y_DIAERESIS 255 // ÿ
/* Public class definition */
class SmartyPS2KeyMap {
class SmartyPS2KeyMap
{
public:
/**
* This constructor just sets the default key mapping to US. */
@ -203,13 +200,11 @@ class SmartyPS2KeyMap {
#define PS2_DELETE 0x7F
#define PS2_SPACE 0x20
/* Standard ASCII control characters array */
/* in order of PS2_KEY_* values order is important */
const uint8_t _control_codes[] = {
PS2_DELETE, PS2_ESC, PS2_BACKSPACE,
PS2_TAB, PS2_ENTER, PS2_SPACE
};
PS2_TAB, PS2_ENTER, PS2_SPACE};
// convert codes based on SHIFT and not SHIFT only for base US-ASCII
const uint16_t _US_ASCII[][2] = {
@ -252,8 +247,7 @@ const uint16_t _US_ASCII[][ 2 ] = {
{PS2_KEY_CLOSE_SQ, ']'},
{PS2_SHIFT + PS2_KEY_CLOSE_SQ, '}'},
{PS2_KEY_EQUAL, '='},
{ PS2_SHIFT + PS2_KEY_EQUAL, '+' }
};
{PS2_SHIFT + PS2_KEY_EQUAL, '+'}};
const uint16_t _UKmap[][2] = {
{PS2_SHIFT + '@', '"'},
@ -351,8 +345,7 @@ const uint16_t _FRmap[][ 2 ] = {
{PS2_KEY_EUROPE2 + PS2_SHIFT + PS2_ALT_GR, '|'},
{PS2_KEY_EUROPE2 + PS2_ALT_GR, '|'},
{PS2_SHIFT + PS2_KEY_EUROPE2, '>'},
{ PS2_KEY_EUROPE2, '<' }
};
{PS2_KEY_EUROPE2, '<'}};
#endif
#ifdef GERMAN
const uint16_t _DEmap[][2] = {
@ -410,8 +403,7 @@ const uint16_t _DEmap[][ 2 ] = {
{PS2_KEY_EUROPE2 + PS2_SHIFT + PS2_ALT_GR, '|'},
{PS2_KEY_EUROPE2 + PS2_ALT_GR, '|'},
{PS2_SHIFT + PS2_KEY_EUROPE2, '>'},
{ PS2_KEY_EUROPE2, '<' }
};
{PS2_KEY_EUROPE2, '<'}};
#endif
#ifdef SPANISH
const uint16_t _ESmap[][2] = {
@ -453,8 +445,7 @@ const uint16_t _ESmap[][ 2 ] = {
{PS2_SHIFT + '<', ';'},
{PS2_SHIFT + '>', ':'},
{PS2_KEY_DIV, '-'},
{ PS2_SHIFT + PS2_KEY_DIV, '+' }
};
{PS2_SHIFT + PS2_KEY_DIV, '+'}};
#endif
#ifdef ITALIAN
const uint16_t _ITmap[][2] = {
@ -493,8 +484,7 @@ const uint16_t _ITmap[][ 2 ] = {
{PS2_SHIFT + '<', ';'},
{PS2_SHIFT + '>', ':'},
{PS2_KEY_DIV, '-'},
{ PS2_SHIFT + PS2_KEY_DIV, '+' }
};
{PS2_SHIFT + PS2_KEY_DIV, '+'}};
#endif
#ifdef SPECIAL
const uint16_t _SpecialMap[][2] = {
@ -503,7 +493,8 @@ const uint16_t _SpecialMap[][ 2 ] = {
#endif
// The following structures define the key maps available
typedef struct {
typedef struct
{
char *name; // 2 Character ISO country code
uint8_t size; // Number of entries in map array
uint16_t *map; // Map array pointer
@ -529,16 +520,13 @@ const PS2Advmap _KeyMaps[ ] = {
{"--", sizeof(_SpecialMap) / (2 * sizeof(uint16_t)), (uint16_t *)_SpecialMap},
#endif
{"UK", sizeof(_UKmap) / (2 * sizeof(uint16_t)), (uint16_t *)_UKmap},
{ "GB", sizeof( _UKmap ) / ( 2 * sizeof( uint16_t ) ), (uint16_t *)_UKmap }
};
{"GB", sizeof(_UKmap) / (2 * sizeof(uint16_t)), (uint16_t *)_UKmap}};
uint8_t _selected_map;
// Private country code strings
const char *_USStr[] = {"US"};
/* Common function to scan a Look up table table and
return a value or 0 for not found
@ -566,12 +554,6 @@ uint16_t *maparray;
size = (_KeyMaps[index].size) << 1;
maparray = (uint16_t *)_KeyMaps[index].map;
//for (int i = 0 ; i < 10 ; i+=2)
//{
// Serial.print("maparray: "); Serial.print(i);
// Serial.print(" 0x"); Serial.println(pgm_read_word(maparray + i), HEX);
//}
// Leave only required bits for checking against
result = data & (PS2_ALT_GR + PS2_SHIFT + 0xFF);
@ -589,7 +571,6 @@ for( idx = 0; idx < size; idx += 2 )
return data;
}
/*
Pass in 2 character string for the ISO 2 letter country code in use
For UK "UK" or "GB" are valid
@ -604,28 +585,13 @@ uint8_t outer, idx, end;
end = sizeof(_KeyMaps) / sizeof(PS2Advmap);
//for (int i; i < end; i++)
//{
// Serial.print("MMap ");
// Serial.print(i); Serial.print(" ");
// Serial.print(_KeyMaps[i].name[0]); Serial.print(_KeyMaps[i].name[1]); Serial.print(" ");
// Serial.println(_KeyMaps[i].size);
//}
for (outer = 0; outer < end; outer++)
{
// Serial.print("Map "); Serial.print(outer);
for (idx = 0; idx < 3; idx++)
{
// Serial.print(" _KeyMap[].name[i] ");
// Serial.write(_KeyMaps[ outer ].name[ idx ]);
// Serial.print(" ISO[ idx ] ");
// Serial.write(ISO[ idx ]);
// Serial.print(" ");
if (_KeyMaps[outer].name[idx] != ISO[idx])
break; // No match
}
// Serial.println();
if (idx == 3) // done whole match
break;
}
@ -637,7 +603,6 @@ if( outer < end ) // found entry
return 0;
}
/*
Return selected map as a string pointer (2 chars and terminator)
*/
@ -646,7 +611,6 @@ const char *SmartyPS2KeyMap::getMap( void )
return (_KeyMaps[_selected_map].name);
}
/* Pass in unsigned int returned from PS2KeyAdvanced
Returns 0 for error
uint16_t for valid code
@ -693,8 +657,7 @@ else
else
{
// Some keys ignore shift, the extra keys on numeric keypad
if( ( temp >= PS2_KEY_KP_DOT && temp <= PS2_KEY_KP_DIV )
|| temp == PS2_KEY_KP_EQUAL || temp == PS2_KEY_KP_COMMA )
if ((temp >= PS2_KEY_KP_DOT && temp <= PS2_KEY_KP_DIV) || temp == PS2_KEY_KP_EQUAL || temp == PS2_KEY_KP_COMMA)
code &= ~PS2_SHIFT;
// Do default US-ASCII mapping of remainders excluding (CTRL, ALT, GUI)
code = (code & PS2_ALT_GR) + scan_map(code & ~PS2_ALT_GR, 0);
@ -712,7 +675,6 @@ else
return code;
}
/*
Returns uint8_t version of remapKey ONLY for standard ASCII/UTF-8 codes
Invalid codes returned as 0
@ -729,7 +691,6 @@ data = remapKey( code );
return (uint8_t)(data & 0xFF);
}
/* Class constructor
At construction ensure default map selected
*/
@ -743,20 +704,21 @@ SmartyPS2KeyMap keymap;
void setup()
{
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(LORATE_CLOCK_PIN, OUTPUT);
pinMode(LORATE_CLOCK_LED_PIN, OUTPUT);
pinMode(KEYBOARD_RD_PIN, INPUT_PULLUP);
for (int bit = 0; bit < 8; bit++) {
for (int bit = 0; bit < 8; bit++)
{
pinMode(KeyboardPort[bit], OUTPUT);
}
pinMode(KEYBOARD_BIT7_PIN, OUTPUT);
digitalWrite(KEYBOARD_BIT7_PIN, LOW);
Serial.begin(9600);
Serial.println(F("SmartyKit 1 PS2 Keyboard is ready..."));
if (autoMode)
delay(7500); // for start without Keyboard connected
else
@ -767,25 +729,6 @@ void setup()
delay(6);
uint16_t scan_code = keyboard.read();
#ifdef _SMARTY_DEBUG_
Serial.print(F("0.scan_code: 0x"));
Serial.print(scan_code, HEX);
Serial.println();
if( (scan_code & 0xff) == PS2_KEY_ECHO || (scan_code & 0xff) == PS2_KEY_BAT || (scan_code & 0xff) == 0xe8 )
Serial.println(F("Keyboard OK..")); // Response was Echo or power up
else
if ((scan_code & 0xff) == 0)
{
Serial.println(F("Keyboard Not Found") );
}
else
{
Serial.print(F("Invalid Code received of 0x"));
Serial.println(scan_code, HEX);
}
#endif
keyboard.setNoBreak(1);
keyboard.setNoRepeat(1);
// keymap.selectMap((char *)"UK");
@ -832,15 +775,6 @@ void loop()
if (scan_code > 0)
{
#ifdef _SMARTY_DEBUG_
Serial.print(F("1.scan_code: 0x"));
Serial.print(scan_code, HEX);
Serial.print(F(", c: 0x"));
Serial.print(c, HEX);
Serial.print(F(", ASCII "));
Serial.write(c);
Serial.println();
#endif
// process Backspace, Left Arrow, Delete as Apple I backspace '_'
switch (c)
{
@ -854,31 +788,10 @@ void loop()
c = '_';
break;
}
#ifdef _SMARTY_DEBUG_
Serial.print("2.scan_code: 0x");
Serial.print(scan_code, HEX);
Serial.print(", c: 0x");
Serial.print(c, HEX);
Serial.print(", ASCII ");
Serial.write(c);
Serial.println();
#endif
// make all symbols uppercase (from 'a' (ASCII code 0x61) to 'z' (ASCII code 0x7A))
// as in original Apple-1
c = toupper(c);
// print c to Keyboard Port to be read by CPU
#ifdef _SMARTY_DEBUG_
Serial.print("3.scan_code: 0x");
Serial.print(scan_code, HEX);
Serial.print(", c: 0x");
Serial.print(c, HEX);
Serial.print(", ASCII ");
Serial.write(c);
Serial.println();
#endif
sendCharToKeyboardPort(c);
}
}