Included keys are Num Lock, Backspace, Enter, Space, English letters and numerals.

Dependents:   eee212projectWithSD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PS2ASCII.cpp Source File

PS2ASCII.cpp

00001 #include "PS2ASCII.h"
00002 
00003 //===================================
00004 //*Constructor*
00005 //===================================
00006 
00007 PS2ASCII::PS2ASCII(PinName data, PinName clk) : kbd(data,clk) ,_caps(false), _shift(false),  _E0flag(false) ,_numlock(false)
00008 {
00009     
00010 }
00011 
00012 //===================================
00013 //Functions
00014 //===================================
00015 
00016 unsigned char PS2ASCII::getChar()
00017 {
00018      while(1)
00019      {
00020          _E0flag = false;
00021          unsigned char keypress = kbd.rd();                //code in
00022         
00023         if(keypress == 0xE0)                            //is it a special E0 key?
00024         {
00025             keypress = kbd.rd();                        //next byte in
00026             if(keypress == 0xF0)                        //is it a break code?
00027             {
00028                 keypress = kbd.rd();                    //if so chew it up and back to start
00029                 
00030                 continue;
00031             }
00032             
00033             _E0flag = true;                                //tell us that it is an E0 function
00034             return keypress;
00035         }
00036         
00037         if((keypress == 0x12) || (keypress == 0x59))    //SHIFT pressed?
00038         {
00039             _shift = true;
00040             continue;
00041         }
00042         
00043         if(keypress == 0xF0)                            //gets rid of byte 1 of break code
00044         {
00045             keypress = kbd.rd();                        //byte 2 of break code in
00046                 
00047             if((keypress == 0x12) || (keypress == 0x59))
00048             {
00049                 _shift = false;
00050             }
00051             continue;
00052         }
00053         
00054         switch (keypress) {
00055             case 0x58:
00056                 _caps = !_caps; //CAPS LOCK key
00057                 kbd.wr(0xED);
00058                 if (_caps && _numlock)
00059                     kbd.wr(0x06);
00060                 else if (_caps && !_numlock)
00061                     kbd.wr(0x04);
00062                 else if (!_caps && _numlock)
00063                     kbd.wr(0x02);
00064                 else
00065                     kbd.wr(0x00);                                
00066                 continue;
00067             case 0x77:
00068                 _numlock = !_numlock;
00069                 kbd.wr(0xED);
00070                 if (_caps && _numlock)
00071                     kbd.wr(0x06);
00072                 else if (_caps && !_numlock)
00073                     kbd.wr(0x04);
00074                 else if (!_caps && _numlock)
00075                     kbd.wr(0x02);
00076                 else
00077                     kbd.wr(0x00); 
00078                 continue;
00079             default:
00080                 break;
00081         }
00082         
00083 // We do not need shift and caps lock in our project, since we do not use uppercase letters.        
00084         
00085 //        unsigned char initial_keypress = keypress;
00086         
00087 //        if(_shift == true)                                //if SHIFT is pressed take shifted character
00088 //        {
00089 //            keypress = shift_on[keypress];
00090 //        }
00091 //            
00092 //       if((_caps == true)&&(initial_keypress >= 97)&&(initial_keypress <= 127))
00093 //        {
00094 //            keypress = shift_on[keypress];                //if caps is on shift the letter up
00095 //        }
00096 
00097         return(keypress);    
00098               
00099     }
00100 }
00101 
00102 unsigned char PS2ASCII::getASCII(unsigned char keypress) {
00103     unsigned char character = ps2CharMap[keypress];
00104     
00105     if (!_E0flag && !_numlock) {
00106             if (keypress == 0x70 || keypress == 0x69 || keypress == 0x72 || keypress == 0x7A ||
00107                 keypress == 0x6B || keypress == 0x73 || keypress == 0x74 || keypress == 0x6C ||
00108                 keypress == 0x75 || keypress == 0x7D)
00109                 character = ' ';
00110                  
00111         }
00112         
00113     return character;
00114 }
00115     
00116 bool PS2ASCII::E0flag()
00117 {
00118     return _E0flag;
00119 }
00120 
00121 bool PS2ASCII::numlock()
00122 {
00123     return _numlock;
00124 }
00125      
00126 bool PS2ASCII::caps() {
00127     return _caps;
00128 }
00129 
00130 bool PS2ASCII::shift() {
00131     return _shift;     
00132 }