aboutsummaryrefslogtreecommitdiff
path: root/libraries/Robot_Control/utility/VirtualKeyboard.cpp
blob: ad73c7519b7dbcc90576c59d7da0309234fcbc07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "VirtualKeyboard.h"

int VirtualKeyboard::getColLin(int val){
  uint8_t col,lin;
  lin=val/10;
  col=val%10; // saving 36 bytes :(
  /*if(0<=val && 9>=val){
    col=val;
    lin=0;
  }else if(10<=val && 19>=val){
    col=val-10;
    lin=1;
  }else if(20<=val && 29>=val){
    col=val-20;
    lin=2;
  }else if(30<=val && 39>=val){
    col=val-30;
    lin=3;
  }*/
  return (col<<8)+lin;	//Put col and lin in one int
}
void VirtualKeyboard::run(){
/** visually select a letter on the keyboard
*	The selection boarder is 1px higher than the character, 
*	1px on the bottom, 2px to the left and 2px to the right.
*
*/
  if(!onOff)return;
  //Serial.println(onOff);
  static int oldColLin=0;
  uint8_t val=map(Robot.knobRead(),0,1023,0,38);
  if(val==38)val=37;	//The last value is jumpy when using batteries
  int colLin=getColLin(val);
  
  if(oldColLin!=colLin){
    uint8_t x=(oldColLin>>8 & 0xFF)*11+10;//col*11+1+9
    uint8_t y=(oldColLin & 0xFF)*11+1+top;//lin*11+1+top
    uint8_t w=9;
    if(oldColLin==1795)  //last item "Enter", col=7 lin=3
      w=33; //(5+1)*6-1+2+2  charWidth=5, charMargin=1, count("Enter")=6, lastItem_MarginRight=0, marginLeft==marginRight=2
    Robot.drawRect(x,y,w,9,hideColor);


    x=(colLin>>8 & 0xFF)*11+10;
    y=(colLin & 0xFF)*11+1+top;
    w=9;
    if(colLin==1795)  //last item "Enter", col=7 lin=3
      w=33; //(5+1)*6-1+2+2  charWidth=5, charMargin=1, count("Enter")=6, lastItem_MarginRight=0, marginLeft==marginRight=2
    Robot.drawRect(x,y,w,9,showColor);
    oldColLin=colLin;
  }
}

char VirtualKeyboard::getSelection(){
  if(!onOff)return -1;

  uint8_t val=map(Robot.knobRead(),0,1023,0,38);
  if(0<=val && 9>=val)
    val='0'+val;	
  else if(10<=val && 35>=val)
    val='A'+val-10;	
  else if(val==36)
    val=' ';
  else if(val>=37)
    val='\0';
  
  return val;
}
void VirtualKeyboard::hide(){
	onOff=false;
	Robot.fillRect(0,top,128,44,hideColor);//11*4
}

void VirtualKeyboard::display(uint8_t top, uint16_t showColor, uint16_t hideColor){
/** Display the keyboard at y position of top
*	formular:
*	When text size is 1, one character is 5*7
*	margin-left==margin-right==3,
*	margin-top==margin-bottom==2,
*	keyWidth=5+3+3==11,
*	keyHeight=7+2+2==11,
*	keyboard-margin-left=keyboard-margin-right==9
*	so character-x=11*col+9+3=11*col+12
*	character-y=11*lin+2+top
*	
**/
  this->top=top;
  this->onOff=true;
  
  this->showColor=showColor;
  this->hideColor=hideColor;
  
  for(uint8_t i=0;i<36;i++){
	Robot.setCursor(i%10*11+12,2+top+i/10*11);
	if(i<10)
		Robot.print(char('0'+i));
	else
		Robot.print(char(55+i));//'A'-10=55
  }//for saving 58 bytes :(
  
  /*for(int i=0;i<10;i++){
    Robot.setCursor(i*11+12,2+top);//11*0+2+top
    Robot.print(char('0'+i));//line_1: 0-9
  }
  for(int i=0;i<10;i++){
    Robot.setCursor(i*11+12,13+top);//11*1+2+top
    Robot.print(char('A'+i));//line_2: A-J
  }
  for(int i=0;i<10;i++){
    Robot.setCursor(i*11+12,24+top);//11*2+2+top
    Robot.print(char('K'+i));//line_3: K-T
  }
  for(int i=0;i<6;i++){
    Robot.setCursor(i*11+12,35+top);//11*3+2+top
    Robot.print(char('U'+i));//line_4: U-Z
  }*/
  //space and enter at the end of the last line.
  Robot.setCursor(78,35+top);//6*11+12=78
  Robot.print('_');//_
  
  Robot.setCursor(89,35+top);//7*11+12=89
  Robot.print("Enter");//enter
}



VirtualKeyboard Vkey=VirtualKeyboard();