Go Down

Topic: SoftwareSerial: print(data) (Read 99 times) previous topic - next topic

malok

The official documentation says: "It works the same as the Serial.print() function."
But in the source code of the library we can see:
Code: [Select]

//SoftwareSerial.h
 using Print::write;


Code: [Select]

//SoftwareSerial.cpp
size_t SoftwareSerial::write(uint8_t b)
{
  if (_tx_delay == 0) {
    setWriteError();
    return 0;
  }

  uint8_t oldSREG = SREG;
  cli();  // turn off interrupts for a clean txmit

  // Write the start bit
  tx_pin_write(_inverse_logic ? HIGH : LOW);
  tunedDelay(_tx_delay + XMIT_START_ADJUSTMENT);

  // Write each of the 8 bits
  if (_inverse_logic)
  {
    for (byte mask = 0x01; mask; mask <<= 1)
    {
      if (b & mask) // choose bit
        tx_pin_write(LOW); // send 1
      else
        tx_pin_write(HIGH); // send 0
    
      tunedDelay(_tx_delay);
    }

    tx_pin_write(LOW); // restore pin to natural state
  }
  else
  {
    for (byte mask = 0x01; mask; mask <<= 1)
    {
      if (b & mask) // choose bit
        tx_pin_write(HIGH); // send 1
      else
        tx_pin_write(LOW); // send 0
    
      tunedDelay(_tx_delay);
    }

    tx_pin_write(HIGH); // restore pin to natural state
  }

  SREG = oldSREG; // turn interrupts back on
  tunedDelay(_tx_delay);
  
  return 1;
}



How the other options are implemented functions print()?

P.S. Implementation of the function println() I do not see it.

pYro_65

#1
Feb 22, 2015, 09:54 am Last Edit: Feb 22, 2015, 09:56 am by pYro_65
The Print functions are inherited by SoftwareSerial, as it inherits the Print class.

The using Print::write; is needed as SoftwareSerial implements its own version (required as its virtual), however it hides the overloads of write (non-virtual) in the base Print class. This simply exposes all of them to the user.

The Print::print(ln) functions ultimately call the write function defined in the derived class (SoftwareSerial), which do the actual writing/printing. The print(ln) methods allow you to format the data before writing it out.

malok

#2
Feb 22, 2015, 01:09 pm Last Edit: Feb 22, 2015, 01:18 pm by malok
Sorry, but I do not see where in the code described that the implementation of print() overlaps virtual version of the description it in the base class Stream. See in https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp and https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h

malok

#3
Feb 22, 2015, 06:25 pm Last Edit: Feb 22, 2015, 09:30 pm by malok
I'm all figured out. In
https://code.google.com/p/arduino/source/browse/trunk/hardware/arduino/cores/arduino/Print.cpp?r=982
Code: [Select]

void Print::write(const char *str)
{
  while (*str)
    write(*str++);
}

/* default implementation: may be overridden */
void Print::write(const uint8_t *buffer, size_t size)
{
  while (size--)
    write(*buffer++);
}

void Print::print(const String &s)
{
  print(s.toCharArray());
}
//.....

I translated into Russian this page

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy