[Edit:] You can download code for this post here.
I created a new project based on the example code given previously. The first class I called GPS. The second class I replaced with NewSoftSerial which can be downloaded here. So the tabs in my Arduino editor are:
- Main sketch (.PDE)
- GPS.cpp
- GPS.h
- NewSoftSerial.cpp
- NewSoftSerial.h
#ifndef GPS_h
#define GPS_h
#include "NewSoftSerial.h"
class GPS
{
public:
GPS(int pinReceive, int pinTransmit, int baudRate);
char getChar();
private:
NewSoftSerial gpsSerial;
};
#endif
The GPS.cpp file looks like this:
#include "GPS.h"
GPS::GPS(int pinReceive, int pinTransmit, int baudRate) : gpsSerial(pinReceive, pinTransmit)
{
gpsSerial.begin(baudRate);
}
char GPS::getChar()
{
while(!gpsSerial.available());
return gpsSerial.read();
}
And the main sketch (.PDE file) is this:
#include "GPS.h"
// Pinouts: http://shieldlist.org/sparkfun/gps
#define GPS_TRANSMIT_PIN 2
#define GPS_RECEIVE_PIN 3
#define GPS_BAUD 4800
GPS gps(GPS_RECEIVE_PIN, GPS_TRANSMIT_PIN, GPS_BAUD);
void setup()
{
Serial.begin(115200);
Serial.println("setup()");
}
void loop()
{
Serial.print(gps.getChar());
}
Not a lot of code. And what does it do? Well, it just dumps GPS data to the serial monitor. At this stage we want to know we have reliable data coming out of the device and that the pin connections and base code all works. Here's an output sample:
There really isn't much code yet, but I'll do a walk-through on the code anyway.
The main file includes GPS.h and not NewSoftSerial. As discussed previously, including all your header files at the main sketch level is not required as long as you understand and use initialiser lists where necessary. I've used a list of defines to keep track of pin assignments and things like the baud rate. I think as the project progresses I'll keep all the pins defined in the main sketch so it's easy to see if there's a conflict. The GPS object gps is declared and instantiated next.
The setup() function just kicks off the serial port for the serial monitor. And loop() makes repeated calls to the getChar() function on the gps object and prints it to the serial monitor. Voila.
GPS.h shows an include for NewSoftSerial. There's the signature for the constructor which takes the pins for receive and transmit in that order and then the baud rate. The getChar() function returns a single character, as you'd expect. Oh, and in the private: section, you can see the declaration of the variable gpsSerial which will be the object through which the gps object accesses the serial port for the GPS device.
In GPS.cpp you can see the constructor (GPS::GPS) passes out the receive/transmit pin numbers in the initialiser list (after the colon ":" in the top line) and then calls the begin() function, passing the baud rate that way. Other than that, there's the implementation for the getChar() function which shows a while statement repeats endlessly until data becomes available, then the return statement reads the available character and returns it. The loop() function in the main sketch calls getChar() as soon as it completes so every character output from the GPS device is printed to the serial monitor as soon as it becomes available.
The next goal is to collect the serial data a sentence at a time, validate it, and then disassemble it for the discrete data it contains. Initially I want to get the current UTC time. For this I'm reading the EM-406A User Manual and the NMEA Reference Manual.
That is all.
Thanks for writing these tutorials! I was having problems getting my classes to use other classes, and your examples solved my problems.
ReplyDeleteGary
Thank you very much for the tutorial. I was trying to make a gsm module work for the whole morning... It was after seeing your complete example when I realized that my classes were correctly defined and the problem is a pesky delay function inside the constructor...
ReplyDelete