Merge pull request #270 from SukkoPera/Fix-ifSerial

Added "if serial" and deprecated isConnected()
This commit is contained in:
Roger Clark 2017-07-06 08:36:20 +10:00 committed by GitHub
commit 385dfbf29c
16 changed files with 42 additions and 33 deletions

View File

@ -119,7 +119,7 @@ size_t n = 0;
size_t USBSerial::write(const uint8 *buf, uint32 len)
{
size_t n = 0;
if (!this->isConnected() || !buf) {
if (!(bool) *this || !buf) {
return 0;
}
@ -190,10 +190,6 @@ uint8 USBSerial::pending(void) {
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) {
return usb_cdcacm_get_dtr();
}
@ -202,6 +198,10 @@ uint8 USBSerial::getRTS(void) {
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
#ifdef SERIAL_USB
USBSerial Serial;

View File

@ -44,41 +44,50 @@ public:
void begin(void);
// Roger Clark. Added dummy function so that existing Arduino sketches which specify baud rate will compile.
void begin(unsigned long);
void begin(unsigned long, uint8_t);
// Roger Clark. Added dummy function so that existing Arduino sketches which specify baud rate will compile.
void begin(unsigned long);
void begin(unsigned long, uint8_t);
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
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 read(void);
int availableForWrite(void);
virtual void flush(void);
size_t write(uint8);
size_t write(const char *str);
size_t write(const uint8*, uint32);
uint8 getRTS();
uint8 getDTR();
uint8 isConnected();
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:
static bool _hasBegun;
};
#ifdef SERIAL_USB
extern USBSerial Serial;
#ifdef SERIAL_USB
extern USBSerial Serial;
#endif
#endif

View File

@ -15,7 +15,7 @@ void setup() {
// Initialize virtual COM over USB on Maple Mini
Serial.begin(9600); // BAUD has no effect on USB serial: placeholder for physical UART
// 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
delay(100); // fast blink

View File

@ -18,7 +18,7 @@ void setup()
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
Serial.setTimeout(timeoutPeriod); // default is 1 second
// 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
delay(100); // fast blink

View File

@ -19,7 +19,7 @@ void setup()
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
Serial.setTimeout(timeoutPeriod); // default is 1 second
// 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
delay(100); // fast blink

View File

@ -38,7 +38,7 @@ void setup(void)
pinMode(A_RANDOM_ANALOG_PIN, INPUT_ANALOG);
// 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
delay(100); // fast blink
@ -48,7 +48,7 @@ void setup(void)
setup_temperature_sensor();
// announce start up
if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS()))
if(Serial)
Serial.println("Temp mon startup");
}
@ -69,7 +69,7 @@ void loop(void)
t2 = micros();
vsense = adc_read(ADC1, 16);
t3 = micros();
if(Serial.isConnected() && (Serial.getDTR() || Serial.getRTS())) {
if(Serial) {
sprintf(buf,"%04x %08x %04x %08x" , vsense, t3-t2, alogpin, t2-t1);
Serial.println(buf);
}

View File

@ -29,7 +29,7 @@ void setup()
pinMode(33, OUTPUT);
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
// 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
delay(100); // fast blink

View File

@ -18,7 +18,7 @@ void setup() {
pinMode(33, OUTPUT);
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
// 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
delay(100); // fast blink

View File

@ -18,7 +18,7 @@ void setup() {
pinMode(33, OUTPUT);
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
// 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
delay(100); // fast blink

View File

@ -15,7 +15,7 @@ void setup()
pinMode(33, OUTPUT);
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
// 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
delay(100); // fast blink

View File

@ -17,7 +17,7 @@ void setup() // run once, when the sketch starts
pinMode(33, OUTPUT);
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
// 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
delay(100); // fast blink

View File

@ -16,7 +16,7 @@ void setup() // run once, when the sketch starts
pinMode(33, OUTPUT);
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UART
// 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
delay(100); // fast blink

View File

@ -13,7 +13,7 @@ void setup() {
pinMode(33, OUTPUT);
Serial.begin(BAUD); // BAUD has no effect on USB serial: placeholder for physical UAR
// 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
delay(100); // fast blink

View File

@ -34,7 +34,7 @@ void setup() {
pinMode(33, OUTPUT);
Serial.begin(); // USB does not require BAUD
// 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
delay(100); // fast blink

View File

@ -19,7 +19,7 @@ void setup() {
pinMode(33, OUTPUT);
Serial.begin(9600);
// 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
delay(100); // fast blink