Solved

How to creat good software interface ?

Posted on 2016-08-08
1
66 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

776 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