Skip to content

Commit

Permalink
Added hardware serial support, back compatible w/Arduino 0023
Browse files Browse the repository at this point in the history
  • Loading branch information
PaintYourDragon committed Apr 27, 2012
1 parent 47b173d commit bd99b45
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 140 deletions.
128 changes: 65 additions & 63 deletions Adafruit_GPS.cpp
@@ -1,11 +1,11 @@
/***********************************
This is a our GPS library
This is our GPS library
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above must be included in any redistribution
****************************************/
Expand Down Expand Up @@ -161,93 +161,98 @@ boolean Adafruit_GPS::parse(char *nmea) {
char Adafruit_GPS::read(void) {
char c = 0;

if (paused)
return c;
if (paused) return c;


if (gpsSwSerial->available()) {
if(gpsSwSerial) {
if(!gpsSwSerial->available()) return c;
c = gpsSwSerial->read();
} else {
if(!gpsHwSerial->available()) return c;
c = gpsHwSerial->read();
}

//Serial.print(c);
//Serial.print(c);

if (c == '$') {
currentline[lineidx] = 0;
lineidx = 0;
}
if (c == '\n') {
currentline[lineidx] = 0;

if (currentline == line1) {
currentline = line2;
lastline = line1;
} else {
currentline = line1;
lastline = line2;
}


//Serial.println("----");
//Serial.println((char *)lastline);
//Serial.println("----");
lineidx = 0;
recvdflag = true;
if (c == '$') {
currentline[lineidx] = 0;
lineidx = 0;
}
if (c == '\n') {
currentline[lineidx] = 0;

if (currentline == line1) {
currentline = line2;
lastline = line1;
} else {
currentline = line1;
lastline = line2;
}

currentline[lineidx++] = c;
if (lineidx >= MAXLINELENGTH)
lineidx = MAXLINELENGTH-1;

//Serial.println("----");
//Serial.println((char *)lastline);
//Serial.println("----");
lineidx = 0;
recvdflag = true;
}

currentline[lineidx++] = c;
if (lineidx >= MAXLINELENGTH)
lineidx = MAXLINELENGTH-1;

return c;
}

// Constructor when using SoftwareSerial or NewSoftSerial
#if ARDUINO >= 100
Adafruit_GPS::Adafruit_GPS(SoftwareSerial *ser)
#else
Adafruit_GPS::Adafruit_GPS(NewSoftSerial *ser)
Adafruit_GPS::Adafruit_GPS(NewSoftSerial *ser)
#endif
{
common_init(); // Set everything to common state, then...
gpsSwSerial = ser; // ...override swSerial with value passed.

recvdflag = false;
paused = false;
lineidx = 0;
currentline = line1;
lastline = line2;

hour = minute = seconds = year = month = day = milliseconds = 0;
latitude = longitude = geoidheight = altitude = 0;
speed = angle = magvariation = HDOP = 0;
lat = lon = mag = 0;
fix = false;
fixquality = satellites = 0;
common_init(); // Set everything to common state, then...
gpsSwSerial = ser; // ...override gpsSwSerial with value passed.
}


void Adafruit_GPS::begin(uint16_t baud)
{
gpsSwSerial->begin(baud);
// Constructor when using HardwareSerial
Adafruit_GPS::Adafruit_GPS(HardwareSerial *ser) {
common_init(); // Set everything to common state, then...
gpsHwSerial = ser; // ...override gpsHwSerial with value passed.
}

// Initialization code used by all constructor types
void Adafruit_GPS::common_init(void) {
gpsSwSerial = NULL;
gpsHwSerial = NULL;
gpsSwSerial = NULL; // Set both to NULL, then override correct
gpsHwSerial = NULL; // port pointer in corresponding constructor
recvdflag = false;
paused = false;
lineidx = 0;
currentline = line1;
lastline = line2;

hour = minute = seconds = year = month = day =
fixquality = satellites = 0; // uint8_t
lat = lon = mag = 0; // char
fix = false; // boolean
milliseconds = 0; // uint16_t
latitude = longitude = geoidheight = altitude =
speed = angle = magvariation = HDOP = 0.0; // float
}

void Adafruit_GPS::begin(uint16_t baud)
{
if(gpsSwSerial) gpsSwSerial->begin(baud);
else gpsHwSerial->begin(baud);
}

void Adafruit_GPS::sendCommand(char *str) {
gpsSwSerial->println(str);
if(gpsSwSerial) gpsSwSerial->println(str);
else gpsHwSerial->println(str);
}



boolean Adafruit_GPS::newNMEAreceived(void) {
return recvdflag;
}


void Adafruit_GPS::pause(boolean p) {
paused = p;
}
Expand All @@ -257,7 +262,6 @@ char *Adafruit_GPS::lastNMEA(void) {
return (char *)lastline;
}


// read a Hex value and return the decimal equivalent
uint8_t Adafruit_GPS::parseHex(char c) {
if (c < '0')
Expand All @@ -270,7 +274,6 @@ uint8_t Adafruit_GPS::parseHex(char c) {
return (c - 'A')+10;
}


boolean Adafruit_GPS::waitForSentence(char *wait4me, uint8_t max) {
char str[20];

Expand All @@ -296,7 +299,6 @@ boolean Adafruit_GPS::LOCUS_StartLogger(void) {
return waitForSentence(PMTK_LOCUS_LOGSTARTED);
}


boolean Adafruit_GPS::LOCUS_ReadStatus(void) {
sendCommand(PMTK_LOCUS_QUERY_STATUS);

Expand Down
4 changes: 2 additions & 2 deletions examples/blank/blank.ino → examples/blank/blank.pde
@@ -1,5 +1,5 @@
// this sketch will allow you to bypass the Atmega chip
// and connect the fingerprint sensor directly to the USB/Serial
// and connect the GPS sensor directly to the USB/Serial
// chip converter.

// Connect VIN to +5V
Expand All @@ -8,4 +8,4 @@
// Connect GPS TX (data out from GPS) to Digital 1

void setup() {}
void loop() {}
void loop() {}
39 changes: 27 additions & 12 deletions examples/echo/echo.pde
Expand Up @@ -10,22 +10,36 @@
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>

// these are for Arduino 1.0
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);

// if using Arduino v23 or earlier, uncomment these
// two lines and comment out the above. You will
// need to install NewSoftSerial
// #include <NewSoftSerial.h>
// NewSoftSerial mySerial(3, 2);
#if ARDUINO >= 100
#include <SoftwareSerial.h>
#else
// Older Arduino IDE requires NewSoftSerial, download from:
// http://arduiniana.org/libraries/newsoftserial/
#include <NewSoftSerial.h>
// DO NOT install NewSoftSerial if using Arduino 1.0 or later!
#endif

// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// Connect the GPS TX (transmit) pin to Digital 3
// Connect the GPS RX (receive) pin to Digital 2
// If using software serial (sketch example default):
// Connect the GPS TX (transmit) pin to Digital 3
// Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
// Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
// Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
#if ARDUINO >= 100
SoftwareSerial mySerial(3, 2);
#else
NewSoftSerial mySerial(3, 2);
#endif
Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);


// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
Expand All @@ -34,6 +48,7 @@ Adafruit_GPS GPS(&mySerial);
// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy

void setup()
{
Expand Down
Expand Up @@ -11,22 +11,35 @@
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>

// these are for Arduino 1.0
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);

// if using Arduino v23 or earlier, uncomment these
// two lines and comment out the above. You will
// need to install NewSoftSerial
// #include <NewSoftSerial.h>
// NewSoftSerial mySerial(3, 2);
#if ARDUINO >= 100
#include <SoftwareSerial.h>
#else
// Older Arduino IDE requires NewSoftSerial, download from:
// http://arduiniana.org/libraries/newsoftserial/
#include <NewSoftSerial.h>
// DO NOT install NewSoftSerial if using Arduino 1.0 or later!
#endif

// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// Connect the GPS TX (transmit) pin to Digital 3
// Connect the GPS RX (receive) pin to Digital 2
// If using software serial (sketch example default):
// Connect the GPS TX (transmit) pin to Digital 3
// Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
// Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
// Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
#if ARDUINO >= 100
SoftwareSerial mySerial(3, 2);
#else
NewSoftSerial mySerial(3, 2);
#endif
Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);

void setup()
{
Expand All @@ -39,7 +52,8 @@ void setup()
GPS.begin(9600);

GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_OFF);


// If using hardware serial (e.g. Arduino Mega), change this to Serial1, etc.
while (mySerial.available())
mySerial.read();

Expand All @@ -51,10 +65,10 @@ void setup()

void loop() // run over and over again
{
// If using hardware serial (e.g. Arduino Mega), change this to Serial1, etc.
if (mySerial.available()) {
char c = mySerial.read();
if (c) UDR0 = c;
}
}


39 changes: 27 additions & 12 deletions examples/locus_start/locus_start.pde
Expand Up @@ -11,22 +11,36 @@
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>

// these are for Arduino 1.0
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);

// if using Arduino v23 or earlier, uncomment these
// two lines and comment out the above. You will
// need to install NewSoftSerial
// #include <NewSoftSerial.h>
// NewSoftSerial mySerial(3, 2);
#if ARDUINO >= 100
#include <SoftwareSerial.h>
#else
// Older Arduino IDE requires NewSoftSerial, download from:
// http://arduiniana.org/libraries/newsoftserial/
#include <NewSoftSerial.h>
// DO NOT install NewSoftSerial if using Arduino 1.0 or later!
#endif

// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// Connect the GPS TX (transmit) pin to Digital 3
// Connect the GPS RX (receive) pin to Digital 2
// If using software serial (sketch example default):
// Connect the GPS TX (transmit) pin to Digital 3
// Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
// Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
// Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
#if ARDUINO >= 100
SoftwareSerial mySerial(3, 2);
#else
NewSoftSerial mySerial(3, 2);
#endif
Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);


// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
Expand All @@ -35,6 +49,7 @@ Adafruit_GPS GPS(&mySerial);
// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy

void setup()
{
Expand Down

0 comments on commit bd99b45

Please sign in to comment.