1#[allow(unused_imports)]
15use crate::{
16 base58::Uid, byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
17 low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum HatBrickFunction {
24 SetSleepMode,
25 GetSleepMode,
26 SetBrickletPower,
27 GetBrickletPower,
28 GetVoltages,
29 SetVoltagesCallbackConfiguration,
30 GetVoltagesCallbackConfiguration,
31 SetRtcDriver,
32 GetRtcDriver,
33 GetSpitfpErrorCount,
34 SetBootloaderMode,
35 GetBootloaderMode,
36 SetWriteFirmwarePointer,
37 WriteFirmware,
38 SetStatusLedConfig,
39 GetStatusLedConfig,
40 GetChipTemperature,
41 Reset,
42 WriteUid,
43 ReadUid,
44 GetIdentity,
45 CallbackVoltages,
46}
47impl From<HatBrickFunction> for u8 {
48 fn from(fun: HatBrickFunction) -> Self {
49 match fun {
50 HatBrickFunction::SetSleepMode => 1,
51 HatBrickFunction::GetSleepMode => 2,
52 HatBrickFunction::SetBrickletPower => 3,
53 HatBrickFunction::GetBrickletPower => 4,
54 HatBrickFunction::GetVoltages => 5,
55 HatBrickFunction::SetVoltagesCallbackConfiguration => 6,
56 HatBrickFunction::GetVoltagesCallbackConfiguration => 7,
57 HatBrickFunction::SetRtcDriver => 9,
58 HatBrickFunction::GetRtcDriver => 10,
59 HatBrickFunction::GetSpitfpErrorCount => 234,
60 HatBrickFunction::SetBootloaderMode => 235,
61 HatBrickFunction::GetBootloaderMode => 236,
62 HatBrickFunction::SetWriteFirmwarePointer => 237,
63 HatBrickFunction::WriteFirmware => 238,
64 HatBrickFunction::SetStatusLedConfig => 239,
65 HatBrickFunction::GetStatusLedConfig => 240,
66 HatBrickFunction::GetChipTemperature => 242,
67 HatBrickFunction::Reset => 243,
68 HatBrickFunction::WriteUid => 248,
69 HatBrickFunction::ReadUid => 249,
70 HatBrickFunction::GetIdentity => 255,
71 HatBrickFunction::CallbackVoltages => 8,
72 }
73 }
74}
75pub const HAT_BRICK_RTC_DRIVER_PCF8523: u8 = 0;
76pub const HAT_BRICK_RTC_DRIVER_DS1338: u8 = 1;
77pub const HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
78pub const HAT_BRICK_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
79pub const HAT_BRICK_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
80pub const HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
81pub const HAT_BRICK_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
82pub const HAT_BRICK_BOOTLOADER_STATUS_OK: u8 = 0;
83pub const HAT_BRICK_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
84pub const HAT_BRICK_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
85pub const HAT_BRICK_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
86pub const HAT_BRICK_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
87pub const HAT_BRICK_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
88pub const HAT_BRICK_STATUS_LED_CONFIG_OFF: u8 = 0;
89pub const HAT_BRICK_STATUS_LED_CONFIG_ON: u8 = 1;
90pub const HAT_BRICK_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
91pub const HAT_BRICK_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
92
93#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
94pub struct SleepMode {
95 pub power_off_delay: u32,
96 pub power_off_duration: u32,
97 pub raspberry_pi_off: bool,
98 pub bricklets_off: bool,
99 pub enable_sleep_indicator: bool,
100}
101impl FromByteSlice for SleepMode {
102 fn bytes_expected() -> usize {
103 11
104 }
105 fn from_le_byte_slice(bytes: &[u8]) -> SleepMode {
106 SleepMode {
107 power_off_delay: <u32>::from_le_byte_slice(&bytes[0..4]),
108 power_off_duration: <u32>::from_le_byte_slice(&bytes[4..8]),
109 raspberry_pi_off: <bool>::from_le_byte_slice(&bytes[8..9]),
110 bricklets_off: <bool>::from_le_byte_slice(&bytes[9..10]),
111 enable_sleep_indicator: <bool>::from_le_byte_slice(&bytes[10..11]),
112 }
113 }
114}
115
116#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
117pub struct Voltages {
118 pub voltage_usb: u16,
119 pub voltage_dc: u16,
120}
121impl FromByteSlice for Voltages {
122 fn bytes_expected() -> usize {
123 4
124 }
125 fn from_le_byte_slice(bytes: &[u8]) -> Voltages {
126 Voltages { voltage_usb: <u16>::from_le_byte_slice(&bytes[0..2]), voltage_dc: <u16>::from_le_byte_slice(&bytes[2..4]) }
127 }
128}
129
130#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
131pub struct VoltagesCallbackConfiguration {
132 pub period: u32,
133 pub value_has_to_change: bool,
134}
135impl FromByteSlice for VoltagesCallbackConfiguration {
136 fn bytes_expected() -> usize {
137 5
138 }
139 fn from_le_byte_slice(bytes: &[u8]) -> VoltagesCallbackConfiguration {
140 VoltagesCallbackConfiguration {
141 period: <u32>::from_le_byte_slice(&bytes[0..4]),
142 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
143 }
144 }
145}
146
147#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
148pub struct VoltagesEvent {
149 pub voltage_usb: u16,
150 pub voltage_dc: u16,
151}
152impl FromByteSlice for VoltagesEvent {
153 fn bytes_expected() -> usize {
154 4
155 }
156 fn from_le_byte_slice(bytes: &[u8]) -> VoltagesEvent {
157 VoltagesEvent { voltage_usb: <u16>::from_le_byte_slice(&bytes[0..2]), voltage_dc: <u16>::from_le_byte_slice(&bytes[2..4]) }
158 }
159}
160
161#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
162pub struct SpitfpErrorCount {
163 pub error_count_ack_checksum: u32,
164 pub error_count_message_checksum: u32,
165 pub error_count_frame: u32,
166 pub error_count_overflow: u32,
167}
168impl FromByteSlice for SpitfpErrorCount {
169 fn bytes_expected() -> usize {
170 16
171 }
172 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
173 SpitfpErrorCount {
174 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
175 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
176 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
177 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
178 }
179 }
180}
181
182#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
183pub struct Identity {
184 pub uid: String,
185 pub connected_uid: String,
186 pub position: char,
187 pub hardware_version: [u8; 3],
188 pub firmware_version: [u8; 3],
189 pub device_identifier: u16,
190}
191impl FromByteSlice for Identity {
192 fn bytes_expected() -> usize {
193 25
194 }
195 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
196 Identity {
197 uid: <String>::from_le_byte_slice(&bytes[0..8]),
198 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
199 position: <char>::from_le_byte_slice(&bytes[16..17]),
200 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
201 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
202 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
203 }
204 }
205}
206
207#[derive(Clone)]
209pub struct HatBrick {
210 device: Device,
211}
212impl HatBrick {
213 pub const DEVICE_IDENTIFIER: u16 = 111;
214 pub const DEVICE_DISPLAY_NAME: &'static str = "HAT Brick";
215 pub fn new(uid: Uid, connection: AsyncIpConnection) -> HatBrick {
217 let mut result = HatBrick { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
218 result.device.response_expected[u8::from(HatBrickFunction::SetSleepMode) as usize] = ResponseExpectedFlag::False;
219 result.device.response_expected[u8::from(HatBrickFunction::GetSleepMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
220 result.device.response_expected[u8::from(HatBrickFunction::SetBrickletPower) as usize] = ResponseExpectedFlag::False;
221 result.device.response_expected[u8::from(HatBrickFunction::GetBrickletPower) as usize] = ResponseExpectedFlag::AlwaysTrue;
222 result.device.response_expected[u8::from(HatBrickFunction::GetVoltages) as usize] = ResponseExpectedFlag::AlwaysTrue;
223 result.device.response_expected[u8::from(HatBrickFunction::SetVoltagesCallbackConfiguration) as usize] = ResponseExpectedFlag::True;
224 result.device.response_expected[u8::from(HatBrickFunction::GetVoltagesCallbackConfiguration) as usize] =
225 ResponseExpectedFlag::AlwaysTrue;
226 result.device.response_expected[u8::from(HatBrickFunction::SetRtcDriver) as usize] = ResponseExpectedFlag::False;
227 result.device.response_expected[u8::from(HatBrickFunction::GetRtcDriver) as usize] = ResponseExpectedFlag::AlwaysTrue;
228 result.device.response_expected[u8::from(HatBrickFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
229 result.device.response_expected[u8::from(HatBrickFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
230 result.device.response_expected[u8::from(HatBrickFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
231 result.device.response_expected[u8::from(HatBrickFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
232 result.device.response_expected[u8::from(HatBrickFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
233 result.device.response_expected[u8::from(HatBrickFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
234 result.device.response_expected[u8::from(HatBrickFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
235 result.device.response_expected[u8::from(HatBrickFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
236 result.device.response_expected[u8::from(HatBrickFunction::Reset) as usize] = ResponseExpectedFlag::False;
237 result.device.response_expected[u8::from(HatBrickFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
238 result.device.response_expected[u8::from(HatBrickFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
239 result.device.response_expected[u8::from(HatBrickFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
240 result
241 }
242
243 pub fn get_response_expected(&mut self, fun: HatBrickFunction) -> Result<bool, GetResponseExpectedError> {
258 self.device.get_response_expected(u8::from(fun))
259 }
260
261 pub fn set_response_expected(&mut self, fun: HatBrickFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
270 self.device.set_response_expected(u8::from(fun), response_expected)
271 }
272
273 pub fn set_response_expected_all(&mut self, response_expected: bool) {
275 self.device.set_response_expected_all(response_expected)
276 }
277
278 pub fn get_api_version(&self) -> [u8; 3] {
281 self.device.api_version
282 }
283
284 pub async fn get_voltages_callback_receiver(&mut self) -> impl Stream<Item = VoltagesEvent> {
293 self.device
294 .get_callback_receiver(u8::from(HatBrickFunction::CallbackVoltages))
295 .await
296 .map(|p| VoltagesEvent::from_le_byte_slice(p.body()))
297 }
298
299 pub async fn set_sleep_mode(
322 &mut self,
323 power_off_delay: u32,
324 power_off_duration: u32,
325 raspberry_pi_off: bool,
326 bricklets_off: bool,
327 enable_sleep_indicator: bool,
328 ) -> Result<(), TinkerforgeError> {
329 let mut payload = [0; 11];
330 power_off_delay.write_to_slice(&mut payload[0..4]);
331 power_off_duration.write_to_slice(&mut payload[4..8]);
332 raspberry_pi_off.write_to_slice(&mut payload[8..9]);
333 bricklets_off.write_to_slice(&mut payload[9..10]);
334 enable_sleep_indicator.write_to_slice(&mut payload[10..11]);
335
336 #[allow(unused_variables)]
337 let result = self.device.set(u8::from(HatBrickFunction::SetSleepMode), &payload).await?;
338 Ok(())
339 }
340
341 pub async fn get_sleep_mode(&mut self) -> Result<SleepMode, TinkerforgeError> {
343 let payload = [0; 0];
344
345 #[allow(unused_variables)]
346 let result = self.device.get(u8::from(HatBrickFunction::GetSleepMode), &payload).await?;
347 Ok(SleepMode::from_le_byte_slice(result.body()))
348 }
349
350 pub async fn set_bricklet_power(&mut self, bricklet_power: bool) -> Result<(), TinkerforgeError> {
352 let mut payload = [0; 1];
353 bricklet_power.write_to_slice(&mut payload[0..1]);
354
355 #[allow(unused_variables)]
356 let result = self.device.set(u8::from(HatBrickFunction::SetBrickletPower), &payload).await?;
357 Ok(())
358 }
359
360 pub async fn get_bricklet_power(&mut self) -> Result<bool, TinkerforgeError> {
362 let payload = [0; 0];
363
364 #[allow(unused_variables)]
365 let result = self.device.get(u8::from(HatBrickFunction::GetBrickletPower), &payload).await?;
366 Ok(bool::from_le_byte_slice(result.body()))
367 }
368
369 pub async fn get_voltages(&mut self) -> Result<Voltages, TinkerforgeError> {
384 let payload = [0; 0];
385
386 #[allow(unused_variables)]
387 let result = self.device.get(u8::from(HatBrickFunction::GetVoltages), &payload).await?;
388 Ok(Voltages::from_le_byte_slice(result.body()))
389 }
390
391 pub async fn set_voltages_callback_configuration(&mut self, period: u32, value_has_to_change: bool) -> Result<(), TinkerforgeError> {
404 let mut payload = [0; 5];
405 period.write_to_slice(&mut payload[0..4]);
406 value_has_to_change.write_to_slice(&mut payload[4..5]);
407
408 #[allow(unused_variables)]
409 let result = self.device.set(u8::from(HatBrickFunction::SetVoltagesCallbackConfiguration), &payload).await?;
410 Ok(())
411 }
412
413 pub async fn get_voltages_callback_configuration(&mut self) -> Result<VoltagesCallbackConfiguration, TinkerforgeError> {
419 let payload = [0; 0];
420
421 #[allow(unused_variables)]
422 let result = self.device.get(u8::from(HatBrickFunction::GetVoltagesCallbackConfiguration), &payload).await?;
423 Ok(VoltagesCallbackConfiguration::from_le_byte_slice(result.body()))
424 }
425
426 pub async fn set_rtc_driver(&mut self, rtc_driver: u8) -> Result<(), TinkerforgeError> {
441 let mut payload = [0; 1];
442 rtc_driver.write_to_slice(&mut payload[0..1]);
443
444 #[allow(unused_variables)]
445 let result = self.device.set(u8::from(HatBrickFunction::SetRtcDriver), &payload).await?;
446 Ok(())
447 }
448
449 pub async fn get_rtc_driver(&mut self) -> Result<u8, TinkerforgeError> {
458 let payload = [0; 0];
459
460 #[allow(unused_variables)]
461 let result = self.device.get(u8::from(HatBrickFunction::GetRtcDriver), &payload).await?;
462 Ok(u8::from_le_byte_slice(result.body()))
463 }
464
465 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
477 let payload = [0; 0];
478
479 #[allow(unused_variables)]
480 let result = self.device.get(u8::from(HatBrickFunction::GetSpitfpErrorCount), &payload).await?;
481 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
482 }
483
484 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
507 let mut payload = [0; 1];
508 mode.write_to_slice(&mut payload[0..1]);
509
510 #[allow(unused_variables)]
511 let result = self.device.get(u8::from(HatBrickFunction::SetBootloaderMode), &payload).await?;
512 Ok(u8::from_le_byte_slice(result.body()))
513 }
514
515 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
524 let payload = [0; 0];
525
526 #[allow(unused_variables)]
527 let result = self.device.get(u8::from(HatBrickFunction::GetBootloaderMode), &payload).await?;
528 Ok(u8::from_le_byte_slice(result.body()))
529 }
530
531 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
538 let mut payload = [0; 4];
539 pointer.write_to_slice(&mut payload[0..4]);
540
541 #[allow(unused_variables)]
542 let result = self.device.set(u8::from(HatBrickFunction::SetWriteFirmwarePointer), &payload).await?;
543 Ok(())
544 }
545
546 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
555 let mut payload = [0; 64];
556 data.write_to_slice(&mut payload[0..64]);
557
558 #[allow(unused_variables)]
559 let result = self.device.get(u8::from(HatBrickFunction::WriteFirmware), &payload).await?;
560 Ok(u8::from_le_byte_slice(result.body()))
561 }
562
563 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
577 let mut payload = [0; 1];
578 config.write_to_slice(&mut payload[0..1]);
579
580 #[allow(unused_variables)]
581 let result = self.device.set(u8::from(HatBrickFunction::SetStatusLedConfig), &payload).await?;
582 Ok(())
583 }
584
585 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
593 let payload = [0; 0];
594
595 #[allow(unused_variables)]
596 let result = self.device.get(u8::from(HatBrickFunction::GetStatusLedConfig), &payload).await?;
597 Ok(u8::from_le_byte_slice(result.body()))
598 }
599
600 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
607 let payload = [0; 0];
608
609 #[allow(unused_variables)]
610 let result = self.device.get(u8::from(HatBrickFunction::GetChipTemperature), &payload).await?;
611 Ok(i16::from_le_byte_slice(result.body()))
612 }
613
614 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
621 let payload = [0; 0];
622
623 #[allow(unused_variables)]
624 let result = self.device.set(u8::from(HatBrickFunction::Reset), &payload).await?;
625 Ok(())
626 }
627
628 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
634 let mut payload = [0; 4];
635 uid.write_to_slice(&mut payload[0..4]);
636
637 #[allow(unused_variables)]
638 let result = self.device.set(u8::from(HatBrickFunction::WriteUid), &payload).await?;
639 Ok(())
640 }
641
642 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
645 let payload = [0; 0];
646
647 #[allow(unused_variables)]
648 let result = self.device.get(u8::from(HatBrickFunction::ReadUid), &payload).await?;
649 Ok(u32::from_le_byte_slice(result.body()))
650 }
651
652 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
662 let payload = [0; 0];
663
664 #[allow(unused_variables)]
665 let result = self.device.get(u8::from(HatBrickFunction::GetIdentity), &payload).await?;
666 Ok(Identity::from_le_byte_slice(result.body()))
667 }
668}