Main Page | Directories | File List | Globals | Examples

analogue_out.c

This is an example of how to use the analogue output module.

#include <bluesense.h>

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

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

        // check argument count
        if(argc < 3) {
                fprintf(stderr, "\n");
                fprintf(stderr, 
                        "Please re-run this example with:\n");
                fprintf(stderr, 
                        "       identifier: identifier of the module\n");
                fprintf(stderr, 
                        "       port: output pin (1..6)\n");
                fprintf(stderr, 
                        "       value: out (0..255)\n");
                fprintf(stderr, "\n");
                fprintf(stderr, 
                        "Usage: %s <identifier> <port> <out 0..255>\n", progname);
                fprintf(stderr, 
                        "   eg: %s 10 1 200\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 from command line
        identifier_t id = atoi(argv[0]);

        // parses the port 
        uint8_t port = atoi(argv[1]);
        // to be set duty cycle
        uint8_t outvalue = atoi(argv[2]);
        
        // 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);

#if 1
        // connect to the ethernet router
        bsMaster_addIPAddress(master, "1623", "admin", "admin");
#endif

        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
        bsDacOutputDevice_specifics_t specDev = bsDacOutputDevice_getDeviceSpecifics(dev);
        
        fflush(stdout);

        uint8_t i = 0;
        uint32_t autoYieldParam =0;

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

        bsDacOutputDevice_requestSetOutput(specDev,(port-1),outvalue);
        autoYieldParam = bsMaster_autoYield(master,200000,2000,autoYieldParam);

        printf("Device %d current outputs \n", id);
        for (i=0; i<6; i++) {
                printf("  port %d has a value of %d\n",
                        i+1,
                        bsDacOutputDevice_getCurrentOutput(specDev,i));
        }

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