1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/// Declares a static global `ASK_DRIVER` instance protected by a `critical_section` mutex.
///
/// This macro creates a `static` singleton `ASK_DRIVER` suitable for use in
/// interrupt-based environments, where both the main thread and an ISR need
/// to safely access the shared driver state.
///
/// # Arguments
/// - `$tx`: The concrete type of the TX pin (must implement `OutputPin`)
/// - `$rx`: The concrete type of the RX pin (must implement `InputPin`)
/// - `$ptt`: The concrete type of the PRR pin (must implement `OutputPin`). **NOTE**: While you
/// might not have a PTT pin, it is imperative that you pass a type here.
///
/// # Example
/// ```rust
/// use ask433::init_ask_driver;
/// use embedded_hal_mock::eh1::digital::{Mock as Pin};
/// init_ask_driver!(Pin, Pin, Pin);
/// ```
/// Initializes the global `ASK_DRIVER` singleton with a new driver instance.
///
/// This macro wraps construction of the `AskDriver` and stores it inside the
/// globally declared `ASK_DRIVER` created by `init_ask_driver!`.
///
/// # Arguments
/// - `$tx`: The TX pin (must implement `OutputPin`)
/// - `$rx`: The RX pin (must implement `InputPin`)
/// - `$ptt`: The optional PTT pin (must implement `OutputPin`)
/// - `$tpb`: The number of ticks per bit (e.g., 8 for a radio at 2 kbps and an interrupt at ~2MHz)
/// - `$ptt_inverted`: The optional boolean specifying whether the PTT pin should be inverted
/// - `$rx_inverted`: The optional boolean specifying whether the RX pin should be inverted
///
/// # Example
/// ```rust
/// use embedded_hal_mock::eh1::digital::{Mock as Pin, Transaction as Tx, State as St};
/// use ask433::{init_ask_driver, setup_ask_driver};
///
/// init_ask_driver!(Pin, Pin, Pin);
///
/// fn main() {
/// # let tx = Pin::new(&[Tx::set(St::Low)]);
/// # let rx = Pin::new(&[]);
/// setup_ask_driver!(tx, rx, None, 8, None, None);
/// # critical_section::with(|cs| {
/// # if let Some(driver) = ASK_DRIVER.borrow(cs).borrow_mut().as_mut() {
/// # driver.tx.done();
/// # driver.rx.done();
/// # }
/// # });
/// }
/// ```
///
/// # Notes
/// - Must be called inside a critical section-aware context (safe in `main()`).
/// - Requires `init_ask_driver!` to have been used earlier.
/// Calls `tick()` on the global `ASK_DRIVER` if it has been initialized.
///
/// This macro is intended to be invoked from a timer ISR or scheduler to
/// advance the ASK state machine at regular intervals (e.g., every 62.5 µs).
///
/// # Example
/// ```rust,ignore
/// use ask433::tick_ask_timer;
///
/// #[interrupt]
/// fn TIM2() {
/// tick_ask_timer!();
/// }
/// ```
///
/// # Notes
/// - This macro assumes `ASK_DRIVER` was declared with `init_ask_driver!`
/// and initialized via `setup_ask_driver!`.
/// - Safe to call repeatedly — will silently do nothing if the driver hasn't been set up yet.
/// Attempts to receive a completed message from the global ASK driver instance.
///
/// This macro checks whether a valid and complete message is available using
/// `driver.availabile()`, and if so, returns a reference to the decoded message
/// payload slice. If no message is available, or the driver is currently transmitting,
/// it returns `None`.
///
/// # Requirements
/// - The global `ASK_DRIVER` instance must have been initialized using
/// [`init_ask_driver!`](crate::init_ask_driver) and [`setup_ask_driver!`](crate::setup_ask_driver).
/// - Must be called in a context where `critical_section` is available and safe to use.
///
/// # Returns
/// - `Some(&[u8])`: A reference to the message payload if available
/// - `None`: If no message is currently available
///
/// # Example
/// ```rust
/// use ask433::{receive_from_ask, init_ask_driver};
/// # use embedded_hal_mock::eh1::digital::{Mock as Pin};
///
/// init_ask_driver!(Pin, Pin, Pin);
///
/// fn main() {
/// // ...
/// if let Some(msg) = receive_from_ask!() {
/// // Process message payload
/// }
/// }
/// ```
/// Sends a message from the global ASK driver using a heapless `Vec<u8>`.
///
/// This macro supports two usage patterns:
/// 1. Repeating a single element: `send_from_ask!(0xAA; 10)`
/// 2. Sending an explicit sequence of bytes: `send_from_ask!(0x01, 0x02, 0x03)`
///
/// The message is internally collected into a `heapless::Vec<u8, ASK_MAX_MESSAGE_LEN_USIZE>`
/// and passed to the `send()` method on the global driver.
///
/// # Requirements
/// - The driver must be initialized via `init_global_ask_driver!` and `setup_global_ask_driver!`.
///
/// # Panics
/// - Will panic at runtime if the message exceeds `ASK_MAX_MESSAGE_LEN`
///
/// # Example
/// ```rust,ignore
/// use ask433::{send_from_ask, init_ask_driver};
/// # use embedded_hal_mock::eh1::digital::{Mock as Pin};
///
/// init_ask_driver!(Pin, Pin, Pin);
///
/// fn main() {
///
/// // ...
///
/// let sent1 = send_from_ask!(0xAB; 4); // send [0xAB, 0xAB, 0xAB, 0xAB]
/// let sent2 = send_from_ask!(1, 2, 3, 4, 5); // send [1, 2, 3, 4, 5]
/// let sent3 = send_from_ask!("Hello, World!"); // send [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21]
/// }
/// ```
/// Sends a message from the global ASK driver using a heapless `Vec<u8>`.
///
/// This macro supports two usage patterns:
/// 1. Repeating a single element: `send_from_ask!(0xAA; 10)`
/// 2. Sending an explicit sequence of bytes: `send_from_ask!(0x01, 0x02, 0x03)`
///
/// The message is internally collected into a `heapless::Vec<u8, ASK_MAX_MESSAGE_LEN_USIZE>`
/// and passed to the `send()` method on the global driver.
///
/// # Requirements
/// - The driver must be initialized via `init_ask_driver!` and `setup_ask_driver!`.
///
/// # Panics
/// - Will panic at runtime if the message exceeds `ASK_MAX_MESSAGE_LEN`
///
/// # Example
/// ```rust
/// use ask433::{send_from_ask, init_ask_driver};
/// # use embedded_hal_mock::eh1::digital::{Mock as Pin};
///
/// init_ask_driver!(Pin, Pin, Pin);
///
/// fn main() {
///
/// // ...
///
/// let sent1: bool = send_from_ask![0xAB; 4]; // send [0xAB, 0xAB, 0xAB, 0xAB]
/// let sent2: bool = send_from_ask![1, 2, 3, 4, 5]; // send [1, 2, 3, 4, 5]
/// let sent3: bool = send_from_ask!("Hello, World!"); // send [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21]
///
/// }
/// ```
// see src/lib.rs for tests