int main(void) { // Create IP connection IPConnection ipcon; ipcon_create(&ipcon); // Create device object DistanceIR dir; distance_ir_create(&dir, UID, &ipcon); // Connect to brickd if(ipcon_connect(&ipcon, HOST, PORT) < 0) { fprintf(stderr, "Could not connect\n"); return 1; } // Don't use device before ipcon is connected // Get current distance (unit is mm) uint16_t distance; if(distance_ir_get_distance(&dir, &distance) < 0) { fprintf(stderr, "Could not get distance, probably timeout\n"); return 1; } printf("Distance: %f cm\n", distance/10.0); printf("Press key to exit\n"); getchar(); distance_ir_destroy(&dir); ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally return 0; }
TinkerforgeSensors::~TinkerforgeSensors() { bool is_ipcon = false; // clean up tf devices while(!sensors.empty()) { SensorDevice *dev = sensors.front(); switch (dev->getType()) { case AMBIENT_LIGHT_DEVICE_IDENTIFIER: ambient_light_destroy((AmbientLight*)dev->getDev()); break; case AMBIENT_LIGHT_V2_DEVICE_IDENTIFIER: ambient_light_v2_destroy((AmbientLightV2*)dev->getDev()); break; case DISTANCE_IR_DEVICE_IDENTIFIER: distance_ir_destroy((DistanceIR*)dev->getDev()); break; case DISTANCE_US_DEVICE_IDENTIFIER: distance_us_destroy((DistanceUS*)dev->getDev()); break; case DUAL_BUTTON_DEVICE_IDENTIFIER: dual_button_destroy((DualButton*)dev->getDev()); break; case GPS_DEVICE_IDENTIFIER: gps_destroy((GPS*)dev->getDev()); break; case IMU_DEVICE_IDENTIFIER: imu_leds_off((IMU*)dev->getDev()); imu_destroy((IMU*)dev->getDev()); break; case IMU_V2_DEVICE_IDENTIFIER: imu_v2_leds_off((IMUV2*)dev->getDev()); imu_v2_destroy((IMUV2*)dev->getDev()); break; case TEMPERATURE_DEVICE_IDENTIFIER: temperature_destroy((Temperature*)dev->getDev()); break; } delete dev; sensors.pop_front(); is_ipcon = true; } if (is_ipcon) { ipcon_destroy(&ipcon); } }