Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:231dc8341ad9, committed 2009-11-12
- Comitter:
- simon
- Date:
- Thu Nov 12 00:51:56 2009 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 231dc8341ad9 PS2ASCII.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PS2ASCII.cpp Thu Nov 12 00:51:56 2009 +0000
@@ -0,0 +1,92 @@
+#include "PS2ASCII.h"
+
+//===================================
+//*Constructor*
+//===================================
+
+PS2ASCII::PS2ASCII(PinName data, PinName clk) : kbd(data,clk) ,_caps(false), _shift(false), _E0flag(false) ,_numlock(false)
+{
+
+}
+
+//===================================
+//Functions
+//===================================
+
+unsigned char PS2ASCII::getChar()
+{
+ while(1)
+ {
+ _E0flag = false;
+ unsigned char keypress = kbd.rd(); //code in
+
+ if(keypress == 0xE0) //is it a special E0 key?
+ {
+ keypress = kbd.rd(); //next byte in
+ if(keypress == NULL) //is it a break code?
+ {
+ keypress = kbd.rd(); //if so chew it up and back to start
+
+ continue;
+ }
+
+ _E0flag = true; //tell us that it is an E0 function
+ return keypress;
+ }
+
+ if((keypress == 0x12) || (keypress == 0x59)) //SHIFT pressed?
+ {
+ _shift = true;
+ continue;
+ }
+
+ if(keypress == NULL) //gets rid of byte 1 of break code
+ {
+ keypress = kbd.rd(); //byte 2 of break code in
+
+ if((keypress == 0x12) || (keypress == 0x59))
+ {
+ _shift = false;
+ }
+ continue;
+ }
+
+ switch (keypress) {
+ case 0x58:
+ _caps = !_caps; //CAPS LOCK key
+ continue;
+ case 0x80:
+ _numlock = !_numlock;
+ continue;
+ default:
+ break;
+ }
+
+ unsigned char initial_keypress = keypress;
+
+ if(_shift == true) //if SHIFT is pressed take shifted character
+ {
+ keypress = shift_on[keypress];
+ }
+
+ if((_caps == true)&&(initial_keypress >= 97)&&(initial_keypress <= 127))
+ {
+ keypress = shift_on[keypress]; //if caps is on shift the letter up
+ }
+
+ return(keypress);
+
+ }
+}
+
+bool PS2ASCII::E0flag()
+{
+ return _E0flag;
+}
+
+bool PS2ASCII::numlock()
+{
+ return _numlock;
+}
+
+
diff -r 000000000000 -r 231dc8341ad9 PS2ASCII.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PS2ASCII.h Thu Nov 12 00:51:56 2009 +0000
@@ -0,0 +1,47 @@
+#ifndef MBED_PS2ASCII_H
+#define MBED_PS2ASCII_H
+
+#include "PS2Kbd.h"
+#include "mbed.h"
+
+class PS2ASCII {
+
+public:
+
+ PS2ASCII(PinName data, PinName clk);
+ unsigned char getChar();
+ bool caps();
+ bool shift();
+ bool E0flag();
+ bool numlock();
+
+private:
+
+ PS2Kbd kbd;
+ bool _caps;
+ bool _shift;
+ bool _E0flag;
+ bool _numlock;
+};
+
+ static const unsigned char shift_on[] = {
+// 0 1 2 3 4 5 6 7 8 9 A B C D E F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '`', ' ', // 00-0F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 10-1F
+ ' ', 0x21, 0x22, '~', 0x24, 0x25, 0x26, '@', 0x28, 0x29, 0x30, 0x31, '<', '_', '>', '?', // 20-2F
+ ')', '!', '\"', 0x9C, '$', '%', '^', '&', '*', '(', ' ', ':', ' ', '+', ' ', ' ', // 30-3F
+ 0xAA, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', // 40-6F
+ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', ' ', ' ', // 50-7F
+ ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', // 60-4F
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, // 70-5F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 80-8F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 90-9F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // A0-AF
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // B0-BF
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // C0-CF
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // D0-DF
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // E0-EF
+ NULL, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // F0-FF
+};
+
+#endif
diff -r 000000000000 -r 231dc8341ad9 PS2Kbd.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PS2Kbd.cpp Thu Nov 12 00:51:56 2009 +0000
@@ -0,0 +1,73 @@
+#include "PS2Kbd.h"
+#include "mbed.h"
+
+
+/* ==================================================================
+ * Constructor
+ ================================================================= */
+
+PS2Kbd::PS2Kbd(PinName clk, PinName din)
+ : _ps2clk(clk), _ps2din(din) {
+
+}
+
+// =============================
+//
+// =============================
+unsigned char PS2Kbd::rd(void)
+{
+ unsigned int buf = 0x00;
+ int i;
+
+ _ps2clk = 0;
+
+ // both input
+ _ps2din.input();
+ _ps2clk.input();
+
+ for(i=0;i<11;i++) {
+ while(_ps2clk);
+ while(!_ps2clk);
+ buf = buf >> 1;
+ buf |= _ps2din ? 512 : 0;
+ }
+
+ // output
+ _ps2clk.output();
+
+ buf &= 0xFF;
+
+ return(ps2KeyMap[(unsigned char)buf]);
+ //return (unsigned char)buf;
+}
+
+
+// =============================
+//
+// =============================
+void PS2Kbd::wr(unsigned char)
+{
+ unsigned int buf = 0x00;
+ int i;
+
+ _ps2clk = 0;
+
+ // both input
+ _ps2din.input();
+ _ps2clk.input();
+
+ for(i=0;i<11;i++) {
+ while(_ps2clk);
+ while(!_ps2clk);
+ buf = buf >> 1;
+ buf |= _ps2din ? 512 : 0;
+ }
+
+ // output
+ _ps2clk.output();
+
+ buf &= 0xFF;
+
+}
+
+
diff -r 000000000000 -r 231dc8341ad9 PS2Kbd.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PS2Kbd.h Thu Nov 12 00:51:56 2009 +0000
@@ -0,0 +1,41 @@
+#ifndef PS2KBD_H
+#define PS2KBD_H
+
+#include "mbed.h"
+
+class PS2Kbd {
+
+public:
+
+ PS2Kbd(PinName clk, PinName din);
+ unsigned char rd(void);
+ void wr(unsigned char);
+
+protected:
+ DigitalInOut _ps2clk;
+ DigitalInOut _ps2din;
+
+};
+
+
+static const unsigned char ps2KeyMap[] = {
+// 0 1 2 3 4 5 6 7 8 9 A B C D E F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '`', ' ', // 00-0F
+ ' ', ' ', 0x12, ' ', ' ', 'q', '1', ' ', ' ', ' ', 'z', 's', 'a', 'w', '2', ' ', // 10-1F
+ ' ', 'c', 'x', 'd', 'e', '4', '3', ' ', ' ', ' ', 'v', 'f', 't', 'r', '5', ' ', // 20-2F
+ ' ', 'n', 'b', 'h', 'g', 'y', '6', ' ', ' ', ' ', 'm', 'j', 'u', '7', '8', ' ', // 30-3F
+ ' ', ',', 'k', 'i', 'o', '0', '9', ' ', ' ', '.', '/', 'l', ';', 'p', '-', ' ', // 40-4F
+ ' ', ' ','\'', ' ', '[', '=', ' ', ' ', 0x58, 0x59, 0x5A, ']', ' ','\\', ' ', ' ', // 50-5F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', '4', '7', ' ', ' ', '1', // 60-6F
+ '0', '.', '2', '5', '6', '8', 0x1B, 0x80, ' ', '+', '3', '-', '*', '9', ' ', ' ', // 70-7F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 80-8F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 90-9F
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // A0-AF
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // B0-BF
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // C0-CF
+ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // D0-DF
+ 0xE0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // E0-EF
+ NULL, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // F0-FF
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 231dc8341ad9 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Nov 12 00:51:56 2009 +0000
@@ -0,0 +1,15 @@
+#include "mbed.h"
+#include "PS2ASCII.h"
+
+PS2ASCII myKeyboard(p28, p26);
+DigitalOut LED(LED1);
+
+int main() {
+ while (1) {
+ unsigned char symbol = myKeyboard.getChar();
+ if(myKeyboard.E0flag() == true) { //is the make code one or two bytes long?
+ printf(" E0 ");
+ }
+ printf("%c", symbol);
+ }
+}
\ No newline at end of file
diff -r 000000000000 -r 231dc8341ad9 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Nov 12 00:51:56 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479