Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:32:18+00:00 2026-06-09T04:32:18+00:00

I am working on some code that impliments various features such as a PID

  • 0

I am working on some code that impliments various features such as a PID controller, signal generator, etc.

My hardware provides various inputs and outputs. Just now I have a load of SWITCH statement to determine the source and destination of my calculations.

For example for the PID controller, there is a switch command every 100ms that decides which input to pass to the pid_calculate function, followed by another switch to decide what to do with the return value. As I have 32 analog inputs, as well as can, lin and serial as possible inputs, the switch statements are HUGE!

I would like to reference to, or a physical example of how something could be coded as a hardware independent (within the confines of the pic) function. I am sure that the answer lies with pointers, but I am new to C and not really sure where to begin with the pointers.

I envisage the function prototype being something like
int pid_init(*source,*destination)
Where the source is a pointer to the input, eg an ADC buffer, and the destination could be for example the pwm duty cycle register. Just now I have to switch each possible condition then load the data into the registers.

So to clarify, how would I impliment a function that allows it to be input and output independent, and how would I de-reference the pointers(assuming pointers are the correct way to do this)

I hope this makes sense, thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:32:20+00:00Added an answer on June 9, 2026 at 4:32 am

    You can implement a kind of object oriented programming in C so long as you take a bit of care. In the ways that I have done it, I have used function pointers as well as structs with function pointers as members.

    For instance to have a kind of data independence, you may have a function that like the quick sort (qsort) function is provided data long with a pointer to a function that operates on the data. Typically with this approach you end up using void pointers of various kinds to allow the use of pointers to any data type.

    Another approach that I have used is to have what amounts to an abstract interface for the common operations that the various types of devices support and to then create a struct which provides a function pointer template for those operations. Then I will create an array of these structs and fill the array elements in by using function pointers to the particular function that implements that operation for a particular device.

    For instance something like this very simple example is what I have used to provide a standard interface for several different devices. This interface hides the device differences by providing a standard interface.

    // I create a struct that specifies what the function
    // interface looks like for each of these operations for the
    // devices that we are supporting.
    typedef struct {
        int  (*pInput)(int iValue);
        char *(*pName) (void);
        int  (*pDance) (int iTune, char *aszName);
    } HardwareInterface;
    
    // next I provide the function prototypes for the various devices.
    // we have two different devices, Device_01 and Device_02 which
    // have a common set of operations with the same interface.
    int  Device_01_Input (int iValue);
    char *Device_01_Name (void);
    int  Device_01_Dance (int iTune, char *aszName);
    
    int  Device_02_Input (int iValue);
    char *Device_02_Name (void);
    int  Device_02_Dance (int iTune, char *aszName);
    
    // now I define my array of the device operation interfaces.
    // this is where I provide the link between a specific operation
    // on a specific device.  I will number my devices beginning with
    // zero to the number of devices supported minus one.
    HardwareInterface HardwareList [] = {
        {Device_01_Input, Device_01_Name, Device_01_Dance},
        {Device_02_Input, Device_02_Name, Device_02_Dance},
    };
    

    At this point I can have a function that I call to obtain which of the devices I actually want to use. This function returns an index into the hardware list. So it might go something like this.

    int DeviceStdInput (int iValue)
    {
        int  i = GetDeviceType ();
        return HardwareList[i].pInput (iValue);
    }
    

    Or I might use a handle approach so that I would call a function which provides the handle to a device that is specified in some description which is passed to the function. Then any calls to my standard interface would specify the handle. Underneath, the handle is merely the index into the array of different devices.

    {
        int iHandle = GetDeviceHandle ("Device_01");
        int iXvalue = DeviceStdInput (iHandle, iValue);
    }
    

    And the function DeviceStdInput() would look like:

    int DeviceStdInput (int iHandle, int iValue)
    {
        return HardwareList[iHandle].pInput (iValue);
    }
    

    You will still need to implement the actual functions used for each of the devices however this provides a way to have a standard interface to several devices with common operations that you can then just use the standard interface within the rest of your code.

    I have used this to provide a standard interface for output in which the output devices were a file, a printer, and a web service. The actual sink or output device was selected by the user. The code using the interface did not change. Adding more devices was pretty easy so long as the interface did not change. In some cases I would have devices that did not support a particular operation and that function would just return without doing anything.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working some code that inserts csv rows into an SQLite database using Python.
I'm working on some code that reads from a socket, and it goes wrong
I'm working with some code that I've found online and I don't quite understand
I'm working on some code that takes a hexadecimal string input and produces the
I am working on some code that I inherited from a programmer who is
I'm working on some code that loads an xml file at run time. At
I'm working on some code that deals with parsing files (mainly XML, but there
I am working on some code that previously was using a cfquery, and is
I'm working on some existing c++ code that appears to be written poorly, and
I am working on a memory matching game. I have some code that checks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.