Display text on screen.

Dependencies:   TextLCD mbed MaxSonar RTC-DS1307

Fork of TextLCD_HelloWorld by Simon Ford

Committer:
aueangpanit
Date:
Fri May 26 11:13:59 2017 +0000
Revision:
14:2bde99b3345c
Parent:
13:95eea857ed37
Child:
15:1e08396885bf
working clock

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:7418a52375a0 1 // Hello World! for the TextLCD
simon 1:7418a52375a0 2
simon 0:334327d1a416 3 #include "mbed.h"
aueangpanit 4:c669026b6f6e 4 #include <string>
simon 0:334327d1a416 5 #include "TextLCD.h"
aueangpanit 5:2f13ec8efe0b 6 #include "MaxSonar.h"
aueangpanit 14:2bde99b3345c 7 #include "Rtc_Ds1307.h"
aueangpanit 14:2bde99b3345c 8
aueangpanit 5:2f13ec8efe0b 9 using std::string;
simon 0:334327d1a416 10
aueangpanit 3:5e0ba6e35849 11 TextLCD lcd(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20); // rs, e, d4-d7
aueangpanit 3:5e0ba6e35849 12
aueangpanit 7:f00ddf85cacc 13 DigitalOut questionScreen(PTE3);
aueangpanit 4:c669026b6f6e 14 DigitalOut screen1(PTE5);
aueangpanit 4:c669026b6f6e 15 DigitalOut screen2(PTE4);
aueangpanit 4:c669026b6f6e 16
aueangpanit 9:39190ed25585 17 //All functions for controlling screens
aueangpanit 4:c669026b6f6e 18 void UpdateScreen(DigitalOut screen, string text);
aueangpanit 5:2f13ec8efe0b 19 void UpdateScreen(DigitalOut screen, char text[1024]);
aueangpanit 10:21c24327e65f 20 void UpdateScreen(DigitalOut screen, int firstLineLocation, string firstLineText, int secondLineLocation, string secondLineText);
aueangpanit 11:f6a39ae5ecbc 21 string GetLocationSpaces(int location);
aueangpanit 9:39190ed25585 22 void ClearAllScreen();
aueangpanit 9:39190ed25585 23
aueangpanit 9:39190ed25585 24 //All funcitons for range sensors
aueangpanit 9:39190ed25585 25 void UpdateRange();
aueangpanit 9:39190ed25585 26
aueangpanit 9:39190ed25585 27 //All functions for menu page
aueangpanit 9:39190ed25585 28 void GameOption();
aueangpanit 9:39190ed25585 29 void StartGame(int gameOptionLocation);
aueangpanit 9:39190ed25585 30
aueangpanit 9:39190ed25585 31 //All functions for exit screen menu
aueangpanit 9:39190ed25585 32 bool ExitScreen();
aueangpanit 9:39190ed25585 33 bool CheckExit(float r1, float r2);
aueangpanit 9:39190ed25585 34
aueangpanit 9:39190ed25585 35 //all functions for Multi-Maths game
aueangpanit 9:39190ed25585 36 void MultiChoice();
aueangpanit 8:8ce2ab8191cf 37 void DisplayNewQuestion();
aueangpanit 8:8ce2ab8191cf 38 bool CheckCorrect(int screenNumber);
aueangpanit 8:8ce2ab8191cf 39
aueangpanit 14:2bde99b3345c 40 //All functions for clock
aueangpanit 14:2bde99b3345c 41 void Clock();
aueangpanit 14:2bde99b3345c 42
aueangpanit 14:2bde99b3345c 43 Rtc_Ds1307 rtc(PTE0, PTE1);
aueangpanit 14:2bde99b3345c 44 DigitalOut red(LED1);
aueangpanit 14:2bde99b3345c 45
aueangpanit 14:2bde99b3345c 46 DigitalIn pin(PTC7); //button
aueangpanit 14:2bde99b3345c 47 Serial pc(USBTX, USBRX);
aueangpanit 14:2bde99b3345c 48 Rtc_Ds1307::Time_rtc tim = {};
aueangpanit 14:2bde99b3345c 49 PwmOut boop(PTD4); //alarm buzzer
aueangpanit 14:2bde99b3345c 50 Timer t;
aueangpanit 14:2bde99b3345c 51
aueangpanit 14:2bde99b3345c 52 void displayTime();
aueangpanit 14:2bde99b3345c 53 int setAlrm(bool ho);
aueangpanit 14:2bde99b3345c 54 bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim);
aueangpanit 14:2bde99b3345c 55
aueangpanit 14:2bde99b3345c 56 int Halrm = -1;
aueangpanit 14:2bde99b3345c 57 int Malrm = 0;
aueangpanit 14:2bde99b3345c 58
aueangpanit 14:2bde99b3345c 59
aueangpanit 8:8ce2ab8191cf 60 string questions[4] = {"What is 2*2?",
aueangpanit 8:8ce2ab8191cf 61 "What is 5*2?",
aueangpanit 8:8ce2ab8191cf 62 "what is 2+2?",
aueangpanit 8:8ce2ab8191cf 63 "What is 5*4?"};
aueangpanit 8:8ce2ab8191cf 64
aueangpanit 8:8ce2ab8191cf 65 string screen1Answers[4] = {"4 ", "10 ", "5 ", "18 "};
aueangpanit 8:8ce2ab8191cf 66 string screen2Answers[4] = {"5 ", "8 ", "4 ", "20 "};
aueangpanit 8:8ce2ab8191cf 67 int correctScreen[4] = {1, 1, 2, 2}; //1 = screen1; 2 = screen2;
aueangpanit 8:8ce2ab8191cf 68
aueangpanit 8:8ce2ab8191cf 69 int questionCount = 0;
simon 0:334327d1a416 70
aueangpanit 11:f6a39ae5ecbc 71 //all functions for pong game
aueangpanit 11:f6a39ae5ecbc 72 void Pong();
aueangpanit 13:95eea857ed37 73 void UpdatePlayerLocation();
aueangpanit 13:95eea857ed37 74 void UpdateBallLocation();
aueangpanit 13:95eea857ed37 75 void Draw();
aueangpanit 9:39190ed25585 76
aueangpanit 9:39190ed25585 77 MaxSonar *range1;
aueangpanit 5:2f13ec8efe0b 78 float r1;
aueangpanit 6:fa30f8383b99 79
aueangpanit 6:fa30f8383b99 80 MaxSonar *range2;
aueangpanit 6:fa30f8383b99 81 float r2;
aueangpanit 9:39190ed25585 82 int main() {
aueangpanit 11:f6a39ae5ecbc 83
aueangpanit 12:75b8081e1304 84 //UpdateScreen(questionScreen, 5, "0", 7, "___");
aueangpanit 11:f6a39ae5ecbc 85
aueangpanit 11:f6a39ae5ecbc 86 /*questionScreen = 1;
aueangpanit 11:f6a39ae5ecbc 87
aueangpanit 11:f6a39ae5ecbc 88 for(int i = 0; i < 10; i++){
aueangpanit 11:f6a39ae5ecbc 89 lcd.cls();
aueangpanit 11:f6a39ae5ecbc 90 lcd.locate(2, 0);
aueangpanit 11:f6a39ae5ecbc 91 lcd.printf("0");
aueangpanit 11:f6a39ae5ecbc 92 lcd.locate(2, 1);
aueangpanit 11:f6a39ae5ecbc 93 lcd.printf("===");
aueangpanit 11:f6a39ae5ecbc 94 }*/
aueangpanit 12:75b8081e1304 95 //wait(5);
aueangpanit 10:21c24327e65f 96
aueangpanit 5:2f13ec8efe0b 97 // Create and configure object for 3.3V powered LV-series device,
aueangpanit 5:2f13ec8efe0b 98 // accessed with analog reads (in cm) on p16, triggered by p7.
aueangpanit 5:2f13ec8efe0b 99 range1 = new MaxSonar(MS_LV, MS_ANALOG, PTB8, PTC2);
aueangpanit 5:2f13ec8efe0b 100 range1->setVoltage(3.3);
aueangpanit 5:2f13ec8efe0b 101 range1->setUnits(MS_CM);
aueangpanit 5:2f13ec8efe0b 102
aueangpanit 6:fa30f8383b99 103 range2 = new MaxSonar(MS_LV, MS_ANALOG, PTB9, PTB3);
aueangpanit 6:fa30f8383b99 104 range2->setVoltage(3.3);
aueangpanit 6:fa30f8383b99 105 range2->setUnits(MS_CM);
aueangpanit 9:39190ed25585 106
aueangpanit 9:39190ed25585 107 ClearAllScreen();
aueangpanit 9:39190ed25585 108
aueangpanit 14:2bde99b3345c 109 //Pong();
aueangpanit 14:2bde99b3345c 110 GameOption();
aueangpanit 14:2bde99b3345c 111
aueangpanit 14:2bde99b3345c 112 /*
aueangpanit 14:2bde99b3345c 113 UpdateScreen(questionScreen, "question screen");
aueangpanit 14:2bde99b3345c 114 UpdateScreen(screen1, "screen1");
aueangpanit 14:2bde99b3345c 115 UpdateScreen(screen2, "screen2");
aueangpanit 14:2bde99b3345c 116 */
aueangpanit 9:39190ed25585 117 }
aueangpanit 9:39190ed25585 118
aueangpanit 9:39190ed25585 119 int gameOptionLocation = 0;
aueangpanit 14:2bde99b3345c 120 string gameName[3] = {"Muti-Maths", "Pong!", "Clock"};
aueangpanit 9:39190ed25585 121 void GameOption()
aueangpanit 9:39190ed25585 122 {
aueangpanit 14:2bde99b3345c 123 //Clear all screens (so that all of the screens are blank)
aueangpanit 14:2bde99b3345c 124 ClearAllScreen();
aueangpanit 9:39190ed25585 125
aueangpanit 14:2bde99b3345c 126 //Display the selected game on "questionScreen"
aueangpanit 9:39190ed25585 127 UpdateScreen(questionScreen, "Select a game: >" + gameName[gameOptionLocation]);
aueangpanit 9:39190ed25585 128
aueangpanit 9:39190ed25585 129 bool gameOption = true;
aueangpanit 9:39190ed25585 130 while(gameOption)
aueangpanit 9:39190ed25585 131 {
aueangpanit 14:2bde99b3345c 132 //trigger the distance sensors to take a reading and store them in r1 and r2
aueangpanit 9:39190ed25585 133 UpdateRange();
aueangpanit 9:39190ed25585 134
aueangpanit 14:2bde99b3345c 135 if(r1 < 25 && r2 < 25) //select
aueangpanit 9:39190ed25585 136 {
aueangpanit 9:39190ed25585 137 UpdateScreen(questionScreen, "Entering " + gameName[gameOptionLocation] + ".....");
aueangpanit 9:39190ed25585 138 wait(1);
aueangpanit 9:39190ed25585 139
aueangpanit 9:39190ed25585 140 gameOption = false;
aueangpanit 14:2bde99b3345c 141 }
aueangpanit 14:2bde99b3345c 142 else if(r1 < 20) //down
aueangpanit 9:39190ed25585 143 {
aueangpanit 9:39190ed25585 144 if(gameOptionLocation > 0) { gameOptionLocation--; UpdateScreen(questionScreen, "Select a game: >" + gameName[gameOptionLocation]); }
aueangpanit 9:39190ed25585 145 }
aueangpanit 14:2bde99b3345c 146 else if(r2 < 20) //up
aueangpanit 9:39190ed25585 147 {
aueangpanit 14:2bde99b3345c 148 if(gameOptionLocation < 2) { gameOptionLocation++; UpdateScreen(questionScreen, "Select a game: >" + gameName[gameOptionLocation]); }
aueangpanit 9:39190ed25585 149 }
aueangpanit 9:39190ed25585 150 }
aueangpanit 9:39190ed25585 151
aueangpanit 9:39190ed25585 152 StartGame(gameOptionLocation);
aueangpanit 9:39190ed25585 153 }
aueangpanit 9:39190ed25585 154
aueangpanit 9:39190ed25585 155 void StartGame(int gameOptionLocation)
aueangpanit 9:39190ed25585 156 {
aueangpanit 9:39190ed25585 157 switch(gameOptionLocation)
aueangpanit 9:39190ed25585 158 {
aueangpanit 9:39190ed25585 159 case 0:
aueangpanit 9:39190ed25585 160 MultiChoice();
aueangpanit 9:39190ed25585 161 case 1:
aueangpanit 11:f6a39ae5ecbc 162 Pong();
aueangpanit 14:2bde99b3345c 163 case 2:
aueangpanit 14:2bde99b3345c 164 Clock();
aueangpanit 9:39190ed25585 165 }
aueangpanit 9:39190ed25585 166 }
aueangpanit 9:39190ed25585 167
aueangpanit 9:39190ed25585 168 void MultiChoice()
aueangpanit 9:39190ed25585 169 {
aueangpanit 9:39190ed25585 170 bool multiMaths = true;
aueangpanit 9:39190ed25585 171
aueangpanit 9:39190ed25585 172 DisplayNewQuestion();
aueangpanit 9:39190ed25585 173 while(multiMaths) {
aueangpanit 9:39190ed25585 174 UpdateRange();
aueangpanit 9:39190ed25585 175
aueangpanit 9:39190ed25585 176 if(CheckExit(r1, r2))
aueangpanit 9:39190ed25585 177 {
aueangpanit 9:39190ed25585 178 GameOption();
aueangpanit 9:39190ed25585 179 }
aueangpanit 9:39190ed25585 180
aueangpanit 8:8ce2ab8191cf 181 if(r1 < 20)
aueangpanit 8:8ce2ab8191cf 182 {
aueangpanit 8:8ce2ab8191cf 183 if(CheckCorrect(1))
aueangpanit 8:8ce2ab8191cf 184 {
aueangpanit 8:8ce2ab8191cf 185 UpdateScreen(questionScreen, "Correct!");
aueangpanit 8:8ce2ab8191cf 186 wait(1);
aueangpanit 8:8ce2ab8191cf 187 DisplayNewQuestion();
aueangpanit 8:8ce2ab8191cf 188 }
aueangpanit 8:8ce2ab8191cf 189 else
aueangpanit 8:8ce2ab8191cf 190 {
aueangpanit 8:8ce2ab8191cf 191 UpdateScreen(questionScreen, "Try Again!");
aueangpanit 8:8ce2ab8191cf 192 wait(1);
aueangpanit 8:8ce2ab8191cf 193 UpdateScreen(questionScreen, questions[questionCount]);
aueangpanit 8:8ce2ab8191cf 194 }
aueangpanit 8:8ce2ab8191cf 195 }
aueangpanit 8:8ce2ab8191cf 196 if(r2 < 20)
aueangpanit 8:8ce2ab8191cf 197 {
aueangpanit 8:8ce2ab8191cf 198 if(CheckCorrect(2))
aueangpanit 8:8ce2ab8191cf 199 {
aueangpanit 8:8ce2ab8191cf 200 UpdateScreen(questionScreen, "Correct!");
aueangpanit 8:8ce2ab8191cf 201 wait(1);
aueangpanit 8:8ce2ab8191cf 202 DisplayNewQuestion();
aueangpanit 8:8ce2ab8191cf 203 }
aueangpanit 8:8ce2ab8191cf 204 else
aueangpanit 8:8ce2ab8191cf 205 {
aueangpanit 8:8ce2ab8191cf 206 UpdateScreen(questionScreen, "Try Again!");
aueangpanit 8:8ce2ab8191cf 207 wait(1);
aueangpanit 8:8ce2ab8191cf 208 UpdateScreen(questionScreen, questions[questionCount]);
aueangpanit 8:8ce2ab8191cf 209 }
aueangpanit 8:8ce2ab8191cf 210 }
aueangpanit 8:8ce2ab8191cf 211
aueangpanit 5:2f13ec8efe0b 212 wait(0.5);
aueangpanit 5:2f13ec8efe0b 213 }
aueangpanit 4:c669026b6f6e 214 }
aueangpanit 4:c669026b6f6e 215
aueangpanit 8:8ce2ab8191cf 216 void DisplayNewQuestion()
aueangpanit 8:8ce2ab8191cf 217 {
aueangpanit 8:8ce2ab8191cf 218 if(questionCount >= 3)
aueangpanit 8:8ce2ab8191cf 219 {
aueangpanit 8:8ce2ab8191cf 220 questionCount = 0;
aueangpanit 8:8ce2ab8191cf 221 }
aueangpanit 8:8ce2ab8191cf 222 else
aueangpanit 8:8ce2ab8191cf 223 {
aueangpanit 8:8ce2ab8191cf 224 questionCount++;
aueangpanit 8:8ce2ab8191cf 225 }
aueangpanit 8:8ce2ab8191cf 226
aueangpanit 8:8ce2ab8191cf 227 UpdateScreen(questionScreen, questions[questionCount]);
aueangpanit 8:8ce2ab8191cf 228 UpdateScreen(screen1, screen1Answers[questionCount]);
aueangpanit 8:8ce2ab8191cf 229 UpdateScreen(screen2, screen2Answers[questionCount]);
aueangpanit 8:8ce2ab8191cf 230 }
aueangpanit 8:8ce2ab8191cf 231
aueangpanit 8:8ce2ab8191cf 232 bool CheckCorrect(int screenNumber)
aueangpanit 8:8ce2ab8191cf 233 {
aueangpanit 8:8ce2ab8191cf 234 if(screenNumber == correctScreen[questionCount])
aueangpanit 8:8ce2ab8191cf 235 {
aueangpanit 8:8ce2ab8191cf 236 return true;
aueangpanit 8:8ce2ab8191cf 237 }
aueangpanit 8:8ce2ab8191cf 238 else
aueangpanit 8:8ce2ab8191cf 239 {
aueangpanit 8:8ce2ab8191cf 240 return false;
aueangpanit 8:8ce2ab8191cf 241 }
aueangpanit 8:8ce2ab8191cf 242 }
aueangpanit 8:8ce2ab8191cf 243
aueangpanit 13:95eea857ed37 244 //player
aueangpanit 13:95eea857ed37 245 int pLocation = 5;
aueangpanit 14:2bde99b3345c 246 string player = "=====";
aueangpanit 13:95eea857ed37 247
aueangpanit 13:95eea857ed37 248 //ball
aueangpanit 14:2bde99b3345c 249 string ball = "0";
aueangpanit 13:95eea857ed37 250
aueangpanit 13:95eea857ed37 251 int ballPX = 0;
aueangpanit 13:95eea857ed37 252 int ballPY = 0;
aueangpanit 13:95eea857ed37 253
aueangpanit 13:95eea857ed37 254 int ballVX = 1;
aueangpanit 13:95eea857ed37 255 int ballVY = 1;
aueangpanit 14:2bde99b3345c 256 int score = 0;
aueangpanit 13:95eea857ed37 257
aueangpanit 13:95eea857ed37 258 void Pong() //this is a version of pong where you can't see the middle part of the board (15*10 pixel but only 4 horrizontal pixels are displaying)
aueangpanit 9:39190ed25585 259 {
aueangpanit 12:75b8081e1304 260 bool pong = true;
aueangpanit 12:75b8081e1304 261
aueangpanit 14:2bde99b3345c 262 UpdateScreen(questionScreen, "Welcome to Pong!");
aueangpanit 14:2bde99b3345c 263 wait(1);
aueangpanit 12:75b8081e1304 264
aueangpanit 14:2bde99b3345c 265 char score_char_array[10];
aueangpanit 14:2bde99b3345c 266 sprintf(score_char_array, "%d", score);
aueangpanit 12:75b8081e1304 267
aueangpanit 14:2bde99b3345c 268 UpdateScreen(questionScreen, score_char_array);
aueangpanit 14:2bde99b3345c 269 UpdateScreen(screen1, 0, ball, 0, "");
aueangpanit 12:75b8081e1304 270 UpdateScreen(screen2, 0, "", pLocation, player);
aueangpanit 12:75b8081e1304 271
aueangpanit 12:75b8081e1304 272 while(pong)
aueangpanit 12:75b8081e1304 273 {
aueangpanit 12:75b8081e1304 274 UpdateRange();
aueangpanit 12:75b8081e1304 275 if(CheckExit(r1, r2))
aueangpanit 12:75b8081e1304 276 {
aueangpanit 12:75b8081e1304 277 GameOption();
aueangpanit 12:75b8081e1304 278 }
aueangpanit 9:39190ed25585 279
aueangpanit 13:95eea857ed37 280 UpdatePlayerLocation();
aueangpanit 13:95eea857ed37 281 UpdateBallLocation();
aueangpanit 12:75b8081e1304 282
aueangpanit 13:95eea857ed37 283 Draw();
aueangpanit 12:75b8081e1304 284 }
aueangpanit 9:39190ed25585 285 }
aueangpanit 9:39190ed25585 286
aueangpanit 13:95eea857ed37 287 void UpdatePlayerLocation()
aueangpanit 13:95eea857ed37 288 {
aueangpanit 13:95eea857ed37 289 if(r2 < 20)
aueangpanit 13:95eea857ed37 290 {
aueangpanit 13:95eea857ed37 291 if(pLocation > 0)
aueangpanit 13:95eea857ed37 292 {
aueangpanit 13:95eea857ed37 293 pLocation--;
aueangpanit 13:95eea857ed37 294 }
aueangpanit 13:95eea857ed37 295 }
aueangpanit 13:95eea857ed37 296 if(r1 < 20)
aueangpanit 13:95eea857ed37 297 {
aueangpanit 14:2bde99b3345c 298 if(pLocation < 9)
aueangpanit 13:95eea857ed37 299 {
aueangpanit 13:95eea857ed37 300 pLocation++;
aueangpanit 13:95eea857ed37 301 }
aueangpanit 13:95eea857ed37 302 }
aueangpanit 13:95eea857ed37 303
aueangpanit 13:95eea857ed37 304 wait(0.5);
aueangpanit 13:95eea857ed37 305 }
aueangpanit 13:95eea857ed37 306
aueangpanit 13:95eea857ed37 307 void UpdateBallLocation()
aueangpanit 13:95eea857ed37 308 {
aueangpanit 13:95eea857ed37 309 if(ballPY == 0) { ballVY = 1; }
aueangpanit 13:95eea857ed37 310 if(ballPY == 9) { ballVY = -1; }
aueangpanit 13:95eea857ed37 311 if(ballPX == 0) { ballVX = 1; }
aueangpanit 13:95eea857ed37 312 if(ballPX == 15) { ballVX = -1; }
aueangpanit 13:95eea857ed37 313
aueangpanit 14:2bde99b3345c 314 if(ballPY == 8 && ballPX >= pLocation && ballPX <= pLocation + 5)
aueangpanit 14:2bde99b3345c 315 {
aueangpanit 14:2bde99b3345c 316 ballVY = -1;
aueangpanit 14:2bde99b3345c 317 }
aueangpanit 14:2bde99b3345c 318
aueangpanit 13:95eea857ed37 319 ballPY += ballVY;
aueangpanit 13:95eea857ed37 320 ballPX += ballVX;
aueangpanit 13:95eea857ed37 321 }
aueangpanit 13:95eea857ed37 322
aueangpanit 13:95eea857ed37 323 void Draw()
aueangpanit 13:95eea857ed37 324 {
aueangpanit 13:95eea857ed37 325 //draw ball also
aueangpanit 13:95eea857ed37 326 if(ballPY < 2 || ballPY > 7)
aueangpanit 13:95eea857ed37 327 {
aueangpanit 14:2bde99b3345c 328 if(ballPY == 0) { UpdateScreen(screen1, ballPX, ball, 0, ""); }
aueangpanit 14:2bde99b3345c 329 if(ballPY == 1) { UpdateScreen(screen1, 0, "", ballPX, ball); }
aueangpanit 14:2bde99b3345c 330 if(ballPY == 8) { UpdateScreen(screen2, ballPX, ball, pLocation, player); }
aueangpanit 14:2bde99b3345c 331 if(ballPY == 9) { UpdateScreen(screen2, 0, "", ballPX, ball); }
aueangpanit 13:95eea857ed37 332 }
aueangpanit 13:95eea857ed37 333 else //only draw player and enermy
aueangpanit 13:95eea857ed37 334 {
aueangpanit 13:95eea857ed37 335 ClearAllScreen();
aueangpanit 14:2bde99b3345c 336 UpdateScreen(screen2, 0, "", pLocation, player);
aueangpanit 13:95eea857ed37 337 }
aueangpanit 13:95eea857ed37 338 }
aueangpanit 13:95eea857ed37 339
aueangpanit 13:95eea857ed37 340
aueangpanit 13:95eea857ed37 341
aueangpanit 9:39190ed25585 342 string exitOption[2] = {"Yes", "No"};
aueangpanit 9:39190ed25585 343 bool CheckExit(float r1, float r2)
aueangpanit 9:39190ed25585 344 {
aueangpanit 9:39190ed25585 345 if(r1 < 25 && r2 < 25)
aueangpanit 9:39190ed25585 346 {
aueangpanit 9:39190ed25585 347 return ExitScreen();
aueangpanit 9:39190ed25585 348 }
aueangpanit 9:39190ed25585 349 else
aueangpanit 9:39190ed25585 350 {
aueangpanit 9:39190ed25585 351 return false;
aueangpanit 9:39190ed25585 352 }
aueangpanit 9:39190ed25585 353 }
aueangpanit 9:39190ed25585 354
aueangpanit 9:39190ed25585 355 bool ExitScreen()
aueangpanit 9:39190ed25585 356 {
aueangpanit 9:39190ed25585 357 int exitOption_int = 0;
aueangpanit 9:39190ed25585 358 UpdateScreen(questionScreen, "AreYouSureYouWantToExit? >" + exitOption[exitOption_int]);
aueangpanit 9:39190ed25585 359
aueangpanit 9:39190ed25585 360 wait(1);
aueangpanit 9:39190ed25585 361
aueangpanit 9:39190ed25585 362 bool choosing = true;
aueangpanit 9:39190ed25585 363 while(choosing)
aueangpanit 9:39190ed25585 364 {
aueangpanit 9:39190ed25585 365 wait(0.5);
aueangpanit 9:39190ed25585 366 UpdateRange();
aueangpanit 9:39190ed25585 367 if(r1 < 25 && r2 < 25)
aueangpanit 9:39190ed25585 368 {
aueangpanit 9:39190ed25585 369 if(exitOption_int == 0)
aueangpanit 9:39190ed25585 370 {
aueangpanit 9:39190ed25585 371 choosing = false;
aueangpanit 9:39190ed25585 372 UpdateScreen(questionScreen, "Exiting...");
aueangpanit 9:39190ed25585 373 wait(1);
aueangpanit 9:39190ed25585 374 return true;
aueangpanit 9:39190ed25585 375 }
aueangpanit 9:39190ed25585 376 else
aueangpanit 9:39190ed25585 377 {
aueangpanit 9:39190ed25585 378 choosing = false;
aueangpanit 9:39190ed25585 379 UpdateScreen(questionScreen, "Returning to game");
aueangpanit 9:39190ed25585 380 wait(1);
aueangpanit 9:39190ed25585 381 return false;
aueangpanit 9:39190ed25585 382 }
aueangpanit 9:39190ed25585 383 }
aueangpanit 9:39190ed25585 384 else if(r1 < 20)
aueangpanit 9:39190ed25585 385 {
aueangpanit 9:39190ed25585 386 if(exitOption_int > 0) { exitOption_int--; UpdateScreen(questionScreen, "AreYouSureYouWantToExit? >" + exitOption[exitOption_int]); }
aueangpanit 9:39190ed25585 387 }
aueangpanit 9:39190ed25585 388 else if(r2 < 20)
aueangpanit 9:39190ed25585 389 {
aueangpanit 9:39190ed25585 390 if(exitOption_int < 1) { exitOption_int++; UpdateScreen(questionScreen, "AreYouSureYouWantToExit? >" + exitOption[exitOption_int]); }
aueangpanit 9:39190ed25585 391 }
aueangpanit 9:39190ed25585 392 }
aueangpanit 9:39190ed25585 393 }
aueangpanit 9:39190ed25585 394
aueangpanit 4:c669026b6f6e 395 void UpdateScreen(DigitalOut screen, string text)
aueangpanit 4:c669026b6f6e 396 {
aueangpanit 4:c669026b6f6e 397 //disable all E pin for all screens
aueangpanit 8:8ce2ab8191cf 398 questionScreen = 0;
aueangpanit 4:c669026b6f6e 399 screen1 = 0;
aueangpanit 4:c669026b6f6e 400 screen2 = 0;
aueangpanit 4:c669026b6f6e 401
aueangpanit 4:c669026b6f6e 402 //enable E pin for the scrren that we want to update
aueangpanit 4:c669026b6f6e 403 screen = 1;
aueangpanit 4:c669026b6f6e 404
aueangpanit 5:2f13ec8efe0b 405 //convert text to char array
aueangpanit 5:2f13ec8efe0b 406 char text_char_array[1024];
aueangpanit 5:2f13ec8efe0b 407 strcpy(text_char_array, text.c_str());
aueangpanit 4:c669026b6f6e 408 //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
aueangpanit 5:2f13ec8efe0b 409 for(int i = 0; i < 10; i++)
aueangpanit 4:c669026b6f6e 410 {
aueangpanit 3:5e0ba6e35849 411 lcd.cls();
aueangpanit 4:c669026b6f6e 412 lcd.printf(text_char_array);
aueangpanit 3:5e0ba6e35849 413 }
aueangpanit 5:2f13ec8efe0b 414 }
aueangpanit 5:2f13ec8efe0b 415
aueangpanit 5:2f13ec8efe0b 416 void UpdateScreen(DigitalOut screen, char text[1024])
aueangpanit 5:2f13ec8efe0b 417 {
aueangpanit 5:2f13ec8efe0b 418 //disable all E pin for all screens
aueangpanit 8:8ce2ab8191cf 419 questionScreen = 0;
aueangpanit 5:2f13ec8efe0b 420 screen1 = 0;
aueangpanit 5:2f13ec8efe0b 421 screen2 = 0;
aueangpanit 4:c669026b6f6e 422
aueangpanit 5:2f13ec8efe0b 423 //enable E pin for the scrren that we want to update
aueangpanit 5:2f13ec8efe0b 424 screen = 1;
aueangpanit 4:c669026b6f6e 425
aueangpanit 5:2f13ec8efe0b 426 //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
aueangpanit 5:2f13ec8efe0b 427 for(int i = 0; i < 10; i++)
aueangpanit 5:2f13ec8efe0b 428 {
aueangpanit 5:2f13ec8efe0b 429 lcd.cls();
aueangpanit 5:2f13ec8efe0b 430 lcd.printf(text);
aueangpanit 5:2f13ec8efe0b 431 }
simon 0:334327d1a416 432 }
aueangpanit 9:39190ed25585 433
aueangpanit 10:21c24327e65f 434 void UpdateScreen(DigitalOut screen, int firstLineLocation, string firstLineText, int secondLineLocation, string secondLineText)
aueangpanit 10:21c24327e65f 435 {
aueangpanit 10:21c24327e65f 436 //disable all E pin for all screens
aueangpanit 10:21c24327e65f 437 questionScreen = 0;
aueangpanit 10:21c24327e65f 438 screen1 = 0;
aueangpanit 10:21c24327e65f 439 screen2 = 0;
aueangpanit 10:21c24327e65f 440
aueangpanit 10:21c24327e65f 441 //enable E pin for the scrren that we want to update
aueangpanit 10:21c24327e65f 442 screen = 1;
aueangpanit 10:21c24327e65f 443
aueangpanit 11:f6a39ae5ecbc 444 string line1text = GetLocationSpaces(firstLineLocation) + firstLineText;
aueangpanit 11:f6a39ae5ecbc 445 string line2text = GetLocationSpaces(secondLineLocation) + secondLineText;
aueangpanit 11:f6a39ae5ecbc 446
aueangpanit 10:21c24327e65f 447 //convert text to char array
aueangpanit 10:21c24327e65f 448 char firstLine_char_array[1024];
aueangpanit 10:21c24327e65f 449 char secondLine_char_array[1024];
aueangpanit 11:f6a39ae5ecbc 450 strcpy(firstLine_char_array, line1text.c_str());
aueangpanit 11:f6a39ae5ecbc 451 strcpy(secondLine_char_array, line2text.c_str());
aueangpanit 10:21c24327e65f 452
aueangpanit 10:21c24327e65f 453 //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
aueangpanit 10:21c24327e65f 454 for(int i = 0; i < 10; i++)
aueangpanit 10:21c24327e65f 455 {
aueangpanit 10:21c24327e65f 456 lcd.cls();
aueangpanit 11:f6a39ae5ecbc 457 lcd.locate(0, 0);
aueangpanit 10:21c24327e65f 458 lcd.printf(firstLine_char_array);
aueangpanit 11:f6a39ae5ecbc 459 lcd.locate(0, 1);
aueangpanit 10:21c24327e65f 460 lcd.printf(secondLine_char_array);
aueangpanit 10:21c24327e65f 461 }
aueangpanit 10:21c24327e65f 462 }
aueangpanit 10:21c24327e65f 463
aueangpanit 11:f6a39ae5ecbc 464 string GetLocationSpaces(int location)
aueangpanit 10:21c24327e65f 465 {
aueangpanit 11:f6a39ae5ecbc 466 string space = "";
aueangpanit 11:f6a39ae5ecbc 467 for(int i = 0; i < location; i++)
aueangpanit 10:21c24327e65f 468 {
aueangpanit 11:f6a39ae5ecbc 469 space += " ";
aueangpanit 11:f6a39ae5ecbc 470 }
aueangpanit 11:f6a39ae5ecbc 471 return space;
aueangpanit 10:21c24327e65f 472 }
aueangpanit 10:21c24327e65f 473
aueangpanit 9:39190ed25585 474 void ClearAllScreen()
aueangpanit 9:39190ed25585 475 {
aueangpanit 9:39190ed25585 476 questionScreen = 1;
aueangpanit 9:39190ed25585 477 screen1 = 1;
aueangpanit 9:39190ed25585 478 screen2 = 1;
aueangpanit 9:39190ed25585 479
aueangpanit 9:39190ed25585 480 lcd.cls();
aueangpanit 9:39190ed25585 481 }
aueangpanit 9:39190ed25585 482
aueangpanit 9:39190ed25585 483 void UpdateRange()
aueangpanit 9:39190ed25585 484 {
aueangpanit 9:39190ed25585 485 // Trigger read, wait 49ms until ranger finder has
aueangpanit 9:39190ed25585 486 // finished, then read.
aueangpanit 9:39190ed25585 487 range1->triggerRead();
aueangpanit 9:39190ed25585 488 wait_ms(49);
aueangpanit 9:39190ed25585 489 r1 = range1->read();
aueangpanit 9:39190ed25585 490
aueangpanit 9:39190ed25585 491 range2->triggerRead();
aueangpanit 9:39190ed25585 492 wait_ms(49);
aueangpanit 9:39190ed25585 493 r2 = range2->read();
aueangpanit 14:2bde99b3345c 494 }
aueangpanit 14:2bde99b3345c 495
aueangpanit 14:2bde99b3345c 496 void Clock() {
aueangpanit 14:2bde99b3345c 497
aueangpanit 14:2bde99b3345c 498 questionScreen.write(1);
aueangpanit 14:2bde99b3345c 499 screen1 = 0;
aueangpanit 14:2bde99b3345c 500 screen2 = 0;
aueangpanit 14:2bde99b3345c 501
aueangpanit 14:2bde99b3345c 502 boop.write(0); //initialise buzzer
aueangpanit 14:2bde99b3345c 503 boop.period(5);
aueangpanit 14:2bde99b3345c 504
aueangpanit 14:2bde99b3345c 505 Ticker cloo; //declare a ticker to update clock every sec
aueangpanit 14:2bde99b3345c 506 cloo.attach(&displayTime, 1); //the ticker runs the function that wipes the screen and displays the updated time
aueangpanit 14:2bde99b3345c 507
aueangpanit 14:2bde99b3345c 508 rtc.startClock();
aueangpanit 14:2bde99b3345c 509
aueangpanit 14:2bde99b3345c 510 while(1){
aueangpanit 14:2bde99b3345c 511
aueangpanit 14:2bde99b3345c 512 wait_ms(50); //wait so button states do not overlap
aueangpanit 14:2bde99b3345c 513
aueangpanit 14:2bde99b3345c 514 if(pin.read()){ //once it's 1 start the detection loop to decide whether this is a hold or a double click
aueangpanit 14:2bde99b3345c 515
aueangpanit 14:2bde99b3345c 516 cloo.detach(); //stop the ticker while performing button operations
aueangpanit 14:2bde99b3345c 517
aueangpanit 14:2bde99b3345c 518 t.reset();
aueangpanit 14:2bde99b3345c 519 t.start(); //start timer
aueangpanit 14:2bde99b3345c 520
aueangpanit 14:2bde99b3345c 521 while(t.read() < 1){ //if it's not unpressed in a second - HOLD case
aueangpanit 14:2bde99b3345c 522
aueangpanit 14:2bde99b3345c 523 if(!pin.read()){ //if it goes low, check for double click
aueangpanit 14:2bde99b3345c 524
aueangpanit 14:2bde99b3345c 525 t.reset();
aueangpanit 14:2bde99b3345c 526
aueangpanit 14:2bde99b3345c 527 while(t.read() < 1){ //listen for 1 sec for a second click
aueangpanit 14:2bde99b3345c 528
aueangpanit 14:2bde99b3345c 529 if(pin.read()){ //if x = 1 execute DOUBLE CLICK - set alarm
aueangpanit 14:2bde99b3345c 530 lcd.cls();
aueangpanit 14:2bde99b3345c 531 lcd.printf("settin' alarm!");
aueangpanit 14:2bde99b3345c 532 wait(2);
aueangpanit 14:2bde99b3345c 533
aueangpanit 14:2bde99b3345c 534 Halrm = setAlrm(true);
aueangpanit 14:2bde99b3345c 535 if(Halrm >= 0){
aueangpanit 14:2bde99b3345c 536 Malrm = setAlrm(false);
aueangpanit 14:2bde99b3345c 537 lcd.cls();
aueangpanit 14:2bde99b3345c 538 lcd.printf("Alarm: %d:%d", Halrm,Malrm);
aueangpanit 14:2bde99b3345c 539 wait(2);
aueangpanit 14:2bde99b3345c 540 }
aueangpanit 14:2bde99b3345c 541 break;
aueangpanit 14:2bde99b3345c 542 }
aueangpanit 14:2bde99b3345c 543 }
aueangpanit 14:2bde99b3345c 544 break;
aueangpanit 14:2bde99b3345c 545 }
aueangpanit 14:2bde99b3345c 546 }
aueangpanit 14:2bde99b3345c 547
aueangpanit 14:2bde99b3345c 548
aueangpanit 14:2bde99b3345c 549
aueangpanit 14:2bde99b3345c 550 while(pin.read()){ //still pressed? Execute Button HOLD case, exit hold when no longer held
aueangpanit 14:2bde99b3345c 551
aueangpanit 14:2bde99b3345c 552 rtc.getTime(tim);
aueangpanit 14:2bde99b3345c 553
aueangpanit 14:2bde99b3345c 554 lcd.cls();
aueangpanit 14:2bde99b3345c 555 lcd.printf("Today is the :\n%02d/%02d/%02d", tim.date, tim.mon, tim.year); //display date
aueangpanit 14:2bde99b3345c 556 wait(1);
aueangpanit 14:2bde99b3345c 557
aueangpanit 14:2bde99b3345c 558 }
aueangpanit 14:2bde99b3345c 559
aueangpanit 14:2bde99b3345c 560 cloo.attach(&displayTime, 1); //re-enable ticker after date release
aueangpanit 14:2bde99b3345c 561
aueangpanit 14:2bde99b3345c 562 }
aueangpanit 14:2bde99b3345c 563
aueangpanit 14:2bde99b3345c 564 t.stop();
aueangpanit 14:2bde99b3345c 565
aueangpanit 14:2bde99b3345c 566 }
aueangpanit 14:2bde99b3345c 567
aueangpanit 14:2bde99b3345c 568 }
aueangpanit 14:2bde99b3345c 569
aueangpanit 14:2bde99b3345c 570 void displayTime(){ //get the time from the rtc chip and display it on the LCD
aueangpanit 14:2bde99b3345c 571
aueangpanit 14:2bde99b3345c 572 Rtc_Ds1307::Time_rtc tm = tim;
aueangpanit 14:2bde99b3345c 573
aueangpanit 14:2bde99b3345c 574 if (rtc.getTime(tm) ) {
aueangpanit 14:2bde99b3345c 575 lcd.cls();
aueangpanit 14:2bde99b3345c 576 lcd.printf("The time is :\n%02d:%02d:%02d", tm.hour, tm.min, tm.sec); //display time
aueangpanit 14:2bde99b3345c 577
aueangpanit 14:2bde99b3345c 578 if(hitIt(Halrm, Malrm, tm)){ //check if the time is right for alarmage
aueangpanit 14:2bde99b3345c 579 boop.write(0.5);
aueangpanit 14:2bde99b3345c 580 while(!pin.read()){
aueangpanit 14:2bde99b3345c 581 lcd.cls();
aueangpanit 14:2bde99b3345c 582 lcd.printf("You should be alarmed");
aueangpanit 14:2bde99b3345c 583 wait(0.2);
aueangpanit 14:2bde99b3345c 584 }
aueangpanit 14:2bde99b3345c 585 boop.write(0);
aueangpanit 14:2bde99b3345c 586 }
aueangpanit 14:2bde99b3345c 587
aueangpanit 14:2bde99b3345c 588 }
aueangpanit 14:2bde99b3345c 589 red = !red;
aueangpanit 14:2bde99b3345c 590 }
aueangpanit 14:2bde99b3345c 591
aueangpanit 14:2bde99b3345c 592 int setAlrm(bool ho){
aueangpanit 14:2bde99b3345c 593
aueangpanit 14:2bde99b3345c 594 int mod;
aueangpanit 14:2bde99b3345c 595 float spd;
aueangpanit 14:2bde99b3345c 596 if(ho){
aueangpanit 14:2bde99b3345c 597 mod = 24;
aueangpanit 14:2bde99b3345c 598 spd = 0.5;
aueangpanit 14:2bde99b3345c 599 }else{
aueangpanit 14:2bde99b3345c 600 mod = 60;
aueangpanit 14:2bde99b3345c 601 spd = 0.25;
aueangpanit 14:2bde99b3345c 602 }
aueangpanit 14:2bde99b3345c 603
aueangpanit 14:2bde99b3345c 604 int homin = 0;
aueangpanit 14:2bde99b3345c 605 bool sat = false;
aueangpanit 14:2bde99b3345c 606 bool DC = false;
aueangpanit 14:2bde99b3345c 607
aueangpanit 14:2bde99b3345c 608 while(!sat){
aueangpanit 14:2bde99b3345c 609
aueangpanit 14:2bde99b3345c 610 while(!pin.read() && !sat){
aueangpanit 14:2bde99b3345c 611
aueangpanit 14:2bde99b3345c 612 lcd.cls();
aueangpanit 14:2bde99b3345c 613 if(ho) lcd.printf("Hour: %02d\nDC to confirm", homin);
aueangpanit 14:2bde99b3345c 614 else lcd.printf("Minutes: %02d\nDC to confirm", homin);
aueangpanit 14:2bde99b3345c 615
aueangpanit 14:2bde99b3345c 616 wait(0.2);
aueangpanit 14:2bde99b3345c 617 if(pin.read()){ //once it's 1 start the detection loop to decide whether this is a hold or a double click
aueangpanit 14:2bde99b3345c 618
aueangpanit 14:2bde99b3345c 619 t.reset();
aueangpanit 14:2bde99b3345c 620 t.start(); //start timer
aueangpanit 14:2bde99b3345c 621
aueangpanit 14:2bde99b3345c 622 while(t.read() < 1){ //if it's not unpressed in a second - HOLD case
aueangpanit 14:2bde99b3345c 623
aueangpanit 14:2bde99b3345c 624 if(!pin.read()){ //if it goes low, check for double click
aueangpanit 14:2bde99b3345c 625
aueangpanit 14:2bde99b3345c 626 t.reset();
aueangpanit 14:2bde99b3345c 627
aueangpanit 14:2bde99b3345c 628 while(t.read() < 1){ //listen for 1 sec for a second click
aueangpanit 14:2bde99b3345c 629
aueangpanit 14:2bde99b3345c 630 if(pin.read()){ //if x = 1 execute DOUBLE CLICK - confirm selection
aueangpanit 14:2bde99b3345c 631 lcd.cls();
aueangpanit 14:2bde99b3345c 632 if(ho) lcd.printf("Hour: %02d\nSet!", homin);
aueangpanit 14:2bde99b3345c 633 else lcd.printf("Minutes: %02d\nSet!", homin);
aueangpanit 14:2bde99b3345c 634 sat = true;
aueangpanit 14:2bde99b3345c 635 DC = true;
aueangpanit 14:2bde99b3345c 636 wait(2);
aueangpanit 14:2bde99b3345c 637 break;
aueangpanit 14:2bde99b3345c 638 }
aueangpanit 14:2bde99b3345c 639 }
aueangpanit 14:2bde99b3345c 640 break;
aueangpanit 14:2bde99b3345c 641 }
aueangpanit 14:2bde99b3345c 642 }
aueangpanit 14:2bde99b3345c 643 if(!pin.read() && !DC){ //if unpressed - single click detected - cancel alarm
aueangpanit 14:2bde99b3345c 644 lcd.cls();
aueangpanit 14:2bde99b3345c 645 lcd.printf("Alarm Canceled!");
aueangpanit 14:2bde99b3345c 646 homin = -1;
aueangpanit 14:2bde99b3345c 647 return homin;
aueangpanit 14:2bde99b3345c 648 }
aueangpanit 14:2bde99b3345c 649
aueangpanit 14:2bde99b3345c 650 while(pin.read()){ //still pressed? Execute Button HOLD case, exit hold when no longer held
aueangpanit 14:2bde99b3345c 651
aueangpanit 14:2bde99b3345c 652 homin = (homin + 1)%mod; //increment the value while the button is held
aueangpanit 14:2bde99b3345c 653 lcd.cls();
aueangpanit 14:2bde99b3345c 654 if(ho) lcd.printf("Hour: %02d", homin);
aueangpanit 14:2bde99b3345c 655 else lcd.printf("Minutes: %02d", homin);
aueangpanit 14:2bde99b3345c 656 wait(spd);
aueangpanit 14:2bde99b3345c 657
aueangpanit 14:2bde99b3345c 658 }
aueangpanit 14:2bde99b3345c 659 }
aueangpanit 14:2bde99b3345c 660 }
aueangpanit 14:2bde99b3345c 661
aueangpanit 14:2bde99b3345c 662 }
aueangpanit 14:2bde99b3345c 663
aueangpanit 14:2bde99b3345c 664 return homin;
aueangpanit 14:2bde99b3345c 665 }
aueangpanit 14:2bde99b3345c 666
aueangpanit 14:2bde99b3345c 667 //this function checks whether it's time to sound the alarms
aueangpanit 14:2bde99b3345c 668 //if the minute and the hour is the same as that of the alarm, return true
aueangpanit 14:2bde99b3345c 669 bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim){
aueangpanit 14:2bde99b3345c 670 int x = teim.hour - hor;
aueangpanit 14:2bde99b3345c 671 if(x == 0){
aueangpanit 14:2bde99b3345c 672 int y = teim.min - min;
aueangpanit 14:2bde99b3345c 673 if(y == 0){
aueangpanit 14:2bde99b3345c 674 return true;
aueangpanit 14:2bde99b3345c 675 }
aueangpanit 14:2bde99b3345c 676 }
aueangpanit 14:2bde99b3345c 677 return false;
aueangpanit 9:39190ed25585 678 }