Main Page | Directories | File List | Globals | Examples

counter.c

This is an example of how to use the SwitchInput to count pulses.

#include <bluesense.h>

#include <stdlib.h>
#include <stdio.h>

bsSwitchInputDevice_portEnum parsePort(char* s) {
        char side = s[0];
        uint8_t output = atoi(&s[1]);
  
        switch (side) {
                case 'A': {
                        switch (output) {
                                case 1: return bsSwitchInput_pA_1;
                                case 2: return bsSwitchInput_pA_2;
                                case 3: return bsSwitchInput_pA_3;
                                case 4: return bsSwitchInput_pA_4;
                                case 5: return bsSwitchInput_pA_5;
                                case 6: return bsSwitchInput_pA_6;
                        }
                }
                case 'B': {
                        switch (output) {
                                case 1: return bsSwitchInput_pB_1;
                                case 2: return bsSwitchInput_pB_2;
                                case 3: return bsSwitchInput_pB_3;
                                case 4: return bsSwitchInput_pB_4;
                                case 5: return bsSwitchInput_pB_5;
                                case 6: return bsSwitchInput_pB_6;
                        }
                }
        }
        return bsSwitchInput_pA_1;
}


int main(int argc, char **argv) {
        char *progname = *argv++; argc--;

        // check argument count
        if(argc < 2) {
          fprintf(stderr, "\n");
          fprintf(stderr, "Please re-run this example with an identifier and input\n");
          fprintf(stderr, "  as argument to specifiy which input to count.\n");
          fprintf(stderr, "\n");
          fprintf(stderr, "Usage: %s <identifier> <input>\n", progname);
          fprintf(stderr, "  eg.: %s 10 B2\n", progname);
          fprintf(stderr, "\n");
          fprintf(stderr, 
            "To find out what devices are connected try the 'list_devices' example.\n");
          fprintf(stderr, "\n");
          return(1);
        }

        // get identifier and input from command line
        identifier_t id = atoi(argv[0]);
        bsSwitchInputDevice_portEnum port = parsePort(argv[1]);

        // allocate memory for an interface table and resigster a known device interfaces
        bsDeviceInterface_registeredDeviceInterfaces_t registered = bsDeviceInterface_allocInterfaceTable();
        
        // allocate memory of a device table and open a master device
        bsDeviceTable_table_t devices = bsDeviceTable_allocDeviceTable();
        bsMaster_device_t master = bsMaster_allocMaster(registered,devices,0);

        printf("Detecting device %d...\n", id);
        fflush(stdout);

        // try to find our device within the next 5 seconds
        bsDeviceTable_deviceInfo_t dev = bsMaster_detectDevice(master, id, 5 * 1000 * 1000);

        // device not found?
        if(!dev) {
                fprintf(stderr, "Device %d not found!\n", id);

                // clean up previously allocated memory
                bsMaster_freeMaster(master);
                bsDeviceTable_freeDeviceTable(devices);
                bsDeviceInterface_freeInterfaceTable(registered);
                return(0);
        }

        // setup port as counter
        bsSwitchInputDevice_specifics_t specDev = bsSwitchInputDevice_getDeviceSpecifics(dev);
        bsSwitchInputDevice_requestNeedsCounter(specDev, port, 1);

        printf("Increase counter beyond 100 to quit...\n");
        fflush(stdout);

        // start showing counter state
        uint32_t last;
        for(last = 0; last < 100;) {
                // check if counter state changed
                uint32_t v = bsSwitchInputDevice_getLastSampledCounter(specDev, port);
                if(v != last) {
                        printf("Counter [%s at %u] = %u\n", argv[1], id, v);
                        fflush(stdout);
                        last = v;
                }

                // give BlueSense network some time
                bsMaster_yield(master);
        }

        // clean up previously allocated memory
        bsMaster_freeMaster(master);
        bsDeviceTable_freeDeviceTable(devices);
        bsDeviceInterface_freeInterfaceTable(registered);
        return(0);
}       


Generated on Mon Jan 21 17:51:55 2008 for BlueSense by  doxygen 1.4.4