Solved

How to creat good software interface ?

Posted on 2016-08-08
1
67 Views
Last Modified: 2016-08-08
/*  Our embedded target board is based on Renesas RH850F1L Microcontroller.  Our language is 'C'. */
/* A software library creates a instance of this structure and provides me a pointer to this structure.   What is a good application interface that can be used to provide d0.Fact and d1.Fact to application periodically? */
typedef struct {
               unsigned short  count;
               unsigned short  hold;
               unsigned char    Fact;
} LtRXData;

typedef struct {
   unsigned short  speed;
   LtRXData  d0;
   LtRXData  d1;
} LtRData;

Open in new window



/* A software library creates instance of this structure and provides me pointer to the structure.  What is a good application interface that can be used to provide d0_s1.Fact, d0_s2.Fact, d0_s3.Fact, d1_s1.Fact, d1_s2.Fact, and d1_s3.Fact to application periodically? */
typed struct {
   unsigned short  count;
   unsigned short  hold;
   unsigned char   Fact;
} LtSXData;

typedef struct {
   unsigned short speed;
   LtSXData  d0_s1;
   LtSXData  d0_s2;
   LtSXData  d0_s3;
   LtSXData  d1_s1;
   LtSXData  d1_s2;
   LtSXData  d1_s3;
} LtSData;

Open in new window

0
Comment
Question by:naseeam
1 Comment
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 41747705
library creates instance of this structure and provides me pointer to the structure

assume there is no foreign library but you have some fixed initial "records" of LtRData (same applies for LtSData).

then you could do:

#define MAX_LTR 100
#define LTR_INIT 3


LtRData rdata[MAX_LTR] = 
{
     {     10, { 0, 0, 'F' }, { 1, 2, 'P' }, }, 
     {   100, { 7, 8, 'A' }, { 2, 1, 'N' }, }, 
     {  1000, { 99, 0, 'A' }, { 1, 1, 'L' }, }, 
};
static int maxidx = LTR_INIT;

Open in new window


you see the 'table' of records initially is sorted by speed,

you easily can write a function to get d0.Fact of a given "row" of the table.

char LTR_GetD0FactByIndex(int idx)
{
      if (idx < 0 || idx >= maxidx) return ' ';

      return rdata[idx].d0.Fact;
}

Open in new window



you also can search for a given speed.

char LTR_GetD0FactBySpeed(int speed)
{
      int i;

      for (i = 0; i < maxidx; ++i) 
            if (speed == rdata[i].speed)
                 return rdata[i].d0.Fact;
      return ' ';
}

Open in new window



if you get new LTR records you may add them to the table:

int RDataCompare(void * pLeft, void * pRight)
{
      if (((LtRData *)pLeft)->speed < ((LtRData *)pRight)->speed)
          return -1;
      else if  (((LtRData *)pLeft)->speed > ((LtRData *)pRight)->speed)
          return 1;
      return 0;
}

int LTR_AddRecord(LtRData * pLtR)
{
       if (pLtR == NULL || maxidx == MAX_LTR) return -1;
       
       rdata[maxidx++] = *pLtR;
       qsort(rdata, maxidx, sizeof(LtRData), RDataCompare);

       return 0;
};

Open in new window


Sara
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question