avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] Creating a table of numbers with different types


From: Dave Hylands
Subject: Re: [avr-libc-dev] Creating a table of numbers with different types
Date: Sat, 19 Mar 2011 11:11:55 -0700

Hi,

On Sat, Mar 19, 2011 at 5:11 AM, Marek Pietrzak <address@hidden> wrote:
> I don't think the compiler can tell what the address of "x" would be at
> compile time. Common practice is to initialize such a table at run time.
> I can imagine (just a thought), an assembler .org directive to reserve a
> pgm memory for your table at fixed address which would be a constant
> expression then, but it's not worth doing in my opinion.
>
> Regards, Marek
>
> On Sat, 2011-03-19 at 00:20 +0100, Volkmar Dierkes wrote:
>> Hello,
>>
>> I want to create a table like this:
>>
>> unsigned char lcd_display_function[] PROGMEM = {
>>          LOK_FCT0_POSX, LOK_FCT0_POSY, LOK_FCT0_ENDX, LOK_FCT0_POSY, 
>> SOFTKEY_BORDER0_COLOR,
>>         CLCD_FIFO_CMD_SUBEND};
>>
>> Each value is a constant in a range of 0<=x<=255 defined with #define.
>>
>> This works fine so far, but I am in the need to add also an address of
>> another table in PROGMEM, like this:
>>
>> unsigned char foo[] PROGMEM = {0, 1, 2};
>>
>> I tried several ways to add this to the list above, like this:
>>
>> #define lo8(x) (((int)(x)&0xff))
>> #define hi8(x) (((int)(x)>>8))
>>
>> unsigned char lcd_display_function[] PROGMEM = {
>>         CLCD_FIFO_CMD_FILLRECT, LOK_FCT0_POSX, LOK_FCT0_POSY, LOK_FCT0_ENDX, 
>> LOK_FCT0_POSY, SOFTKEY_BORDER0_COLOR,
>>         lo8(foo), hi8(foo),
>>         CLCD_FIFO_CMD_SUBEND};

You might want to create one more level of indirection.
Have an array of pointers, of which foo is one, and store the index of
the element rather than the address  of it.

I think that the other way of solving this is to use inline assembler
to create your variables. Then you can mix up the types and store some
bytes, store some pointers, etc.

One other way, which would be pure C, would be to create a custom
structure which has a mixture of bytes and pointers, and instantiate
one of those. You may need to use packing to ensure that there are no
gaps before the pointer (I don't recall if pointers in the AVR require
2 byte alignment or not).

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com



reply via email to

[Prev in Thread] Current Thread [Next in Thread]