0

Sorry for the crappy title I Don't know how to ask what im looking for. Here is my problem. I have a msp430 processor connected to a bunch of transceivers. I have individual discrete outputs going to going to the re/de select of each transceivers. Normal I define the select of each chip like this

#define enable_port_rx  P1OUT &= ~BIT2
#define disable_port_rx P1OUT |= BIT2
#define enable_port_tx  P1OUT |= BIT1
#define disable_port_tx P1OUT &= ~BIT1

I have a struct for each transceivers that i put in an array so that I can iterate though them with loops.

extern motors m[10];
typedef struct
{
    unsigned char address;
    unsigned int value;
    unsigned int xdata[10];
} motors;

I want to be able to enable/disable rx/tx from the struct. I want to do something like this.

m[0].enable_tx = P1OUT |= BIT1;
m[0].disable_tx = P1OUT &= ~BIT1;
m[1].enable_tx = P3OUT |= BIT2;
m[1].disable_tx = P3OUT &= ~BIT2;

so that i can call them when i need them..

m[0].disable_tx; //disable tx
m[1].disable_tx; //disable tx

I thought something like this below would work. That is i saved that pointer to the register and the bit needed that i could access them when need. but this does not work.

//inside strut
volatile unsigned char *rxPxOUT;
unsigned char rxBITx;
volatile unsigned char *txPxOUT;
unsigned char txBITx;

//setup
m[0].txPxOUT=&P1OUT;
m[0].txBITx = BIT1;

//function call
&m[0].txPxOUT &= ~m[0].txBITx;

Thanks in advance

gezzuzz
  • 188
  • 2
  • 16
  • 1
    in `motors`, there is no member named `enable_tx`. then? – Sourav Ghosh Apr 22 '15 at 15:27
  • @SouravGhosh that's the question from OP. He wants to know how to add [functions inside structs](http://stackoverflow.com/questions/17052443/c-function-inside-struct) – Eregrith Apr 22 '15 at 15:28

2 Answers2

1

I obviously can't test it myself, but I think the only error is

&m[0].txPxOUT &= ~m[0].txBITx;

which should be

*m[0].txPxOUT &= ~m[0].txBITx;

Edit: You could put the code into a member function like this:

typedef struct
{
    ....
    void disable_tx();
} motors;

void motors::disable_tx()
{
    *txPxOUT &= ~txBITx;
}

And call it like this:

m[0].disable_tx();
alain
  • 11,939
  • 2
  • 31
  • 51
  • Yep that was my problem.. i was still hoping to find a more elegant solution.. but this does work. Thank a lot for pointing that out – gezzuzz Apr 22 '15 at 16:15
  • @gezzuzz I'm glad it worked, I have updated the answer with a more elegant solution. – alain Apr 22 '15 at 18:27
0

You can add function pointers to your struct and then call them.

extern motors m[10];
typedef struct
{
  unsigned char address;
  unsigned int value;
  unsigned int xdata[10];

  void (*disable_tx)(void);
} motors;

[...]

void disable_tx_1(void)
{
  P1OUT &= ~BIT1;
}

[...]

m[0].disable_tx = disable_tx_1

[...]

m[0].disable_tx();

[...]

If you want to have a lambda-style function like the one in your question, you could have a look at GCC anonymous functions or other means of doing anonymous functions in C.

Eregrith
  • 4,263
  • 18
  • 39
  • instead on creating named functions for each one.. how can i just pas it an anonymous function. something like m[0].disable_tx = (void){P1OUT &= ~BIT1;}; – gezzuzz Apr 22 '15 at 15:41
  • @gezzuzz You could have a look at [GCC anonymous functions](http://en.wikipedia.org/wiki/Anonymous_function#C_.28non-standard_extension.29) which are not part of standard C – Eregrith Apr 22 '15 at 15:47