Display text on screen.

Dependencies:   TextLCD mbed MaxSonar RTC-DS1307

Fork of TextLCD_HelloWorld by Simon Ford

main.cpp

Committer:
aueangpanit
Date:
2017-05-24
Revision:
5:2f13ec8efe0b
Parent:
4:c669026b6f6e
Child:
6:fa30f8383b99

File content as of revision 5:2f13ec8efe0b:

// 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(PTB8);
DigitalOut screen1(PTE5);
DigitalOut screen2(PTE4);

void UpdateScreen(DigitalOut screen, string text);
void UpdateScreen(DigitalOut screen, char text[1024]);

int main() {
    UpdateScreen(screen1, "Hello!");
    
    MaxSonar *range1;
    float r1;
 
    // 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);
    
    
 
    while(1) {
        // Trigger read, wait 49ms until ranger finder has
        // finished, then read. 
        range1->triggerRead();
        wait_ms(49);
        r1 = range1->read();
      
        // Print and delay 0.5s.
        char str[1024];
        sprintf (str, "Range: %.3f cm\n", r1);
        UpdateScreen(screen2, str);
        
        wait(0.5);
    }
}

void UpdateScreen(DigitalOut screen, string text)
{
    //disable all E pin for all screens
    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
    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);
    }
}