Main Page | Directories | File List | Globals | Examples

blink_output.c

This is an example of how to use the OCOutput to let it blink leds.


#include <bluesense.h>

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

bsOCOutputDevice_portEnum parsePort(char* s) {
        char side = s[0];
        uint8_t output = atoi(&s[1]);
  
        switch (side) {
                case 'A': {
                        switch (output) {
                                case 1: return bsOCOutput_pA_1;
                                case 2: return bsOCOutput_pA_2;
                                case 3: return bsOCOutput_pA_3;
                                case 4: return bsOCOutput_pA_4;
                                case 5: return bsOCOutput_pA_5;
                                case 6: return bsOCOutput_pA_6;
                                case 7: return bsOCOutput_pA_7;
                                case 8: return bsOCOutput_pA_8;
                        }
                }
                case 'B': {
                        switch (output) {
                                case 1: return bsOCOutput_pB_1;
                                case 2: return bsOCOutput_pB_2;
                                case 3: return bsOCOutput_pB_3;
                                case 4: return bsOCOutput_pB_4;
                                case 5: return bsOCOutput_pB_5;
                                case 6: return bsOCOutput_pB_6;
                                case 7: return bsOCOutput_pB_7;
                                case 8: return bsOCOutput_pB_8;
                        }
                }
        }
        return bsOCOutput_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 output to blink.\n");
                fprintf(stderr, "\n");
                fprintf(stderr, "Usage: %s <identifier> <side><pin>\n", progname);
                fprintf(stderr, "   eg: %s 10 A5\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 side from command line
        identifier_t id = atoi(argv[0]);

        // parses the port 
        bsOCOutputDevice_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);
        }

        // get specific device
        bsOCOutputDevice_specifics_t specDev = bsOCOutputDevice_getDeviceSpecifics(dev);

        printf("Blinking 5 times...\n");        // 5 times on and 5 times off makes a for loop of 10
        fflush(stdout);

        // start blinking the output
        uint8_t i, v = 0;
        uint32_t autoYieldParam =0;

        // wait for 200ms to retrieve initial states
        autoYieldParam = bsMaster_autoYield(master,200000,2000,autoYieldParam);

        bsOCOutputDevice_requestSetOutput(specDev, port, 1);
        autoYieldParam = bsMaster_autoYield(master,200000,2000,autoYieldParam);


                
        for(i = 0; i < 10; i++) {
                // update output state, note that the output lies between 0 and 15, while the boards count in two sides from 1 to 8
                bsOCOutputDevice_requestSetOutput(specDev, port, v);

                printf("Output [%s at %u] = %s\n", argv[1], id, v?"on":"off");
                fflush(stdout);

                // invert output value (toggle)
                v = 1 - v;

                // give BlueSense network some time
                // autoYield for 1 second, each 2 ms call yield
                autoYieldParam = bsMaster_autoYield(master,1000000,2000,autoYieldParam);
        }

        // 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