]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino
Merge branch 'master' into chibios
[tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / pl2303 / pl2303_tinygps / pl2303_tinygps.ino
1 /* USB Host to PL2303-based USB GPS unit interface */
2 /* Navibee GM720 receiver - Sirf Star III */
3 /* Mikal Hart's TinyGPS library */
4 /* test_with_gps_device library example modified for PL2302 access */
5
6 /* USB support */
7 #include <usbhub.h>
8
9 /* CDC support */
10 #include <cdcacm.h>
11 #include <cdcprolific.h>
12
13 #include <TinyGPS.h>
14
15 // Satisfy the IDE, which needs to see the include statment in the ino too.
16 #ifdef dobogusinclude
17 #include <spi4teensy3.h>
18 #include <SPI.h>
19 #endif
20
21 /* This sample code demonstrates the normal use of a TinyGPS object.
22 Modified to be used with USB Host Shield Library r2.0
23 and USB Host Shield 2.0
24 */
25
26 class PLAsyncOper : public CDCAsyncOper
27 {
28 public:
29 uint8_t OnInit(ACM *pacm);
30 };
31
32 uint8_t PLAsyncOper::OnInit(ACM *pacm)
33 {
34 uint8_t rcode;
35
36 // Set DTR = 1
37 rcode = pacm->SetControlLineState(1);
38
39 if (rcode) {
40 ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
41 return rcode;
42 }
43
44 LINE_CODING lc;
45 lc.dwDTERate = 4800; //default serial speed of GPS unit
46 lc.bCharFormat = 0;
47 lc.bParityType = 0;
48 lc.bDataBits = 8;
49
50 rcode = pacm->SetLineCoding(&lc);
51
52 if (rcode) {
53 ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
54 }
55
56 return rcode;
57 }
58
59 USB Usb;
60 //USBHub Hub(&Usb);
61 PLAsyncOper AsyncOper;
62 PL2303 Pl(&Usb, &AsyncOper);
63 TinyGPS gps;
64
65 void gpsdump(TinyGPS &gps);
66 bool feedgps();
67 void printFloat(double f, int digits = 2);
68
69 void setup()
70 {
71
72 Serial.begin(115200);
73 #if !defined(__MIPSEL__)
74 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
75 #endif
76
77 Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
78 Serial.println("by Mikal Hart");
79 Serial.println();
80 Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
81 Serial.println();
82 /* USB Initialization */
83 if (Usb.Init() == -1) {
84 Serial.println("OSCOKIRQ failed to assert");
85 }
86
87 delay( 200 );
88 }
89
90 void loop()
91 {
92 Usb.Task();
93
94 if( Pl.isReady()) {
95
96 bool newdata = false;
97 unsigned long start = millis();
98
99 // Every 5 seconds we print an update
100 while (millis() - start < 5000) {
101 if( feedgps()) {
102 newdata = true;
103 }
104 }//while (millis()...
105
106 if (newdata) {
107 Serial.println("Acquired Data");
108 Serial.println("-------------");
109 gpsdump(gps);
110 Serial.println("-------------");
111 Serial.println();
112 }//if( newdata...
113 }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING...
114 }
115
116 void printFloat(double number, int digits)
117 {
118 // Handle negative numbers
119 if (number < 0.0)
120 {
121 Serial.print('-');
122 number = -number;
123 }
124
125 // Round correctly so that print(1.999, 2) prints as "2.00"
126 double rounding = 0.5;
127 for (uint8_t i=0; i<digits; ++i)
128 rounding /= 10.0;
129
130 number += rounding;
131
132 // Extract the integer part of the number and print it
133 unsigned long int_part = (unsigned long)number;
134 double remainder = number - (double)int_part;
135 Serial.print(int_part);
136
137 // Print the decimal point, but only if there are digits beyond
138 if (digits > 0)
139 Serial.print(".");
140
141 // Extract digits from the remainder one at a time
142 while (digits-- > 0)
143 {
144 remainder *= 10.0;
145 int toPrint = int(remainder);
146 Serial.print(toPrint);
147 remainder -= toPrint;
148 }
149 }
150
151 void gpsdump(TinyGPS &gps)
152 {
153 long lat, lon;
154 float flat, flon;
155 unsigned long age, date, time, chars;
156 int year;
157 byte month, day, hour, minute, second, hundredths;
158 unsigned short sentences, failed;
159
160 gps.get_position(&lat, &lon, &age);
161 Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon);
162 Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
163
164 feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors
165
166 gps.f_get_position(&flat, &flon, &age);
167 Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5);
168 Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
169
170 feedgps();
171
172 gps.get_datetime(&date, &time, &age);
173 Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" Time(hhmmsscc): "); Serial.print(time);
174 Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
175
176 feedgps();
177
178 gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
179 Serial.print("Date: "); Serial.print(static_cast<int>(month)); Serial.print("/"); Serial.print(static_cast<int>(day)); Serial.print("/"); Serial.print(year);
180 Serial.print(" Time: "); Serial.print(static_cast<int>(hour)); Serial.print(":"); Serial.print(static_cast<int>(minute)); Serial.print(":"); Serial.print(static_cast<int>(second)); Serial.print("."); Serial.print(static_cast<int>(hundredths));
181 Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
182
183 feedgps();
184
185 Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Course(10^-2 deg): "); Serial.print(gps.course()); Serial.print(" Speed(10^-2 knots): "); Serial.println(gps.speed());
186 Serial.print("Alt(float): "); printFloat(gps.f_altitude()); Serial.print(" Course(float): "); printFloat(gps.f_course()); Serial.println();
187 Serial.print("Speed(knots): "); printFloat(gps.f_speed_knots()); Serial.print(" (mph): "); printFloat(gps.f_speed_mph());
188 Serial.print(" (mps): "); printFloat(gps.f_speed_mps()); Serial.print(" (kmph): "); printFloat(gps.f_speed_kmph()); Serial.println();
189
190 feedgps();
191
192 gps.stats(&chars, &sentences, &failed);
193 Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed);
194 }
195
196 bool feedgps()
197 {
198 uint8_t rcode;
199 uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint
200 uint16_t rcvd = 64;
201 {
202 /* reading the GPS */
203 rcode = Pl.RcvData(&rcvd, buf);
204 if (rcode && rcode != hrNAK)
205 ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
206 rcode = false;
207 if( rcvd ) { //more than zero bytes received
208 for( uint16_t i=0; i < rcvd; i++ ) {
209 if( gps.encode((char)buf[i])) { //feed a character to gps object
210 rcode = true;
211 }//if( gps.encode(buf[i]...
212 }//for( uint16_t i=0; i < rcvd; i++...
213 }//if( rcvd...
214 }
215 return( rcode );
216 }
217
Imprint / Impressum