aboutsummaryrefslogtreecommitdiff
path: root/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-08-18 18:20:25 +1000
committerGitHub <noreply@github.com>2021-08-18 18:20:25 +1000
commitb16091659cc9a724a8800f77e631643b4ab089ad (patch)
treee44933472c6d100bd4fc5d8a693d9d21e3c32f6f /lib/usbhost/USB_Host_Shield_2.0/examples/pl2303
parentcf5e40c25139ff64ff246f1c6280e983ef75551c (diff)
downloadqmk_firmware-b16091659cc9a724a8800f77e631643b4ab089ad.tar.gz
qmk_firmware-b16091659cc9a724a8800f77e631643b4ab089ad.zip
Move USB Host Shield and Arduino core to `lib/` (#13973)
Diffstat (limited to 'lib/usbhost/USB_Host_Shield_2.0/examples/pl2303')
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.ino101
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gps/pl2303_gps.ino88
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino217
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino117
4 files changed, 523 insertions, 0 deletions
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.ino
new file mode 100644
index 000000000..7c4c9f6cb
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.ino
@@ -0,0 +1,101 @@
1/* Arduino terminal for PL2303 USB to serial converter and DealeXtreme GPRS modem. */
2/* USB support */
3#include <usbhub.h>
4/* CDC support */
5#include <cdcacm.h>
6#include <cdcprolific.h>
7
8// Satisfy the IDE, which needs to see the include statment in the ino too.
9#ifdef dobogusinclude
10#include <spi4teensy3.h>
11#include <SPI.h>
12#endif
13
14class PLAsyncOper : public CDCAsyncOper
15{
16public:
17 uint8_t OnInit(ACM *pacm);
18};
19
20uint8_t PLAsyncOper::OnInit(ACM *pacm)
21{
22 uint8_t rcode;
23
24 // Set DTR = 1
25 rcode = pacm->SetControlLineState(1);
26
27 if (rcode)
28 {
29 ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
30 return rcode;
31 }
32
33 LINE_CODING lc;
34 //lc.dwDTERate = 9600;
35 lc.dwDTERate = 115200;
36 lc.bCharFormat = 0;
37 lc.bParityType = 0;
38 lc.bDataBits = 8;
39
40 rcode = pacm->SetLineCoding(&lc);
41
42 if (rcode)
43 ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
44
45 return rcode;
46}
47USB Usb;
48//USBHub Hub(&Usb);
49PLAsyncOper AsyncOper;
50PL2303 Pl(&Usb, &AsyncOper);
51
52void setup()
53{
54 Serial.begin( 115200 );
55#if !defined(__MIPSEL__)
56 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
57#endif
58 Serial.println("Start");
59
60 if (Usb.Init() == -1)
61 Serial.println("OSCOKIRQ failed to assert");
62
63 delay( 200 );
64}
65
66void loop()
67{
68 Usb.Task();
69
70 if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
71 {
72 uint8_t rcode;
73
74 /* reading the keyboard */
75 if(Serial.available()) {
76 uint8_t data= Serial.read();
77
78 /* sending to the phone */
79 rcode = Pl.SndData(1, &data);
80 if (rcode)
81 ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
82 }//if(Serial.available()...
83
84 /* reading the converter */
85 /* buffer size must be greater or equal to max.packet size */
86 /* it it set to 64 (largest possible max.packet size) here, can be tuned down
87 for particular endpoint */
88 uint8_t buf[64];
89 uint16_t rcvd = 64;
90 rcode = Pl.RcvData(&rcvd, buf);
91 if (rcode && rcode != hrNAK)
92 ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
93
94 if( rcvd ) { //more than zero bytes received
95 for(uint16_t i=0; i < rcvd; i++ ) {
96 Serial.print((char)buf[i]); //printing on the screen
97 }
98 }//if( rcvd ...
99 }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
100}
101
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gps/pl2303_gps.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gps/pl2303_gps.ino
new file mode 100644
index 000000000..e8c8a0223
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_gps/pl2303_gps.ino
@@ -0,0 +1,88 @@
1/* USB Host to PL2303-based USB GPS unit interface */
2/* Navibee GM720 receiver - Sirf Star III */
3/* USB support */
4#include <usbhub.h>
5/* CDC support */
6#include <cdcacm.h>
7#include <cdcprolific.h>
8
9// Satisfy the IDE, which needs to see the include statment in the ino too.
10#ifdef dobogusinclude
11#include <spi4teensy3.h>
12#include <SPI.h>
13#endif
14
15class PLAsyncOper : public CDCAsyncOper {
16public:
17 uint8_t OnInit(ACM *pacm);
18};
19
20uint8_t PLAsyncOper::OnInit(ACM *pacm) {
21 uint8_t rcode;
22
23 // Set DTR = 1
24 rcode = pacm->SetControlLineState(1);
25
26 if(rcode) {
27 ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
28 return rcode;
29 }
30
31 LINE_CODING lc;
32 lc.dwDTERate = 4800; //default serial speed of GPS unit
33 lc.bCharFormat = 0;
34 lc.bParityType = 0;
35 lc.bDataBits = 8;
36
37 rcode = pacm->SetLineCoding(&lc);
38
39 if(rcode)
40 ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
41
42 return rcode;
43}
44
45USB Usb;
46USBHub Hub(&Usb);
47PLAsyncOper AsyncOper;
48PL2303 Pl(&Usb, &AsyncOper);
49uint32_t read_delay;
50#define READ_DELAY 100
51
52void setup() {
53 Serial.begin(115200);
54#if !defined(__MIPSEL__)
55 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
56#endif
57 Serial.println("Start");
58
59 if(Usb.Init() == -1)
60 Serial.println("OSCOKIRQ failed to assert");
61
62 delay(200);
63}
64
65void loop() {
66 uint8_t rcode;
67 uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint
68 uint16_t rcvd = 64;
69
70 Usb.Task();
71
72 if(Pl.isReady()) {
73 /* reading the GPS */
74 if((long)(millis() - read_delay) >= 0L) {
75 read_delay += READ_DELAY;
76 rcode = Pl.RcvData(&rcvd, buf);
77 if(rcode && rcode != hrNAK)
78 ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
79 if(rcvd) { //more than zero bytes received
80 for(uint16_t i = 0; i < rcvd; i++) {
81 Serial.print((char)buf[i]); //printing on the screen
82 }//for( uint16_t i=0; i < rcvd; i++...
83 }//if( rcvd
84 }//if( read_delay > millis()...
85 }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
86}
87
88
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino
new file mode 100644
index 000000000..d527eabe0
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_tinygps/pl2303_tinygps.ino
@@ -0,0 +1,217 @@
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
26class PLAsyncOper : public CDCAsyncOper
27{
28public:
29 uint8_t OnInit(ACM *pacm);
30};
31
32uint8_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
59USB Usb;
60//USBHub Hub(&Usb);
61PLAsyncOper AsyncOper;
62PL2303 Pl(&Usb, &AsyncOper);
63TinyGPS gps;
64
65void gpsdump(TinyGPS &gps);
66bool feedgps();
67void printFloat(double f, int digits = 2);
68
69void 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
90void 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
116void 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
151void 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
196bool 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
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino
new file mode 100644
index 000000000..67b7dab60
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino
@@ -0,0 +1,117 @@
1/* Arduino terminal for PL2303 USB to serial converter and XBee radio. */
2/* Inserts linefeed after carriage return in data sent to and received from Xbee */
3/* USB support */
4#include <usbhub.h>
5/* CDC support */
6#include <cdcacm.h>
7#include <cdcprolific.h>
8
9// Satisfy the IDE, which needs to see the include statment in the ino too.
10#ifdef dobogusinclude
11#include <spi4teensy3.h>
12#include <SPI.h>
13#endif
14
15class PLAsyncOper : public CDCAsyncOper
16{
17public:
18 uint8_t OnInit(ACM *pacm);
19};
20
21uint8_t PLAsyncOper::OnInit(ACM *pacm)
22{
23 uint8_t rcode;
24
25 // Set DTR = 1
26 rcode = pacm->SetControlLineState(1);
27
28 if (rcode)
29 {
30 ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
31 return rcode;
32 }
33
34 LINE_CODING lc;
35 lc.dwDTERate = 115200;
36 lc.bCharFormat = 0;
37 lc.bParityType = 0;
38 lc.bDataBits = 8;
39
40 rcode = pacm->SetLineCoding(&lc);
41
42 if (rcode)
43 ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
44
45 return rcode;
46}
47USB Usb;
48//USBHub Hub(&Usb);
49PLAsyncOper AsyncOper;
50PL2303 Pl(&Usb, &AsyncOper);
51
52void setup()
53{
54 Serial.begin( 115200 );
55#if !defined(__MIPSEL__)
56 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
57#endif
58 Serial.println("Start");
59
60 if (Usb.Init() == -1)
61 Serial.println("OSCOKIRQ failed to assert");
62
63 delay( 200 );
64}
65
66void loop()
67{
68 Usb.Task();
69
70 if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
71 {
72 uint8_t rcode;
73
74 /* reading the keyboard */
75 if(Serial.available()) {
76 uint8_t data= Serial.read();
77
78 if ( data == '\r' ) {
79 Serial.print("\r\n"); //insert linefeed
80 }
81 else {
82 Serial.print( data ); //echo back to the screen
83 }
84
85 /* sending to the phone */
86 rcode = Pl.SndData(1, &data);
87 if (rcode)
88 ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
89 }//if(Serial.available()...
90
91 delay(50);
92
93 /* reading the converter */
94 /* buffer size must be greater or equal to max.packet size */
95 /* it it set to 64 (largest possible max.packet size) here, can be tuned down
96 for particular endpoint */
97 uint8_t buf[64];
98 uint16_t rcvd = 64;
99 rcode = Pl.RcvData(&rcvd, buf);
100 if (rcode && rcode != hrNAK)
101 ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
102
103 if( rcvd ) { //more than zero bytes received
104 for(uint16_t i=0; i < rcvd; i++ ) {
105 if( buf[i] =='\r' ) {
106 Serial.print("\r\n"); //insert linefeed
107 }
108 else {
109 Serial.print((char)buf[i]); //printing on the screen
110 }
111 }
112 }
113 delay(10);
114 }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
115}
116
117