a generic variable for different structures

hello everyone and thank you for your help in advance! I consider myself a beginner in C++ as I have manly programmed in C. I am developing a library for a processor which has 6 USART ports. I have a re-defined printf to get messages on the screen from the processor via the serial port. it works but this is what I have in the printf.c source code

1
2
3
4
5
6
#define USARTx USART1
//#define USARTx USART2
//#define USARTx USART3
//#define USARTx USART4
//#define USARTx USART5
//#define USARTx USART6 


each USART is a structure. my question is: is there a way to have a single variable to service them all and not having to comment/uncomment lines each time? something like ...

 
#define USARTx (USART1 | USART2 | USART3 | USART4 | USART5 | USART6) 


thank you again
I an not familiar with using the pre-processor directives however in c++ I dont think it is needed in this instance. Does this not just replace all instances of USARTx as USART1?

Could be my inexperience but why not just declare them and make a function that returns whichever you need? Or if there is some reason you must use the pre-processor, you can make a define function macro that defines it differently based on conditions.

hi pata and thank you for your reply! The idea is to have USARTx identifying the USART I am using but I can't figure out how to tell that to this function as I cannot have that as a function parameter. I have such constant in the USART library

1
2
3
4
5
6
7
8
9
10
/** @defgroup USART_Exported_Constants
  * @{
  */ 
  
#define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \
                                     ((PERIPH) == USART2) || \
                                     ((PERIPH) == USART3) || \
                                     ((PERIPH) == USART4) || \
                                     ((PERIPH) == USART5) || \
                                     ((PERIPH) == USART6)) 


I was thinking to do check it like so

1
2
3
4
5
6
  if IS_USART_ALL_PERIPH(USART1)
	#define USARTx USART1
  else if IS_USART_ALL_PERIPH(USART2)
	#define USARTx USART2
  else IS_USART_ALL_PERIPH(USART3)
	#define USARTx USART3 


I tried that but I know it wouldn't work. any idea?

thank you!
Topic archived. No new replies allowed.