It looks like that I had not expressed my doubt very well. I am using a set of 3 digital input, 1 digital output & 1 analog input for a device named BAG. I am allocating such set to total 4 devices. I had executed program by defining individual pin for the required function. I was curious to know can I define a structure for the device with device name as array like BAG[0] to BAG[4]. The working code is as given below:
struct BAG_UNIT
{
AnalogIn PRESS;
DigitalIn INT1,INT2,LRSTS;
DigitalOut RES;
// constructor
BAG_UNIT(PinName pin1, PinName pin2, PinName pin3, PinName pin4, PinName pin5 ) : PRESS(pin1), INT1(pin2), INT2(pin3), LRSTS(pin4), RES(pin5) {};
};
BAG_UNIT BAG_0(p15,p19,p20,p21,p5);
BAG_UNIT BAG_1(p16,p22,p23,p24,p6);
BAG_UNIT BAG_2(p17,p25,p26,p27,p7);
BAG_UNIT BAG_3(p18,p28,p29,p30,p8);
void update_gauges(int bg_add){
int j;
int int1,int2,lrsts,reset;
float adc_count;
adc_count= 0;
int1 = int2 = lrsts=reset = 0;
if(bg_add == 0)
{
for(j=0;j<N;j++){
adc_count = adc_count + BAG_0.PRESS;
}
adc_count = (adc_count/N)*4096;
int1 = BAG_0.INT1.read();
int2 = BAG_0.INT2.read();
lrsts = BAG_0.LRSTS.read();
reset = BAG_0.RES.read();
}
else
if(bg_add == 1)
{
for(j=0;j<N;j++){
adc_count = adc_count + BAG_1.PRESS;
}
adc_count = (adc_count/N)*4096;
int1 = BAG_1.INT1.read();
int2 = BAG_1.INT2.read();
lrsts = BAG_1.LRSTS.read();
reset = BAG_1.RES.read();
}
else
if(bg_add == 2)
{
for(j=0;j<N;j++){
adc_count = adc_count + BAG_2.PRESS;
}
adc_count = (adc_count/N)*4096;
int1 = BAG_2.INT1.read();
int2 = BAG_2.INT2.read();
lrsts = BAG_2.LRSTS.read();
reset = BAG_2.RES.read();
}
else
if(bg_add == 3)
{
for(j=0;j<N;j++){
adc_count = adc_count + BAG_3.PRESS;
}
adc_count = (adc_count/N)*4096;
int1 = BAG_3.INT1.read();
int2 = BAG_3.INT2.read();
lrsts = BAG_3.LRSTS.read();
reset = BAG_3.RES.read();
}
pc.printf("@%d%04.0f,%1d,%1d,%1d,%1d#\n",bg_add,adc_count,int1,int2,lrsts,reset);
}
I wish to declare the structure as array but it gives me error. This will ease to set/reset pins & read analog input in a function like given below:
Instead of writing
struct BAG_UNIT
{
AnalogIn PRESS;
DigitalIn INT1,INT2,LRSTS;
DigitalOut RES;
// constructor
BAG_UNIT(PinName pin1, PinName pin2, PinName pin3, PinName pin4, PinName pin5 ) : PRESS(pin1), INT1(pin2), INT2(pin3), LRSTS(pin4), RES(pin5) {};
};
//not able to define as array I wish too //
BAG_UNIT BAG[0](p15,p19,p20,p21,p5); //error: #94: the size of an array must be greater than zero
BAG_UNIT BAG[1](p16,p22,p23,p24,p6);
BAG_UNIT BAG[2](p17,p25,p26,p27,p7);
BAG_UNIT BAG[3](p18,p28,p29,p30,p8);
void update_gauges(int bg_add){
int j;
const int N = 100;
int int1,int2,lrsts,reset;
float adc_count;
adc_count= 0;
for(j=0;j<N;j++){
adc_count = adc_count + BAG[bg_add].PRESS;
}
adc_count = (adc_count/N)*4096;
int1 = BAG[bg_add].INT1.read();
int2 = BAG[bg_add].INT2.read();
lrsts = BAG[bg_add].LRSTS.read();
reset = BAG[bg_add].RES.read();
pc.printf("@%d%04.0f,%1d,%1d,%1d,%1d#\n",bg_add,adc_count,int1,int2,lrsts,reset);
}
Kindly reply if I have not described the problem well.
Hi
Probably a noob question but is it possible to use eg: a DigitalOut in a struct but then declaring the output pin later when initialising the struct?
I came across this code sample http://www.uchobby.com/wp-content/uploads/2007/10/arduino_beer_thermostat.txt for arduino which I thought was quite elegant and wanted to reproduce with mbed. I am stuck declaring the output pins (the code example just defines them as ints)
Cheers
Evan