Author Topic: C-code; array of struct ? (Atmel Studio 6)  (Read 57877 times)

0 Members and 1 Guest are viewing this topic.

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11790
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #100 on: August 13, 2013, 04:31:13 am »
Could one issue be that the initialisation is convoluted in some cases?

Code: [Select]
Buttons[28].left = BtnCol8Left;
The above would expand out to:

Code: [Select]
Buttons[28].left = 32 + (7 * (82 + 13));
Can I (should I) change those #define to constants with a calculated (at compile time) value?

That's easy to do. First, change your formula to one with variables in it. For example, it might be

Code: [Select]
(row-1)*80+(col-1)*12+16
That being the case, define a macro:

Code: [Select]
#define BTNCOLUMN(row, col)  (row-1)*80+(col-1)*12+16
Now use the macro in your initializer:

Code: [Select]
Buttons[28].left = BTNCOLUMN(3,4)
 

Offline ve7xen

  • Super Contributor
  • ***
  • Posts: 1192
  • Country: ca
    • VE7XEN Blog
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #101 on: August 13, 2013, 04:36:51 am »
Could one issue be that the initialisation is convoluted in some cases?

Code: [Select]
Buttons[28].left = BtnCol8Left;
The above would expand out to:

Code: [Select]
Buttons[28].left = 32 + (7 * (82 + 13));
Can I (should I) change those #define to constants with a calculated (at compile time) value?
This is what macros are for in the first place. The preprocessor will expand it, the compiler will know it's all literals and turn it into a simple assignment. Keep things clear, there's usually no reason to 'simplify' expressions, the compiler can handle that just fine.

If you assign caption = "" at startup, how do the buttons gain a caption later?
73 de VE7XEN
He/Him
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #102 on: August 13, 2013, 04:40:24 am »
That's easy to do. First, change your formula to one with variables in it. For example, it might be

Code: [Select]
(row-1)*80+(col-1)*12+16
That being the case, define a macro:

Code: [Select]
#define BTNCOLUMN(row, col)  (row-1)*80+(col-1)*12+16
Now use the macro in your initializer:

Code: [Select]
Buttons[28].left = BTNCOLUMN(3,4)

Wouldn't that assume that all rows and columns use the same H & V spacing?  In my case there is one row spaced further up the screen than the rest.
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #103 on: August 13, 2013, 04:42:39 am »
If you assign caption = "" at startup, how do the buttons gain a caption later?

The problem doesn't appear at startup (first draw), only later when I redraw a button that (then) has a caption of "" or " ".

EDIT: I did try to not draw the text if the caption was "", but didn't figure out the syntax.
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #104 on: August 13, 2013, 05:23:48 am »
Just for kicks, I set the button draw routine to print the caption pointer address instead of the caption itself.  Each address seems to be (decimal) 19 higher than the one before.
 

Offline ve7xen

  • Super Contributor
  • ***
  • Posts: 1192
  • Country: ca
    • VE7XEN Blog
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #105 on: August 13, 2013, 07:22:22 am »
If you assign caption = "" at startup, how do the buttons gain a caption later?

The problem doesn't appear at startup (first draw), only later when I redraw a button that (then) has a caption of "" or " ".

EDIT: I did try to not draw the text if the caption was "", but didn't figure out the syntax.
I meant "show me the code you use to update the caption later" :).
73 de VE7XEN
He/Him
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1289
  • Country: fi
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #106 on: August 13, 2013, 07:56:31 am »
I was going to ask you to post the entire source unless it is somehow confidential.
The usual suspects are overwritten pointers somewhere; not easily located by just some code snippets.
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline HackedFridgeMagnet

  • Super Contributor
  • ***
  • Posts: 2028
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #107 on: August 13, 2013, 08:03:10 am »
Did you have a debugger? that would help.
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #108 on: August 13, 2013, 08:09:55 am »
Quote
I meant "show me the code you use to update the caption later" :).

The code that updates the caption later is the same procedure that draws them the first time.

Code: [Select]
Buttons[0].caption = "";
DrawButtons(&Buttons[0]);

Quote
I was going to ask you to post the entire source unless it is somehow confidential.

It is a commercial product in development, so I don't want to publicly release the code for it.  I realise that may seem rude when you guys are helping me.   :-X

Quote
Did you have a debugger? that would help.

Ummm... I don't know...  The programmer is an AVRISPII.   :-//
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1289
  • Country: fi
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #109 on: August 13, 2013, 08:49:02 am »

Quote
I was going to ask you to post the entire source unless it is somehow confidential.

It is a commercial product in development, so I don't want to publicly release the code for it.  I realise that may seem rude when you guys are helping me.   :-X

Quote
Did you have a debugger? that would help.

Ummm... I don't know...  The programmer is an AVRISPII.   :-//

Even though buttons have been re-invented any number of times, i understand if you don't want to disclose a commercial effort.

Re AVRISP - it is just a programmer, i.e. you don't have a debugger. Since this is a commercial effort you should make it a priority to get one and learn how to use it.
Any number of bugs can easily stay hidden in released code unless you have a proper test plan, and execute the plan as well. An integral part of all low level testing is debugging. Just as an example, i always walk through every algorithm i write, in the Studio simulator. First with inputs that make sense, to verify that the functionality is there (testing for functionality) then with inputs that don't make sense, to test for robustness and recovery from errors elsewhere (testing against bugs).

In by book anyone who develops commercial software should understand the principle of the V model of quality assurance https://en.wikipedia.org/wiki/V-Modelwhether they actually use one of its many variants or not. While a relatively waterfall-ish model that seems ill suited to modern agile ways of working such as scrum, it is relatively easily integrated into cyclical development as well. In practice you have to go through the phases anyway in order to have a verified end result ready for deployment.
See also http://istqbexamcertification.com/what-is-v-model-advantages-disadvantages-and-when-to-use-it/
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #110 on: August 13, 2013, 09:57:17 am »
Just changed the code to use arrays and get this error now!
Code: [Select]
Error 1 unable to find a register to spill in class 'POINTER_REGS'
That is an internal compiler error. First check if your compiler is up-to-date, and if the error persists report the bug.

EDIT: I should add that the best place to ask AVR-specific questions is the AVR Freaks forum. That's all they do!
« Last Edit: August 13, 2013, 10:02:26 am by andersm »
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7695
  • Country: de
  • A qualified hobbyist ;)
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #111 on: August 13, 2013, 12:14:25 pm »
OK, I've rolled the code back to where it compiled this morning.  Maybe we should look at those warnings (to do with the font selection)

Code: [Select]
static void LCD_SetFont(char *font)
{
cfont.font=font;
cfont.x_size=fontbyte(0);
cfont.y_size=fontbyte(1);
cfont.offset=fontbyte(2);
cfont.numchars=fontbyte(3);
}

Are you accessing the font (stored in PROGMEM) via pgm_read_byte()?
« Last Edit: August 13, 2013, 12:16:11 pm by madires »
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #112 on: August 13, 2013, 12:21:45 pm »
I don't find it to work. For instance:
...

Works for me on GCC 4.7.2.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #113 on: August 13, 2013, 12:23:20 pm »
Are you accessing the font (stored in PROGMEM) via pgm_read_byte()?

Yes.  That part seems to be all good now.
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #114 on: August 13, 2013, 12:29:40 pm »
That is an internal compiler error. First check if your compiler is up-to-date, and if the error persists report the bug.

EDIT: I should add that the best place to ask AVR-specific questions is the AVR Freaks forum. That's all they do!

I did join the AVR forum, but have found so far that you guys (here) have a good handle on the overall scheme of things.   :)

As for the compiler version, I did check today and Atmel Studio says I have the latest one.  There are reports of the bug, duplicate reports and reports that it's fixed, but here it still is.  I don't know enough to make a useful bug report I suspect.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11790
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #115 on: August 13, 2013, 01:27:36 pm »
I don't find it to work. For instance:
...

Works for me on GCC 4.7.2.

What if you try it with the -ansi flag?
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #116 on: August 13, 2013, 01:35:45 pm »
Still works. However:

Code: [Select]
$ gcc -ansi -pedantic test.c
test.c: In function 'main':
test.c:7:20: warning: ISO C90 forbids compound literals [-pedantic]

Looks like it's one of the C99 features they silently backported to the main compiler. Whoops... :-[

(However, if it works for you it could end up being the most efficient way to accomplish struct assignment, so if you only plan on compiling your code with GCC or other C99-supporting compilers I'd say go for it)
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #117 on: August 13, 2013, 02:43:56 pm »
There are reports of the bug, duplicate reports and reports that it's fixed, but here it still is.  I don't know enough to make a useful bug report I suspect.
Many errors can cause the same error message, it just indicates that something's wrong with the register allocator. For a bug report you basically need a way to reproduce it. If you can't cut down your program and still trigger the bug, you'd have to check with Atmel if they can accept a test case under non-disclosure.

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #118 on: August 13, 2013, 11:25:41 pm »
Getting the display to print the pointer addresses was actually very helpful.  I can now better visualise how the various parts of the structure is stored and accessed.

I can see that changing the caption field to a static array would potentially use more space, but the strings would then be part of the structure itself.

I'll continue to play around and learn more to solve the root cause of the issue.
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #119 on: August 14, 2013, 01:28:31 am »
A bit more experimenting shows that the "spill error" is only triggered when the structure is more than 20 bytes in size.

That is, I can add and remove fields or change their size (char, int, etc) to make the structure over 20 bytes or not.

Changing the size of the array (of structs) didn't seem to have any effect.

So, in the meantime perhaps I can remove the caption (char array) from the struct and move the captions to their own array.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #120 on: August 14, 2013, 01:36:26 am »
Can you post the line where you define the struct array? And when the struct is too big, what does the compiler think these are:

Code: [Select]
// This is the array, just to say what I'm calling the type and array name
struct Button buttons[30];

sizeof (struct Button);
sizeof (buttons[0]);

The last line might give a warning, but it should compile.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #121 on: August 14, 2013, 01:44:09 am »
Those lines each give the error:

Code: [Select]
expected identifier or '(' before 'sizeof'
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #122 on: August 14, 2013, 01:46:19 am »
Syntax error before those lines? Sizeof is standard C, I can't see them throwing an error like that.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVDTopic starter

  • Super Contributor
  • ***
  • Posts: 2797
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #123 on: August 14, 2013, 01:49:39 am »
I expected the error as it needs to be assigned to something?
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11790
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #124 on: August 14, 2013, 01:51:01 am »
Those lines each give the error:

Code: [Select]
expected identifier or '(' before 'sizeof'

Try putting

Code: [Select]
#include <stddef.h>
at the top of your source file.

Also, you might want to do something like this in order to see what the size is:

Code: [Select]
size_t size = sizeof(struct Button);
printf("Size is: %u\n", size);

(And for printf you would need <stdio.h>)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf