G'day fellow Arduino people.
I have recently put together a class which inherits the Arduino core 'Print' class, it implements a memory target which provides all the standard print functions.
I got encouraged by its possibilities and decided to flesh it out for sharing. Now its a nice alternative to 'Strings' as it allows easy concatenation of data. without a malloc/free pair for every little operation. Also it can replace basic usage of sprintf.
When using libraries such as Serial, it has little overhead as the print functionality may already be used and included.
All this said, I have not really tested it heavily, and probably needs tweaks for certain scenarios.
Any ideas are welcome also.
Here is a list of functionality.
- template< typename T > GString( const T *u_DataPtr );
Constructor: pass in a valid pointer to any memory.
operator char*( void );
Conversion operator ( GString to char* ), also allows accessing string elements directly using '[]' operators.template< typename T > String &operator +=( const T &t );
Allows use of the '+=' operator on anything the Print class accepts.void clear( void );
Resets the string to empty.void clear( bool b_Empty );
Clears any previous data if requested. then resets.template< typename T > GString &concat( const T &t );
Provides chainable print methods.template< typename T > GString &concat( const T &t, const int i );
Provides chanable print methods utilising the second parameter.size_t count( void ) const;
Returns the number of characters printed into the string.void end( void );
Adds a terminating null. Buffer will still increase after ending a string.size_t find( const char &c_Character );
Find a character within the string. Its index is returned or -1.pft printf( const char *format, ... );
Prints formatted text to the buffer.void repeat( const char &c_Character, unsigned char u_Count );
Prints c_Character into the string repeating u_Count times.void toLower( void );
Converts the string into its lower case equivalent.void toUpper( void );
Converts the string into its upper case equivalent.void translate( const char &c_StartLow, const char &c_StartHigh, const char &c_EndLow );
Converts a range of characters to a different part of the ascii table.[/quote]The library code is at the bottom, below is its usage.
Note: all examples produce output resembling the following:
This is a float test: 123.456
Analog pin 0 value: 439//Buffer to store data.
char TestArr[ 128 ];
//Create string, passing in buffer pointer.
GString g_Test( TestArr );
//Use like Serial
g_Test.print( "This is a float test: " );
g_Test.println( 123.456, 3 );
g_Test.print( "Analog pin 0 value: " );
g_Test.print( analogRead( A0 ), DEC );
The library also supports += operators:
GString g_Test( TestArr );
g_Test += "This is a float test: ";
g_Test += 123.456f;
g_Test += "\r\nAnalog pin 0 value: ";
g_Test += analogRead( A0 );
I have also added a function 'concat' for chained printing ( notice anonymous GString rather than variable ):
GString( TestArr ).concat( "This is a float test: " ).concat( 123.456, 3 ).concat( "\r\nAnalog pin 0 value: " ).concat( analogRead( A0 ), DEC );
And the GString class can be printed itself.
Serial.print( g_Test );
- *EDIT**: As the print functionality does not deal with null's, you have to take care when using the original pointer. Either clear the pointer initially before use, or call end() as the last call to the GString before using the pointer. The built in conversions are safe, a null is added, but will be overwritten if more print operations take place. [Update message.](http://forum.arduino.cc/index.php?topic=166540.msg1241168#msg1241168) - *The library**: - *Due to message length limits, the code is now an attachment to this post, an external link will be available soon.** Enjoy :) - *EDIT**: new code update, file located in this post: http://forum.arduino.cc/index.php?topic=166540.msg1247875#msg1247875. [Google code download link.](https://code.google.com/p/arduino-extensions/downloads/list), download GString.h [GString.h|attachment](upload://uyYzQapwi8TaOsVfzOZg8TFhW3K.h) (5.38 KB)