int main(void) { // Create IP connection IPConnection ipcon; ipcon_create(&ipcon); // Create device object RotaryEncoder re; rotary_encoder_create(&re, 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 // Register count callback to function cb_count rotary_encoder_register_callback(&re, ROTARY_ENCODER_CALLBACK_COUNT, (void *)cb_count, NULL); // Set period for count callback to 0.05s (50ms) // Note: The count callback is only called every 0.05 seconds // if the count has changed since the last call! rotary_encoder_set_count_callback_period(&re, 50); printf("Press key to exit\n"); getchar(); rotary_encoder_destroy(&re); ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally return 0; }
RotaryEncoder::RotaryEncoder(MasterConnection::Ptr pMasterConn, const std::string& uid): BrickletType("io.macchina.tf.rotaryencoder", "Tinkerforge Rotary Encoder Bricklet") { addProperty("displayValue", &RotaryEncoder::getDisplayValue); addProperty("countChangedPeriod", &RotaryEncoder::getCountChangedPeriod, &RotaryEncoder::setCountChangedPeriod); IPConnection *ipcon = pMasterConn.cast<MasterConnectionImpl>()->ipcon(); rotary_encoder_create(&_rotaryEncoder, uid.c_str(), ipcon); char deviceUID[8]; char masterUID[8]; char position; Poco::UInt8 hardwareVersion[3]; Poco::UInt8 firmwareVersion[3]; Poco::UInt16 deviceType; if (rotary_encoder_get_identity(&_rotaryEncoder, deviceUID, masterUID, &position, hardwareVersion, firmwareVersion, &deviceType) == E_OK) { setIdentity(deviceUID, masterUID, position, hardwareVersion, firmwareVersion, deviceType); } rotary_encoder_register_callback(&_rotaryEncoder, ROTARY_ENCODER_CALLBACK_COUNT, reinterpret_cast<void*>(onCountChanged), this); rotary_encoder_register_callback(&_rotaryEncoder, ROTARY_ENCODER_CALLBACK_PRESSED, reinterpret_cast<void*>(onButtonPressed), this); rotary_encoder_register_callback(&_rotaryEncoder, ROTARY_ENCODER_CALLBACK_RELEASED, reinterpret_cast<void*>(onButtonReleased), this); }