Skip to content

Support both char* and uint8* in Stream and Print #1762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2013

Conversation

matthijskooijman
Copy link
Collaborator

Before, a program like this would fail:

void setup(){
    uint8_t foo[4];
    Serial.readBytes(foo, 4);
    Serial.write(foo, 4);

    char bar[4];
    Serial.readBytes(bar, 4);
    Serial.write(bar, 4);
}

Test15.ino: In function ‘void setup()’:
Test15.ino:3:25: error: invalid conversion from ‘uint8_t* {aka unsigned char*}’ to ‘char*’ [-fpermissive]
[...]
Stream.h:76:10: error:   initializing argument 1 of ‘size_t Stream::readBytes(char*, size_t)’ [-fpermissive]
   size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
      ^
Test15.ino:8:21: error: invalid conversion from ‘char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]
[...]
Print.h:53:20: error:   initializing argument 1 of ‘virtual size_t Print::write(const uint8_t*, size_t)’ [-fpermissive]
     virtual size_t write(const uint8_t *buffer, size_t size);

As you can see, it doesn't matter if you use a char[] or uint8_t[], both will
fail. This is because Print::write only worked with uint8_t buffers and
the Stream methods only work with char buffers.

These commits make both of them work with both types of buffers. By
putting the definitions in the .h file, the compiler can just inline
them, meaning there is no impact on the generated code size.

The new function just calls Print::write(const uint8_t *, size_t), but
this allows writing out a buffer of chars (without having to learn about
casts).
The new functions just call their char* equivalents, but this allows
reading bytes into a buffer of uint8_t as well as chars.
Now that Print::write(const char*) is also available, these casts are no
longer needed.
cmaglie added a commit that referenced this pull request Dec 30, 2013
Support both char* and uint8* in Stream and Print
@cmaglie cmaglie merged commit 0b72c88 into arduino:ide-1.5.x Dec 30, 2013
@cmaglie
Copy link
Member

cmaglie commented Dec 30, 2013

Nice, thanks!
C

@matthijskooijman matthijskooijman deleted the ide-1.5.x-write-char branch February 14, 2014 09:35
ollie1400 pushed a commit to ollie1400/Arduino that referenced this pull request May 2, 2022
…-char

Support both char* and uint8* in Stream and Print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants