Solved

How to creat good software interface ?

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

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now