Show Posts
Pages: 1 ... 72 73 [74] 75 76 ... 121
1096  Using Arduino / Programming Questions / Re: PROGMEM: storing and retrieving unsigned long data on: October 16, 2012, 08:09:22 am
Here is a link to all the PROGMEM functionality

the macros like pgm_read_byte are what you need.
1097  Using Arduino / Programming Questions / Re: Creating a struct of RGB Values on: October 14, 2012, 11:59:37 pm
I still need to work out how to dereference the struct to derive the individual rgb values. Suggestions?

Do you mean like this?

BtnCol main_menu = {{0, 32, 0},{34, 139, 34},{144, 238, 144}};

//... Use main_menu somewhere
analogWrite( 3, main_menu.fill.r );

If you need a 32-bit colour, define your struct as follows.

struct RGB {

 operator uint32_t&(){ return *( uint32_t* ) this; }

 uint8_t a;
 uint8_t r;
 uint8_t g;
 uint8_t b;
1098  Using Arduino / Programming Questions / Re: Bitshifting bytes to form a long var fails! on: October 14, 2012, 08:58:38 pm
I understand this, however, consider

Serial.println (0xFECDA, HEX);

0xFECDA is also a compile time constant.

If the inputs were dynamic, it would be interesting to see how my two 8-bit ( moved 4 bits ) shifts compare to the previous 4 shifts of mixed sizes.
1099  Using Arduino / Programming Questions / Re: Bitshifting bytes to form a long var fails! on: October 14, 2012, 08:40:31 pm
Not sure what the go is here, but the code I posted above appears faster than manually typing:

Serial.println (0xFECDA, HEX);
1100  Using Arduino / Programming Questions / Re: Bitshifting bytes to form a long var fails! on: October 14, 2012, 07:07:20 pm
How about this:

 struct NewType{
    char z;
    char a;
    char b;
    char c;
  union NewUnion{
    NewType n_T;
    unsigned long l;
void setup ()
void loop ()
  byte a = 0x0F;
  byte b = 0x0E;
  byte c = 0x0C;
  byte d = 0x0D;
  byte e = 0x0A;

