[136] | 1 | #include "VirtualKeyboard.h"
|
---|
| 2 |
|
---|
| 3 | int VirtualKeyboard::getColLin(int val){
|
---|
| 4 | uint8_t col,lin;
|
---|
| 5 | lin=val/10;
|
---|
| 6 | col=val%10; // saving 36 bytes :(
|
---|
| 7 | /*if(0<=val && 9>=val){
|
---|
| 8 | col=val;
|
---|
| 9 | lin=0;
|
---|
| 10 | }else if(10<=val && 19>=val){
|
---|
| 11 | col=val-10;
|
---|
| 12 | lin=1;
|
---|
| 13 | }else if(20<=val && 29>=val){
|
---|
| 14 | col=val-20;
|
---|
| 15 | lin=2;
|
---|
| 16 | }else if(30<=val && 39>=val){
|
---|
| 17 | col=val-30;
|
---|
| 18 | lin=3;
|
---|
| 19 | }*/
|
---|
| 20 | return (col<<8)+lin; //Put col and lin in one int
|
---|
| 21 | }
|
---|
| 22 | void VirtualKeyboard::run(){
|
---|
| 23 | /** visually select a letter on the keyboard
|
---|
| 24 | * The selection boarder is 1px higher than the character,
|
---|
| 25 | * 1px on the bottom, 2px to the left and 2px to the right.
|
---|
| 26 | *
|
---|
| 27 | */
|
---|
| 28 | if(!onOff)return;
|
---|
| 29 | //Serial.println(onOff);
|
---|
| 30 | static int oldColLin=0;
|
---|
| 31 | uint8_t val=map(Robot.knobRead(),0,1023,0,38);
|
---|
| 32 | if(val==38)val=37; //The last value is jumpy when using batteries
|
---|
| 33 | int colLin=getColLin(val);
|
---|
| 34 |
|
---|
| 35 | if(oldColLin!=colLin){
|
---|
| 36 | uint8_t x=(oldColLin>>8 & 0xFF)*11+10;//col*11+1+9
|
---|
| 37 | uint8_t y=(oldColLin & 0xFF)*11+1+top;//lin*11+1+top
|
---|
| 38 | uint8_t w=9;
|
---|
| 39 | if(oldColLin==1795) //last item "Enter", col=7 lin=3
|
---|
| 40 | w=33; //(5+1)*6-1+2+2 charWidth=5, charMargin=1, count("Enter")=6, lastItem_MarginRight=0, marginLeft==marginRight=2
|
---|
| 41 | Robot.drawRect(x,y,w,9,hideColor);
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | x=(colLin>>8 & 0xFF)*11+10;
|
---|
| 45 | y=(colLin & 0xFF)*11+1+top;
|
---|
| 46 | w=9;
|
---|
| 47 | if(colLin==1795) //last item "Enter", col=7 lin=3
|
---|
| 48 | w=33; //(5+1)*6-1+2+2 charWidth=5, charMargin=1, count("Enter")=6, lastItem_MarginRight=0, marginLeft==marginRight=2
|
---|
| 49 | Robot.drawRect(x,y,w,9,showColor);
|
---|
| 50 | oldColLin=colLin;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | char VirtualKeyboard::getSelection(){
|
---|
| 55 | if(!onOff)return -1;
|
---|
| 56 |
|
---|
| 57 | uint8_t val=map(Robot.knobRead(),0,1023,0,38);
|
---|
| 58 | if(0<=val && 9>=val)
|
---|
| 59 | val='0'+val;
|
---|
| 60 | else if(10<=val && 35>=val)
|
---|
| 61 | val='A'+val-10;
|
---|
| 62 | else if(val==36)
|
---|
| 63 | val=' ';
|
---|
| 64 | else if(val>=37)
|
---|
| 65 | val='\0';
|
---|
| 66 |
|
---|
| 67 | return val;
|
---|
| 68 | }
|
---|
| 69 | void VirtualKeyboard::hide(){
|
---|
| 70 | onOff=false;
|
---|
| 71 | Robot.fillRect(0,top,128,44,hideColor);//11*4
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void VirtualKeyboard::display(uint8_t top, uint16_t showColor, uint16_t hideColor){
|
---|
| 75 | /** Display the keyboard at y position of top
|
---|
| 76 | * formular:
|
---|
| 77 | * When text size is 1, one character is 5*7
|
---|
| 78 | * margin-left==margin-right==3,
|
---|
| 79 | * margin-top==margin-bottom==2,
|
---|
| 80 | * keyWidth=5+3+3==11,
|
---|
| 81 | * keyHeight=7+2+2==11,
|
---|
| 82 | * keyboard-margin-left=keyboard-margin-right==9
|
---|
| 83 | * so character-x=11*col+9+3=11*col+12
|
---|
| 84 | * character-y=11*lin+2+top
|
---|
| 85 | *
|
---|
| 86 | **/
|
---|
| 87 | this->top=top;
|
---|
| 88 | this->onOff=true;
|
---|
| 89 |
|
---|
| 90 | this->showColor=showColor;
|
---|
| 91 | this->hideColor=hideColor;
|
---|
| 92 |
|
---|
| 93 | for(uint8_t i=0;i<36;i++){
|
---|
| 94 | Robot.setCursor(i%10*11+12,2+top+i/10*11);
|
---|
| 95 | if(i<10)
|
---|
| 96 | Robot.print(char('0'+i));
|
---|
| 97 | else
|
---|
| 98 | Robot.print(char(55+i));//'A'-10=55
|
---|
| 99 | }//for saving 58 bytes :(
|
---|
| 100 |
|
---|
| 101 | /*for(int i=0;i<10;i++){
|
---|
| 102 | Robot.setCursor(i*11+12,2+top);//11*0+2+top
|
---|
| 103 | Robot.print(char('0'+i));//line_1: 0-9
|
---|
| 104 | }
|
---|
| 105 | for(int i=0;i<10;i++){
|
---|
| 106 | Robot.setCursor(i*11+12,13+top);//11*1+2+top
|
---|
| 107 | Robot.print(char('A'+i));//line_2: A-J
|
---|
| 108 | }
|
---|
| 109 | for(int i=0;i<10;i++){
|
---|
| 110 | Robot.setCursor(i*11+12,24+top);//11*2+2+top
|
---|
| 111 | Robot.print(char('K'+i));//line_3: K-T
|
---|
| 112 | }
|
---|
| 113 | for(int i=0;i<6;i++){
|
---|
| 114 | Robot.setCursor(i*11+12,35+top);//11*3+2+top
|
---|
| 115 | Robot.print(char('U'+i));//line_4: U-Z
|
---|
| 116 | }*/
|
---|
| 117 | //space and enter at the end of the last line.
|
---|
| 118 | Robot.setCursor(78,35+top);//6*11+12=78
|
---|
| 119 | Robot.print('_');//_
|
---|
| 120 |
|
---|
| 121 | Robot.setCursor(89,35+top);//7*11+12=89
|
---|
| 122 | Robot.print("Enter");//enter
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | VirtualKeyboard Vkey=VirtualKeyboard();
|
---|