Show Posts
Pages: 1 2 3 [4] 5 6 ... 147
46  Using Arduino / Programming Questions / Re: Low memory available on: August 04, 2014, 05:35:27 am
No hash defines do not use any extra memory.

It'd be the declaration after them that uses the extra memory.
47  Using Arduino / Programming Questions / Re: Low memory available on: August 03, 2014, 09:26:39 pm
As you have no code of your own there, you can only modify the libraries in an attempt to reduce the ram usage.

Looks like your collection of libraries are best suited for a Mega2560, with 8k of ram.
48  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 10:26:10 am
Quote
I don't think you *would* have seen the issue, as there was only the one global declaration for the pointer.

Uh huh,

Its not an issue, you created a local variable.
If you use the same name in a nested scope, it replaces the ancestor declaration ( function scope is nested in file scope ). You must explicitly access any version replaced ( :: is your friend ).

Code:
PARAMETERS * ArrayPointer;

void setup(){
  ArrayPointer[ 0 ] = 1; //Global variable.
}

void Foo( void ){

ArrayPointer[ 0 ] = 1; //Global variable.

PARAMETERS * ArrayPointer; //Local variable declared with the same name.

ArrayPointer[ 0 ] = 2; //local variable.

::ArrayPointer[ 0 ] = 3; //Global variable.
}

void Foo2( PARAMETERS * ArrayPointer ){
  ArrayPointer[ 0 ] = 1; //formal parameter has automatic storage ( like local variables ) and replaces global name.
  ::ArrayPointer[ 0 ] = 3; //Global variable.
}

To sum it up, your issue would have been spotted by anybody comfortable with C or C++.
49  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 09:53:13 am
Quote
The fact that it is preceded by "PARAMETERS *"  creates another instance of my pointer.  The functions that refer to the globally-defined pointer are using a pointer that points nowhere...

That's what you get for not posting your code, would have been resolved some time ago.
50  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 07:19:34 am
So, how about referencing the array contents ?
Code:
TparmsA[index].p_type = p_char;
TparmsA[index].parms[0] = p_value;
Is that right ?

Yep.

If your code is expecting the data to be zero/in a default state, add a constructor, or default initialize the POD like the code below.
 
Code:
T *ptr = new T[ N ]();
51  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 06:00:33 am
If T is your type/structure, and N is a constant:

Code:
T *ptr = new T[ N ];
//...
delete [] ptr;

If you use 1.5.7, you can enable C++11 and simply use auto:
Code:
auto ptr = new T[ N ];
//...
delete [] ptr;
52  Using Arduino / Programming Questions / Re: declaring several arrays (LUTs) on: August 02, 2014, 05:55:42 am
If your arrays are the same length, as AWOL pointed out, a typedef would be useful.

Code:
typedef uint16_t arr_t[ WAVE_SAMPLES ];

arr_t ii, iii, iiii, iv;
53  Using Arduino / Programming Questions / Re: Send serialized data improvement -Union vs struct on: August 02, 2014, 05:09:22 am
Quote
The compiler claims it can't do it
As mentioned, you need to cast to a char type. I think .write accepts unsigned char*

Unions can add functionality, which may be useful. However westfw pointed out that the Serial lib can accept a pointer and length. If this wasn't the case a union would be useful if only single elements/bytes are accepted, but again not necessary.


If I was going to use one, I would do this.

Create a generic 'Serialize' class:
Code:
template< typename T >
  union Serialize{
    T data;
    uint8_t bytes[ sizeof( T ) ];
    
    const uint8_t *begin(){ return bytes; }
    const uint8_t *end(){ return bytes + sizeof( T ); }
};

Using your current structures, I could now declare a type with serialization features. And rather than creating instances of packet_t, we use the aliased type:
Code:
typedef Serialize< packet_t > Frame;

Frame f;

This is basically where you were at with your specific union. You can access the object through 'f.data', and each byte through 'f.bytes'.

The begin and end methods give you the start and end pointers, however stl iterators can use these to iterate your object.

And whats even better, your serialized data is now compatible with C++11 ranged loops:
Code:
 for( auto i : f )
    Serial.write( i );
54  Using Arduino / Programming Questions / Re: Pointer problems on: August 02, 2014, 04:31:09 am
Quote
Operator new/new[] does not take bytes, but the number of elements.
It's supposed to be bytes by the time the function gets called, but apparently the compiler should be converting the "new" or new[] statement into the appropriate byte count for you.  (it does seem to work, too.)

The 'new function' does take bytes, which is shown in the raw allocation I posted above ( its a function call ). However the 'operator new' is only used with types, internally it uses sizeof to call the new function before running the objects constructor.
55  Using Arduino / Programming Questions / Re: Pointer problems on: August 01, 2014, 11:02:30 pm
New[] and delete[] were added 2 years ago ( since at least 1.0.5 ). They do work as I've used them.

https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/new.h

Your code may have errors elsewhere that may appear to be the allocations fault.

Operator new/new[] does not take bytes, but the number of elements.

To allocate raw bytes you must call the new function, not the operator:
Code:
//Calculate the length of 10 float objects.
int len = sizeof( float ) * 10;
 
//Allocate 'len' bytes of data.
void *ptr = ::operator new( len );
56  Using Arduino / Programming Questions / Re: How to clear a single variable on: August 01, 2014, 10:17:19 am
B11000111 | B00001000(0x08) = B11001111

B11001111 ^ B00001000(0x08) = B11000111

The Xor operator is only useful when you know the state of a bit, I.e toggling.
If you want to ensure a bit is off, you must mask it and remove it from the set.

Code: (turns a bit on)
B11001111 ^ B00010000
57  Using Arduino / Programming Questions / Re: How to clear a single variable on: August 01, 2014, 10:06:54 am
I Tried the "and" alone and it did not work. What does the ~ do?


This should explain it: http://stackoverflow.com/a/791340
58  Using Arduino / Programming Questions / Re: How to clear a single variable on: August 01, 2014, 09:50:22 am
I believe this may be what you are after, it requires a read though:

Code:
{PORTA &= ~(1<<PA4);}
59  Using Arduino / Programming Questions / Re: how to initialize struct constructor with string on: August 01, 2014, 08:36:18 am
Loose the typedef, you're in C++.

In the IDE 1.0.x, or 1.5.6 and below, you will have to use the body of the constructor to assign the data:
Code:
struct Sensor_t{
  char    code[5];
  char   value[5];
  Sensor_t(){
    strcpy( code, "SXXX" );
    strcpy( value, "0000" );
  }
};
Sensor_t income,outcome;

If you have 1.5.7, you can enable C++11 and do this:
Code:
struct Sensor_t{
  char    code[6];
  char   value[6];
  Sensor_t():code{"SXXX"},value{"0000"}{}
};
Sensor_t income,outcome;
60  Using Arduino / Programming Questions / Re: error typecasting word to byte. on: July 31, 2014, 08:48:53 pm
'Output' is an array and an rvalue, you cannot assign to it.

To index a particular element of Output, use the sub script operators:
Code:
Output[ 0 ] =  (byte)(Accum/255);

Why is it an array of size one. Its only useful if a function you use requires an array. Why not just a plain old byte.
Pages: 1 2 3 [4] 5 6 ... 147