LCD Menu Select with LCD SDcard Encoder Bluetooth GCODE
Dependencies: FRA221_9A SDFileSystem mbed
main.cpp@0:7e94da02b931, 2015-12-10 (annotated)
- Committer:
- sweilz
- Date:
- Thu Dec 10 08:34:15 2015 +0000
- Revision:
- 0:7e94da02b931
LCD Menu Select
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sweilz | 0:7e94da02b931 | 1 | #include "mbed.h" |
sweilz | 0:7e94da02b931 | 2 | #include "SDFileSystem.h" |
sweilz | 0:7e94da02b931 | 3 | #include "LCDTaonoi.h" |
sweilz | 0:7e94da02b931 | 4 | #include "QEI.h" |
sweilz | 0:7e94da02b931 | 5 | #include <string> |
sweilz | 0:7e94da02b931 | 6 | #include <vector> |
sweilz | 0:7e94da02b931 | 7 | |
sweilz | 0:7e94da02b931 | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
sweilz | 0:7e94da02b931 | 9 | Serial device(D8,D2); |
sweilz | 0:7e94da02b931 | 10 | Serial phone(PA_11,PA_12); |
sweilz | 0:7e94da02b931 | 11 | SDFileSystem sd(D11, D12, D13, D10,"sd"); |
sweilz | 0:7e94da02b931 | 12 | LCDTaonoi lcd(I2C_SDA, I2C_SCL); |
sweilz | 0:7e94da02b931 | 13 | QEI encoder(A0, A1); |
sweilz | 0:7e94da02b931 | 14 | |
sweilz | 0:7e94da02b931 | 15 | |
sweilz | 0:7e94da02b931 | 16 | DigitalIn btn(A2); |
sweilz | 0:7e94da02b931 | 17 | |
sweilz | 0:7e94da02b931 | 18 | |
sweilz | 0:7e94da02b931 | 19 | void initial(); |
sweilz | 0:7e94da02b931 | 20 | int selectMenu(string head,vector<string> strVec); |
sweilz | 0:7e94da02b931 | 21 | void getTemperature(); |
sweilz | 0:7e94da02b931 | 22 | void displayData(); |
sweilz | 0:7e94da02b931 | 23 | string stringData(char ch,string cmd); |
sweilz | 0:7e94da02b931 | 24 | void ProcessCmd(string cmd); |
sweilz | 0:7e94da02b931 | 25 | void ProcessFile(string filename); |
sweilz | 0:7e94da02b931 | 26 | |
sweilz | 0:7e94da02b931 | 27 | vector<string> strDisplay; |
sweilz | 0:7e94da02b931 | 28 | vector<string> strMode; |
sweilz | 0:7e94da02b931 | 29 | vector<string> strFilename; |
sweilz | 0:7e94da02b931 | 30 | vector<string> strBackground; |
sweilz | 0:7e94da02b931 | 31 | vector<string> strSize; |
sweilz | 0:7e94da02b931 | 32 | |
sweilz | 0:7e94da02b931 | 33 | int selectItem[5]; |
sweilz | 0:7e94da02b931 | 34 | bool can_back = false; |
sweilz | 0:7e94da02b931 | 35 | Timer timer; |
sweilz | 0:7e94da02b931 | 36 | Ticker TimetickTemp; |
sweilz | 0:7e94da02b931 | 37 | Ticker TimetickLCD; |
sweilz | 0:7e94da02b931 | 38 | DigitalInOut TempSensor(D4); |
sweilz | 0:7e94da02b931 | 39 | DigitalOut TempStatus(D3); |
sweilz | 0:7e94da02b931 | 40 | int temperature=0; |
sweilz | 0:7e94da02b931 | 41 | float process=99.9; |
sweilz | 0:7e94da02b931 | 42 | int run=0; |
sweilz | 0:7e94da02b931 | 43 | int FullSize=0,NowSize=0; |
sweilz | 0:7e94da02b931 | 44 | int fexit=0; |
sweilz | 0:7e94da02b931 | 45 | |
sweilz | 0:7e94da02b931 | 46 | int main() |
sweilz | 0:7e94da02b931 | 47 | { |
sweilz | 0:7e94da02b931 | 48 | pc.baud(9600); |
sweilz | 0:7e94da02b931 | 49 | device.baud(9600); |
sweilz | 0:7e94da02b931 | 50 | phone.baud(9600); |
sweilz | 0:7e94da02b931 | 51 | |
sweilz | 0:7e94da02b931 | 52 | pc.printf("\nHello World\n"); |
sweilz | 0:7e94da02b931 | 53 | |
sweilz | 0:7e94da02b931 | 54 | TimetickTemp.attach(&getTemperature, 0.5); |
sweilz | 0:7e94da02b931 | 55 | TimetickLCD.attach(&displayData, 1.0); |
sweilz | 0:7e94da02b931 | 56 | |
sweilz | 0:7e94da02b931 | 57 | initial(); |
sweilz | 0:7e94da02b931 | 58 | |
sweilz | 0:7e94da02b931 | 59 | //////////////////////////////// Main LCD Process ////////////////////// |
sweilz | 0:7e94da02b931 | 60 | mainmenu: |
sweilz | 0:7e94da02b931 | 61 | can_back = false; |
sweilz | 0:7e94da02b931 | 62 | selectItem[0] = selectMenu("*** SELECT MODE ***",strMode); |
sweilz | 0:7e94da02b931 | 63 | |
sweilz | 0:7e94da02b931 | 64 | if(selectItem[0]==0) { |
sweilz | 0:7e94da02b931 | 65 | filemenu: |
sweilz | 0:7e94da02b931 | 66 | selectItem[1] = selectMenu("** FILES TO PRINT **",strFilename); |
sweilz | 0:7e94da02b931 | 67 | |
sweilz | 0:7e94da02b931 | 68 | if(selectItem[1]==strDisplay.size()-1) { |
sweilz | 0:7e94da02b931 | 69 | goto mainmenu; |
sweilz | 0:7e94da02b931 | 70 | } else { |
sweilz | 0:7e94da02b931 | 71 | bgmenu: |
sweilz | 0:7e94da02b931 | 72 | selectItem[2] = selectMenu("**** BACKGROUND ****",strBackground); |
sweilz | 0:7e94da02b931 | 73 | if(selectItem[2]==strDisplay.size()-1) { |
sweilz | 0:7e94da02b931 | 74 | goto filemenu; |
sweilz | 0:7e94da02b931 | 75 | } else { |
sweilz | 0:7e94da02b931 | 76 | selectItem[3] = selectMenu("***** BG SIZES *****",strSize); |
sweilz | 0:7e94da02b931 | 77 | if(selectItem[3]==strDisplay.size()-1) { |
sweilz | 0:7e94da02b931 | 78 | goto bgmenu; |
sweilz | 0:7e94da02b931 | 79 | } else { |
sweilz | 0:7e94da02b931 | 80 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 81 | lcd.print("PROCESS :"); |
sweilz | 0:7e94da02b931 | 82 | lcd.setCursor(0,1); |
sweilz | 0:7e94da02b931 | 83 | lcd.print("TEMPERATURE :"); |
sweilz | 0:7e94da02b931 | 84 | lcd.setCursor(0,3); |
sweilz | 0:7e94da02b931 | 85 | lcd.print("FILE: "); |
sweilz | 0:7e94da02b931 | 86 | lcd.print(strFilename[selectItem[1]].c_str()); |
sweilz | 0:7e94da02b931 | 87 | } |
sweilz | 0:7e94da02b931 | 88 | } |
sweilz | 0:7e94da02b931 | 89 | } |
sweilz | 0:7e94da02b931 | 90 | } else if(selectItem[0]==1) { |
sweilz | 0:7e94da02b931 | 91 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 92 | lcd.print(" AUTO PANCAKE MAKER "); |
sweilz | 0:7e94da02b931 | 93 | lcd.setCursor(0,2); |
sweilz | 0:7e94da02b931 | 94 | lcd.print("TEMPERATURE :"); |
sweilz | 0:7e94da02b931 | 95 | while(btn.read()) { |
sweilz | 0:7e94da02b931 | 96 | lcd.setCursor(15,2); |
sweilz | 0:7e94da02b931 | 97 | //getTemperature(); |
sweilz | 0:7e94da02b931 | 98 | string temp; |
sweilz | 0:7e94da02b931 | 99 | sprintf(&temp[0],"%.1fC",float(temperature)/10); |
sweilz | 0:7e94da02b931 | 100 | lcd.print(temp.c_str()); |
sweilz | 0:7e94da02b931 | 101 | //wait(0.2); |
sweilz | 0:7e94da02b931 | 102 | } |
sweilz | 0:7e94da02b931 | 103 | while(!btn.read()); |
sweilz | 0:7e94da02b931 | 104 | goto mainmenu; |
sweilz | 0:7e94da02b931 | 105 | } else if(selectItem[0]==2) { |
sweilz | 0:7e94da02b931 | 106 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 107 | lcd.print(" AUTO PANCAKE MAKER "); |
sweilz | 0:7e94da02b931 | 108 | lcd.setCursor(0,2); |
sweilz | 0:7e94da02b931 | 109 | lcd.print(" GET GCODE FROM "); |
sweilz | 0:7e94da02b931 | 110 | lcd.setCursor(0,3); |
sweilz | 0:7e94da02b931 | 111 | lcd.print(" SMARTPHONE "); |
sweilz | 0:7e94da02b931 | 112 | while(btn.read()) { |
sweilz | 0:7e94da02b931 | 113 | if(phone.readable()) { |
sweilz | 0:7e94da02b931 | 114 | char ch = phone.getc(); |
sweilz | 0:7e94da02b931 | 115 | pc.putc(ch); |
sweilz | 0:7e94da02b931 | 116 | device.putc(ch); |
sweilz | 0:7e94da02b931 | 117 | } |
sweilz | 0:7e94da02b931 | 118 | } |
sweilz | 0:7e94da02b931 | 119 | while(!btn.read()); |
sweilz | 0:7e94da02b931 | 120 | goto mainmenu; |
sweilz | 0:7e94da02b931 | 121 | } |
sweilz | 0:7e94da02b931 | 122 | //////////////////////////////////////////////////////////////////// |
sweilz | 0:7e94da02b931 | 123 | |
sweilz | 0:7e94da02b931 | 124 | run=1; |
sweilz | 0:7e94da02b931 | 125 | pc.printf("Send File GCODE\n"); |
sweilz | 0:7e94da02b931 | 126 | ProcessFile(strFilename[selectItem[1]].c_str()); |
sweilz | 0:7e94da02b931 | 127 | |
sweilz | 0:7e94da02b931 | 128 | run=0; |
sweilz | 0:7e94da02b931 | 129 | if(!fexit) { |
sweilz | 0:7e94da02b931 | 130 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 131 | lcd.setCursor(0,0); |
sweilz | 0:7e94da02b931 | 132 | lcd.print(" WAITING TO FILL "); |
sweilz | 0:7e94da02b931 | 133 | lcd.setCursor(0,1); |
sweilz | 0:7e94da02b931 | 134 | lcd.print("===================="); |
sweilz | 0:7e94da02b931 | 135 | lcd.setCursor(0,3); |
sweilz | 0:7e94da02b931 | 136 | lcd.print(" AUTO PANCAKE MAKER "); |
sweilz | 0:7e94da02b931 | 137 | |
sweilz | 0:7e94da02b931 | 138 | device.printf("$%d S%d\r",selectItem[2]+1,selectItem[3]+1); |
sweilz | 0:7e94da02b931 | 139 | while(1) { |
sweilz | 0:7e94da02b931 | 140 | if(device.readable()) { |
sweilz | 0:7e94da02b931 | 141 | if(device.getc()=='$') { |
sweilz | 0:7e94da02b931 | 142 | break; |
sweilz | 0:7e94da02b931 | 143 | } |
sweilz | 0:7e94da02b931 | 144 | } |
sweilz | 0:7e94da02b931 | 145 | } |
sweilz | 0:7e94da02b931 | 146 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 147 | } else { |
sweilz | 0:7e94da02b931 | 148 | goto mainmenu; |
sweilz | 0:7e94da02b931 | 149 | } |
sweilz | 0:7e94da02b931 | 150 | fexit=0; |
sweilz | 0:7e94da02b931 | 151 | |
sweilz | 0:7e94da02b931 | 152 | |
sweilz | 0:7e94da02b931 | 153 | |
sweilz | 0:7e94da02b931 | 154 | |
sweilz | 0:7e94da02b931 | 155 | |
sweilz | 0:7e94da02b931 | 156 | |
sweilz | 0:7e94da02b931 | 157 | |
sweilz | 0:7e94da02b931 | 158 | |
sweilz | 0:7e94da02b931 | 159 | |
sweilz | 0:7e94da02b931 | 160 | |
sweilz | 0:7e94da02b931 | 161 | |
sweilz | 0:7e94da02b931 | 162 | |
sweilz | 0:7e94da02b931 | 163 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 164 | while(btn.read()); |
sweilz | 0:7e94da02b931 | 165 | while(!btn.read()); |
sweilz | 0:7e94da02b931 | 166 | pc.printf("\nGoodBye World\n"); |
sweilz | 0:7e94da02b931 | 167 | } |
sweilz | 0:7e94da02b931 | 168 | |
sweilz | 0:7e94da02b931 | 169 | |
sweilz | 0:7e94da02b931 | 170 | |
sweilz | 0:7e94da02b931 | 171 | |
sweilz | 0:7e94da02b931 | 172 | |
sweilz | 0:7e94da02b931 | 173 | void initial() |
sweilz | 0:7e94da02b931 | 174 | { |
sweilz | 0:7e94da02b931 | 175 | pc.printf("DEBUG initial"); |
sweilz | 0:7e94da02b931 | 176 | |
sweilz | 0:7e94da02b931 | 177 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 178 | lcd.print("***** WELCOME! *****"); |
sweilz | 0:7e94da02b931 | 179 | lcd.setCursor(0,1); |
sweilz | 0:7e94da02b931 | 180 | lcd.print(" AUTO PANCAKE MAKER "); |
sweilz | 0:7e94da02b931 | 181 | lcd.setCursor(0,3); |
sweilz | 0:7e94da02b931 | 182 | lcd.print(" PRESS TO START "); |
sweilz | 0:7e94da02b931 | 183 | |
sweilz | 0:7e94da02b931 | 184 | strMode.push_back("PRINT FILE"); |
sweilz | 0:7e94da02b931 | 185 | strMode.push_back("WATCH TEMPERATURE"); |
sweilz | 0:7e94da02b931 | 186 | strMode.push_back("SMART PHONE"); |
sweilz | 0:7e94da02b931 | 187 | string path = "/sd/FileToPrint"; |
sweilz | 0:7e94da02b931 | 188 | struct dirent *dirp; |
sweilz | 0:7e94da02b931 | 189 | DIR *dp = opendir(path.c_str()); |
sweilz | 0:7e94da02b931 | 190 | while((dirp = readdir(dp)) != NULL) { |
sweilz | 0:7e94da02b931 | 191 | strFilename.push_back(string(dirp->d_name)); |
sweilz | 0:7e94da02b931 | 192 | } |
sweilz | 0:7e94da02b931 | 193 | closedir(dp); |
sweilz | 0:7e94da02b931 | 194 | |
sweilz | 0:7e94da02b931 | 195 | strBackground.push_back("SQUARE"); |
sweilz | 0:7e94da02b931 | 196 | strBackground.push_back("CIRCLE"); |
sweilz | 0:7e94da02b931 | 197 | strBackground.push_back("ELLIPS"); |
sweilz | 0:7e94da02b931 | 198 | |
sweilz | 0:7e94da02b931 | 199 | strSize.push_back("FIT"); |
sweilz | 0:7e94da02b931 | 200 | strSize.push_back("NORMAL"); |
sweilz | 0:7e94da02b931 | 201 | strSize.push_back("LARGE"); |
sweilz | 0:7e94da02b931 | 202 | |
sweilz | 0:7e94da02b931 | 203 | pc.printf(" - end initial\n"); |
sweilz | 0:7e94da02b931 | 204 | |
sweilz | 0:7e94da02b931 | 205 | while(btn.read()); |
sweilz | 0:7e94da02b931 | 206 | while(!btn.read()); |
sweilz | 0:7e94da02b931 | 207 | } |
sweilz | 0:7e94da02b931 | 208 | |
sweilz | 0:7e94da02b931 | 209 | |
sweilz | 0:7e94da02b931 | 210 | |
sweilz | 0:7e94da02b931 | 211 | |
sweilz | 0:7e94da02b931 | 212 | void ProcessFile(string filename) |
sweilz | 0:7e94da02b931 | 213 | { |
sweilz | 0:7e94da02b931 | 214 | NowSize=0; |
sweilz | 0:7e94da02b931 | 215 | process=0.0; |
sweilz | 0:7e94da02b931 | 216 | bool comment = false; |
sweilz | 0:7e94da02b931 | 217 | string cmd=""; |
sweilz | 0:7e94da02b931 | 218 | string readPath = "/sd/FileToPrint/" + filename; |
sweilz | 0:7e94da02b931 | 219 | |
sweilz | 0:7e94da02b931 | 220 | FILE *fInput = fopen(readPath.c_str(), "r"); |
sweilz | 0:7e94da02b931 | 221 | |
sweilz | 0:7e94da02b931 | 222 | if (fInput != NULL) { |
sweilz | 0:7e94da02b931 | 223 | fseek( fInput, 0, SEEK_END ); |
sweilz | 0:7e94da02b931 | 224 | FullSize = ftell(fInput); |
sweilz | 0:7e94da02b931 | 225 | rewind(fInput); |
sweilz | 0:7e94da02b931 | 226 | |
sweilz | 0:7e94da02b931 | 227 | int8_t ch = fgetc(fInput); |
sweilz | 0:7e94da02b931 | 228 | while(ch != EOF) { |
sweilz | 0:7e94da02b931 | 229 | if(ch != '\n') { |
sweilz | 0:7e94da02b931 | 230 | if (ch == '(') comment = true; |
sweilz | 0:7e94da02b931 | 231 | if (!comment) cmd.push_back(ch); |
sweilz | 0:7e94da02b931 | 232 | if (ch == ')') comment = false; |
sweilz | 0:7e94da02b931 | 233 | } else { |
sweilz | 0:7e94da02b931 | 234 | wait_us(100); |
sweilz | 0:7e94da02b931 | 235 | if(cmd.size()>1) { |
sweilz | 0:7e94da02b931 | 236 | if(!cmd.find("G0 ")||!cmd.find("G1 ")||!cmd.find("G2 ")||!cmd.find("G3 ")) { |
sweilz | 0:7e94da02b931 | 237 | ProcessCmd(cmd); |
sweilz | 0:7e94da02b931 | 238 | } |
sweilz | 0:7e94da02b931 | 239 | } |
sweilz | 0:7e94da02b931 | 240 | cmd.clear(); |
sweilz | 0:7e94da02b931 | 241 | } |
sweilz | 0:7e94da02b931 | 242 | if(!btn.read()) { |
sweilz | 0:7e94da02b931 | 243 | ch = EOF; |
sweilz | 0:7e94da02b931 | 244 | fexit=1; |
sweilz | 0:7e94da02b931 | 245 | break; |
sweilz | 0:7e94da02b931 | 246 | } else { |
sweilz | 0:7e94da02b931 | 247 | ch = fgetc(fInput); |
sweilz | 0:7e94da02b931 | 248 | } |
sweilz | 0:7e94da02b931 | 249 | NowSize++; |
sweilz | 0:7e94da02b931 | 250 | process = float(NowSize)/float(FullSize)*100; |
sweilz | 0:7e94da02b931 | 251 | |
sweilz | 0:7e94da02b931 | 252 | } |
sweilz | 0:7e94da02b931 | 253 | process = 100.0; |
sweilz | 0:7e94da02b931 | 254 | } |
sweilz | 0:7e94da02b931 | 255 | fclose(fInput); |
sweilz | 0:7e94da02b931 | 256 | |
sweilz | 0:7e94da02b931 | 257 | } |
sweilz | 0:7e94da02b931 | 258 | |
sweilz | 0:7e94da02b931 | 259 | void ProcessCmd(string cmd) |
sweilz | 0:7e94da02b931 | 260 | { |
sweilz | 0:7e94da02b931 | 261 | string tempCmd="",sendCmd=""; |
sweilz | 0:7e94da02b931 | 262 | |
sweilz | 0:7e94da02b931 | 263 | if(!cmd.find("G0 ") || !cmd.find("G1 "))sendCmd = "G0 "; |
sweilz | 0:7e94da02b931 | 264 | else if(!cmd.find("G2 "))sendCmd = "G2 "; |
sweilz | 0:7e94da02b931 | 265 | else if(!cmd.find("G3 "))sendCmd = "G3 "; |
sweilz | 0:7e94da02b931 | 266 | |
sweilz | 0:7e94da02b931 | 267 | if(cmd.find('X',0)!= std::string::npos)sendCmd.append(stringData('X',cmd)); |
sweilz | 0:7e94da02b931 | 268 | if(cmd.find('Y',0)!= std::string::npos)sendCmd.append(stringData('Y',cmd)); |
sweilz | 0:7e94da02b931 | 269 | if(cmd.find('Z',0)!= std::string::npos)sendCmd.append(stringData('Z',cmd)); |
sweilz | 0:7e94da02b931 | 270 | if(cmd.find('I',0)!= std::string::npos)sendCmd.append(stringData('I',cmd)); |
sweilz | 0:7e94da02b931 | 271 | if(cmd.find('J',0)!= std::string::npos)sendCmd.append(stringData('J',cmd)); |
sweilz | 0:7e94da02b931 | 272 | |
sweilz | 0:7e94da02b931 | 273 | device.printf("%s\r",sendCmd.c_str()); |
sweilz | 0:7e94da02b931 | 274 | pc.printf("Device : %s\n\r",sendCmd.c_str()); |
sweilz | 0:7e94da02b931 | 275 | while(1) { |
sweilz | 0:7e94da02b931 | 276 | if(device.readable()) { |
sweilz | 0:7e94da02b931 | 277 | if(device.getc()=='#') { |
sweilz | 0:7e94da02b931 | 278 | break; |
sweilz | 0:7e94da02b931 | 279 | } |
sweilz | 0:7e94da02b931 | 280 | } |
sweilz | 0:7e94da02b931 | 281 | if(pc.readable()) { |
sweilz | 0:7e94da02b931 | 282 | if(pc.getc()=='#') { |
sweilz | 0:7e94da02b931 | 283 | break; |
sweilz | 0:7e94da02b931 | 284 | } |
sweilz | 0:7e94da02b931 | 285 | } |
sweilz | 0:7e94da02b931 | 286 | } |
sweilz | 0:7e94da02b931 | 287 | } |
sweilz | 0:7e94da02b931 | 288 | |
sweilz | 0:7e94da02b931 | 289 | string stringData(char ch,string cmd) |
sweilz | 0:7e94da02b931 | 290 | { |
sweilz | 0:7e94da02b931 | 291 | size_t pos = cmd.find(ch,0); |
sweilz | 0:7e94da02b931 | 292 | string tempCmd,temp = cmd.substr(pos+1,8); |
sweilz | 0:7e94da02b931 | 293 | float number = atof(temp.c_str()); |
sweilz | 0:7e94da02b931 | 294 | if(ch=='Z')number = number>0?1.0:0.0; |
sweilz | 0:7e94da02b931 | 295 | sprintf(&tempCmd[0],"%c%.3f ",ch,number); |
sweilz | 0:7e94da02b931 | 296 | return tempCmd.c_str(); |
sweilz | 0:7e94da02b931 | 297 | } |
sweilz | 0:7e94da02b931 | 298 | |
sweilz | 0:7e94da02b931 | 299 | int selectMenu(string head,vector<string> strVec) |
sweilz | 0:7e94da02b931 | 300 | { |
sweilz | 0:7e94da02b931 | 301 | pc.printf("DEBUG selectMemu\n"); |
sweilz | 0:7e94da02b931 | 302 | |
sweilz | 0:7e94da02b931 | 303 | lcd.clear(); |
sweilz | 0:7e94da02b931 | 304 | lcd.print(head.c_str()); |
sweilz | 0:7e94da02b931 | 305 | |
sweilz | 0:7e94da02b931 | 306 | strDisplay.clear(); |
sweilz | 0:7e94da02b931 | 307 | for(int i=0; i<strVec.size(); i++) |
sweilz | 0:7e94da02b931 | 308 | strDisplay.push_back(strVec[i].c_str()); |
sweilz | 0:7e94da02b931 | 309 | if(can_back)strDisplay.push_back("<< BACK"); |
sweilz | 0:7e94da02b931 | 310 | /*for(int i=0; i<strDisplay.size(); i++) |
sweilz | 0:7e94da02b931 | 311 | pc.printf("%s\n",strDisplay[i].c_str());*/ |
sweilz | 0:7e94da02b931 | 312 | |
sweilz | 0:7e94da02b931 | 313 | |
sweilz | 0:7e94da02b931 | 314 | int item_index = 0; |
sweilz | 0:7e94da02b931 | 315 | int list_index = 0; |
sweilz | 0:7e94da02b931 | 316 | int last_index = 1; |
sweilz | 0:7e94da02b931 | 317 | lcd.setCursor(0,item_index+1); |
sweilz | 0:7e94da02b931 | 318 | lcd.print("->"); |
sweilz | 0:7e94da02b931 | 319 | |
sweilz | 0:7e94da02b931 | 320 | while(btn.read()) { |
sweilz | 0:7e94da02b931 | 321 | item_index+=encoder.getDirection(); |
sweilz | 0:7e94da02b931 | 322 | |
sweilz | 0:7e94da02b931 | 323 | if(item_index>2) { |
sweilz | 0:7e94da02b931 | 324 | item_index=2; |
sweilz | 0:7e94da02b931 | 325 | list_index++; |
sweilz | 0:7e94da02b931 | 326 | for(int i=1; i<4; i++) { |
sweilz | 0:7e94da02b931 | 327 | lcd.setCursor(3,i); |
sweilz | 0:7e94da02b931 | 328 | lcd.print(" "); |
sweilz | 0:7e94da02b931 | 329 | } |
sweilz | 0:7e94da02b931 | 330 | } |
sweilz | 0:7e94da02b931 | 331 | if(item_index<0) { |
sweilz | 0:7e94da02b931 | 332 | item_index=0; |
sweilz | 0:7e94da02b931 | 333 | list_index--; |
sweilz | 0:7e94da02b931 | 334 | for(int i=1; i<4; i++) { |
sweilz | 0:7e94da02b931 | 335 | lcd.setCursor(3,i); |
sweilz | 0:7e94da02b931 | 336 | lcd.print(" "); |
sweilz | 0:7e94da02b931 | 337 | } |
sweilz | 0:7e94da02b931 | 338 | } |
sweilz | 0:7e94da02b931 | 339 | if(item_index>strDisplay.size()-1)item_index = strDisplay.size()-1; |
sweilz | 0:7e94da02b931 | 340 | if(list_index<=0)list_index=0; |
sweilz | 0:7e94da02b931 | 341 | if(list_index>strDisplay.size()-3)list_index=strDisplay.size()-3; |
sweilz | 0:7e94da02b931 | 342 | |
sweilz | 0:7e94da02b931 | 343 | if(item_index != last_index) { |
sweilz | 0:7e94da02b931 | 344 | lcd.setCursor(0,last_index+1); |
sweilz | 0:7e94da02b931 | 345 | lcd.print(" "); |
sweilz | 0:7e94da02b931 | 346 | lcd.setCursor(0,item_index+1); |
sweilz | 0:7e94da02b931 | 347 | lcd.print("->"); |
sweilz | 0:7e94da02b931 | 348 | last_index = item_index; |
sweilz | 0:7e94da02b931 | 349 | } |
sweilz | 0:7e94da02b931 | 350 | |
sweilz | 0:7e94da02b931 | 351 | if(strDisplay.size()<=3) { |
sweilz | 0:7e94da02b931 | 352 | for(int i=0; i<strDisplay.size(); i++) { |
sweilz | 0:7e94da02b931 | 353 | lcd.setCursor(3,i+1); |
sweilz | 0:7e94da02b931 | 354 | lcd.print(strDisplay[i].c_str()); |
sweilz | 0:7e94da02b931 | 355 | } |
sweilz | 0:7e94da02b931 | 356 | } else { |
sweilz | 0:7e94da02b931 | 357 | lcd.setCursor(3,1); |
sweilz | 0:7e94da02b931 | 358 | lcd.print(strDisplay[list_index].c_str()); |
sweilz | 0:7e94da02b931 | 359 | lcd.setCursor(3,2); |
sweilz | 0:7e94da02b931 | 360 | lcd.print(strDisplay[list_index+1].c_str()); |
sweilz | 0:7e94da02b931 | 361 | lcd.setCursor(3,3); |
sweilz | 0:7e94da02b931 | 362 | lcd.print(strDisplay[list_index+2].c_str()); |
sweilz | 0:7e94da02b931 | 363 | } |
sweilz | 0:7e94da02b931 | 364 | wait(0.05); |
sweilz | 0:7e94da02b931 | 365 | } |
sweilz | 0:7e94da02b931 | 366 | while(!btn.read()); |
sweilz | 0:7e94da02b931 | 367 | can_back=true; |
sweilz | 0:7e94da02b931 | 368 | pc.printf("%d %s\n",item_index+list_index,strDisplay[item_index+list_index].c_str()); |
sweilz | 0:7e94da02b931 | 369 | return item_index+list_index; |
sweilz | 0:7e94da02b931 | 370 | } |
sweilz | 0:7e94da02b931 | 371 | |
sweilz | 0:7e94da02b931 | 372 | void getTemperature() |
sweilz | 0:7e94da02b931 | 373 | { |
sweilz | 0:7e94da02b931 | 374 | int start,end; |
sweilz | 0:7e94da02b931 | 375 | timer.start(); |
sweilz | 0:7e94da02b931 | 376 | |
sweilz | 0:7e94da02b931 | 377 | TempSensor.output(); |
sweilz | 0:7e94da02b931 | 378 | TempSensor.write(0); |
sweilz | 0:7e94da02b931 | 379 | wait_ms(10); |
sweilz | 0:7e94da02b931 | 380 | TempSensor.write(1); |
sweilz | 0:7e94da02b931 | 381 | wait_us(40); |
sweilz | 0:7e94da02b931 | 382 | TempSensor.input(); |
sweilz | 0:7e94da02b931 | 383 | |
sweilz | 0:7e94da02b931 | 384 | while (!TempSensor.read()); |
sweilz | 0:7e94da02b931 | 385 | start = timer.read_us(); |
sweilz | 0:7e94da02b931 | 386 | while (TempSensor.read()); |
sweilz | 0:7e94da02b931 | 387 | end = timer.read_us(); |
sweilz | 0:7e94da02b931 | 388 | |
sweilz | 0:7e94da02b931 | 389 | if (end - start> 60) { |
sweilz | 0:7e94da02b931 | 390 | for (int i = 0; i < 32; i++) { |
sweilz | 0:7e94da02b931 | 391 | while (!TempSensor.read()); |
sweilz | 0:7e94da02b931 | 392 | start = timer.read_us(); |
sweilz | 0:7e94da02b931 | 393 | while (TempSensor.read()); |
sweilz | 0:7e94da02b931 | 394 | end = timer.read_us(); |
sweilz | 0:7e94da02b931 | 395 | |
sweilz | 0:7e94da02b931 | 396 | if (end - start > 50) { |
sweilz | 0:7e94da02b931 | 397 | temperature <<= 1; |
sweilz | 0:7e94da02b931 | 398 | temperature |= 1; |
sweilz | 0:7e94da02b931 | 399 | } else { |
sweilz | 0:7e94da02b931 | 400 | temperature <<= 1; |
sweilz | 0:7e94da02b931 | 401 | } |
sweilz | 0:7e94da02b931 | 402 | } |
sweilz | 0:7e94da02b931 | 403 | temperature &= 0xFFFF; |
sweilz | 0:7e94da02b931 | 404 | } |
sweilz | 0:7e94da02b931 | 405 | timer.reset(); |
sweilz | 0:7e94da02b931 | 406 | //pc.printf("Temperature : %d\n",temperature); |
sweilz | 0:7e94da02b931 | 407 | } |
sweilz | 0:7e94da02b931 | 408 | void displayData() |
sweilz | 0:7e94da02b931 | 409 | { |
sweilz | 0:7e94da02b931 | 410 | |
sweilz | 0:7e94da02b931 | 411 | if(run==1) { |
sweilz | 0:7e94da02b931 | 412 | lcd.setCursor(15,1); |
sweilz | 0:7e94da02b931 | 413 | string temp; |
sweilz | 0:7e94da02b931 | 414 | sprintf(&temp[0],"%.1fC",float(temperature)/10); |
sweilz | 0:7e94da02b931 | 415 | lcd.print(temp.c_str()); |
sweilz | 0:7e94da02b931 | 416 | temp.clear(); |
sweilz | 0:7e94da02b931 | 417 | lcd.setCursor(15,0); |
sweilz | 0:7e94da02b931 | 418 | sprintf(&temp[0],"%.2f",process); |
sweilz | 0:7e94da02b931 | 419 | lcd.print(temp.c_str()); |
sweilz | 0:7e94da02b931 | 420 | |
sweilz | 0:7e94da02b931 | 421 | } |
sweilz | 0:7e94da02b931 | 422 | } |