#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--;
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);
}
identifier_t id = atoi(argv[0]);
bsSwitchInputDevice_portEnum port = parsePort(argv[1]);
bsDeviceInterface_registeredDeviceInterfaces_t registered = bsDeviceInterface_allocInterfaceTable();
bsDeviceTable_table_t devices = bsDeviceTable_allocDeviceTable();
bsMaster_device_t master = bsMaster_allocMaster(registered,devices,0);
printf("Detecting device %d...\n", id);
fflush(stdout);
bsDeviceTable_deviceInfo_t dev = bsMaster_detectDevice(master, id, 5 * 1000 * 1000);
if(!dev) {
fprintf(stderr, "Device %d not found!\n", id);
bsMaster_freeMaster(master);
bsDeviceTable_freeDeviceTable(devices);
bsDeviceInterface_freeInterfaceTable(registered);
return(0);
}
bsSwitchInputDevice_specifics_t specDev = bsSwitchInputDevice_getDeviceSpecifics(dev);
bsSwitchInputDevice_requestNeedsCounter(specDev, port, 1);
printf("Increase counter beyond 100 to quit...\n");
fflush(stdout);
uint32_t last;
for(last = 0; last < 100;) {
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;
}
bsMaster_yield(master);
}
bsMaster_freeMaster(master);
bsDeviceTable_freeDeviceTable(devices);
bsDeviceInterface_freeInterfaceTable(registered);
return(0);
}