Show Posts
Pages: 1 2 3 [4] 5 6 ... 146
46  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;
47  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;
48  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 );
49  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.
50  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 );
51  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
52  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
53  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);}
54  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;
55  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.
56  Using Arduino / Programming Questions / Re: Why does sketch using SD.h suddenly need #include <SPI.h> to compile? on: July 30, 2014, 05:00:43 pm
Read the third bullet point:

http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html

The SD card uses SPI for its communication.
57  Using Arduino / Programming Questions / Re: How to get Int value ? on: July 30, 2014, 01:38:12 am
Also,  the following line looks invalid.   Even if "mirf.payload"  evaluates to a valid value for the length of an array,   I don't think you can define an array this way.   The array declaration has to either have a length which can be evaluated at compile-time,  or else it has to be dynamically allocated.

Code:
byte data[Mirf.payload];

In C++ its an extension provided by GCC, its only part of ISO C99

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
58  Community / Website and Forum / Re: Unread posts not showing on: July 29, 2014, 11:38:09 pm
1 day I think.

The Arduino boards click over at 3:00pm my local time, Which is in 23 mins, I'll let you know.

Here something cool.

All unread posts from 'Programming Questions' can be accessed from the index:

http://forum.arduino.cc/index.php?action=unread;board=4.0

Which &all can be added to.

But I view more than one forum, so what about filtering out the boards you do not use ( other languages, etc... ). Took a bit to figure out but I got the syntax:

This will show all unread replies from:
  • Programming Questions
  • General Electronics
  • Website and forum

http://forum.arduino.cc/index.php?action=unread;boards=4.0,5.0,24.0;all

notice 'board' is now 'boards', also ';' can replace '&'.

Very handy indeed.
59  Community / Website and Forum / Re: Unread posts not showing on: July 29, 2014, 11:18:09 pm
Its not supposed to show all of them, just the ones in a certain time frame.

If you want to view all the posts, add &all to the end of the URL:

http://forum.arduino.cc/index.php?action=unread&all

My unread topics is currently 16 pages, with &all its 1086 pages.
60  Using Arduino / Programming Questions / Re: multiple if or for loop with if inside on: July 28, 2014, 09:51:25 pm
That is neat.

I'd be needing to crack a book for a good while to catch up to C++11, for sure. smiley-grin

Cheers.

For quick entry to C++11, searching google for the topics listed in the following link will probably help out: https://gcc.gnu.org/gcc-4.8/cxx0x_status.html
Pages: 1 2 3 [4] 5 6 ... 146