Advertisement
RebounD11

tastatura_lcd_4x40_serial

Dec 6th, 2020
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 34.77 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <PS2KeyAdvanced.h>
  4.  
  5. //#define DEBUG
  6.  
  7. #ifdef DEBUG
  8.   #define DEBUG_PRINT(x) Serial.print(x)
  9.   #define DEBUG_PRINTLN(x) Serial.println(x)
  10.   #define DEBUG_PRINTLNHEX(x) Serial.println(x, HEX)
  11.   #define DEBUG_BEGIN(x) Serial.begin(x)
  12.   #define DEBUG_BEGIN_MODE(x,y) Serial.begin(x,y)
  13. #else
  14.   #define DEBUG_PRINT(x)
  15.   #define DEBUG_PRINTLN(x)
  16.   #define DEBUG_PRINTLNHEX(x)
  17.   #define DEBUG_BEGIN(x)
  18.   #define DEBUG_BEGIN_MODE
  19. #endif
  20.  
  21. #define DATAPIN 4
  22. #define IRQPIN  3
  23.  
  24. #define NUM_OF_LINES 2
  25. #define NUM_OF_COLS 40
  26. #define TAB_STOP 4
  27. #define UPPER 0
  28. #define LOWER 1
  29.  
  30. PS2KeyAdvanced keyboard;
  31. LiquidCrystal_I2C lcd(0x27, 40, 2);  // set the LCD address to 0x27 for a 40 chars and 2 line display
  32. // will demux the enable for 2 screens/half of the screen
  33.  
  34. int E1 = 7;
  35. int E2 = 6;
  36. int E = 2;
  37.  
  38. volatile int halfSel;
  39. int lineNum;
  40. int colNum;
  41. int cursorPos;
  42. bool isShift, isCaps, isOvwr;
  43. bool kbOK;
  44.  
  45. uint16_t c;
  46. // init display with whitespaces
  47. int l, i;
  48. char msg[(2*NUM_OF_LINES*NUM_OF_COLS)] = "";
  49.  
  50. void selectScreen() {
  51.   if (halfSel == LOWER) {
  52.     digitalWrite(E2, digitalRead(E));
  53.   }
  54.   else {
  55.     digitalWrite(E1, digitalRead(E));
  56.   }
  57. }
  58.  
  59. void setup() {  
  60.   DEBUG_BEGIN(115200);
  61.   pinMode(E1, OUTPUT);
  62.   pinMode(E2, OUTPUT);
  63.   pinMode(E, INPUT);
  64.   attachInterrupt(digitalPinToInterrupt(E), selectScreen, CHANGE);
  65.   lineNum = 0;
  66.   colNum = 0;
  67.   cursorPos = (lineNum*40) + colNum;
  68.   halfSel = UPPER;
  69.   lcd.begin(NUM_OF_COLS, NUM_OF_LINES);
  70.   lcd.backlight();
  71.   lcd.clear();
  72.   lcd.setCursor(colNum, lineNum);
  73.   halfSel = LOWER;
  74.   lcd.begin(NUM_OF_COLS, NUM_OF_LINES);
  75.   lcd.clear();
  76.   lcd.setCursor(colNum, lineNum);
  77.   halfSel = UPPER;
  78.   lcd.cursor();
  79.   delay(1000);
  80.   kbOK = false;
  81.   keyboard.begin( DATAPIN, IRQPIN );
  82.   keyboard.setNoBreak(1);         // No break codes for keys (when key released)
  83.   keyboard.setNoRepeat(1);        // Don't repeat shift ctrl etc
  84.   while (!(kbOK)) {
  85.     keyboard.echo( );              // ping keyboard to see if there
  86.     delay(6);
  87.     c = keyboard.read( );
  88.     if ((c & 0xFF) == PS2_KEY_ECHO || (c & 0xFF) == PS2_KEY_BAT) {
  89.       DEBUG_PRINTLN("Keyboard OK..");    // Response was Echo or power up
  90.       halfSel = UPPER;
  91.       lcd.print("Keyboard OK..");    // Response was Echo or power up
  92.       kbOK = true;
  93.     }
  94.     else if ((c & 0xFF) == 0) {
  95.       DEBUG_PRINTLN("Keyboard Not Found");
  96.       halfSel = UPPER;
  97.       lcd.print("Keyboard Not Found! Please insert a keyboard!");
  98.     }
  99.     else
  100.     {
  101.       halfSel = UPPER;
  102.       lcd.print("Keyboard code invalid! Please re-insert keyboard!");
  103.       DEBUG_PRINT("Invalid Code received of ");
  104.       DEBUG_PRINTLNHEX(c);
  105.     }
  106.     delay(1000);
  107.     lcd.clear();
  108.   }
  109.   isShift = false;
  110.   isCaps = false;
  111.   isOvwr = false;
  112.   delay(1000);
  113.   for (i=0;i<(2*NUM_OF_LINES*NUM_OF_COLS);i++) {
  114.     l = strlen(msg);
  115.     msg[l] = ' ';
  116.     msg[l+1] = '\0';
  117.   }
  118. }
  119.  
  120. void loop() {
  121.   if (keyboard.available()) {
  122.     // read the next key
  123.     c = keyboard.read();
  124.     DEBUG_PRINTLNHEX(c);
  125.     // key released - do nothing
  126.     if ((c & PS2_BREAK) != 0) {
  127.         DEBUG_PRINTLN("Key Released");
  128.     }
  129.     else {
  130.       if ((c & PS2_SHIFT) != 0) {
  131.         isShift = true;
  132.         DEBUG_PRINTLN("SHIFT key pressed");
  133.       }
  134.       else {
  135.         isShift = false;
  136.       }
  137.       if ((c & PS2_CAPS) != 0) {
  138.         isCaps = true;
  139.         DEBUG_PRINTLN("CAPS LOCK ON");
  140.       }
  141.       else {
  142.         isCaps = false;
  143.       }
  144.       if (((c & PS2_CTRL) == 0) && ((c & PS2_ALT) == 0) && ((c & PS2_ALT_GR) == 0)) {
  145.         switch (c & 0xFF) {
  146.           case PS2_KEY_HOME: {
  147.             if (colNum != 0) {
  148.               colNum = 0;
  149.               if (lineNum > 1) {
  150.                 halfSel = LOWER;
  151.                 lcd.setCursor(colNum, (lineNum-2));
  152.               }
  153.               else {
  154.                 halfSel = UPPER;
  155.                 lcd.setCursor(colNum, lineNum);
  156.               }
  157.             }
  158.             break;
  159.           }
  160.           case PS2_KEY_END: {
  161.             if (colNum != (NUM_OF_COLS-1)) {
  162.               colNum = NUM_OF_COLS-1;
  163.               if (lineNum > 1) {
  164.                 halfSel = LOWER;
  165.                 lcd.setCursor(colNum, (lineNum-2));
  166.               }
  167.               else {
  168.                 halfSel = UPPER;
  169.                 lcd.setCursor(colNum, lineNum);
  170.               }
  171.             }
  172.             break;
  173.           }
  174.           case PS2_KEY_PGUP: {
  175.             if (lineNum > 1) {
  176.               lineNum = lineNum - 2;
  177.               jumpSetCursor(colNum, lineNum, isOvwr, true);
  178.             }
  179.             else {
  180.               lineNum = 0;
  181.               colNum = 0;
  182.               jumpSetCursor(colNum, lineNum, isOvwr, true);
  183.             }
  184.             break;
  185.           }
  186.           case PS2_KEY_PGDN: {
  187.             if (lineNum <= 1) {
  188.               lineNum = lineNum + 2;
  189.               jumpSetCursor(colNum, lineNum, isOvwr, true);
  190.             }
  191.             else {
  192.               lineNum = 2*NUM_OF_LINES-1;
  193.               colNum = NUM_OF_COLS-1;
  194.               jumpSetCursor(colNum, lineNum, isOvwr, true);
  195.             }
  196.             break;
  197.           }
  198.           case PS2_KEY_L_ARROW: {
  199.             if (colNum == 0) {
  200.               colNum = NUM_OF_COLS-1;
  201.               if (lineNum == 0) {
  202.                 lineNum = 2*NUM_OF_LINES-1;
  203.               }
  204.               else {
  205.                 lineNum = lineNum - 1;
  206.               }
  207.             }
  208.             else {
  209.               colNum = colNum - 1;
  210.             }
  211.             jumpSetCursor(colNum, lineNum, isOvwr, true);
  212.             break;
  213.           }
  214.           case PS2_KEY_R_ARROW: {
  215.             if (colNum == (NUM_OF_COLS-1)) {
  216.               colNum = 0;
  217.               if (lineNum == (2*NUM_OF_LINES-1)) {
  218.                 lineNum = 0;
  219.               }
  220.               else {
  221.                 lineNum = lineNum + 1;
  222.               }
  223.             }
  224.             else {
  225.               colNum = colNum + 1;
  226.             }
  227.             jumpSetCursor(colNum, lineNum, isOvwr, true);
  228.             break;
  229.           }
  230.           case PS2_KEY_UP_ARROW: {
  231.             if (lineNum == 0) {
  232.               lineNum = 2*NUM_OF_LINES-1;
  233.             }
  234.             else {
  235.               lineNum = lineNum - 1;
  236.             }
  237.             jumpSetCursor(colNum, lineNum, isOvwr, true);
  238.             break;
  239.           }
  240.           case PS2_KEY_DN_ARROW: {
  241.             if (lineNum == (2*NUM_OF_LINES-1)) {
  242.               lineNum = 0;
  243.             }
  244.             else {
  245.               lineNum = lineNum + 1;
  246.             }
  247.             jumpSetCursor(colNum, lineNum, isOvwr, true);
  248.             break;
  249.           }
  250.           case PS2_KEY_INSERT: {
  251.             isOvwr = !isOvwr;
  252.             jumpSetCursor(colNum, lineNum, isOvwr, false);
  253.             break;
  254.           }
  255.           case PS2_KEY_DELETE: {
  256.             cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  257.             deleteChar(cursorPos, msg);
  258.             refreshDisplay(cursorPos, msg, isOvwr);
  259.             break;
  260.           }
  261.           case PS2_KEY_BS: {
  262.             if (colNum > 0) {
  263.               colNum--;
  264.               cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  265.               deleteChar(cursorPos, msg);
  266.               refreshDisplay(cursorPos, msg, isOvwr);
  267.             }
  268.             else {
  269.               if (lineNum > 0) {
  270.                 lineNum--;
  271.                 colNum = NUM_OF_COLS-1;
  272.                 cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  273.                 deleteChar(cursorPos, msg);
  274.                 refreshDisplay(cursorPos, msg, isOvwr);
  275.               }
  276.             }
  277.             break;
  278.           }
  279.           case PS2_KEY_TAB: {
  280.             cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  281.             colNum = colNum + (TAB_STOP - (colNum % TAB_STOP));
  282.             if (colNum >= NUM_OF_COLS) {
  283.               if (lineNum >= (2*NUM_OF_LINES - 1)) {
  284.                 colNum = NUM_OF_COLS - 1;
  285.               }
  286.               else {
  287.                 colNum = 0;
  288.                 lineNum++;
  289.               }
  290.             }
  291.             for (i=cursorPos;i<((lineNum*NUM_OF_COLS) + colNum);i++) {
  292.               if (isOvwr) {
  293.                 deleteChar(i, msg);
  294.               }
  295.               insertChar (i, msg, ' ');
  296.             }
  297.             cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  298.             refreshDisplay(cursorPos, msg, isOvwr);
  299.             break;
  300.           }
  301.           case PS2_KEY_ENTER: {
  302.             cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  303.             if (lineNum >= (2*NUM_OF_LINES - 1)) {
  304.               colNum = NUM_OF_COLS - 1;
  305.             }
  306.             else {
  307.               lineNum++;
  308.               colNum = 0;
  309.             }
  310.             if (isOvwr) {
  311.               deleteChar(cursorPos, msg);
  312.             }
  313.             insertChar (cursorPos, msg, ' ');
  314.             for (i=(cursorPos+1);i<((lineNum*NUM_OF_COLS) + colNum);i++) {
  315.               insertChar (i, msg, ' ');
  316.             }
  317.             if (colNum) {
  318.               insertChar (i, msg, ' ');
  319.             }
  320.             cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  321.             refreshDisplay(cursorPos, msg, isOvwr);
  322.             break;
  323.           }
  324.           case PS2_KEY_SPACE: {
  325.             cursorPos = addCharOnDisp(lineNum, colNum, msg, ' ', isOvwr);
  326.             colNum = cursorPos % NUM_OF_COLS;
  327.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  328.             break;
  329.           }
  330.           case PS2_KEY_0: {
  331.             if (isShift) {
  332.               cursorPos = addCharOnDisp(lineNum, colNum, msg, ')', isOvwr);
  333.             }
  334.             else {
  335.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '0', isOvwr);
  336.             }
  337.             colNum = cursorPos % NUM_OF_COLS;
  338.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  339.             break;
  340.           }
  341.           case PS2_KEY_1: {
  342.             if (isShift) {
  343.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '!', isOvwr);
  344.             }
  345.             else {
  346.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '1', isOvwr);
  347.             }
  348.             colNum = cursorPos % NUM_OF_COLS;
  349.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  350.             break;
  351.           }
  352.           case PS2_KEY_2: {
  353.             if (isShift) {
  354.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '@', isOvwr);
  355.             }
  356.             else {
  357.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '2', isOvwr);
  358.             }
  359.             colNum = cursorPos % NUM_OF_COLS;
  360.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  361.             break;
  362.           }
  363.           case PS2_KEY_3: {
  364.             if (isShift) {
  365.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '#', isOvwr);
  366.             }
  367.             else {
  368.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '3', isOvwr);
  369.             }
  370.             colNum = cursorPos % NUM_OF_COLS;
  371.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  372.             break;
  373.           }
  374.           case PS2_KEY_4: {
  375.             if (isShift) {
  376.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '$', isOvwr);
  377.             }
  378.             else {
  379.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '4', isOvwr);
  380.             }
  381.             colNum = cursorPos % NUM_OF_COLS;
  382.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  383.             break;
  384.           }
  385.           case PS2_KEY_5: {
  386.             if (isShift) {
  387.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '%', isOvwr);
  388.             }
  389.             else {
  390.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '5', isOvwr);
  391.             }
  392.             colNum = cursorPos % NUM_OF_COLS;
  393.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  394.             break;
  395.           }
  396.           case PS2_KEY_6: {
  397.             if (isShift) {
  398.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '^', isOvwr);
  399.             }
  400.             else {
  401.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '6', isOvwr);
  402.             }
  403.             colNum = cursorPos % NUM_OF_COLS;
  404.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  405.             break;
  406.           }
  407.           case PS2_KEY_7: {
  408.             if (isShift) {
  409.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '&', isOvwr);
  410.             }
  411.             else {
  412.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '7', isOvwr);
  413.             }
  414.             colNum = cursorPos % NUM_OF_COLS;
  415.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  416.             break;
  417.           }
  418.           case PS2_KEY_8: {
  419.             if (isShift) {
  420.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '*', isOvwr);
  421.             }
  422.             else {
  423.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '8', isOvwr);
  424.             }
  425.             colNum = cursorPos % NUM_OF_COLS;
  426.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  427.             break;
  428.           }
  429.           case PS2_KEY_9: {
  430.             if (isShift) {
  431.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '(', isOvwr);
  432.             }
  433.             else {
  434.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '9', isOvwr);
  435.             }
  436.             colNum = cursorPos % NUM_OF_COLS;
  437.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  438.             break;
  439.           }
  440.           case PS2_KEY_APOS: {
  441.             if (isShift) {
  442.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '\"', isOvwr);
  443.             }
  444.             else {
  445.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '\'', isOvwr);
  446.             }
  447.             colNum = cursorPos % NUM_OF_COLS;
  448.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  449.             break;
  450.           }
  451.           case PS2_KEY_COMMA: {
  452.             if (isShift) {
  453.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '<', isOvwr);
  454.             }
  455.             else {
  456.               cursorPos = addCharOnDisp(lineNum, colNum, msg, ',', isOvwr);
  457.             }
  458.             colNum = cursorPos % NUM_OF_COLS;
  459.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  460.             break;
  461.           }
  462.           case PS2_KEY_MINUS: {
  463.             if (isShift) {
  464.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '_', isOvwr);
  465.             }
  466.             else {
  467.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '-', isOvwr);
  468.             }
  469.             colNum = cursorPos % NUM_OF_COLS;
  470.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  471.             break;
  472.           }
  473.           case PS2_KEY_DOT: {
  474.             if (isShift) {
  475.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '>', isOvwr);
  476.             }
  477.             else {
  478.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '.', isOvwr);
  479.             }
  480.             colNum = cursorPos % NUM_OF_COLS;
  481.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  482.             break;
  483.           }
  484.           case PS2_KEY_DIV: {
  485.             if (isShift) {
  486.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '\?', isOvwr);
  487.             }
  488.             else {
  489.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '/', isOvwr);
  490.             }
  491.             colNum = cursorPos % NUM_OF_COLS;
  492.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  493.             break;
  494.           }
  495.           case PS2_KEY_SINGLE: {
  496.             if (isShift) {
  497.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '~', isOvwr);
  498.             }
  499.             else {
  500.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '`', isOvwr);
  501.             }
  502.             colNum = cursorPos % NUM_OF_COLS;
  503.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  504.             break;
  505.           }
  506.           case PS2_KEY_A: {
  507.             if ((isCaps) || (isShift)) {
  508.               if ((isCaps) && (isShift)) {
  509.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'a', isOvwr);
  510.               }
  511.               else {
  512.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'A', isOvwr);
  513.               }
  514.             }
  515.             else {
  516.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'a', isOvwr);
  517.             }
  518.             colNum = cursorPos % NUM_OF_COLS;
  519.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  520.             break;
  521.           }
  522.           case PS2_KEY_B: {
  523.             if ((isCaps) || (isShift)) {
  524.               if ((isCaps) && (isShift)) {
  525.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'b', isOvwr);
  526.               }
  527.               else {
  528.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'B', isOvwr);
  529.               }
  530.             }
  531.             else {
  532.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'b', isOvwr);
  533.             }
  534.             colNum = cursorPos % NUM_OF_COLS;
  535.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  536.             break;
  537.           }
  538.           case PS2_KEY_C: {
  539.             if ((isCaps) || (isShift)) {
  540.               if ((isCaps) && (isShift)) {
  541.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'c', isOvwr);
  542.               }
  543.               else {
  544.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'C', isOvwr);
  545.               }
  546.             }
  547.             else {
  548.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'c', isOvwr);
  549.             }
  550.             colNum = cursorPos % NUM_OF_COLS;
  551.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  552.             break;
  553.           }
  554.           case PS2_KEY_D: {
  555.             if ((isCaps) || (isShift)) {
  556.               if ((isCaps) && (isShift)) {
  557.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'b', isOvwr);
  558.               }
  559.               else {
  560.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'D', isOvwr);
  561.               }
  562.             }
  563.             else {
  564.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'd', isOvwr);
  565.             }
  566.             colNum = cursorPos % NUM_OF_COLS;
  567.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  568.             break;
  569.           }
  570.           case PS2_KEY_E: {
  571.             if ((isCaps) || (isShift)) {
  572.               if ((isCaps) && (isShift)) {
  573.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'e', isOvwr);
  574.               }
  575.               else {
  576.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'E', isOvwr);
  577.               }
  578.             }
  579.             else {
  580.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'e', isOvwr);
  581.             }
  582.             colNum = cursorPos % NUM_OF_COLS;
  583.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  584.             break;
  585.           }
  586.           case PS2_KEY_F: {
  587.             if ((isCaps) || (isShift)) {
  588.               if ((isCaps) && (isShift)) {
  589.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'f', isOvwr);
  590.               }
  591.               else {
  592.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'F', isOvwr);
  593.               }
  594.             }
  595.             else {
  596.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'f', isOvwr);
  597.             }
  598.             colNum = cursorPos % NUM_OF_COLS;
  599.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  600.             break;
  601.           }
  602.           case PS2_KEY_G: {
  603.             if ((isCaps) || (isShift)) {
  604.               if ((isCaps) && (isShift)) {
  605.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'g', isOvwr);
  606.               }
  607.               else {
  608.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'G', isOvwr);
  609.               }
  610.             }
  611.             else {
  612.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'g', isOvwr);
  613.             }
  614.             colNum = cursorPos % NUM_OF_COLS;
  615.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  616.             break;
  617.           }
  618.           case PS2_KEY_H: {
  619.             if ((isCaps) || (isShift)) {
  620.               if ((isCaps) && (isShift)) {
  621.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'h', isOvwr);
  622.               }
  623.               else {
  624.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'H', isOvwr);
  625.               }
  626.             }
  627.             else {
  628.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'h', isOvwr);
  629.             }
  630.             colNum = cursorPos % NUM_OF_COLS;
  631.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  632.             break;
  633.           }
  634.           case PS2_KEY_I: {
  635.             if ((isCaps) || (isShift)) {
  636.               if ((isCaps) && (isShift)) {
  637.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'i', isOvwr);
  638.               }
  639.               else {
  640.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'I', isOvwr);
  641.               }
  642.             }
  643.             else {
  644.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'i', isOvwr);
  645.             }
  646.             colNum = cursorPos % NUM_OF_COLS;
  647.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  648.             break;
  649.           }
  650.           case PS2_KEY_J: {
  651.             if ((isCaps) || (isShift)) {
  652.               if ((isCaps) && (isShift)) {
  653.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'j', isOvwr);
  654.               }
  655.               else {
  656.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'J', isOvwr);
  657.               }
  658.             }
  659.             else {
  660.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'j', isOvwr);
  661.             }
  662.             colNum = cursorPos % NUM_OF_COLS;
  663.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  664.             break;
  665.           }
  666.           case PS2_KEY_K: {
  667.             if ((isCaps) || (isShift)) {
  668.               if ((isCaps) && (isShift)) {
  669.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'k', isOvwr);
  670.               }
  671.               else {
  672.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'K', isOvwr);
  673.               }
  674.             }
  675.             else {
  676.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'k', isOvwr);
  677.             }
  678.             colNum = cursorPos % NUM_OF_COLS;
  679.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  680.             break;
  681.           }
  682.           case PS2_KEY_L: {
  683.             if ((isCaps) || (isShift)) {
  684.               if ((isCaps) && (isShift)) {
  685.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'l', isOvwr);
  686.               }
  687.               else {
  688.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'L', isOvwr);
  689.               }
  690.             }
  691.             else {
  692.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'l', isOvwr);
  693.             }
  694.             colNum = cursorPos % NUM_OF_COLS;
  695.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  696.             break;
  697.           }
  698.           case PS2_KEY_M: {
  699.             if ((isCaps) || (isShift)) {
  700.               if ((isCaps) && (isShift)) {
  701.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'm', isOvwr);
  702.               }
  703.               else {
  704.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'M', isOvwr);
  705.               }
  706.             }
  707.             else {
  708.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'm', isOvwr);
  709.             }
  710.             colNum = cursorPos % NUM_OF_COLS;
  711.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  712.             break;
  713.           }
  714.           case PS2_KEY_N: {
  715.             if ((isCaps) || (isShift)) {
  716.               if ((isCaps) && (isShift)) {
  717.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'n', isOvwr);
  718.               }
  719.               else {
  720.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'N', isOvwr);
  721.               }
  722.             }
  723.             else {
  724.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'n', isOvwr);
  725.             }
  726.             colNum = cursorPos % NUM_OF_COLS;
  727.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  728.             break;
  729.           }
  730.           case PS2_KEY_O: {
  731.             if ((isCaps) || (isShift)) {
  732.               if ((isCaps) && (isShift)) {
  733.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'o', isOvwr);
  734.               }
  735.               else {
  736.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'O', isOvwr);
  737.               }
  738.             }
  739.             else {
  740.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'o', isOvwr);
  741.             }
  742.             colNum = cursorPos % NUM_OF_COLS;
  743.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  744.             break;
  745.           }
  746.           case PS2_KEY_P: {
  747.             if ((isCaps) || (isShift)) {
  748.               if ((isCaps) && (isShift)) {
  749.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'p', isOvwr);
  750.               }
  751.               else {
  752.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'P', isOvwr);
  753.               }
  754.             }
  755.             else {
  756.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'p', isOvwr);
  757.             }
  758.             colNum = cursorPos % NUM_OF_COLS;
  759.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  760.             break;
  761.           }
  762.           case PS2_KEY_Q: {
  763.             if ((isCaps) || (isShift)) {
  764.               if ((isCaps) && (isShift)) {
  765.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'q', isOvwr);
  766.               }
  767.               else {
  768.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'Q', isOvwr);
  769.               }
  770.             }
  771.             else {
  772.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'q', isOvwr);
  773.             }
  774.             colNum = cursorPos % NUM_OF_COLS;
  775.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  776.             break;
  777.           }
  778.           case PS2_KEY_R: {
  779.             if ((isCaps) || (isShift)) {
  780.               if ((isCaps) && (isShift)) {
  781.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'r', isOvwr);
  782.               }
  783.               else {
  784.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'R', isOvwr);
  785.               }
  786.             }
  787.             else {
  788.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'r', isOvwr);
  789.             }
  790.             colNum = cursorPos % NUM_OF_COLS;
  791.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  792.             break;
  793.           }
  794.           case PS2_KEY_S: {
  795.             if ((isCaps) || (isShift)) {
  796.               if ((isCaps) && (isShift)) {
  797.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 's', isOvwr);
  798.               }
  799.               else {
  800.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'S', isOvwr);
  801.               }
  802.             }
  803.             else {
  804.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 's', isOvwr);
  805.             }
  806.             colNum = cursorPos % NUM_OF_COLS;
  807.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  808.             break;
  809.           }
  810.           case PS2_KEY_T: {
  811.             if ((isCaps) || (isShift)) {
  812.               if ((isCaps) && (isShift)) {
  813.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 't', isOvwr);
  814.               }
  815.               else {
  816.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'T', isOvwr);
  817.               }
  818.             }
  819.             else {
  820.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 't', isOvwr);
  821.             }
  822.             colNum = cursorPos % NUM_OF_COLS;
  823.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  824.             break;
  825.           }
  826.           case PS2_KEY_U: {
  827.             if ((isCaps) || (isShift)) {
  828.               if ((isCaps) && (isShift)) {
  829.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'u', isOvwr);
  830.               }
  831.               else {
  832.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'U', isOvwr);
  833.               }
  834.             }
  835.             else {
  836.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'u', isOvwr);
  837.             }
  838.             colNum = cursorPos % NUM_OF_COLS;
  839.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  840.             break;
  841.           }
  842.           case PS2_KEY_V: {
  843.             if ((isCaps) || (isShift)) {
  844.               if ((isCaps) && (isShift)) {
  845.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'v', isOvwr);
  846.               }
  847.               else {
  848.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'V', isOvwr);
  849.               }
  850.             }
  851.             else {
  852.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'v', isOvwr);
  853.             }
  854.             colNum = cursorPos % NUM_OF_COLS;
  855.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  856.             break;
  857.           }
  858.           case PS2_KEY_W: {
  859.             if ((isCaps) || (isShift)) {
  860.               if ((isCaps) && (isShift)) {
  861.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'w', isOvwr);
  862.               }
  863.               else {
  864.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'W', isOvwr);
  865.               }
  866.             }
  867.             else {
  868.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'w', isOvwr);
  869.             }
  870.             colNum = cursorPos % NUM_OF_COLS;
  871.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  872.             break;
  873.           }
  874.           case PS2_KEY_X: {
  875.             if ((isCaps) || (isShift)) {
  876.               if ((isCaps) && (isShift)) {
  877.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'x', isOvwr);
  878.               }
  879.               else {
  880.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'X', isOvwr);
  881.               }
  882.             }
  883.             else {
  884.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'x', isOvwr);
  885.             }
  886.             colNum = cursorPos % NUM_OF_COLS;
  887.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  888.             break;
  889.           }
  890.           case PS2_KEY_Y: {
  891.             if ((isCaps) || (isShift)) {
  892.               if ((isCaps) && (isShift)) {
  893.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'y', isOvwr);
  894.               }
  895.               else {
  896.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'Y', isOvwr);
  897.               }
  898.             }
  899.             else {
  900.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'y', isOvwr);
  901.             }
  902.             colNum = cursorPos % NUM_OF_COLS;
  903.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  904.             break;
  905.           }
  906.           case PS2_KEY_Z: {
  907.             if ((isCaps) || (isShift)) {
  908.               if ((isCaps) && (isShift)) {
  909.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'z', isOvwr);
  910.               }
  911.               else {
  912.                 cursorPos = addCharOnDisp(lineNum, colNum, msg, 'Z', isOvwr);
  913.               }
  914.             }
  915.             else {
  916.               cursorPos = addCharOnDisp(lineNum, colNum, msg, 'z', isOvwr);
  917.             }
  918.             colNum = cursorPos % NUM_OF_COLS;
  919.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  920.             break;
  921.           }
  922.           case PS2_KEY_SEMI: {
  923.             if (isShift) {
  924.               cursorPos = addCharOnDisp(lineNum, colNum, msg, ':', isOvwr);
  925.             }
  926.             else {
  927.               cursorPos = addCharOnDisp(lineNum, colNum, msg, ';', isOvwr);
  928.             }
  929.             colNum = cursorPos % NUM_OF_COLS;
  930.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  931.             break;
  932.           }
  933.           case PS2_KEY_BACK: {
  934.             if (isShift) {
  935.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '|', isOvwr);
  936.             }
  937.             else {
  938.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '\\', isOvwr);
  939.             }
  940.             colNum = cursorPos % NUM_OF_COLS;
  941.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  942.             break;
  943.           }
  944.           case PS2_KEY_OPEN_SQ: {
  945.             if (isShift) {
  946.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '{', isOvwr);
  947.             }
  948.             else {
  949.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '[', isOvwr);
  950.             }
  951.             colNum = cursorPos % NUM_OF_COLS;
  952.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  953.             break;
  954.           }
  955.           case PS2_KEY_CLOSE_SQ: {
  956.             if (isShift) {
  957.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '}', isOvwr);
  958.             }
  959.             else {
  960.               cursorPos = addCharOnDisp(lineNum, colNum, msg, ']', isOvwr);
  961.             }
  962.             colNum = cursorPos % NUM_OF_COLS;
  963.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  964.             break;
  965.           }
  966.           case PS2_KEY_EQUAL: {
  967.             if (isShift) {
  968.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '+', isOvwr);
  969.             }
  970.             else {
  971.               cursorPos = addCharOnDisp(lineNum, colNum, msg, '=', isOvwr);
  972.             }
  973.             colNum = cursorPos % NUM_OF_COLS;
  974.             lineNum = (int)(cursorPos / NUM_OF_COLS);
  975.             break;
  976.           }
  977.           default: {
  978.             break;
  979.           } // do nothing
  980.         }
  981.       }
  982.     }
  983.     delay(20);
  984.   }
  985. }
  986.  
  987. void jumpSetCursor(int colNum, int lineNum, bool isOvwr, bool jump) {
  988.   if (lineNum > 1) {
  989.     halfSel = UPPER;
  990.     lcd.noCursor();
  991.     lcd.noBlink();
  992.     halfSel = LOWER;
  993.     lcd.cursor();
  994.     if (isOvwr) {
  995.       lcd.blink();
  996.     }
  997.     else {
  998.       lcd.noBlink();
  999.     }
  1000.     if (jump) {
  1001.       lcd.setCursor(colNum, (lineNum-2));
  1002.     }
  1003.   }
  1004.   else {
  1005.     halfSel = LOWER;
  1006.     lcd.noCursor();
  1007.     lcd.noBlink();
  1008.     halfSel = UPPER;
  1009.     lcd.cursor();
  1010.     if (isOvwr) {
  1011.       lcd.blink();
  1012.     }
  1013.     else {
  1014.       lcd.noBlink();
  1015.     }
  1016.     if (jump) {
  1017.       lcd.setCursor(colNum, lineNum);
  1018.     }
  1019.   }
  1020. }
  1021.  
  1022. void deleteChar (int cursorPos, char str[]) {
  1023.   int len;
  1024.   int i,j;
  1025.  
  1026.   len = (int)strlen(str);
  1027.   for (i=cursorPos;i<(len-1);i++) {
  1028.     str[i] = str[i+1];
  1029.   }
  1030.   str[(len-1)] = ' ';
  1031. }
  1032.  
  1033. void insertChar (int cursorPos, char str[], char ch) {
  1034.   int len;
  1035.   int i;
  1036.  
  1037.   len = (int)strlen(str);
  1038.   for (i=(len-1);i>cursorPos;i--) {
  1039.     str[i] = str[i-1];
  1040.   }
  1041.   str[cursorPos] = ch;
  1042. }
  1043.  
  1044. void refreshDisplay(int cursorPos, char str[], bool isOvwr) {
  1045.   int i,j;
  1046.   int col, line;
  1047.   int len;
  1048.  
  1049.   len = (int)strlen(str);
  1050.   for (j=0;j<(2*NUM_OF_LINES);j++) {
  1051.     if (j > 1) {
  1052.       halfSel = LOWER;
  1053.       lcd.setCursor(0,(j-2));
  1054.       for (i=(j*NUM_OF_COLS);i<((j+1)*NUM_OF_COLS);i++) {
  1055.         lcd.print(str[i]);
  1056.       }
  1057.     }
  1058.     else {
  1059.       halfSel = UPPER;
  1060.       lcd.setCursor(0,j);
  1061.       for (i=(j*NUM_OF_COLS);i<((j+1)*NUM_OF_COLS);i++) {
  1062.         lcd.print(str[i]);
  1063.       }
  1064.     }
  1065.   }
  1066.   col = cursorPos % NUM_OF_COLS;
  1067.   line = (int)(cursorPos / NUM_OF_COLS);
  1068.   jumpSetCursor(col, line, isOvwr, true);
  1069. }
  1070.  
  1071. int addCharOnDisp (int lineNum, int colNum, char msg[], char ch, bool isOvwr) {
  1072.   int cursorPos;
  1073.  
  1074.   cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  1075.   if (isOvwr) {
  1076.     deleteChar(cursorPos, msg);
  1077.   }
  1078.   insertChar (cursorPos, msg, ch);
  1079.   if (colNum == (NUM_OF_COLS-1)) {
  1080.     if (lineNum < (2*NUM_OF_LINES-1)) {
  1081.       colNum = 0;
  1082.       lineNum = lineNum + 1;
  1083.     }
  1084.   }
  1085.   else {
  1086.     colNum = colNum + 1;
  1087.   }
  1088.   cursorPos = (lineNum*NUM_OF_COLS) + colNum;
  1089.   refreshDisplay(cursorPos, msg, isOvwr);
  1090.   return cursorPos;
  1091. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement