This is a problem solved easily with objects.
To completely remove the function situation without explicitly writing the code inline, we can put the code into an object, now a simple declaration provides us with the information.
struct InputByte{
InputByte() : data( waitRead() ) {}
operator char&() { return data; }
InputByte &operator =( char c ){ return data = c, *this; }
static char waitRead(){
while(!Serial.available());
return Serial.read();
}
char data;
};
It can be used quite easily:
InputByte b[ 30 ];
b[29 ] = 0;
Serial.println( ( char* ) b );
If you can wrap you head around this, a template will provide a different solution, which can also be used just like an array.
inline char waitRead(){
while(!Serial.available());
return Serial.read();
}
template< unsigned N > struct CaptureString {
CaptureString() : data( N ? waitRead() : N ) {}
operator char*(){ return (char*) this; }
char data;
CaptureString< N && ( N < 0xFFFF ) ? N - 1 : 0xFFFF > next;
};
template<> struct CaptureString< 0xFFFF >{};
And its use is like below ( equivalent to the non template version above )
CaptureString< 30 > c;
Serial.println( c );
Edit: fixed code error in template version.