Dependencies:   4DGL-uLCD-SE NixieTube MBed_Adafruit-GPS-Library NetServices wave_player mbed spxml

Final Project for ECE 4180

main.cpp

Committer:
fischk08
Date:
2016-12-05
Revision:
0:b35237c80489
Child:
1:a6fbd8341289

File content as of revision 0:b35237c80489:

// Notes https://developer.mbed.org/users/edodm85/notebook/HC-05-bluetooth/
// https://developer.mbed.org/cookbook/Bluetooth-Android-Controlled-MBED  
// https://developer.mbed.org/users/mlee350/notebook/adafruit-ultimate-gps-breakout-board/
// https://developer.mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/
// https://developer.mbed.org/handbook/LocalFileSystem

// Includes Files
#include "mbed.h"
#include "rtos.h"
#include <string>
#include "uLCD_4DGL.h"
#include "wave_player.h"
#include "MBed_Adafruit_GPS.h" // GPS Unit 
#include "spdomparser.hpp" /// Parseing Weather
#include "spxmlnode.hpp" /// Parseing Weather
#include "spxmlhandle.hpp" /// Parseing Weather
#include <TimeUtilities.h> // Used for Clock

#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include <string>


#include "NixieTube.h"
NixieTube test(p23,p24,p25,p26);
/*
int main() {
    test.start_nixie();
    test.update_nixie_tube(1,2);
    test.set_dim(1.0,200);
}
*/


EthernetNetIf eth;
HTTPClient http;
HTTPResult result;

LocalFileSystem local("local");               // Create the local filesystem under the name "local"
AnalogOut DACout(p18);                 // DAC for producing sound
wave_player waver(&DACout);            // wavplayer object
Serial * gps_Serial; // GPS Interface
uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;

//LCD Files Needs to be local to the ulcd screen

// Need To Make a few changes here 
// Weather fucntion to grab local weather data
// Alarm // 
// Clock need to take time and output it to the nixe tubes hr mins
// Initilizing?
//      GPS Setup
//      Confirm Link On ULCD Screen
//      Confirm WIFI setup On ULCD Screen
//      Confirm Bluetoth link? display bluetooth decal when connected? Or just Letter B in Blue just as good
    
// Declarations 

bool pressPlay = false;//default on pause (song select mode)
int volume = 0; 

/// Functions will follow here
void display_screen_debug() {
    }
    
void system_setup () {
    
    }
bool completed = false;

    
void display_weather(int code){
        uLCD.flush_media();
        uLCD.cls();
        uLCD.media_init();
        //uLCD.printf("\n\nAn SD card is needed for image and video data");
        uLCD.set_sector_address(0x0000,(code*65));
        uLCD.display_image(0,0);
        uLCD.cls();
    }    
void request_callback(HTTPResult r)
{
    result = r;
    completed = true;
}

void parseWeather(SP_XmlElementNode *node, string loc)
{
    //extracts current weather XML data fields for LCD
    SP_XmlHandle handle(node);
    SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement();
    //display Weather conditions
    display_weather((int)condition->getAttrValue("code"));
    // Print the name of the city
    uLCD.locate(0,1);
    uLCD.printf("%s:", loc);
    //Print the weather conditions
    uLCD.locate(0,2);
    uLCD.printf("%s",condition->getAttrValue("text"));
    //Print the termperature (in degrees Celcius)
    uLCD.locate(0,3);
    uLCD.printf("%sF",condition->getAttrValue("temp"));
}
// Need To update so that this takes lat longitude gives it to a website to find local time and or time zone
void parseWhereAmI(SP_XmlElementNode *node, string loc)
{
    //extracts current weather XML data fields for LCD
    SP_XmlHandle handle(node);
    SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement();
    //display Weather conditions
    display_weather((int)condition->getAttrValue("code"));
    // Print the name of the city
    uLCD.locate(0,1);
    uLCD.printf("%s:", loc);
    //Print the weather conditions
    uLCD.locate(0,2);
    uLCD.printf("%s",condition->getAttrValue("text"));
    //Print the termperature (in degrees Celcius)
    uLCD.locate(0,3);
    uLCD.printf("%sF",condition->getAttrValue("temp"));
   
}

