简体   繁体   中英

Is it possible to declare array of structures with delays functions as some of the elements

I got an initialization structure for my touchscreen driver from manufacturer.

struct ChipSetting ssd25xxcfgTable[]={
delay(300),
{2,0x01,0x00,0x00},
delay(100),
{2,0x06,0x19,0x0F},
{2,0x07,0x00,0xE0},
{2,0x08,0x00,0xE1},
{2,0x09,0x00,0xE2},
{2,0x0A,0x00,0xE3},
{2,0x0B,0x00,0xE4},
};

According to me the struct Chipsetting should be :

struct Chipsetting
{
   unsigned char reg; // register to be written
   unsigned char len; // length of data
   unsigned char msb; // data
   unsigned char lsb;
};

I want to use the strutcure to program device registers as shown below.

for (index = 0; index < sizeof(ssd25xxcfgTable)/sizeof(ssd25xxcfgTable[0]); index++)
{
    //               command                  address of parameter     parameter length
    WriteReg(ssd25xxcfgTable[index].reg, &ssd25xxcfgTable[index].msb, ssd25xxcfgTable[index].len);

}

Is it possible to do so ? I need the delay functions to be called according to the element order given in the structure. According to me, it is not possible, but need to know if there is some workarounds. I want to avoid splitting the for loop. And I feel the above init structure is more readable.

As I understand your question, it seems you need to actually store delays along with the Chipsetting values, and call them at runtime:

struct ChipsettingWrite {
    int delay;
    struct Chipsetting setting;
};

struct ChipsettingWrite ssd25xxcfgTable[]={
    {300, {2,0x01,0x00,0x00}},
    {100, {2,0x06,0x19,0x0F}},
    {0,   {2,0x07,0x00,0xE0}},
    {0,   {2,0x08,0x00,0xE1}},
    {0,   {2,0x09,0x00,0xE2}},
    {0,   {2,0x0A,0x00,0xE3}},
    {0,   {2,0x0B,0x00,0xE4}},
};

I assume there must exist a function somewhere that writes the values, so you can do something like this:

int NUMBER_OF_SETTINGS = sizeof(ssd25xxcfgTable)/sizeof(ssd25xxcfgTable[0]);
for (int i = 0; i < NUMBER_OF_SETTINGS; i++) {
     if (ssd25xxcfgTable[i].delay > 0) {
          delay(ssd25xxcfgTable[i].delay);
     }
     WriteReg(ssd25xxcfgTable[i].setting.reg, &ssd25xxcfgTable[i].setting.msb, ssd25xxcfgTable[i].setting.len);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM