Merge pull request #270 from SukkoPera/Fix-ifSerial
Added "if serial" and deprecated isConnected()
This commit is contained in:
commit
385dfbf29c
|
@ -119,7 +119,7 @@ size_t n = 0;
|
||||||
size_t USBSerial::write(const uint8 *buf, uint32 len)
|
size_t USBSerial::write(const uint8 *buf, uint32 len)
|
||||||
{
|
{
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
if (!this->isConnected() || !buf) {
|
if (!(bool) *this || !buf) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,10 +190,6 @@ uint8 USBSerial::pending(void) {
|
||||||
return usb_cdcacm_get_pending();
|
return usb_cdcacm_get_pending();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8 USBSerial::isConnected(void) {
|
|
||||||
return usb_is_connected(USBLIB) && usb_is_configured(USBLIB) && usb_cdcacm_get_dtr();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8 USBSerial::getDTR(void) {
|
uint8 USBSerial::getDTR(void) {
|
||||||
return usb_cdcacm_get_dtr();
|
return usb_cdcacm_get_dtr();
|
||||||
}
|
}
|
||||||
|
@ -202,6 +198,10 @@ uint8 USBSerial::getRTS(void) {
|
||||||
return usb_cdcacm_get_rts();
|
return usb_cdcacm_get_rts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
USBSerial::operator bool() {
|
||||||
|
return usb_is_connected(USBLIB) && usb_is_configured(USBLIB) && usb_cdcacm_get_dtr();
|
||||||
|
}
|
||||||
|
|
||||||
#if BOARD_HAVE_SERIALUSB
|
#if BOARD_HAVE_SERIALUSB
|
||||||
#ifdef SERIAL_USB
|
#ifdef SERIAL_USB
|
||||||
USBSerial Serial;
|
USBSerial Serial;
|
||||||
|
|
|
@ -44,19 +44,17 @@ public:
|
||||||
|
|
||||||
void begin(void);
|
void begin(void);
|
||||||
|
|
||||||
// Roger Clark. Added dummy function so that existing Arduino sketches which specify baud rate will compile.
|
// Roger Clark. Added dummy function so that existing Arduino sketches which specify baud rate will compile.
|
||||||
void begin(unsigned long);
|
void begin(unsigned long);
|
||||||
void begin(unsigned long, uint8_t);
|
void begin(unsigned long, uint8_t);
|
||||||
void end(void);
|
void end(void);
|
||||||
|
|
||||||
operator bool() { return true; } // Roger Clark. This is needed because in cardinfo.ino it does if (!Serial) . It seems to be a work around for the Leonardo that we needed to implement just to be compliant with the API
|
|
||||||
|
|
||||||
virtual int available(void);// Changed to virtual
|
virtual int available(void);// Changed to virtual
|
||||||
|
|
||||||
uint32 read(uint8 * buf, uint32 len);
|
uint32 read(uint8 * buf, uint32 len);
|
||||||
// uint8 read(void);
|
// uint8 read(void);
|
||||||
|
|
||||||
// Roger Clark. added functions to support Arduino 1.0 API
|
// Roger Clark. added functions to support Arduino 1.0 API
|
||||||
virtual int peek(void);
|
virtual int peek(void);
|
||||||
virtual int read(void);
|
virtual int read(void);
|
||||||
int availableForWrite(void);
|
int availableForWrite(void);
|
||||||
|
@ -69,16 +67,27 @@ public:
|
||||||
|
|
||||||
uint8 getRTS();
|
uint8 getRTS();
|
||||||
uint8 getDTR();
|
uint8 getDTR();
|
||||||
uint8 isConnected();
|
|
||||||
uint8 pending();
|
uint8 pending();
|
||||||
|
|
||||||
|
/* SukkoPera: This is the Arduino way to check if an USB CDC serial
|
||||||
|
* connection is open.
|
||||||
|
|
||||||
|
* Used for instance in cardinfo.ino.
|
||||||
|
*/
|
||||||
|
operator bool();
|
||||||
|
|
||||||
|
/* Old libmaple way to check for serial connection.
|
||||||
|
*
|
||||||
|
* Deprecated, use the above.
|
||||||
|
*/
|
||||||
|
uint8 isConnected() __attribute__((deprecated("Use !Serial instead"))) { return (bool) *this; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static bool _hasBegun;
|
static bool _hasBegun;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef SERIAL_USB
|
#ifdef SERIAL_USB
|
||||||
extern USBSerial Serial;
|
extern USBSerial Serial;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ void setup() {
|
||||||
// Initialize virtual COM over USB on Maple Mini
|
// Initialize virtual COM over USB on Maple Mini
|
||||||
Serial.begin(9600); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(9600); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -18,7 +18,7 @@ void setup()
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
Serial.setTimeout(timeoutPeriod); // default is 1 second
|
Serial.setTimeout(timeoutPeriod); // default is 1 second
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -19,7 +19,7 @@ void setup()
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
Serial.setTimeout(timeoutPeriod); // default is 1 second
|
Serial.setTimeout(timeoutPeriod); // default is 1 second
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -38,7 +38,7 @@ void setup(void)
|
||||||
pinMode(A_RANDOM_ANALOG_PIN, INPUT_ANALOG);
|
pinMode(A_RANDOM_ANALOG_PIN, INPUT_ANALOG);
|
||||||
|
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
@ -48,7 +48,7 @@ void setup(void)
|
||||||
setup_temperature_sensor();
|
setup_temperature_sensor();
|
||||||
|
|
||||||
// announce start up
|
// announce start up
|
||||||
if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))
|
if(Serial)
|
||||||
Serial.println("Temp mon startup");
|
Serial.println("Temp mon startup");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ void loop(void)
|
||||||
t2 = micros();
|
t2 = micros();
|
||||||
vsense = adc_read(ADC1, 16);
|
vsense = adc_read(ADC1, 16);
|
||||||
t3 = micros();
|
t3 = micros();
|
||||||
if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())) {
|
if(Serial) {
|
||||||
sprintf(buf,"%04x %08x %04x %08x" , vsense, t3-t2, alogpin, t2-t1);
|
sprintf(buf,"%04x %08x %04x %08x" , vsense, t3-t2, alogpin, t2-t1);
|
||||||
Serial.println(buf);
|
Serial.println(buf);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ void setup()
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -18,7 +18,7 @@ void setup() {
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -18,7 +18,7 @@ void setup() {
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -15,7 +15,7 @@ void setup()
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -17,7 +17,7 @@ void setup() // run once, when the sketch starts
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -16,7 +16,7 @@ void setup() // run once, when the sketch starts
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -13,7 +13,7 @@ void setup() {
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UAR
|
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UAR
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
Binary file not shown.
|
@ -34,7 +34,7 @@ void setup() {
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(); // USB does not require BAUD
|
Serial.begin(); // USB does not require BAUD
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
|
@ -19,7 +19,7 @@ void setup() {
|
||||||
pinMode(33, OUTPUT);
|
pinMode(33, OUTPUT);
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
// wait for serial monitor to be connected.
|
// wait for serial monitor to be connected.
|
||||||
while (!(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())))
|
while (!Serial)
|
||||||
{
|
{
|
||||||
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
digitalWrite(33,!digitalRead(33));// Turn the LED from off to on, or on to off
|
||||||
delay(100); // fast blink
|
delay(100); // fast blink
|
||||||
|
|
Loading…
Reference in New Issue