// Query Term  for Weather 
// select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22)
// Query Website https://developer.yahoo.com/yql/console/#h=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22)
//asuming that outputformat of lattitude and logitude is - for west + for east and does not use chars  for directionals as that would be dumb
//"https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22)", &stream, request_callback);
// "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(33.7490%2C-84.3880)%22)"(lat  log) &stream, request_callback)

// Variable Version
//  "https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(lat%2Clong)%22)", &stream, request_callback);
//  "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(%.6f%2C%.6f)%22)"(lat  log) &stream, request_callback)

int weather()
{
    completed = false;
    SP_XmlDomParser parser;
    HTTPStream stream;     
    char buffer [240];
    int n;
    float lat = 33.7490;
    float log = -84.3880;
    n=sprintf(buffer, "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(%.6f%2C%.6f)%22)", lat, log);
    char BigBuf[512 + 1] = {0};
    uLCD.printf(buffer);
    stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
    //Yahoo! weather API for selected city - get XML document for parsing
    HTTPResult r = http.get(buffer, &stream, request_callback); // This is wehre the http query for the local weather occurs 
    while (!completed) {
        Net::poll(); //Polls the Networking stack
        if (stream.readable()) {
            BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
            parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
            stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
        }
    }
    uLCD.locate(0,6);
    if (result == HTTP_OK) {
        uLCD.printf("Weather complete");
    } else {
        uLCD. printf("Weather Error %d", result);
        return -1;
    }

    SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
    SP_XmlElementNode * child2 = rootHandle.getChild( "results" ).getChild( "channel" ).toElement();

    //uLCD.printf(BigBuf);

    if ( child2 ) {
        parseWeather(child2, "Atlanta"); //parses XML "current-conditions" info
    }

    if ( NULL != parser.getError() ) {
        uLCD.printf( "\n error: %s\n", parser.getError() );
    }
    return 0;
}


// Need to update so that if it failes to get a GPS fix after say 10 tries it defaults lat and longitude values and time is found thought another method.
bool  gps_setup(){
    Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
    const int refresh_Time = 2000; //refresh time in ms
    bool gps_off = true;
    char c;  
    gps_Serial = new Serial(p9,p10); //serial object for use w/ GPS tx rx
    Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
    myGPS.begin(9600);  //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
    myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
    myGPS.sendCommand(PGCMD_ANTENNA);
    refresh_Timer.start();  //starts the clock on the timer
    uLCD.printf("Starting Up\n");
    while(gps_off){
        uLCD.cls();
        c = myGPS.read();   //queries the GPS
        if (c) { uLCD.printf("%c", c); } //this line will echo the GPS data if not paused
        //check if we recieved a new message from GPS, if so, attempt to parse it,
        if ( myGPS.newNMEAreceived() ) {
            if ( !myGPS.parse(myGPS.lastNMEA()) ) {
                continue;   
            }    
        }
        //check if enough time has passed to warrant printing GPS info to screen
        //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
        if (refresh_Timer.read_ms() >= refresh_Time) {
            refresh_Timer.reset();
            uLCD.printf("Fix: %d\n", (int) myGPS.fix);
            uLCD.printf("Quality: %d\n", (int) myGPS.fixquality);
            if (myGPS.fix) {
                uLCD.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);   
                uLCD.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
                uLCD.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
                uLCD.printf("Speed: %5.2f knots\n", myGPS.speed);
                uLCD.printf("Angle: %5.2f\n", myGPS.angle);
                uLCD.printf("Altitude: %5.2f\n", myGPS.altitude);
                uLCD.printf("Satellites: %d\n", myGPS.satellites);
                gps_off = false;
            }
        }
    }
    return true;
}    

void sound_alarm(int volume) {
        pressPlay = true;
        FILE *wave_file = fopen("/local/alarm.wav","r");
        waver.play(wave_file,&pressPlay, &volume); //play(FILE *wavefile,bool *pressPlay, int *volume);
        pressPlay = false;
        fclose(wave_file); 
        }
        
RealTimeClock rtc;
 
 void main_test() {
     bool success;
     char TimeTest[] = "05/29/2011 14:15:18 (-6:00)";
     char buf[50];
     success = rtc.SetTime(TimeTest);
     rtc.GetTimeString(buf);
     uLCD.printf("success: %i, time: %s\n", success, buf);
 }       
        
int main() {
    //sound_alarm(1);
    //display_weather(0);
    
    
    }