  NewUnion aa = {{ ( d << 4 ) | e, ( b << 4 ) | c, a, 0 }};
  Serial.println (aa.l, HEX);

2,166 bytes now compared to previous 2,260 bytes

Bad choice of names, I know, just rushing

EDIT: added extra {} to drop warning.
1101  Using Arduino / Programming Questions / Re: Bitshifting bytes to form a long var fails! on: October 14, 2012, 06:17:41 pm
I'm trying to think of your specific case, you stated "It is a hardware based solution available to any avr" there is no barrel shifter and you don't expect hardware multiplication. Just to make sure, you aren't thinking of some obscure method using the SPI shift register or port registers in loop back config are you.

Is it something using asm, ( BTW compiler won't change asm MUL operation, inline asm subverts the optimiser. )  Or just c++?
1102  Using Arduino / Programming Questions / Re: Bitshifting bytes to form a long var fails! on: October 14, 2012, 05:28:52 pm
I have a suspicion...

Keep your speculation coming. Think out of the box and keep it simple.

Well, I suppose a union of a long and the appropriate number of bit fields correctly padded and aligned would produce the desired effect. This would still not be my preferred solution, but I think it could be made to work if the compiler cooperates. You would be at the mercy of the compiler, though, and I don't know whether it would actually work in Arduino. Have you tried it and got it working?

I tried with bit fields, no luck at getting the code close to nicks example's output above.

It is a hardware based solution available to any avr

I think the 'MUL' avr instruction is what will do the work in 2 clock cycles. I don't know ASM so could someone check it out. I'll investigate myself but may be some time as I have to read a little first.

EDIT again: ( x << n ) == ( x MUL 2 ^n )

8 << 3 == 8 MUL 8

^ being power of not C++ XOR
1103  Using Arduino / Programming Questions / Re: switch:case question on: October 10, 2012, 06:32:58 am
What does static contribute to a constant, global variable?

It specifies internal linkage.
Just one use,
If a variable is declared (.h) as extern ( external linkage ), a static definition of a variable gives the 'defined in' translation unit its own copy initialised to the static initialisation, while other translation units share the definition of external variable.
1104  Development / Other Software Development / Replacement for AVR libc ATOMIC_BLOCK macros. UPDATED on: October 07, 2012, 09:17:46 pm
G'day all,

I have finished a major update to the library, it is more of an extension as the original functionality hasn't changed.
The library now supports in-line atomic operations on any kind of element* giving you greater control of how long interrupts are held. Some situations may warrant the atomic/critical section to be as short as possible, accessing 'safe' data may be better with short multiple accesses, compared to one long blocking operation. This creates a very easy interface to do so.

 For example ( explanations in updated reply #1 ):

  • Atomic reads.
  • Atomic writes.
  • Atomic function calls.
  • Atomic pointer manipulation.

You can download the new code from the first post [here]. The documentation has been updated there as well as a full explanation inside the AtomicBlock.h file. There is more documentation to come, I just wanted to get a quick run down of the new features.

This uses some high level C++ techniques to accomplish safe and efficient operation. If you would like explanations of any parts of the code feel free to ask.

*Object member functions are not currently supported, next update.
1105  Development / Other Software Development / Re: Replacement for AVR libc ATOMIC_BLOCK macros. on: October 04, 2012, 10:12:53 pm
The only caveat (as far as I can tell) is placement
The Libc macro does not have the same potential problem.
This is a problem indeed, but I believe the macros do suffer too as the atomic operation doesn't take effect until the macro is encountered. I think this is entirely up to the programmer to order their code carefuly, but worth a mention in the documnetation for sure.

@WizenedEE, that is a nice example of a 'break' failure.

Its good to see a few things being discussed weren't even something I had considered.

Now that I can consider the library mildly stable after doing some tests, I can start experimenting with some new ideas.
I have implemented some nice extensions coming soon, with entirely new ways of doing things for more flexibility.
1106  Using Arduino / Programming Questions / Re: an error "expected unqualified-id before string constant" when compiling on: October 04, 2012, 05:05:51 pm
Check your indentation.
This flavour of C++ doesn't allow function declarations inside other functions.

You aren't supposed to check your indentation, more re-do it so you can read your own code easier.

Maybe you are unsure because they are macros, but ISR(){ //... } is a function definition and must go at file scope like loop(), rather than in loop() itself ( nested ).
//Should not be declared inside a function
ISR(TIMER2_OVF_vect) {

1107  Using Arduino / Programming Questions / Re: Pointer, generic class, functions… so much complex for me on: October 04, 2012, 04:20:15 am
There is no code missing.
As the class does not define any constructors / copy constructors it is considered a POD ( plain old data ) type. Which can be initialised with an 'initialiser list'.

MPLArray< uint8_t, 4 > m_Array = { 'a', 'b', 'c', '\0' };

initialises the first 4 POD's inside the class, which is the array t_Data.

and you probably want to put it where ever the array was declared before.
Also replace { 'a', 'b', 'c', '\0' } with whatever you need to store.

Why are you needing the template, and 'objectizing' your code for no reason is not the way to lean object oriented paradigms.
1108  Using Arduino / Programming Questions / Re: Pointer, generic class, functions… so much complex for me on: October 04, 2012, 02:38:44 am
You are breaking the encapsulation by storing the data outside the class.
Why are you using a template for this, if the result object is inside another class, you need some way of using the template parameter. If not then you don't really need a template as uint8_t is enforced.

Here is an array template that works statically.

template< typename _Type, size_t _Len >
  class MPLArray{
        Length = _Len,
        Size = sizeof( _Type ) * _Len,
      _Type& operator[]( uint16_t i_Index ) { return this->t_Data[ i_Index ]; }
      operator _Type*(){ return this->t_Data; }

      _Type t_Data[ Length ];

Then use it like
MPLArray< uint8_t, 4 > m_Array = { 'a', 'b', 'c', '\0' };

Serial.println( m_Array.Size );
Serial.println( m_Array.Length );
Serial.println( m_Array[ 0 ] );
Serial.println( m_Array );

Just like a normal array, except with a size and length value.

1109  Using Arduino / Programming Questions / Re: Error compiling again! on: October 04, 2012, 02:06:17 am
It appears the tone library and IRremote library both user timer 2 interrupts to do their work.
1110  Development / Other Software Development / Re: runEvery (the next Blink Without Delay) on: October 03, 2012, 07:30:27 am
I agree the value needs to be restricted. If t doesn't cast down then the event never occurs for some inputs, otherwise the value can overflow. My option allowed a compile time solution, but other methods are needed for run-time safety.

Also WizenedEE, your macro is missing an '('

Pages: 1 ... 72 73 [74] 75 76 ... 121