1usestd::{error::Error,io}; 2 3usetinkerforge::{ip_connection::IpConnection,rgb_led_button_bricklet::*}; 4 5constHOST: &str="localhost"; 6constPORT: u16=4223; 7constUID: &str="XYZ";// Change XYZ to the UID of your RGB LED Button Bricklet. 8 9fnmain()-> Result<(),Box<dynError>>{10letipcon=IpConnection::new();// Create IP connection.11letrlb=RgbLedButtonBricklet::new(UID,&ipcon);// Create device object.1213ipcon.connect((HOST,PORT)).recv()??;// Connect to brickd.14// Don't use device before ipcon is connected.1516// Set light blue color17rlb.set_color(0,170,234).recv()?;1819println!("Press enter to exit.");20letmut_input=String::new();21io::stdin().read_line(&mut_input)?;22ipcon.disconnect();23Ok(())24}
1usestd::{error::Error,io}; 2 3usetinkerforge::{ip_connection::IpConnection,rgb_led_button_bricklet::*}; 4 5constHOST: &str="localhost"; 6constPORT: u16=4223; 7constUID: &str="XYZ";// Change XYZ to the UID of your RGB LED Button Bricklet. 8 9fnmain()-> Result<(),Box<dynError>>{10letipcon=IpConnection::new();// Create IP connection.11letrlb=RgbLedButtonBricklet::new(UID,&ipcon);// Create device object.1213ipcon.connect((HOST,PORT)).recv()??;// Connect to brickd.14// Don't use device before ipcon is connected.1516// Get current button state.17letstate=rlb.get_button_state().recv()?;1819ifstate==RGB_LED_BUTTON_BRICKLET_BUTTON_STATE_PRESSED{20println!("State: Pressed");21}elseifstate==RGB_LED_BUTTON_BRICKLET_BUTTON_STATE_RELEASED{22println!("State: Released");23}2425println!("Press enter to exit.");26letmut_input=String::new();27io::stdin().read_line(&mut_input)?;28ipcon.disconnect();29Ok(())30}
1usestd::{error::Error,io,thread}; 2usetinkerforge::{ip_connection::IpConnection,rgb_led_button_bricklet::*}; 3 4constHOST: &str="localhost"; 5constPORT: u16=4223; 6constUID: &str="XYZ";// Change XYZ to the UID of your RGB LED Button Bricklet. 7 8fnmain()-> Result<(),Box<dynError>>{ 9letipcon=IpConnection::new();// Create IP connection.10letrlb=RgbLedButtonBricklet::new(UID,&ipcon);// Create device object.1112ipcon.connect((HOST,PORT)).recv()??;// Connect to brickd.13// Don't use device before ipcon is connected.1415letbutton_state_changed_receiver=rlb.get_button_state_changed_callback_receiver();1617// Spawn thread to handle received callback messages.18// This thread ends when the `rlb` object19// is dropped, so there is no need for manual cleanup.20thread::spawn(move||{21forbutton_state_changedinbutton_state_changed_receiver{22ifbutton_state_changed==RGB_LED_BUTTON_BRICKLET_BUTTON_STATE_PRESSED{23println!("State: Pressed");24}elseifbutton_state_changed==RGB_LED_BUTTON_BRICKLET_BUTTON_STATE_RELEASED{25println!("State: Released");26}27}28});2930println!("Press enter to exit.");31letmut_input=String::new();32io::stdin().read_line(&mut_input)?;33ipcon.disconnect();34Ok(())35}
To allow non-blocking usage, nearly every function of the Rust bindings returns
a wrapper around a mpsc::Receiver. To block until the function has finished and
get your result, call one of the receiver's recv variants. Those return either
the result sent by the device, or any error occurred.
Functions returning a result directly will block until the device has finished
processing the request.
All functions listed below are thread-safe, those which return a receiver are lock-free.
config – Type: u8, Range: See constants, Default: 3
Sets the status LED configuration. By default the LED shows
communication traffic between Brick and Bricklet, it flickers once
for every 10 received data packets.
You can also turn the LED permanently on/off or show a heartbeat.
If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
The following constants are available for this function:
temperature – Type: i16, Unit: 1 °C, Range: [-215 to 215 - 1]
Returns the temperature as measured inside the microcontroller. The
value returned is not the ambient temperature!
The temperature is only proportional to the real temperature and it has bad
accuracy. Practically it is only useful as an indicator for
temperature changes.
Callbacks can be registered to receive
time critical or recurring data from the device. The registration is done
with the corresponding get_*_callback_receiver function, which returns a receiver
for callback events.
Note
Using callbacks for recurring events is always preferred
compared to using getters. It will use less USB bandwidth and the latency
will be a lot better, since there is no round trip time.
Virtual functions don't communicate with the device itself, but operate only on
the API bindings device object. They can be called without the corresponding
IP Connection object being connected.
Returns the version of the API definition implemented
by this API bindings. This is neither the release version of this API bindings
nor does it tell you anything about the represented Brick or Bricklet.
Returns the response expected flag for the function specified by the function
ID parameter. It is true if the function is expected to send a response,
false otherwise.
For getter functions this is enabled by default and cannot be disabled,
because those functions will always send a response. For callback configuration
functions it is enabled by default too, but can be disabled by
RgbLedButtonBricklet::set_response_expected(). For setter functions it is disabled by default
and can be enabled.
Enabling the response expected flag for a setter function allows to detect
timeouts and other error conditions calls of this setter as well. The
device will then send a response for this purpose. If this flag is disabled for
a setter function then no response is sent and errors are silently ignored,
because they cannot be detected.
The following constants are available for this function:
Changes the response expected flag of the function specified by the
function ID parameter. This flag can only be changed for setter (default value:
false) and callback configuration functions (default value: true). For
getter functions it is always enabled.
Enabling the response expected flag for a setter function allows to detect
timeouts and other error conditions calls of this setter as well. The
device will then send a response for this purpose. If this flag is disabled for
a setter function then no response is sent and errors are silently ignored,
because they cannot be detected.
The following constants are available for this function:
Internal functions are used for maintenance tasks such as flashing a new firmware
of changing the UID of a Bricklet. These task should be performed using
Brick Viewer instead of using the internal functions directly.
Sets the bootloader mode and returns the status after the requested
mode change was instigated.
You can change from bootloader mode to firmware mode and vice versa. A change
from bootloader mode to firmware mode will only take place if the entry function,
device identifier and CRC are present and correct.
This function is used by Brick Viewer during flashing. It should not be
necessary to call it in a normal user program.
The following constants are available for this function:
Sets the firmware pointer for RgbLedButtonBricklet::write_firmware(). The pointer has
to be increased by chunks of size 64. The data is written to flash
every 4 chunks (which equals to one page of size 256).
This function is used by Brick Viewer during flashing. It should not be
necessary to call it in a normal user program.