How to access similar components in a loop by indexing?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

4 similar I2C-Components on my PSOC4 are polling sensors.

The I2C components are named  I2C_1, I2C_2, I2C_3, I2C_4.

Today i do something like this:

poll I2C_1;

poll I2C_2;

poll I2C_3;

poll I2C_4;

in detail:

i2cstatus_1 = I2C_1_I2CMasterSendStart(a,0,100);

i2cstatus_2 = I2C_2_I2CMasterSendStart(a,0,100);

i2cstatus_3 = I2C_3_I2CMasterSendStart(a,0,100);

i2cstatus_4 = I2C_4_I2CMasterSendStart(a,0,100);

but i want to do in a loop:

for (componentnumber = 1; componentnumber <= 4; componentnumber ++)

{

  i2cstatus_componentnumber = I2C_componentnumber _I2CMasterSendStart(i,0,100);

...

}

How can i put the named component into an indexed access?

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Usual solution for this is an array of pointers to functions. Have a look here.

Bob

View solution in original post

2 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Usual solution for this is an array of pointers to functions. Have a look here.

Bob

zzz_3221081
Level 5
Level 5
25 solutions authored 100 sign-ins 10 likes received

Why not passing said "componentnumber" as argument to the I2CMasterSendStart() function, and differenciate there ?

I use to avoid function pointers, because they are a real pain with our test tool.

0 Likes