Display text on screen.

Dependencies:   TextLCD mbed MaxSonar RTC-DS1307

Fork of TextLCD_HelloWorld by Simon Ford

main.cpp

Committer:
aueangpanit
Date:
2017-05-25
Revision:
8:8ce2ab8191cf
Parent:
7:f00ddf85cacc
Child:
9:39190ed25585

File content as of revision 8:8ce2ab8191cf:

// Hello World! for the TextLCD

#include "mbed.h"
#include <string>
#include "TextLCD.h"
#include "MaxSonar.h"
using std::string;

TextLCD lcd(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20); // rs, e, d4-d7

DigitalOut questionScreen(PTE3);
DigitalOut screen1(PTE5);
DigitalOut screen2(PTE4);

void UpdateScreen(DigitalOut screen, string text);
void UpdateScreen(DigitalOut screen, char text[1024]);
void DisplayNewQuestion();
bool CheckCorrect(int screenNumber);

string questions[4] = {"What is 2*2?", 
                        "What is 5*2?", 
                        "what is 2+2?",
                        "What is 5*4?"};

string screen1Answers[4] = {"4    ", "10    ", "5    ", "18    "};
string screen2Answers[4] = {"5    ", "8    ", "4    ", "20    "};
int correctScreen[4] = {1, 1, 2, 2}; //1 = screen1; 2 = screen2;

int questionCount = 0;

int main() {
    UpdateScreen(screen2, "Hello!");
    
    MaxSonar *range1;
    float r1;
    
    MaxSonar *range2;
    float r2;
 
    // Create and configure object for 3.3V powered LV-series device, 
    // accessed with analog reads (in cm) on p16, triggered by p7.
    range1 = new MaxSonar(MS_LV, MS_ANALOG, PTB8, PTC2);
    range1->setVoltage(3.3);
    range1->setUnits(MS_CM);
    
    range2 = new MaxSonar(MS_LV, MS_ANALOG, PTB9, PTB3);
    range2->setVoltage(3.3);
    range2->setUnits(MS_CM);
 
    while(1) {
        // Trigger read, wait 49ms until ranger finder has
        // finished, then read. 
        range1->triggerRead();
        wait_ms(49);
        r1 = range1->read();
        
        range2->triggerRead();
        wait_ms(49);
        r2 = range2->read();
      
        if(r1 < 20)
        {
            if(CheckCorrect(1))
            {
                UpdateScreen(questionScreen, "Correct!");
                wait(1);
                DisplayNewQuestion();
            }
            else
            {
                UpdateScreen(questionScreen, "Try Again!");
                wait(1);
                UpdateScreen(questionScreen, questions[questionCount]);
            }
        }
        if(r2 < 20)
        {
            if(CheckCorrect(2))
            {
                UpdateScreen(questionScreen, "Correct!");
                wait(1);
                DisplayNewQuestion();
            }
            else
            {
                UpdateScreen(questionScreen, "Try Again!");
                wait(1);
                UpdateScreen(questionScreen, questions[questionCount]);
            }
        }    
        
        /* 
        // Print and delay 0.5s.
        char range1_char_array[1024];
        char range2_char_array[1024];
        sprintf (range1_char_array, "Range: %.3f cm\n", r1);
        sprintf (range2_char_array, "Range: %.3f cm\n", r2);
        UpdateScreen(questionScreen, range1_char_array);
        UpdateScreen(screen2, range2_char_array);
        */
        
        wait(0.5);
    }
}

void DisplayNewQuestion()
{
    if(questionCount >= 3)
    {
        questionCount = 0;    
    }
    else
    {
        questionCount++;    
    }
    
    UpdateScreen(questionScreen, questions[questionCount]);
    UpdateScreen(screen1, screen1Answers[questionCount]);
    UpdateScreen(screen2, screen2Answers[questionCount]);
}

bool CheckCorrect(int screenNumber)
{
    if(screenNumber == correctScreen[questionCount])
    {
        return true;    
    }
    else
    {
        return false;    
    }
}

void UpdateScreen(DigitalOut screen, string text)
{
    //disable all E pin for all screens
    questionScreen = 0;
    screen1 = 0;
    screen2 = 0;
        
    //enable E pin for the scrren that we want to update
    screen = 1;
    
    //convert text to char array
    char text_char_array[1024];
    strcpy(text_char_array, text.c_str());
    //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
    for(int i = 0; i < 10; i++)
    {
        lcd.cls();
        lcd.printf(text_char_array);
    }
}

void UpdateScreen(DigitalOut screen, char text[1024])
{
    //disable all E pin for all screens
    questionScreen = 0;
    screen1 = 0;
    screen2 = 0;
        
    //enable E pin for the scrren that we want to update
    screen = 1;
    
    //some weird behaviour after disabling the E pin once means that we need to update the screen several times for it to display properly
    for(int i = 0; i < 10; i++)
    {
        lcd.cls();
        lcd.printf(text);
    }
}