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 SegmentDisplay4x7V2BrickletFunction {
24 SetSegments,
25 GetSegments,
26 SetBrightness,
27 GetBrightness,
28 SetNumericValue,
29 SetSelectedSegment,
30 GetSelectedSegment,
31 StartCounter,
32 GetCounterValue,
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 CallbackCounterFinished,
46}
47impl From<SegmentDisplay4x7V2BrickletFunction> for u8 {
48 fn from(fun: SegmentDisplay4x7V2BrickletFunction) -> Self {
49 match fun {
50 SegmentDisplay4x7V2BrickletFunction::SetSegments => 1,
51 SegmentDisplay4x7V2BrickletFunction::GetSegments => 2,
52 SegmentDisplay4x7V2BrickletFunction::SetBrightness => 3,
53 SegmentDisplay4x7V2BrickletFunction::GetBrightness => 4,
54 SegmentDisplay4x7V2BrickletFunction::SetNumericValue => 5,
55 SegmentDisplay4x7V2BrickletFunction::SetSelectedSegment => 6,
56 SegmentDisplay4x7V2BrickletFunction::GetSelectedSegment => 7,
57 SegmentDisplay4x7V2BrickletFunction::StartCounter => 8,
58 SegmentDisplay4x7V2BrickletFunction::GetCounterValue => 9,
59 SegmentDisplay4x7V2BrickletFunction::GetSpitfpErrorCount => 234,
60 SegmentDisplay4x7V2BrickletFunction::SetBootloaderMode => 235,
61 SegmentDisplay4x7V2BrickletFunction::GetBootloaderMode => 236,
62 SegmentDisplay4x7V2BrickletFunction::SetWriteFirmwarePointer => 237,
63 SegmentDisplay4x7V2BrickletFunction::WriteFirmware => 238,
64 SegmentDisplay4x7V2BrickletFunction::SetStatusLedConfig => 239,
65 SegmentDisplay4x7V2BrickletFunction::GetStatusLedConfig => 240,
66 SegmentDisplay4x7V2BrickletFunction::GetChipTemperature => 242,
67 SegmentDisplay4x7V2BrickletFunction::Reset => 243,
68 SegmentDisplay4x7V2BrickletFunction::WriteUid => 248,
69 SegmentDisplay4x7V2BrickletFunction::ReadUid => 249,
70 SegmentDisplay4x7V2BrickletFunction::GetIdentity => 255,
71 SegmentDisplay4x7V2BrickletFunction::CallbackCounterFinished => 10,
72 }
73 }
74}
75pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
76pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
77pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
78pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
79pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
80pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
81pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
82pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
83pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
84pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
85pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
86pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
87pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
88pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
89pub const SEGMENT_DISPLAY_4X7_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
90
91#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
92pub struct Segments {
93 pub digit0: [bool; 8],
94 pub digit1: [bool; 8],
95 pub digit2: [bool; 8],
96 pub digit3: [bool; 8],
97 pub colon: [bool; 2],
98 pub tick: bool,
99}
100impl FromByteSlice for Segments {
101 fn bytes_expected() -> usize {
102 6
103 }
104 fn from_le_byte_slice(bytes: &[u8]) -> Segments {
105 Segments {
106 digit0: <[bool; 8]>::from_le_byte_slice(&bytes[0..1]),
107 digit1: <[bool; 8]>::from_le_byte_slice(&bytes[1..2]),
108 digit2: <[bool; 8]>::from_le_byte_slice(&bytes[2..3]),
109 digit3: <[bool; 8]>::from_le_byte_slice(&bytes[3..4]),
110 colon: <[bool; 2]>::from_le_byte_slice(&bytes[4..5]),
111 tick: <bool>::from_le_byte_slice(&bytes[5..6]),
112 }
113 }
114}
115
116#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
117pub struct SpitfpErrorCount {
118 pub error_count_ack_checksum: u32,
119 pub error_count_message_checksum: u32,
120 pub error_count_frame: u32,
121 pub error_count_overflow: u32,
122}
123impl FromByteSlice for SpitfpErrorCount {
124 fn bytes_expected() -> usize {
125 16
126 }
127 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
128 SpitfpErrorCount {
129 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
130 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
131 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
132 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
133 }
134 }
135}
136
137#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
138pub struct Identity {
139 pub uid: String,
140 pub connected_uid: String,
141 pub position: char,
142 pub hardware_version: [u8; 3],
143 pub firmware_version: [u8; 3],
144 pub device_identifier: u16,
145}
146impl FromByteSlice for Identity {
147 fn bytes_expected() -> usize {
148 25
149 }
150 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
151 Identity {
152 uid: <String>::from_le_byte_slice(&bytes[0..8]),
153 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
154 position: <char>::from_le_byte_slice(&bytes[16..17]),
155 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
156 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
157 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
158 }
159 }
160}
161
162#[derive(Clone)]
164pub struct SegmentDisplay4x7V2Bricklet {
165 device: Device,
166}
167impl SegmentDisplay4x7V2Bricklet {
168 pub const DEVICE_IDENTIFIER: u16 = 2137;
169 pub const DEVICE_DISPLAY_NAME: &'static str = "Segment Display 4x7 Bricklet 2.0";
170 pub fn new(uid: Uid, connection: AsyncIpConnection) -> SegmentDisplay4x7V2Bricklet {
172 let mut result = SegmentDisplay4x7V2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
173 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::SetSegments) as usize] = ResponseExpectedFlag::False;
174 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetSegments) as usize] =
175 ResponseExpectedFlag::AlwaysTrue;
176 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::SetBrightness) as usize] =
177 ResponseExpectedFlag::False;
178 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetBrightness) as usize] =
179 ResponseExpectedFlag::AlwaysTrue;
180 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::SetNumericValue) as usize] =
181 ResponseExpectedFlag::False;
182 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::SetSelectedSegment) as usize] =
183 ResponseExpectedFlag::False;
184 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetSelectedSegment) as usize] =
185 ResponseExpectedFlag::AlwaysTrue;
186 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::StartCounter) as usize] = ResponseExpectedFlag::False;
187 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetCounterValue) as usize] =
188 ResponseExpectedFlag::AlwaysTrue;
189 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetSpitfpErrorCount) as usize] =
190 ResponseExpectedFlag::AlwaysTrue;
191 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::SetBootloaderMode) as usize] =
192 ResponseExpectedFlag::AlwaysTrue;
193 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetBootloaderMode) as usize] =
194 ResponseExpectedFlag::AlwaysTrue;
195 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::SetWriteFirmwarePointer) as usize] =
196 ResponseExpectedFlag::False;
197 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::WriteFirmware) as usize] =
198 ResponseExpectedFlag::AlwaysTrue;
199 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::SetStatusLedConfig) as usize] =
200 ResponseExpectedFlag::False;
201 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetStatusLedConfig) as usize] =
202 ResponseExpectedFlag::AlwaysTrue;
203 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetChipTemperature) as usize] =
204 ResponseExpectedFlag::AlwaysTrue;
205 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
206 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
207 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
208 result.device.response_expected[u8::from(SegmentDisplay4x7V2BrickletFunction::GetIdentity) as usize] =
209 ResponseExpectedFlag::AlwaysTrue;
210 result
211 }
212
213 pub fn get_response_expected(&mut self, fun: SegmentDisplay4x7V2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
228 self.device.get_response_expected(u8::from(fun))
229 }
230
231 pub fn set_response_expected(
240 &mut self,
241 fun: SegmentDisplay4x7V2BrickletFunction,
242 response_expected: bool,
243 ) -> Result<(), SetResponseExpectedError> {
244 self.device.set_response_expected(u8::from(fun), response_expected)
245 }
246
247 pub fn set_response_expected_all(&mut self, response_expected: bool) {
249 self.device.set_response_expected_all(response_expected)
250 }
251
252 pub fn get_api_version(&self) -> [u8; 3] {
255 self.device.api_version
256 }
257
258 pub async fn get_counter_finished_callback_receiver(&mut self) -> impl Stream<Item = ()> {
263 self.device.get_callback_receiver(u8::from(SegmentDisplay4x7V2BrickletFunction::CallbackCounterFinished)).await.map(|_p| ())
264 }
265
266 pub async fn set_segments(
277 &mut self,
278 digit0: &[bool; 8],
279 digit1: &[bool; 8],
280 digit2: &[bool; 8],
281 digit3: &[bool; 8],
282 colon: &[bool; 2],
283 tick: bool,
284 ) -> Result<(), TinkerforgeError> {
285 let mut payload = [0; 6];
286 digit0.write_to_slice(&mut payload[0..1]);
287 digit1.write_to_slice(&mut payload[1..2]);
288 digit2.write_to_slice(&mut payload[2..3]);
289 digit3.write_to_slice(&mut payload[3..4]);
290 colon.write_to_slice(&mut payload[4..5]);
291 tick.write_to_slice(&mut payload[5..6]);
292
293 #[allow(unused_variables)]
294 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::SetSegments), &payload).await?;
295 Ok(())
296 }
297
298 pub async fn get_segments(&mut self) -> Result<Segments, TinkerforgeError> {
300 let payload = [0; 0];
301
302 #[allow(unused_variables)]
303 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetSegments), &payload).await?;
304 Ok(Segments::from_le_byte_slice(result.body()))
305 }
306
307 pub async fn set_brightness(&mut self, brightness: u8) -> Result<(), TinkerforgeError> {
309 let mut payload = [0; 1];
310 brightness.write_to_slice(&mut payload[0..1]);
311
312 #[allow(unused_variables)]
313 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::SetBrightness), &payload).await?;
314 Ok(())
315 }
316
317 pub async fn get_brightness(&mut self) -> Result<u8, TinkerforgeError> {
319 let payload = [0; 0];
320
321 #[allow(unused_variables)]
322 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetBrightness), &payload).await?;
323 Ok(u8::from_le_byte_slice(result.body()))
324 }
325
326 pub async fn set_numeric_value(&mut self, value: &[i8; 4]) -> Result<(), TinkerforgeError> {
340 let mut payload = [0; 4];
341 value.write_to_slice(&mut payload[0..4]);
342
343 #[allow(unused_variables)]
344 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::SetNumericValue), &payload).await?;
345 Ok(())
346 }
347
348 pub async fn set_selected_segment(&mut self, segment: u8, value: bool) -> Result<(), TinkerforgeError> {
357 let mut payload = [0; 2];
358 segment.write_to_slice(&mut payload[0..1]);
359 value.write_to_slice(&mut payload[1..2]);
360
361 #[allow(unused_variables)]
362 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::SetSelectedSegment), &payload).await?;
363 Ok(())
364 }
365
366 pub async fn get_selected_segment(&mut self, segment: u8) -> Result<bool, TinkerforgeError> {
368 let mut payload = [0; 1];
369 segment.write_to_slice(&mut payload[0..1]);
370
371 #[allow(unused_variables)]
372 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetSelectedSegment), &payload).await?;
373 Ok(bool::from_le_byte_slice(result.body()))
374 }
375
376 pub async fn start_counter(&mut self, value_from: i16, value_to: i16, increment: i16, length: u32) -> Result<(), TinkerforgeError> {
389 let mut payload = [0; 10];
390 value_from.write_to_slice(&mut payload[0..2]);
391 value_to.write_to_slice(&mut payload[2..4]);
392 increment.write_to_slice(&mut payload[4..6]);
393 length.write_to_slice(&mut payload[6..10]);
394
395 #[allow(unused_variables)]
396 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::StartCounter), &payload).await?;
397 Ok(())
398 }
399
400 pub async fn get_counter_value(&mut self) -> Result<u16, TinkerforgeError> {
404 let payload = [0; 0];
405
406 #[allow(unused_variables)]
407 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetCounterValue), &payload).await?;
408 Ok(u16::from_le_byte_slice(result.body()))
409 }
410
411 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
423 let payload = [0; 0];
424
425 #[allow(unused_variables)]
426 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
427 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
428 }
429
430 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
453 let mut payload = [0; 1];
454 mode.write_to_slice(&mut payload[0..1]);
455
456 #[allow(unused_variables)]
457 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::SetBootloaderMode), &payload).await?;
458 Ok(u8::from_le_byte_slice(result.body()))
459 }
460
461 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
470 let payload = [0; 0];
471
472 #[allow(unused_variables)]
473 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetBootloaderMode), &payload).await?;
474 Ok(u8::from_le_byte_slice(result.body()))
475 }
476
477 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
484 let mut payload = [0; 4];
485 pointer.write_to_slice(&mut payload[0..4]);
486
487 #[allow(unused_variables)]
488 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
489 Ok(())
490 }
491
492 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
501 let mut payload = [0; 64];
502 data.write_to_slice(&mut payload[0..64]);
503
504 #[allow(unused_variables)]
505 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::WriteFirmware), &payload).await?;
506 Ok(u8::from_le_byte_slice(result.body()))
507 }
508
509 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
523 let mut payload = [0; 1];
524 config.write_to_slice(&mut payload[0..1]);
525
526 #[allow(unused_variables)]
527 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::SetStatusLedConfig), &payload).await?;
528 Ok(())
529 }
530
531 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
539 let payload = [0; 0];
540
541 #[allow(unused_variables)]
542 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetStatusLedConfig), &payload).await?;
543 Ok(u8::from_le_byte_slice(result.body()))
544 }
545
546 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
553 let payload = [0; 0];
554
555 #[allow(unused_variables)]
556 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetChipTemperature), &payload).await?;
557 Ok(i16::from_le_byte_slice(result.body()))
558 }
559
560 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
567 let payload = [0; 0];
568
569 #[allow(unused_variables)]
570 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::Reset), &payload).await?;
571 Ok(())
572 }
573
574 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
580 let mut payload = [0; 4];
581 uid.write_to_slice(&mut payload[0..4]);
582
583 #[allow(unused_variables)]
584 let result = self.device.set(u8::from(SegmentDisplay4x7V2BrickletFunction::WriteUid), &payload).await?;
585 Ok(())
586 }
587
588 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
591 let payload = [0; 0];
592
593 #[allow(unused_variables)]
594 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::ReadUid), &payload).await?;
595 Ok(u32::from_le_byte_slice(result.body()))
596 }
597
598 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
609 let payload = [0; 0];
610
611 #[allow(unused_variables)]
612 let result = self.device.get(u8::from(SegmentDisplay4x7V2BrickletFunction::GetIdentity), &payload).await?;
613 Ok(Identity::from_le_byte_slice(result.body()))
614 }
615}