[136] | 1 | #include <avr/pgmspace.h>
|
---|
| 2 | #include <ArduinoRobot.h>
|
---|
| 3 | #include "VirtualKeyboard.h"
|
---|
| 4 | #include "RobotTextManager.h"
|
---|
| 5 | #include "scripts_Hello_User.h"
|
---|
| 6 |
|
---|
| 7 | const int TextManager::lineHeight=10;
|
---|
| 8 | const int TextManager::charWidth=6;
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | void TextManager::setMargin(int margin_left,int margin_top){
|
---|
| 12 | this->margin_left=margin_left;
|
---|
| 13 | this->margin_top=margin_top;
|
---|
| 14 | }
|
---|
| 15 | int TextManager::getLin(int lineNum){
|
---|
| 16 | return lineNum*lineHeight+margin_top;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | int TextManager::getCol(int colNum){
|
---|
| 20 | return colNum*charWidth+margin_left;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | void TextManager::writeText(int lineNum, int colNum, char* txt, bool onOff){
|
---|
| 24 | if(!onOff)
|
---|
| 25 | Robot.setTextColor(WHITE);
|
---|
| 26 |
|
---|
| 27 | Robot.setCursor(getCol(colNum),getLin(lineNum));
|
---|
| 28 | Robot.print(txt);
|
---|
| 29 |
|
---|
| 30 | Robot.setTextColor(BLACK);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | void TextManager::drawInput(bool onOff){
|
---|
| 34 | if(!onOff)
|
---|
| 35 | Robot.setTextColor(WHITE);
|
---|
| 36 |
|
---|
| 37 | Robot.setCursor(getCol(inputCol),getLin(inputLin)+1);
|
---|
| 38 | Robot.print('_');
|
---|
| 39 |
|
---|
| 40 | Robot.setTextColor(BLACK);
|
---|
| 41 |
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | void TextManager::mvInput(int dire){
|
---|
| 45 | drawInput(0);
|
---|
| 46 | if(dire<0){
|
---|
| 47 | if(inputPos>0){
|
---|
| 48 | inputPos--;
|
---|
| 49 | inputCol--;
|
---|
| 50 | }
|
---|
| 51 | }else{
|
---|
| 52 | if(inputPos<16){
|
---|
| 53 | inputPos++;
|
---|
| 54 | inputCol++;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | drawInput(1);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | char TextManager::selectLetter(){
|
---|
| 61 | static int oldVal;
|
---|
| 62 | char val=map(Robot.knobRead(),0,1023,32,125);
|
---|
| 63 | if(val==oldVal){
|
---|
| 64 | return 0; //No changes
|
---|
| 65 | }else{
|
---|
| 66 | oldVal=val;
|
---|
| 67 | return val; //Current letter
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void TextManager::refreshCurrentLetter(char letter){
|
---|
| 72 | if(letter){
|
---|
| 73 | writeText(inputLin,inputCol,inputPool+inputPos,false);//erase
|
---|
| 74 | inputPool[inputPos]=letter;
|
---|
| 75 | writeText(inputLin,inputCol,inputPool+inputPos,true);//write
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | void TextManager::getInput(int lin, int col){
|
---|
| 81 | writeText(lin,col,">"); //Input indicator
|
---|
| 82 |
|
---|
| 83 | writeText(lin, col+1, inputPool);
|
---|
| 84 |
|
---|
| 85 | inputLin=lin; //Ini input cursor
|
---|
| 86 | inputCol=col+1;
|
---|
| 87 | inputPos=0;
|
---|
| 88 | drawInput(true);
|
---|
| 89 |
|
---|
| 90 | Vkey.display(100);//Vkey is a object of VirtualKeyboard class
|
---|
| 91 |
|
---|
| 92 | while(true){
|
---|
| 93 | switch(Robot.keyboardRead()){
|
---|
| 94 | case BUTTON_LEFT:
|
---|
| 95 | //Robot.beep(BEEP_SIMPLE);
|
---|
| 96 | mvInput(-1);
|
---|
| 97 | break;
|
---|
| 98 | case BUTTON_RIGHT:
|
---|
| 99 | //Robot.beep(BEEP_SIMPLE);
|
---|
| 100 | mvInput(1);
|
---|
| 101 | break;
|
---|
| 102 | case BUTTON_MIDDLE:
|
---|
| 103 | //Robot.beep(BEEP_DOUBLE);
|
---|
| 104 | char selection=Vkey.getSelection();
|
---|
| 105 | if(selection!='\0'){
|
---|
| 106 | refreshCurrentLetter(selection);
|
---|
| 107 | mvInput(1);
|
---|
| 108 | }else{
|
---|
| 109 | drawInput(false);
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | Vkey.run();
|
---|
| 114 | delay(10);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | void TextManager::setInputPool(int code){
|
---|
| 118 | switch(code){
|
---|
| 119 | case USERNAME:
|
---|
| 120 | Robot.userNameRead(inputPool);
|
---|
| 121 | break;
|
---|
| 122 | case ROBOTNAME:
|
---|
| 123 | Robot.robotNameRead(inputPool);
|
---|
| 124 | break;
|
---|
| 125 | case CITYNAME:
|
---|
| 126 | Robot.cityNameRead(inputPool);
|
---|
| 127 | break;
|
---|
| 128 | case COUNTRYNAME:
|
---|
| 129 | Robot.countryNameRead(inputPool);
|
---|
| 130 | break;
|
---|
| 131 | }
|
---|
| 132 | for(int i=0;i<18;i++){
|
---|
| 133 | if(inputPool[i]=='\0'){
|
---|
| 134 | for(int j=i;j<18;j++){
|
---|
| 135 | inputPool[j]='\0';
|
---|
| 136 | }
|
---|
| 137 | break;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | void TextManager::pushInput(int code){
|
---|
| 142 | switch(code){
|
---|
| 143 | case USERNAME:
|
---|
| 144 | Robot.userNameWrite(inputPool);
|
---|
| 145 | break;
|
---|
| 146 | case ROBOTNAME:
|
---|
| 147 | Robot.robotNameWrite(inputPool);
|
---|
| 148 | break;
|
---|
| 149 | case CITYNAME:
|
---|
| 150 | Robot.cityNameWrite(inputPool);
|
---|
| 151 | break;
|
---|
| 152 | case COUNTRYNAME:
|
---|
| 153 | Robot.countryNameWrite(inputPool);
|
---|
| 154 | break;
|
---|
| 155 | }
|
---|
| 156 | for(int i=0;i<18;i++){
|
---|
| 157 | inputPool[i]='\0';
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | void TextManager::input(int lin,int col, int code){
|
---|
| 161 | setInputPool(code);
|
---|
| 162 | getInput(lin,col);
|
---|
| 163 | pushInput(code);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | void TextManager::showPicture(char * filename, int posX, int posY){
|
---|
| 167 | Robot.pause();
|
---|
| 168 | Robot._drawBMP(filename,posX,posY);
|
---|
| 169 | Robot.play();
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void TextManager::getPGMtext(int seq){
|
---|
| 173 | //It takes a string from program space, and fill it
|
---|
| 174 | //in the buffer
|
---|
| 175 | //if(in hello user example){
|
---|
| 176 | if(true){
|
---|
| 177 | strcpy_P(PGMbuffer,(char*)pgm_read_word(&(::scripts_Hello_User[seq])));
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | void TextManager::writeScript(int seq, int line, int col){
|
---|
| 182 | //print a string from program space to a specific line,
|
---|
| 183 | //column on the LCD
|
---|
| 184 |
|
---|
| 185 | //first fill the buffer with text from program space
|
---|
| 186 | getPGMtext(seq);
|
---|
| 187 | //then print it to the screen
|
---|
| 188 | textManager.writeText(line,col,PGMbuffer);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | TextManager textManager=TextManager();
|
---|