Show Posts
Pages: 1 ... 62 63 [64] 65 66 ... 143
946  Development / Other Software Development / Re: String/sprintf alternative specifically for Arduino. on: May 23, 2013, 04:15:22 am
I've been experimenting some different ways to do things.

Here are a few common functions implemented using the Print library.

Code:
inline int sprintf( char * str, const char * format, ... )
{
va_list v_List;
va_start( v_List, format );
GString g( str );
int i_Return = g._printf( format, v_List );
g.end();
va_end( v_List );
return i_Return;
}

inline char *itoa( int value, char * str, int base )
{
GString( str ).print( ( long ) value, base );
return str;
}

inline char *utoa( unsigned int value, char * str, int base )
{
GString( str ).print( ( unsigned long ) value, base );
return str;
}

inline char *ltoa( long value, char * str, int base )
{
GString( str ).print( value, base );
return str;
}

inline char *ultoa( unsigned long value, char * str, int base )
{
GString( str ).print( value, base );
return str;
}

On a large test sketch the optimisations made a huge difference.

  • 7762 bytes: Original Arduino code.
  • 7388 bytes: Print based sprintf
  • 7274 bytes: using Print based itoa, utoa, ltoa, ultoa
  • 7014 bytes: using printf
  • 6958 bytes: force itoa and utoa using 32-bit conversions. ( sprintf does not use 16 bit Print code. )

I have the eeprom code ready too. I'll give it a testing soon too.
947  Development / Suggestions for the Arduino Project / Re: Reference section for "ShiftOut" on: May 23, 2013, 03:41:31 am
Yeah, mistakes like these are questioned often here. It seems that a few reference items have been written in a rush. Unfortunately general public/members do not have access to fix the mistakes.
948  Using Arduino / Programming Questions / Re: What's the mean of "f" in this Code on: May 23, 2013, 03:37:40 am
No worries, remember stack overflow, it has a lot of great non-Arduino answers ( arduino stuff too ).
949  Using Arduino / Programming Questions / Re: What's the mean of "f" in this Code on: May 23, 2013, 02:04:55 am
This has a good answer, there are links to relevant info:
http://stackoverflow.com/questions/4828167/purpose-of-a-f-appended-to-a-number
950  Development / Other Software Development / Re: String/sprintf alternative specifically for Arduino. on: May 22, 2013, 02:33:11 pm
I have applied a few optimisations and PROGMEM strings are working now. It is quite different to the GString version as it uses the print pipeline for everything whereas GString could write to memory for a few things. Maybe a bit slower, but not larger.

Quote
p:   PROGMEM string. No formatting takes place, the string is printed directly.

I'm going to add the EEPROM test asap, then I'll post the files for the patch.
951  Development / Other Software Development / Re: String/sprintf alternative specifically for Arduino. on: May 22, 2013, 10:37:00 am
I would definitely like to do some abstract options too, I've focused mainly on the stuff supported by the Print library. PROGMEM support is within the print library so that'll get added no worries.

Also I'm almost finished testing a patch for the core, which requires my library and two other files. It inserts the print functionality and the new sprintf straight in. I'll finish up and post it shortly, but I just tested this code with flying colours.

Code:
 Serial.printf( "Characters: %c %c \n", 'a', 65 );
  Serial.printf( "Decimals: %d %ld\n", 1977, 650000 );
  Serial.printf( "floats: %f\n", 3.1416f );
  Serial.printf( "Preceding with blanks: %10d \n", 1977 );
  Serial.printf( "Preceding with zeros: %010d \n", 1977 );
  Serial.printf( "Preceding with zeros(width passed as param): %0*d \n", 10, 1977 );
  Serial.printf( "Some different radices: DEC: %ld HEX:%lx \n", 650000, 650000 );
  

Which produces this output:
Quote
Characters: a A
Decimals: 1977 650000
floats: 3.14
Preceding with blanks:       1977
Preceding with zeros: 0000001977
Preceding with zeros(width passed as param): 0000001977
Some different radices: DEC: 650000 HEX:9EB10

