Show Posts
Pages: 1 ... 52 53 [54] 55 56 ... 146
796  Using Arduino / Programming Questions / Re: typedef struct problem on: December 08, 2013, 01:54:01 am
I've used my own prototypes since 0021, never had a problem of duplicate prototypes.

Remember the signature must match exactly:

Pretend sketch:
Code:
void Foo(){
 Serial.print( "HI" );
}

void setup() {
  Foo();
}

void loop() { }

Adding the prototype:
Code:
void Foo();

produces:
Code:
#line 1 "BareMinimum.ino"

#include "Arduino.h"
void setup();
void loop();
#line 2
void Foo();

void Foo(){
 Serial.print( "HI" );
}

void setup() {
  Foo();
}

void loop() { }

If I add the same prototype ( but not a textual match )
Code:
void  Foo (); //Two spaces instead of one.

this produces:
Code:
#line 1 "BareMinimum.ino"

#include "Arduino.h"
void Foo();
void setup();
void loop();
#line 2
void  Foo ();

void Foo(){
 Serial.print( "HI" );
}

void setup() {
  Foo();
}

void loop() { }

The multiple definitions are fine unless using a struct, as the object is still not seen before use.
However like I said I have had no problem with duplicate prototypes appearing and breaking structs.
I have used 0021 to 1.0.5 and tested 1.5.2.

Download my test sketch and try it. If it works, there is no duplicates.
797  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 08, 2013, 12:58:59 am
Quote
Anyone interested in a seventh assembly version?

Give me all the version you have got, I'll add it into the sketch. Then we can have a large set of methods for all different usage/learning purposes.

Also, ppl, with new versions, comment each line that my not be so straight forward to newbies.

Quote
Hey Pyro, after changing "byte table[16]" to "static const byte table[16]" I was able to beat your time by almost 177,084 us

Cool, I'll go fix up my sketch and add in a few others. I still like the idea of not using pre-calculated data, especially static data that will never relinquish its memory, I will have to see how well yours performs as a compile time constant, mine completely evaporates to an LDI.
798  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 07, 2013, 09:40:42 am
The results are in, they were partly as I expected.

There are six algorithms used. I named each one under the person who posted it, not who wrote it.

Order of appearence in thread:

Here are the results.
The fastest function is 28% faster than its closest alternative.
It is 98% faster than the slowest.

If you are tight on flash memory MarkT's version is 68% smaller than the next version,
if you are after speed, my version takes the cake.

Order by speed ( On Uno ).
799  Using Arduino / Programming Questions / Re: Arduino : get the complete generated code by Arduino IDE on: December 07, 2013, 05:49:38 am
In windows you can make your own shortcut.
Open Explorer type 'shell:sendto' in that folder create a .bat file with the below text, fix the path to your IDE.

Code:
D:\arduino-1.5.2\hardware\tools\avr\bin\avr-objdump.exe -S %1 > %1%.txt

Then once compiled the IDE will give you the temp compile path ( with verbose option on ), in that folder right click on XXX.elf and choose 'Send To->yourbat.bat'

it will generate XXX.elf.txt.

The same for RAM usage can be done using this in a bat file:
Code:
D:\arduino-1.5.2\hardware\tools\avr\bin\avr-nm.exe -n -S %1 >nm_out.txt

Outputs nm_out.txt ( use also on XXX.elf )

No need to construct command lines out of file paths once setup.
800  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 07, 2013, 05:25:57 am
So I made that mirror function like it is, (I did not Google it) and it seems to work rather well.

Yes, same goes here. I tried to do it without a loop, which was easier than I thought.

Its good however that others have posted their own and excerpts from Sean Anderson's bit hacks. Out of curiosity, I'm going to profile them to see what each one can do. I want to make an assumption that the short and sweet 64-bit versions will not pull though in a timely fashion, i'm curious to see what the three non-looped versions can do compared to a loop.
801  Using Arduino / Programming Questions / Re: problem with struct in Arduino IDE on: December 06, 2013, 09:33:28 am
I don't think that will work either, you are defining a struct as queue, then trying to alias to the same name.

By providing your own prototype ( below struct ) the IDE will not put one at the top.

Code:
int put ( queue* p ,int x);
802  Using Arduino / Programming Questions / Re: byte mirror code don't work on: December 06, 2013, 07:39:42 am
I had a go at my own and shaved a few bytes off the compiled size. Without a loop it may also be faster, gonna test soon.

EDIT: appears it is heavily inlined. Still not sure on the timing, but would be quite large in a function.

Code:
struct BitByte{
 int A : 1;
 int B : 1;
 int C : 1;
 int D : 1;
 int E : 1;
 int F : 1;
 int T : 1;
 int P : 1;
};

const char MirrorByte( const char c ){

  union{
    char b;
    BitByte a;
  } f = {c};
  
  union{
    BitByte g;
    char h;
  } i = { f.a.P, f.a.T, f.a.F, f.a.E, f.a.D, f.a.C, f.a.B, f.a.A };  
  
  return i.h;
}

void setup() {
  Serial.begin( 9600 );
  char c = 0xFE;
  Serial.println( ( unsigned char ) c, HEX );
  Serial.println( ( unsigned char ) MirrorByte(  c ), HEX );
}

