Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <PS2KeyAdvanced.h>
- //#define DEBUG
- #ifdef DEBUG
- #define DEBUG_PRINT(x) Serial.print(x)
- #define DEBUG_PRINTLN(x) Serial.println(x)
- #define DEBUG_PRINTLNHEX(x) Serial.println(x, HEX)
- #define DEBUG_BEGIN(x) Serial.begin(x)
- #define DEBUG_BEGIN_MODE(x,y) Serial.begin(x,y)
- #else
- #define DEBUG_PRINT(x)
- #define DEBUG_PRINTLN(x)
- #define DEBUG_PRINTLNHEX(x)
- #define DEBUG_BEGIN(x)
- #define DEBUG_BEGIN_MODE
- #endif
- #define DATAPIN 4
- #define IRQPIN 3
- #define NUM_OF_LINES 2
- #define NUM_OF_COLS 40
- #define TAB_STOP 4
- #define UPPER 0
- #define LOWER 1
- PS2KeyAdvanced keyboard;
- LiquidCrystal_I2C lcd(0x27, 40, 2); // set the LCD address to 0x27 for a 40 chars and 2 line display
- // will demux the enable for 2 screens/half of the screen
- int E1 = 7;
- int E2 = 6;
- int E = 2;
- volatile int halfSel;
- int lineNum;
- int colNum;
- int cursorPos;
- bool isShift, isCaps, isOvwr;
- bool kbOK;
- uint16_t c;
- // init display with whitespaces
- int l, i;
- char msg[(2*NUM_OF_LINES*NUM_OF_COLS)] = "";
- void selectScreen() {
- if (halfSel == LOWER) {
- digitalWrite(E2, digitalRead(E));
- }
- else {
- digitalWrite(E1, digitalRead(E));
- }
- }
- void setup() {
- DEBUG_BEGIN(115200);
- pinMode(E1, OUTPUT);
- pinMode(E2, OUTPUT);
- pinMode(E, INPUT);
- attachInterrupt(digitalPinToInterrupt(E), selectScreen, CHANGE);
- lineNum = 0;
- colNum = 0;
- cursorPos = (lineNum*40) + colNum;
- halfSel = UPPER;
- lcd.begin(NUM_OF_COLS, NUM_OF_LINES);
- lcd.backlight();
- lcd.clear();
- lcd.setCursor(colNum, lineNum);
- halfSel = LOWER;
- lcd.begin(NUM_OF_COLS, NUM_OF_LINES);
- lcd.clear();
- lcd.setCursor(colNum, lineNum);
- halfSel = UPPER;
- lcd.cursor();
- delay(1000);
- kbOK = false;
- keyboard.begin( DATAPIN, IRQPIN );
- keyboard.setNoBreak(1); // No break codes for keys (when key released)
- keyboard.setNoRepeat(1); // Don't repeat shift ctrl etc
- while (!(kbOK)) {
- keyboard.echo( ); // ping keyboard to see if there
- delay(6);
- c = keyboard.read( );
- if ((c & 0xFF) == PS2_KEY_ECHO || (c & 0xFF) == PS2_KEY_BAT) {
- DEBUG_PRINTLN("Keyboard OK.."); // Response was Echo or power up
- halfSel = UPPER;
- lcd.print("Keyboard OK.."); // Response was Echo or power up
- kbOK = true;
- }
- else if ((c & 0xFF) == 0) {
- DEBUG_PRINTLN("Keyboard Not Found");
- halfSel = UPPER;
- lcd.print("Keyboard Not Found! Please insert a keyboard!");
- }
- else
- {
- halfSel = UPPER;
- lcd.print("Keyboard code invalid! Please re-insert keyboard!");
- DEBUG_PRINT("Invalid Code received of ");
- DEBUG_PRINTLNHEX(c);
- }
- delay(1000);
- lcd.clear();
- }
- isShift = false;
- isCaps = false;
- isOvwr = false;
- delay(1000);
- for (i=0;i<(2*NUM_OF_LINES*NUM_OF_COLS);i++) {
- l = strlen(msg);
- msg[l] = ' ';
- msg[l+1] = '\0';
- }
- }
- void loop() {
- if (keyboard.available()) {
- // read the next key
- c = keyboard.read();
- DEBUG_PRINTLNHEX(c);
- // key released - do nothing
- if ((c & PS2_BREAK) != 0) {
- DEBUG_PRINTLN("Key Released");
- }
- else {
- if ((c & PS2_SHIFT) != 0) {
- isShift = true;
- DEBUG_PRINTLN("SHIFT key pressed");
- }
- else {
- isShift = false;
- }
- if ((c & PS2_CAPS) != 0) {
- isCaps = true;
- DEBUG_PRINTLN("CAPS LOCK ON");
- }
- else {
- isCaps = false;
- }
- if (((c & PS2_CTRL) == 0) && ((c & PS2_ALT) == 0) && ((c & PS2_ALT_GR) == 0)) {
- switch (c & 0xFF) {
- case PS2_KEY_HOME: {
- if (colNum != 0) {
- colNum = 0;
- if (lineNum > 1) {
- halfSel = LOWER;
- lcd.setCursor(colNum, (lineNum-2));
- }
- else {
- halfSel = UPPER;
- lcd.setCursor(colNum, lineNum);
- }
- }
- break;
- }
- case PS2_KEY_END: {
- if (colNum != (NUM_OF_COLS-1)) {
- colNum = NUM_OF_COLS-1;
- if (lineNum > 1) {
- halfSel = LOWER;
- lcd.setCursor(colNum, (lineNum-2));
- }
- else {
- halfSel = UPPER;
- lcd.setCursor(colNum, lineNum);
- }
- }
- break;
- }
- case PS2_KEY_PGUP: {
- if (lineNum > 1) {
- lineNum = lineNum - 2;
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- }
- else {
- lineNum = 0;
- colNum = 0;
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- }
- break;
- }
- case PS2_KEY_PGDN: {
- if (lineNum <= 1) {
- lineNum = lineNum + 2;
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- }
- else {
- lineNum = 2*NUM_OF_LINES-1;
- colNum = NUM_OF_COLS-1;
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- }
- break;
- }
- case PS2_KEY_L_ARROW: {
- if (colNum == 0) {
- colNum = NUM_OF_COLS-1;
- if (lineNum == 0) {
- lineNum = 2*NUM_OF_LINES-1;
- }
- else {
- lineNum = lineNum - 1;
- }
- }
- else {
- colNum = colNum - 1;
- }
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- break;
- }
- case PS2_KEY_R_ARROW: {
- if (colNum == (NUM_OF_COLS-1)) {
- colNum = 0;
- if (lineNum == (2*NUM_OF_LINES-1)) {
- lineNum = 0;
- }
- else {
- lineNum = lineNum + 1;
- }
- }
- else {
- colNum = colNum + 1;
- }
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- break;
- }
- case PS2_KEY_UP_ARROW: {
- if (lineNum == 0) {
- lineNum = 2*NUM_OF_LINES-1;
- }
- else {
- lineNum = lineNum - 1;
- }
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- break;
- }
- case PS2_KEY_DN_ARROW: {
- if (lineNum == (2*NUM_OF_LINES-1)) {
- lineNum = 0;
- }
- else {
- lineNum = lineNum + 1;
- }
- jumpSetCursor(colNum, lineNum, isOvwr, true);
- break;
- }
- case PS2_KEY_INSERT: {
- isOvwr = !isOvwr;
- jumpSetCursor(colNum, lineNum, isOvwr, false);
- break;
- }
- case PS2_KEY_DELETE: {
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- deleteChar(cursorPos, msg);
- refreshDisplay(cursorPos, msg, isOvwr);
- break;
- }
- case PS2_KEY_BS: {
- if (colNum > 0) {
- colNum--;
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- deleteChar(cursorPos, msg);
- refreshDisplay(cursorPos, msg, isOvwr);
- }
- else {
- if (lineNum > 0) {
- lineNum--;
- colNum = NUM_OF_COLS-1;
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- deleteChar(cursorPos, msg);
- refreshDisplay(cursorPos, msg, isOvwr);
- }
- }
- break;
- }
- case PS2_KEY_TAB: {
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- colNum = colNum + (TAB_STOP - (colNum % TAB_STOP));
- if (colNum >= NUM_OF_COLS) {
- if (lineNum >= (2*NUM_OF_LINES - 1)) {
- colNum = NUM_OF_COLS - 1;
- }
- else {
- colNum = 0;
- lineNum++;
- }
- }
- for (i=cursorPos;i<((lineNum*NUM_OF_COLS) + colNum);i++) {
- if (isOvwr) {
- deleteChar(i, msg);
- }
- insertChar (i, msg, ' ');
- }
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- refreshDisplay(cursorPos, msg, isOvwr);
- break;
- }
- case PS2_KEY_ENTER: {
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- if (lineNum >= (2*NUM_OF_LINES - 1)) {
- colNum = NUM_OF_COLS - 1;
- }
- else {
- lineNum++;
- colNum = 0;
- }
- if (isOvwr) {
- deleteChar(cursorPos, msg);
- }
- insertChar (cursorPos, msg, ' ');
- for (i=(cursorPos+1);i<((lineNum*NUM_OF_COLS) + colNum);i++) {
- insertChar (i, msg, ' ');
- }
- if (colNum) {
- insertChar (i, msg, ' ');
- }
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- refreshDisplay(cursorPos, msg, isOvwr);
- break;
- }
- case PS2_KEY_SPACE: {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, ' ', isOvwr);
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_0: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, ')', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '0', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_1: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '!', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '1', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_2: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '@', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '2', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_3: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '#', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '3', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_4: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '$', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '4', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_5: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '%', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '5', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_6: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '^', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '6', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_7: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '&', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '7', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_8: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '*', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '8', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_9: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '(', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '9', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_APOS: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '\"', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '\'', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_COMMA: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '<', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, ',', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_MINUS: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '_', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '-', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_DOT: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '>', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '.', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_DIV: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '\?', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '/', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_SINGLE: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '~', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '`', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_A: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'a', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'A', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'a', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_B: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'b', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'B', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'b', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_C: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'c', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'C', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'c', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_D: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'b', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'D', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'd', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_E: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'e', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'E', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'e', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_F: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'f', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'F', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'f', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_G: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'g', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'G', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'g', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_H: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'h', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'H', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'h', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_I: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'i', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'I', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'i', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_J: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'j', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'J', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'j', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_K: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'k', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'K', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'k', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_L: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'l', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'L', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'l', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_M: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'm', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'M', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'm', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_N: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'n', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'N', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'n', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_O: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'o', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'O', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'o', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_P: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'p', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'P', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'p', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_Q: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'q', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'Q', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'q', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_R: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'r', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'R', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'r', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_S: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 's', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'S', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 's', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_T: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 't', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'T', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 't', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_U: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'u', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'U', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'u', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_V: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'v', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'V', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'v', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_W: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'w', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'W', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'w', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_X: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'x', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'X', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'x', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_Y: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'y', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'Y', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'y', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_Z: {
- if ((isCaps) || (isShift)) {
- if ((isCaps) && (isShift)) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'z', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'Z', isOvwr);
- }
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, 'z', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_SEMI: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, ':', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, ';', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_BACK: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '|', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '\\', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_OPEN_SQ: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '{', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '[', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_CLOSE_SQ: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '}', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, ']', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- case PS2_KEY_EQUAL: {
- if (isShift) {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '+', isOvwr);
- }
- else {
- cursorPos = addCharOnDisp(lineNum, colNum, msg, '=', isOvwr);
- }
- colNum = cursorPos % NUM_OF_COLS;
- lineNum = (int)(cursorPos / NUM_OF_COLS);
- break;
- }
- default: {
- break;
- } // do nothing
- }
- }
- }
- delay(20);
- }
- }
- void jumpSetCursor(int colNum, int lineNum, bool isOvwr, bool jump) {
- if (lineNum > 1) {
- halfSel = UPPER;
- lcd.noCursor();
- lcd.noBlink();
- halfSel = LOWER;
- lcd.cursor();
- if (isOvwr) {
- lcd.blink();
- }
- else {
- lcd.noBlink();
- }
- if (jump) {
- lcd.setCursor(colNum, (lineNum-2));
- }
- }
- else {
- halfSel = LOWER;
- lcd.noCursor();
- lcd.noBlink();
- halfSel = UPPER;
- lcd.cursor();
- if (isOvwr) {
- lcd.blink();
- }
- else {
- lcd.noBlink();
- }
- if (jump) {
- lcd.setCursor(colNum, lineNum);
- }
- }
- }
- void deleteChar (int cursorPos, char str[]) {
- int len;
- int i,j;
- len = (int)strlen(str);
- for (i=cursorPos;i<(len-1);i++) {
- str[i] = str[i+1];
- }
- str[(len-1)] = ' ';
- }
- void insertChar (int cursorPos, char str[], char ch) {
- int len;
- int i;
- len = (int)strlen(str);
- for (i=(len-1);i>cursorPos;i--) {
- str[i] = str[i-1];
- }
- str[cursorPos] = ch;
- }
- void refreshDisplay(int cursorPos, char str[], bool isOvwr) {
- int i,j;
- int col, line;
- int len;
- len = (int)strlen(str);
- for (j=0;j<(2*NUM_OF_LINES);j++) {
- if (j > 1) {
- halfSel = LOWER;
- lcd.setCursor(0,(j-2));
- for (i=(j*NUM_OF_COLS);i<((j+1)*NUM_OF_COLS);i++) {
- lcd.print(str[i]);
- }
- }
- else {
- halfSel = UPPER;
- lcd.setCursor(0,j);
- for (i=(j*NUM_OF_COLS);i<((j+1)*NUM_OF_COLS);i++) {
- lcd.print(str[i]);
- }
- }
- }
- col = cursorPos % NUM_OF_COLS;
- line = (int)(cursorPos / NUM_OF_COLS);
- jumpSetCursor(col, line, isOvwr, true);
- }
- int addCharOnDisp (int lineNum, int colNum, char msg[], char ch, bool isOvwr) {
- int cursorPos;
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- if (isOvwr) {
- deleteChar(cursorPos, msg);
- }
- insertChar (cursorPos, msg, ch);
- if (colNum == (NUM_OF_COLS-1)) {
- if (lineNum < (2*NUM_OF_LINES-1)) {
- colNum = 0;
- lineNum = lineNum + 1;
- }
- }
- else {
- colNum = colNum + 1;
- }
- cursorPos = (lineNum*NUM_OF_COLS) + colNum;
- refreshDisplay(cursorPos, msg, isOvwr);
- return cursorPos;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement