#define function

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys, I have the following function:

unsigned short RGBto565(unsigned char R8, unsigned char G8, unsigned char B8){

	unsigned short return_color;

	unsigned char R5 = (R8 * 249 + 1014) >> 11;
	unsigned char G6 = (G8 * 253 + 505) >> 10;
	unsigned char B5 = (B8 * 249 + 1014) >> 11;

	return_color = B5;
	return_color = return_color + (G6 << 5);
	return_color = return_color + (B5 << 11);

	return return_color;

}

And the following define:

#define COL21 RGBto565(250,0,250) // MAGENTA

And the following usage:

const unsigned short colsCubeQ[] = {
  COL21,COL21
  //RED, GREEN, BLUE, YELLOW, MAGENTA, CYAN
};

But I get the error COL21 undeclared!

Why?

 

Thanks Freaks.

 

This topic has a solution.
Last Edited: Tue. Sep 15, 2020 - 11:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the define t the top of the code?  If the define is after the function its used in then the compiler is gonna barf up an error

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How is this going to work?

 

Let's do the job of the preprocessor and expand what you wrote:

 

const unsigned short colsCubeQ[] = {
  RGBto565(255,0,255),RGBto565(255,0,355)
  //RED, GREEN, BLUE, YELLOW, MAGENTA, CYAN
};

Is that legal C code??

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

unsigned short RGBto565(unsigned char R8, unsigned char G8, unsigned char B8){

	unsigned short return_color;

	unsigned char R5 = (R8 * 249 + 1014) >> 11;
	unsigned char G6 = (G8 * 253 + 505) >> 10;
	unsigned char B5 = (B8 * 249 + 1014) >> 11;

	return_color = B5;
	return_color = return_color + (G6 << 5);
	return_color = return_color + (B5 << 11);

	return return_color;

}

Side note... you might have made a mistake in the definition of return_color.... should the first line not put R5 in the field rather than B5.......

 

O and to avoid confusion add some more brackets in these formulas: Technically all might go well, but for clear reading you better add them as they might throw you off.

Fianawarrior wrote:

	unsigned char R5 = (R8 * 249 + 1014) >> 11;

Have you also made clear that the unsigned char in your compiler is more than the usual 8 bits...... might even be needing more than 16.....

 

as a side note I made this little program:

#include <avr/io.h>

unsigned short RGBto565(unsigned char R8, unsigned char G8, unsigned char B8){

	unsigned short return_color;

	unsigned char R5 = (R8 * 249 + 1014) >> 11;
	unsigned char G6 = (G8 * 253 + 505) >> 10;
	unsigned char B5 = (B8 * 249 + 1014) >> 11;

	return_color = R5;
	return_color = return_color + (G6 << 5);
	return_color = return_color + (B5 << 11);

	return return_color;

}

#define COL21 RGBto565(250,0,250) // MAGENTA

const unsigned short colsCubeQ[] = {
	COL21,COL21
	//RED, GREEN, BLUE, YELLOW, MAGENTA, CYAN
};

int main(void)
{
	DDRB = colsCubeQ[0];
	PORTB = colsCubeQ[1];
	while (1){
		_delay_ms(250);
		PORTB ^= 0xff;
    }
}

and that gives me this result:

Build succeeded.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

 

So there does not seem to be a problem were you think it is.

Perhaps post a minimal example that you get the errors on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Fianawarrior wrote:
But I get the error COL21 undeclared!
Which means the string substitution was not made during preprocessing. So the #define must not have been visible in that compilation unit.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

>Is that legal C code??

 

I'm not a lawyer, but I don't recall any C compiler allowing a function call in an initializer. That is why we have c++, to get all the goodies c doesn't allow.

 

c

https://godbolt.org/z/v6nafj

c++

https://godbolt.org/z/nGs81M

 

 

>as a side note I made this little program:

 

and compiled in C++, it seems.

 

 

I guess we assume warrior is using some arm device, as usual, and in c++. As long as you are in c++, why not do c++ type of things-

https://godbolt.org/z/a8e3ax

most likely there is something better than this, but whatever is used will be much better than using defines.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

curtvm wrote:
I don't recall any C compiler allowing a function call in an initializer.

It's allowed for  (simple?) auto variables?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Correct lads, I'm porting a little c plus plus code to c.  Problem solved.  i.e. just do the preprocessing myself.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

>It's allowed for  (simple?) auto variables?

 

That seems to be right-

 

int main(){

    char c[] = { someFunc() };

}

 

and I guess makes sense- the creation of the var/array is at runtime, so can use runtime functions. Trying to get the c compiler to initialize something at compile time via a function is a different thing since it has no way to call the function to init the data, and its not going to try to see if its a constant expression either.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

But I get the error COL21 undeclared!

Why?

 

Because it is undeclared. The macro has to be defined in the same translation unit above the point of its first usage. This is the requirement you violated.

 

Also, what language are you using, again?

 

In C objects with static storage duration can only be initialized with constant expressions. A call to `RGBto565` does not qualify as a constant expression. Is your array declared with static storage duration? It is not clear from your post.

 

In C++ there's no such limitation.

Dessine-moi un mouton

Last Edited: Tue. Sep 15, 2020 - 06:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the input guys.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:
Problem solved.

So mark the solution, then.

 

You should know this by now.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...