libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
gpsparser.h
Go to the documentation of this file.
1 #ifndef LIBPROPELLER_GPSPARSER_H_
2 #define LIBPROPELLER_GPSPARSER_H_
3 
5 
16 class GPSParser {
17 public:
18 
25  bool Start(const int rx_pin, const int tx_pin, const int baud) {
26  gps_serial_.Stop();
27  next_character_position_ = 0;
28  gps_serial_.Start(rx_pin, tx_pin, baud);
29 
30  recording_sentence_ = false;
31  return true;
32  }
33 
37  gps_serial_.Stop();
38  }
39 
53  char * Get() {
54  return Get(internal_buffer_);
55  }
56 
69  char * Get(char string[], const int maxBytes = kNmeaMaxLength) {
70  for (;;) {
71 
72  int byte = gps_serial_.Get(0);
73  if (byte == -1) return NULL;
74 
75  if (next_character_position_ == 6) {
76  CheckForPGTOP(string);
77  }
78 
79 
80  if (recording_sentence_ == false && byte != kSentenceStartCharacter) {
81  /* Do nothing */
82  } else if (byte == '\r' || byte == '\n') {
83  return TerminateString(string);
84  } else {
85  //Have a valid byte, now need to add to buffer
86  recording_sentence_ = true;
87  string[next_character_position_++] = byte;
88  }
89 
90  if (next_character_position_ == maxBytes - 1) {
91  return TerminateString(string);
92  }
93  }
94  }
95 
96 protected:
98 
99 private:
100  static const int kNmeaMaxLength = 85;
101  static const int kBufferSize = kNmeaMaxLength;
102  static const char kSentenceStartCharacter = '$';
103 
104  int next_character_position_;
105  char internal_buffer_[kBufferSize]; //Holds 1 NMEA string
106  bool recording_sentence_;
107 
108  char * TerminateString(char string[]) {
109  string[next_character_position_] = 0; //Null terminator
110  next_character_position_ = 0; //Reset nextCharacterPosition
111  recording_sentence_ = false;
112  return string; //Return pointer
113  }
114 
115  void CheckForPGTOP(char string[]) {
116  if (string[1] == 'P' &&
117  string[2] == 'G' &&
118  string[3] == 'T' &&
119  string[4] == 'O' &&
120  string[5] == 'P') {
121  next_character_position_ = 0;
122  recording_sentence_ = false;
123  }
124  }
125 
130  Serial * getSerial(void) {
131  return &gps_serial_;
132  }
133 public:
134  friend class UnityTests;
135 };
136 
137 
138 
139 #endif // LIBPROPELLER_GPSPARSER_H_