You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-reference.md
+96-18Lines changed: 96 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,26 @@
1
1
# API Reference
2
2
3
-
This section documents the public C API exposed by the CRUMBS core and the platform HALs (Arduino and Linux).
3
+
This file documents the public CRUMBS C API (core + provided HAL adapters) so you can use CRUMBS as a compiled library without reading the implementation.
4
4
5
-
The core is intentionally C-friendly and small so it is easy to reuse in both Arduino and native Linux code.
5
+
The core is intentionally small and C-friendly so it is easy to consume from Arduino sketches, PlatformIO projects or native Linux programs.
6
6
7
7
## Key types and constants
8
8
9
-
-`crumbs_message_t` — fixed-size message struct (see `src/crumbs_message.h`). Fields include `slice_address`, `type_id`, `command_type`, `data[7]`, `crc8`.
10
-
-`crumbs_context_t` — per-endpoint context used by the core (role, address, CRC stats, callbacks).
11
-
-`CRUMBS_DATA_LENGTH` — number of float payload elements (7).
12
-
-`CRUMBS_MESSAGE_SIZE` — serialized frame size in bytes (31).
9
+
-`crumbs_message_t` — fixed-size message struct (see `src/crumbs_message.h`). Fields:
-`crumbs_encode_message()` returns CRUMBS_MESSAGE_SIZE on success, 0 on failure (e.g., buffer too small).
61
+
-`crumbs_decode_message()` returns `0` on success, `-1` if buffer too small, and `-2` if the CRC check fails. When a non-NULL context is passed the function will update CRC statistics accessible via `crumbs_get_crc_error_count()` and `crumbs_last_crc_ok()`.
62
+
63
+
- Controller helpers
64
+
-`crumbs_controller_send()` returns `0` on success; non-zero indicates an I2C write error as returned by the platform `write_fn`.
65
+
66
+
- Peripheral helpers
67
+
-`crumbs_peripheral_handle_receive()` returns `0` on success, negative on decode/CRC errors. When successful it will invoke `ctx->on_message` if configured.
68
+
-`crumbs_peripheral_build_reply()` returns `0` on success. If no reply is available the function returns 0 and sets `out_len` to 0.
69
+
70
+
All HAL adapters follow the `crumbs_i2c_*` function signatures in `src/crumbs_i2c.h` — write functions should return 0 on success and non-zero on error; read functions should return number-of-bytes-read (>=0) or negative on error.
47
71
```
48
72
49
-
## Arduino HAL (highlevel)
73
+
## Arduino HAL (high-level conveniences)
50
74
51
-
```c
75
+
````c
52
76
/* Initialize controller/peripheral using default Wire instance */
@@ -67,19 +114,37 @@ int crumbs_linux_init_controller(crumbs_context_t *ctx,
67
114
void crumbs_linux_close(crumbs_linux_i2c_t *i2c);
68
115
int crumbs_linux_i2c_write(void *user_ctx, uint8_t target_addr, const uint8_t *data, size_t len);
69
116
int crumbs_linux_read_message(crumbs_linux_i2c_t *i2c, uint8_t target_addr, crumbs_context_t *ctx, crumbs_message_t *out_msg);
70
-
```
117
+
118
+
Return codes and notes (Linux HAL)
119
+
- `crumbs_linux_init_controller()` returns 0 on success, -1 for bad args, -2 if opening the bus failed.
120
+
- `crumbs_linux_i2c_write()` and `crumbs_linux_read()` will return negative error codes on failure — see `src/crumbs_linux.h` for the small set of linux-wire-mapped errors.
121
+
122
+
Usage (controller, Linux):
123
+
124
+
```c
125
+
crumbs_context_t ctx;
126
+
crumbs_linux_i2c_t bus;
127
+
int rc = crumbs_linux_init_controller(&ctx, &bus, "/dev/i2c-1", 10000);
128
+
// send using crumbs_controller_send(&ctx, addr, &msg, crumbs_linux_i2c_write, &bus);
129
+
````
130
+
131
+
````
71
132
72
133
Note: Most functions return `0` on success and negative or non-zero codes on error — consult the headers for specific return values.
73
134
74
135
## Scanner & read primitive
75
136
76
-
The core provides a CRUMBS-aware scanner which attempts to read a full CRUMBS frame from candidate addresses and accepts only CRC-valid frames. This lets controllers reliably discover devices that actually run the CRUMBS protocol (not merely devices that ACK the bus).
137
+
The core scanner uses a read primitive (`crumbs_i2c_read_fn`) and an optional write primitive to reliably discover devices that actually speak CRUMBS. It reads up to `CRUMBS_MESSAGE_SIZE` bytes and only accepts addresses that provide a CRC-valid CRUMBS frame.
138
+
139
+
Signature (read primitive):
77
140
78
141
```c
79
-
/* Read primitive used by the core scanner and HALs */
Return value: number of discovered CRUMBS device addresses (>=0) or negative on error.
167
+
168
+
Platform-provided read helpers:
169
+
170
+
- `crumbs_arduino_read()` — TwoWire-based read implementation for Arduino.
171
+
- `crumbs_linux_read()` — linux-wire based read implementation for native Linux.
172
+
173
+
---
174
+
175
+
If you need a quick reference to these types and function signatures for direct inclusion into code, the public headers in `src/` are intentionally stable and self-documenting; use them as authoritative API. The summary above is complete enough to consume the library from a binary/compressed distribution (static archive + headers) without reading implementation files.
0 commit comments