From bd99b452fd4d8c1a0b4a910becda98494a083574 Mon Sep 17 00:00:00 2001 From: Paint Your Dragon Date: Fri, 27 Apr 2012 13:49:18 -0700 Subject: [PATCH] Added hardware serial support, back compatible w/Arduino 0023 --- Adafruit_GPS.cpp | 128 +++++++++--------- examples/blank/{blank.ino => blank.pde} | 4 +- examples/echo/echo.pde | 39 ++++-- ...ocus_dumpbasic.ino => locus_dumpbasic.pde} | 42 ++++-- examples/locus_start/locus_start.pde | 39 ++++-- examples/locus_status/locus_status.pde | 51 ++++--- examples/parsing/parsing.pde | 52 ++++--- 7 files changed, 215 insertions(+), 140 deletions(-) rename examples/blank/{blank.ino => blank.pde} (75%) rename examples/locus_dumpbasic/{locus_dumpbasic.ino => locus_dumpbasic.pde} (53%) diff --git a/Adafruit_GPS.cpp b/Adafruit_GPS.cpp index 3689f33..c0b45ab 100644 --- a/Adafruit_GPS.cpp +++ b/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 ****************************************/ @@ -161,42 +161,44 @@ 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; } @@ -204,50 +206,53 @@ char Adafruit_GPS::read(void) { #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; } @@ -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') @@ -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]; @@ -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); diff --git a/examples/blank/blank.ino b/examples/blank/blank.pde similarity index 75% rename from examples/blank/blank.ino rename to examples/blank/blank.pde index d72663b..016ccb6 100644 --- a/examples/blank/blank.ino +++ b/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 @@ -8,4 +8,4 @@ // Connect GPS TX (data out from GPS) to Digital 1 void setup() {} -void loop() {} \ No newline at end of file +void loop() {} diff --git a/examples/echo/echo.pde b/examples/echo/echo.pde index 586435d..8444f00 100644 --- a/examples/echo/echo.pde +++ b/examples/echo/echo.pde @@ -10,22 +10,36 @@ // and help support open source hardware & software! -ada #include - -// these are for Arduino 1.0 -#include -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 mySerial(3, 2); +#if ARDUINO >= 100 + #include +#else + // Older Arduino IDE requires NewSoftSerial, download from: + // http://arduiniana.org/libraries/newsoftserial/ + #include + // 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 @@ -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() { diff --git a/examples/locus_dumpbasic/locus_dumpbasic.ino b/examples/locus_dumpbasic/locus_dumpbasic.pde similarity index 53% rename from examples/locus_dumpbasic/locus_dumpbasic.ino rename to examples/locus_dumpbasic/locus_dumpbasic.pde index 07fccba..4147fec 100644 --- a/examples/locus_dumpbasic/locus_dumpbasic.ino +++ b/examples/locus_dumpbasic/locus_dumpbasic.pde @@ -11,22 +11,35 @@ // and help support open source hardware & software! -ada #include - -// these are for Arduino 1.0 -#include -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 mySerial(3, 2); +#if ARDUINO >= 100 + #include +#else + // Older Arduino IDE requires NewSoftSerial, download from: + // http://arduiniana.org/libraries/newsoftserial/ + #include + // 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() { @@ -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(); @@ -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; } } - diff --git a/examples/locus_start/locus_start.pde b/examples/locus_start/locus_start.pde index 767c9c0..ba5adb6 100644 --- a/examples/locus_start/locus_start.pde +++ b/examples/locus_start/locus_start.pde @@ -11,22 +11,36 @@ // and help support open source hardware & software! -ada #include - -// these are for Arduino 1.0 -#include -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 mySerial(3, 2); +#if ARDUINO >= 100 + #include +#else + // Older Arduino IDE requires NewSoftSerial, download from: + // http://arduiniana.org/libraries/newsoftserial/ + #include + // 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 @@ -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() { diff --git a/examples/locus_status/locus_status.pde b/examples/locus_status/locus_status.pde index 4f45d2f..0ca5e82 100644 --- a/examples/locus_status/locus_status.pde +++ b/examples/locus_status/locus_status.pde @@ -11,22 +11,36 @@ // and help support open source hardware & software! -ada #include - -// these are for Arduino 1.0 -#include -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 mySerial(3, 2); +#if ARDUINO >= 100 + #include +#else + // Older Arduino IDE requires NewSoftSerial, download from: + // http://arduiniana.org/libraries/newsoftserial/ + #include + // 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 @@ -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() { @@ -89,17 +104,17 @@ void loop() // run over and over again if (GPS.LOCUS_mode & 0x10) Serial.print(" Distance"); if (GPS.LOCUS_mode & 0x20) Serial.print(" Speed"); - Serial.print(", Content "); Serial.print(GPS.LOCUS_config); - Serial.print(", Interval "); Serial.print(GPS.LOCUS_interval); - Serial.print(" sec, Distance "); Serial.print(GPS.LOCUS_distance); - Serial.print(" m, Speed "); Serial.print(GPS.LOCUS_speed); + Serial.print(", Content "); Serial.print((int)GPS.LOCUS_config); + Serial.print(", Interval "); Serial.print((int)GPS.LOCUS_interval); + Serial.print(" sec, Distance "); Serial.print((int)GPS.LOCUS_distance); + Serial.print(" m, Speed "); Serial.print((int)GPS.LOCUS_speed); Serial.print(" m/s, Status "); if (GPS.LOCUS_status) Serial.print("LOGGING, "); else Serial.print("OFF, "); - Serial.print(GPS.LOCUS_records); Serial.print(" Records, "); - Serial.print(GPS.LOCUS_percent); Serial.print("% Used "); + Serial.print((int)GPS.LOCUS_records); Serial.print(" Records, "); + Serial.print((int)GPS.LOCUS_percent); Serial.print("% Used "); } } diff --git a/examples/parsing/parsing.pde b/examples/parsing/parsing.pde index 9ce12d9..90acd3b 100644 --- a/examples/parsing/parsing.pde +++ b/examples/parsing/parsing.pde @@ -12,31 +12,45 @@ // and help support open source hardware & software! -ada #include +#if ARDUINO >= 100 + #include +#else + // Older Arduino IDE requires NewSoftSerial, download from: + // http://arduiniana.org/libraries/newsoftserial/ + #include + // DO NOT install NewSoftSerial if using Arduino 1.0 or later! +#endif -// these are for Arduino 1.0 -#include -SoftwareSerial mySerial(3, 2); +// Connect the GPS Power pin to 5V +// Connect the GPS Ground pin to ground +// 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); -// if using Arduino v23 or earlier, uncomment these -// two lines and comment out the above. You will -// need to install NewSoftSerial -// #include -// NewSoftSerial mySerial(3, 2); // 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 #define GPSECHO true -// 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 -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() { @@ -136,8 +150,8 @@ void loop() // run over and over again Serial.print(GPS.day, DEC); Serial.print('/'); Serial.print(GPS.month, DEC); Serial.print("/20"); Serial.println(GPS.year, DEC); - Serial.print("Fix: "); Serial.print(GPS.fix); - Serial.print(" quality: "); Serial.println(GPS.fixquality); + Serial.print("Fix: "); Serial.print((int)GPS.fix); + Serial.print(" quality: "); Serial.println((int)GPS.fixquality); if (GPS.fix) { Serial.print("Location: "); Serial.print(GPS.latitude, 4); Serial.print(GPS.lat); @@ -147,7 +161,7 @@ void loop() // run over and over again Serial.print("Speed (knots): "); Serial.println(GPS.speed); Serial.print("Angle: "); Serial.println(GPS.angle); Serial.print("Altitude: "); Serial.println(GPS.altitude); - Serial.print("Satellites: "); Serial.println(GPS.satellites); + Serial.print("Satellites: "); Serial.println((int)GPS.satellites); } } -} \ No newline at end of file +}