Display text on screen.
Dependencies: TextLCD mbed MaxSonar RTC-DS1307
Fork of TextLCD_HelloWorld by
Diff: main.cpp
- Revision:
- 14:2bde99b3345c
- Parent:
- 13:95eea857ed37
- Child:
- 15:1e08396885bf
--- a/main.cpp Thu May 25 13:19:04 2017 +0000 +++ b/main.cpp Fri May 26 11:13:59 2017 +0000 @@ -4,6 +4,8 @@ #include <string> #include "TextLCD.h" #include "MaxSonar.h" +#include "Rtc_Ds1307.h" + using std::string; TextLCD lcd(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20); // rs, e, d4-d7 @@ -35,6 +37,26 @@ void DisplayNewQuestion(); bool CheckCorrect(int screenNumber); +//All functions for clock +void Clock(); + +Rtc_Ds1307 rtc(PTE0, PTE1); +DigitalOut red(LED1); + +DigitalIn pin(PTC7); //button +Serial pc(USBTX, USBRX); +Rtc_Ds1307::Time_rtc tim = {}; +PwmOut boop(PTD4); //alarm buzzer +Timer t; + +void displayTime(); +int setAlrm(bool ho); +bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim); + +int Halrm = -1; +int Malrm = 0; + + string questions[4] = {"What is 2*2?", "What is 5*2?", "what is 2+2?", @@ -84,38 +106,46 @@ ClearAllScreen(); - Pong(); - //GameOption(); + //Pong(); + GameOption(); + + /* + UpdateScreen(questionScreen, "question screen"); + UpdateScreen(screen1, "screen1"); + UpdateScreen(screen2, "screen2"); + */ } int gameOptionLocation = 0; -string gameName[2] = {"Muti-Maths", "Pong!"}; +string gameName[3] = {"Muti-Maths", "Pong!", "Clock"}; void GameOption() { - ClearAllScreen(); + //Clear all screens (so that all of the screens are blank) + ClearAllScreen(); + //Display the selected game on "questionScreen" UpdateScreen(questionScreen, "Select a game: >" + gameName[gameOptionLocation]); bool gameOption = true; while(gameOption) { + //trigger the distance sensors to take a reading and store them in r1 and r2 UpdateRange(); - if(r1 < 25 && r2 < 25) + if(r1 < 25 && r2 < 25) //select { UpdateScreen(questionScreen, "Entering " + gameName[gameOptionLocation] + "....."); wait(1); gameOption = false; - } - - else if(r1 < 20) + } + else if(r1 < 20) //down { if(gameOptionLocation > 0) { gameOptionLocation--; UpdateScreen(questionScreen, "Select a game: >" + gameName[gameOptionLocation]); } } - else if(r2 < 20) + else if(r2 < 20) //up { - if(gameOptionLocation < 1) { gameOptionLocation++; UpdateScreen(questionScreen, "Select a game: >" + gameName[gameOptionLocation]); } + if(gameOptionLocation < 2) { gameOptionLocation++; UpdateScreen(questionScreen, "Select a game: >" + gameName[gameOptionLocation]); } } } @@ -130,6 +160,8 @@ MultiChoice(); case 1: Pong(); + case 2: + Clock(); } } @@ -211,26 +243,30 @@ //player int pLocation = 5; -string player = "___"; +string player = "====="; //ball -string ball = 0; +string ball = "0"; int ballPX = 0; int ballPY = 0; int ballVX = 1; int ballVY = 1; +int score = 0; 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) { bool pong = true; - - + UpdateScreen(questionScreen, "Welcome to Pong!"); + wait(1); + char score_char_array[10]; + sprintf(score_char_array, "%d", score); - UpdateScreen(questionScreen, "Welcome to Pong!"); + UpdateScreen(questionScreen, score_char_array); + UpdateScreen(screen1, 0, ball, 0, ""); UpdateScreen(screen2, 0, "", pLocation, player); while(pong) @@ -259,7 +295,7 @@ } if(r1 < 20) { - if(pLocation < 12) + if(pLocation < 9) { pLocation++; } @@ -275,6 +311,11 @@ if(ballPX == 0) { ballVX = 1; } if(ballPX == 15) { ballVX = -1; } + if(ballPY == 8 && ballPX >= pLocation && ballPX <= pLocation + 5) + { + ballVY = -1; + } + ballPY += ballVY; ballPX += ballVX; } @@ -284,14 +325,15 @@ //draw ball also if(ballPY < 2 || ballPY > 7) { - if(ballPY == 0) { UpdateScreen(screen1, ballPX, "0", 0, ""); } - if(ballPY == 1) { UpdateScreen(screen1, 0, "", ballPX, "0"); } - if(ballPY == 8) { UpdateScreen(screen2, ballPX, "0", 0, ""); } - if(ballPY == 9) { UpdateScreen(screen2, 0, "", ballPX, "0"); } + if(ballPY == 0) { UpdateScreen(screen1, ballPX, ball, 0, ""); } + if(ballPY == 1) { UpdateScreen(screen1, 0, "", ballPX, ball); } + if(ballPY == 8) { UpdateScreen(screen2, ballPX, ball, pLocation, player); } + if(ballPY == 9) { UpdateScreen(screen2, 0, "", ballPX, ball); } } else //only draw player and enermy { ClearAllScreen(); + UpdateScreen(screen2, 0, "", pLocation, player); } } @@ -449,4 +491,188 @@ range2->triggerRead(); wait_ms(49); r2 = range2->read(); +} + +void Clock() { + + questionScreen.write(1); + screen1 = 0; + screen2 = 0; + + boop.write(0); //initialise buzzer + boop.period(5); + + Ticker cloo; //declare a ticker to update clock every sec + cloo.attach(&displayTime, 1); //the ticker runs the function that wipes the screen and displays the updated time + + rtc.startClock(); + + while(1){ + + wait_ms(50); //wait so button states do not overlap + + if(pin.read()){ //once it's 1 start the detection loop to decide whether this is a hold or a double click + + cloo.detach(); //stop the ticker while performing button operations + + t.reset(); + t.start(); //start timer + + while(t.read() < 1){ //if it's not unpressed in a second - HOLD case + + if(!pin.read()){ //if it goes low, check for double click + + t.reset(); + + while(t.read() < 1){ //listen for 1 sec for a second click + + if(pin.read()){ //if x = 1 execute DOUBLE CLICK - set alarm + lcd.cls(); + lcd.printf("settin' alarm!"); + wait(2); + + Halrm = setAlrm(true); + if(Halrm >= 0){ + Malrm = setAlrm(false); + lcd.cls(); + lcd.printf("Alarm: %d:%d", Halrm,Malrm); + wait(2); + } + break; + } + } + break; + } + } + + + + while(pin.read()){ //still pressed? Execute Button HOLD case, exit hold when no longer held + + rtc.getTime(tim); + + lcd.cls(); + lcd.printf("Today is the :\n%02d/%02d/%02d", tim.date, tim.mon, tim.year); //display date + wait(1); + + } + + cloo.attach(&displayTime, 1); //re-enable ticker after date release + + } + + t.stop(); + + } + +} + +void displayTime(){ //get the time from the rtc chip and display it on the LCD + + Rtc_Ds1307::Time_rtc tm = tim; + + if (rtc.getTime(tm) ) { + lcd.cls(); + lcd.printf("The time is :\n%02d:%02d:%02d", tm.hour, tm.min, tm.sec); //display time + + if(hitIt(Halrm, Malrm, tm)){ //check if the time is right for alarmage + boop.write(0.5); + while(!pin.read()){ + lcd.cls(); + lcd.printf("You should be alarmed"); + wait(0.2); + } + boop.write(0); + } + + } + red = !red; +} + +int setAlrm(bool ho){ + + int mod; + float spd; + if(ho){ + mod = 24; + spd = 0.5; + }else{ + mod = 60; + spd = 0.25; + } + + int homin = 0; + bool sat = false; + bool DC = false; + + while(!sat){ + + while(!pin.read() && !sat){ + + lcd.cls(); + if(ho) lcd.printf("Hour: %02d\nDC to confirm", homin); + else lcd.printf("Minutes: %02d\nDC to confirm", homin); + + wait(0.2); + if(pin.read()){ //once it's 1 start the detection loop to decide whether this is a hold or a double click + + t.reset(); + t.start(); //start timer + + while(t.read() < 1){ //if it's not unpressed in a second - HOLD case + + if(!pin.read()){ //if it goes low, check for double click + + t.reset(); + + while(t.read() < 1){ //listen for 1 sec for a second click + + if(pin.read()){ //if x = 1 execute DOUBLE CLICK - confirm selection + lcd.cls(); + if(ho) lcd.printf("Hour: %02d\nSet!", homin); + else lcd.printf("Minutes: %02d\nSet!", homin); + sat = true; + DC = true; + wait(2); + break; + } + } + break; + } + } + if(!pin.read() && !DC){ //if unpressed - single click detected - cancel alarm + lcd.cls(); + lcd.printf("Alarm Canceled!"); + homin = -1; + return homin; + } + + while(pin.read()){ //still pressed? Execute Button HOLD case, exit hold when no longer held + + homin = (homin + 1)%mod; //increment the value while the button is held + lcd.cls(); + if(ho) lcd.printf("Hour: %02d", homin); + else lcd.printf("Minutes: %02d", homin); + wait(spd); + + } + } + } + + } + + return homin; +} + +//this function checks whether it's time to sound the alarms +//if the minute and the hour is the same as that of the alarm, return true +bool hitIt(int hor, int min, Rtc_Ds1307::Time_rtc teim){ + int x = teim.hour - hor; + if(x == 0){ + int y = teim.min - min; + if(y == 0){ + return true; + } + } + return false; } \ No newline at end of file