Skip to content

Commit 760c4d6

Browse files
authored
feat: typed callbacks and config field descriptions (#2)
1 parent 73d4c15 commit 760c4d6

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/lib.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,71 @@ pub struct WatchConfig {
2323
/// What pin changes we listen for ([`RISING`], [`FALLING`] or [`BOTH`])
2424
pub edge: u32,
2525
/// Called when the pin value changes
26-
pub pin_change: *const c_void,
26+
pub pin_change: extern "C" fn(user_data: *mut c_void, pin: PinId, value: u32),
2727
}
2828

2929
#[repr(C)]
30+
/// Defines the callback for the timer
3031
pub struct TimerConfig {
32+
/// Data that will be passed in the first argument to the callback
3133
pub user_data: *const c_void,
32-
pub callback: *const c_void,
34+
/// Called when the timer fires
35+
pub callback: extern "C" fn(user_data: *mut c_void),
3336
}
3437

3538
#[repr(C)]
39+
/// Defines the pins, configuration, and callbacks for the UART device
3640
pub struct UARTConfig {
41+
/// Data that will be passed in the first argument of the callbacks
3742
pub user_data: *const c_void,
43+
/// The RX pin (or NO_PIN to disable RX)
3844
pub rx: PinId,
45+
/// The TX pin (or NO_PIN to disable TX)
3946
pub tx: PinId,
47+
/// The baud rate (e.g. 115200)
4048
pub baud_rate: u32,
41-
pub rx_data: *const c_void,
42-
pub write_done: *const c_void,
49+
/// Called for each byte received on the RX pin
50+
pub rx_data: extern "C" fn(user_data: *mut c_void, byte: u8),
51+
/// Called when data transmission on the TX pin has finished
52+
pub write_done: extern "C" fn(user_data: *mut c_void),
4353
}
4454

4555
#[repr(C)]
56+
/// Defines the pins, address, and callbacks for the I2C device
4657
pub struct I2CConfig {
58+
/// Data that will be passed in the first argument of the callbacks
4759
pub user_data: *const c_void,
60+
/// Listen for requests matching the given I2C address (7-bit). To listen for all requests, set to 0
4861
pub address: u32,
62+
/// The SCL pin
4963
pub scl: PinId,
64+
/// The SDA pin
5065
pub sda: PinId,
51-
pub connect: *const c_void,
52-
pub read: *const c_void,
53-
pub write: *const c_void,
54-
pub disconnect: *const c_void,
66+
/// Called when the chip is addressed on the I2C bus
67+
pub connect: extern "C" fn(user_data: *mut c_void, address: u32, connect: bool) -> bool,
68+
/// Called when the microcontroller wants to read a byte of data from your chip
69+
pub read: extern "C" fn(user_data: *mut c_void) -> u8,
70+
/// Called when the microcontroller writes a byte to your chip
71+
pub write: extern "C" fn(user_data: *mut c_void, data: u8) -> bool,
72+
/// Called when the microcontroller disconnects from your chip
73+
pub disconnect: extern "C" fn(user_data: *mut c_void),
5574
}
5675

5776
#[repr(C)]
77+
/// Defines the pins, mode, and callbacks for the SPI device
5878
pub struct SPIConfig {
79+
/// Data that will be passed in the first argument of the done callback
5980
pub user_data: *const c_void,
81+
/// The clock pin
6082
pub sck: PinId,
83+
/// The MOSI data pin (or NO_PIN to disable MOSI)
6184
pub mosi: PinId,
85+
/// The MISO data pin (or NO_PIN to disable MISO)
6286
pub miso: PinId,
87+
/// SPI mode: 0, 1, 2, or 3 (default: 0)
6388
pub mode: u32,
64-
pub done: *const c_void,
89+
/// Called when an SPI transaction finishes
90+
pub done: extern "C" fn(user_data: *mut c_void, buffer: *mut u8, count: u32),
6591
}
6692

6793
/// # Safety

0 commit comments

Comments
 (0)