It replaced this ugly code:
Code:
sprintf ( buffer, "Characters: %c %c \n", 'a', 65 );
  Serial.print( buffer );  
  sprintf ( buffer, "Decimals: %d %ld\n", 1977, 650000 );
  Serial.print( buffer );
  sprintf ( buffer, "floats: %f\n", 3.1416f );
  Serial.print( buffer );
  sprintf ( buffer, "Preceding with blanks: %10d \n", 1977 );
  Serial.print( buffer );  
  sprintf ( buffer, "Preceding with zeros: %010d \n", 1977 );
  Serial.print( buffer );  
  sprintf ( buffer, "Preceding with zeros(width passed as param): %0*d \n", 10, 1977 );  
  Serial.print( buffer );    
  sprintf ( buffer, "Some different radices: DEC: %ld HEX:%lx \n", 650000, 650000 );
  Serial.print( buffer );

And that same functionality is available to all Print classes like: Ethernet, WiFi, SD, LiquidCrystal, Wire, SoftwareSerial.

The EEPROM idea is nice, I'll look into that, I'm thinking of making a collection of writeable objects so the EEPROM would fit there nicely.
952  Development / Other Software Development / String/sprintf alternative specifically for Arduino. on: May 22, 2013, 07:59:01 am
Had an interesting find. It appears the version of sprintf I'm modifying is the same one used by atmel, which is included with the wifi shield code, and most probably inside libc.a ( AVR Arduinos ).

You can find the resources I used here: http://www.menie.org/georges/embedded/#printf

So using the Arduino framework has proved quite powerful as I now have a system that slots right in and won't break anyone's code ( fingers crossed ).

I have now added a new parameter to the width option, it is the standard '*' which allows specifying the width in another parameter, rather than in the format string.

There are other custom features I can put in if interest arises, like progmem strings and/or data. Also I have put switches ( defines ) in that can disable the extra features, allowing you to use an Arduino standard equivalent at a smaller price.

I'm going to modify the print library now, putting the printf functionality there, this will allow the extensions I mentioned in my previous post.

Google code download link.

There is now a patch released for the Print library with all of the functionality built in. LINK

Although if you use a Due, this is the file you need as the patch is AVR only ( will be compatible soon ), it is out of date now as I have more functionality in the patch. So if you like this version and want the full feature set, leave a post and I'll put a new version together.
953  Development / Other Software Development / Re: String lib alternative / Print class targeting memory. on: May 21, 2013, 10:03:57 am
Awesome new update people!

I have just painstakingly finished a version of my library which now includes a printf function.
This is a big step, as now you can use code like:
Code:
g.printf( "Some numbers: %d, %ld, %010d", 1977, 650000, 1977 );

And produce:
Quote
Some numbers: 1977, 650000, 0000001977

Before I get into to many details there are a few major advantages to using my code.

  • It can be used to create a sprintf function ( see below ).
  • GString compiles smaller than sprintf.
  • GString supports more features than the standard sprintf.
  • If the Arduino team likes my idea, printf can be moved into the 'Print' class where it belongs. This would give all classes deriving 'Print' the ability to print formatted text diretly to their output.
    This list includes Serial, Ethernet, WiFi, SD, LiquidCrystal, Wire, SoftwareSerial, Stream and more!

I have added an overload for sprintf in the class file, so all you have to do is include the file into your sketch.
Here is a breakdown of my sprintf function.

Quote from: GString.h sprintf
sprintf function.
  Prints formatted text to a buffer.
  
  Formatting options use the following syntax:
    %[flags][width][length]specifier
    
  Flags:
    -:  Left-justify within the given field width; Right justification is the default.
    0:  When padding is specified, zeros are used instead of spaces.
    
  Width:
(number):   Minimum number of characters to be printed.
         If the value to be printed is shorter than this number,
         the result is padded with blank spaces.
         The value is not truncated even if the result is larger.                  

    *:   The width is not specified in the format string,
   but as an additional integer value argument preceding
   the argument that has to be formatted.

  Length:
    l:  d, i use long instead of int. u, x use unsigned long instead of unsigned int.
  Specifier:
    s:  String ( null terminated ).
    d:  Signed decimal integer ( 32bits max ).
    i:  Same as 'd'.
    u:  Unsigned decimal integer ( 32bits max ).
    f:  Decimal floating point number.
    x:  Unsigned decimal integer ( 32bits max ).
    c:  Character.
    %:  Escape character for printing '%'

As you can see it supports floating point data.
Below is a test you can use to see the difference.

Code:
//Uncomment the line below to use the improved sprintf
//#include <GString.h>

