Hello,

I develop a small function block based application. Each function block input and output is associated with a bit in a bitfield. Function blocks can be simple logic, timers, ... By "wiring" these function blocks I get a kind of very simple PLC.

My first try is to allocate a byte arrary. In addition there is a get and set function that gets the bit number I want to read or write. Internally I have to do some calculation to find the right bit in the byte array. The attached file shows how it is implemented.

The function blocks uses these get and set functions for accessing their input and output ports. I wonder if there is a better method to implement such a functionality.

- Is it possible to get rid of the function calls (get/set) in the function blocks? Or make these calls as efficient as possible? - Is there a more efficient way to implement my data store? E.g. does it make sense to use bitfields?
- Any other ideas - maybe someone has developed something similar?

Thanks,
Peter

P.S: I'm also looking for a windows tool to "wire" my blocks. Do anyone knows a tool that can do that and is open for my own extension such as downloading the "wires" to the uC.

Thanks a lot,
Peter

--
Peter Mueller
[email protected]

#include "sps_types.h"

#ifdef WIN32
#include <assert.h>
#endif

/**
  * The datastore holds all inputs and outpus of all functionblocks that can
  * can exist in an application.
  * 
  * The memory layout is the following:
  *
  * Byte[0               | 1                      | ...
  * Bit#[0,1,2,3,4,5,6,7 | 8,9,10,11,12,13,14,16, | ...
  *
  */

#define LEN_DATA_STORE 8
#define MAX_POS_DATA_STORE (LEN_DATA_STORE*8)

static unsigned char data_store[LEN_DATA_STORE];

static const unsigned char mappingtable[8] = {128,64,32,16,8,4,2,1};

TBOOL ds_get_bool(TUSIGN8 pos){


        unsigned int isInByte, rest;
        
#ifdef WIN32
        assert(pos <= MAX_POS_DATA_STORE);
#endif

        isInByte = pos/8;
        rest = pos%8; // Bsp.: 9 mod 8 = 1

        if((data_store[isInByte]) & mappingtable[rest]) 
                return TRUE;
        else
                return FALSE;

}


TUSIGN8 ds_set_bool(TUSIGN8 pos, TBOOL val){

        unsigned char isInByte, rest;

#ifdef WIN32
        assert(pos <= MAX_POS_DATA_STORE);
#endif

        isInByte = pos/8;
        rest = pos%8;

        if(val==1)
                data_store[isInByte] |= mappingtable[rest];
        else
                data_store[isInByte] &= ~(mappingtable[rest]);

        return 0;
        
}

Reply via email to