void loop() {}
803  Using Arduino / Programming Questions / Re: Variables scope on: December 06, 2013, 05:40:39 am
Here is a post I made some time ago which shows how globals can come back and bite you, without even so much as a warning from the compiler... very hard to trace in a big application:

http://forum.arduino.cc/index.php?topic=124367.msg939621#msg939621

804  Using Arduino / Storage / Re: sdFat library error code need help on: December 05, 2013, 10:08:16 pm
Can you just save the data to an image file extension ( .jpg, .png )? <<EDIT: just saw post above.
To beat the size, can you place a chuck of empty data in it, or does it go on dimensions?

If its dimensions, you could place a bitmap header reserving a block of data represented by the width and height, then fill in the blank pixel data with your logging.

Kind of similar to packing the data into a 'packet' for network transmission.
805  Using Arduino / Programming Questions / Re: Variables scope on: December 05, 2013, 09:50:42 pm
Quote
Uh oh!  Before you know it, someone's going to install a homebrew channel on your quad-copter.   smiley-wink

That'll be the day.

The code I posted above your post and the stack discussion are in no way unique to AVR, it has consequences in both Harvard and Von-neumann architectures. In fact its even worse on Von-neumann as code memory is not separate, and can lead to code injection.
806  Using Arduino / Programming Questions / Re: Variables scope on: December 05, 2013, 09:39:17 pm
Got it, here is a nice proof of concept about stack variables, and how the memory is reused.

There are two function calls, one has an initialised value, the other doesn't. This is a security nightmare scenario.

Tested on an Uno
Code:
void A(){
  uint64_t a = uint64_t( 0xaabbccdd ) << 32 | 0x11223344;
  Serial.print( "Pointer Address: " );
  Serial.println( (unsigned int )&a );
  Serial.print( "Lower 32: " );
  Serial.println( (unsigned long )a, HEX );
  Serial.print( "Upper 32: " );
  Serial.println( (unsigned long )(a >> 32), HEX );
}

void B(){
  uint64_t b;
  Serial.print( "Pointer Address: " );
  Serial.println( (unsigned int )&b );
  Serial.print( "Lower 32: " );
  Serial.println( (unsigned long )b, HEX );
  Serial.print( "Upper 32: " );
  Serial.println( (unsigned long )(b >> 32), HEX );
}

void setup (){
  Serial.begin (115200);
  A();
  B();
}

void loop () { }
807  Using Arduino / Programming Questions / Re: Variables scope on: December 05, 2013, 09:11:14 pm
All global memory/static data is zero initialised, the stack is too, however the stack is dirtied long before you can get your hands on it.

Local variables are placed on the stack ( forget optimisation into registers for a sec ), this stack data could have contained variables from a previous function call, therefore the data inside is kind of random, maybe the last use of the ram location set it to zero and you are lucky, but it cannot be relied upon.

Its the perfect scenario for security vulnerabilities to occur.
808  Using Arduino / Programming Questions / Re: Variables scope on: December 05, 2013, 08:38:30 pm
Taking your second point first, I tried to prove that local variables are not initialized with not a huge amount of success.

The variables are printed as zero, which suggests they are initialized to zero. However the compiler warns:

Code:
sketch_dec06d.ino: In function 'void foo()':
sketch_dec06d.ino:11: warning: 'b' is used uninitialized in this function
sketch_dec06d.ino:11: warning: 'c' is used uninitialized in this function
sketch_dec06d.ino:11: warning: 'd' is used uninitialized in this function
sketch_dec06d.ino:11: warning: 'e' is used uninitialized in this function
sketch_dec06d.ino:11: warning: 'f' is used uninitialized in this function

So the warning suggests that they are just fortuitously zero, not by design.

This is due ( maybe ) to the vars being spread over registers, and appears they are reset, using volatile I get:

Code:
-821007856
876085505
825438259
1744844341
27721984
-130649
loop
1003460
-821009394
825753857
875769904
1744842806
27721984
-130649
loop
2006988

Each time loop is encountered, the vars have a different value.
Going to read up on how the avr spreads large values accross multiple registers, it might have to clear to 0 for sanity.
809  Using Arduino / Storage / Re: sdFat library error code need help on: December 05, 2013, 08:14:09 pm
Just on a side not, that SD card you are using, does the Wifi capability work in the arduino shield, seems like a really neat idea.
810  Using Arduino / Programming Questions / Re: typedef struct problem on: December 05, 2013, 07:51:30 pm
Just make the struct a real struct, the typedef does nothing useful here ( C++, not C )

This code isn't in a function:
Code:
irSharedData.pin = IR_INPUT_PIN; // initialise the needed structure values, not all need initialising
irSharedData.state = IRSTATE_WAITING;

for(int i = 0; i < sizeof(irSharedData.signals); i++) // don't really need to do this but will make any debugging easier
  irSharedData.signals[i] = 0;
irSharedData.signalIndex = 0;
irSharedData.errorCode = IRERR_NOERROR;
irSharedData.pin = IR_INPUT_PIN; // initialise the needed structure values, not all need initialising
irSharedData.state = IRSTATE_WAITING;
.

Use headers like everyone has pointed out.

Pages: 1 ... 52 53 [54] 55 56 ... 146