void setup(){
  char buffer[ 64 ];
  Serial.begin(115200);
  Serial.println( "=================================================" );
  Serial.println( "This is a test using standard print functionality\r\n" );
  Serial.println( 123456 );
  Serial.println( 123.456 );
  Serial.println( 1193046, HEX );
  Serial.println( "=================================================" );
  Serial.println( "This is a test using sprintf functionality\r\n" );
  sprintf ( buffer, "Characters: %c %c \n", 'a', 65 );
  Serial.print( buffer );  
  sprintf ( buffer, "Decimals: %d %ld\n", 1977, 650000 );
  Serial.print( buffer );
  sprintf ( buffer, "floats: %f\n", 3.1416f );
  Serial.print( buffer );
  sprintf ( buffer, "Preceding with blanks: %10d \n", 1977 );
  Serial.print( buffer );  
  sprintf ( buffer, "Preceding with zeros: %010d \n", 1977 );
  Serial.print( buffer );    
  sprintf ( buffer, "Some different radices: DEC: %ld HEX:%lx \n", 650000, 650000 );
  Serial.print( buffer );
}

void loop(){}

On a mega the code is 574 bytes smaller, on an Uno 594 bytes less.
This leaves more room for things like a precision option or even the nice improvements under-way by robtillart here; these would allow different notations ( sprintf 'e' specifier ). And these might even still be smaller.

If you like these improvements please leave a message and tell your friends smiley-razz, it would be great to have something of my own added to the Arduino core.

Note: You have to be logged in to download the files unfortunately.

The file is best viewed in a document viewer like WordPad, notepad++ as the IDE doesn't handle tabs great.

THERE IS A NEW VERSION, CHECK MY POST BELOW!
954  Development / Other Software Development / Re: [GUI] Advanced Object Oriented User Interface (ArdUI) on: May 20, 2013, 10:23:03 pm
Just to clarify for people, this is for the Due, or AVR series?
955  Using Arduino / Programming Questions / Re: Proofread Code Please? on: May 20, 2013, 09:49:24 pm
This code is controlling multiple inputs. Yours just looks like one.

It uses arrays to control all 5 servos: http://www.cplusplus.com/doc/tutorial/arrays/
956  Using Arduino / Programming Questions / Re: Proofread Code Please? on: May 20, 2013, 09:48:40 pm
Check the spelling, the names don't match.
957  Using Arduino / Programming Questions / Re: Proofread Code Please? on: May 20, 2013, 09:45:40 pm
You make two checks for 'else if (bytereceipt == 202)'
so your checks only go from 201 to 204, you want it to go to 205?

Here is a version of your code using arrays:

Code:
#include <Servo.h>

Servo Servos[ 5 ];

void setup()
{
  for( char i = 0 ; i < 5 ; ++i )
    Servos[ i ].attach( i + 4 );
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available())
  {
    byte bytereceipt = Serial.read();
    
    if( bytereceipt >= 201 && bytereceipt <= 205 ){
      delay(5);
      Servos[ bytereceipt - 201 ].write(bytereceipt);
      Serial.write(bytereceipt);
    }
  }
}

Code edited to remove second read from serial.
958  Using Arduino / Programming Questions / Re: am i doing some thing wrong that i dont get any help on: May 19, 2013, 10:48:03 pm
You might have to wait a while for a response ( some people are asleep right now! ), liudr may even see your previous thread at some stage.

How do you set alarms now?
Can you set more than one?
Can you just add more alarms for the off time?

Do you have a link to the original project you are using?
959  Using Arduino / Programming Questions / Re: exit() seems premature on: May 19, 2013, 10:09:59 pm
With your version, the code posted in the OP would work.

Yes, you are correct interrupts are disabled, I assume the OP got the general gist of my comment though.

Quote
The concept of exiting simply doesn't translate to an Arduino environment; there is nothing to exit to.

Not going to name names, but I use an operating system that occasionally likes to exit out to nowhere by showing a lovely blue screen when it can't cope any more.
960  Using Arduino / Programming Questions / Re: Inexplicable action of the Arduino MEGA on: May 19, 2013, 03:23:09 pm
Code:
int Afremmodule[8]= {23,25,27,29,31,33,35,37};

Pin 31 is index 4, not 5
Pages: 1 ... 62 63 [64] 65 66 ... 143