use crate::{
byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
ip_connection::GetRequestSender,
};
pub enum RealTimeClockV2BrickletFunction {
SetDateTime,
GetDateTime,
GetTimestamp,
SetOffset,
GetOffset,
SetDateTimeCallbackConfiguration,
GetDateTimeCallbackConfiguration,
SetAlarm,
GetAlarm,
GetSpitfpErrorCount,
SetBootloaderMode,
GetBootloaderMode,
SetWriteFirmwarePointer,
WriteFirmware,
SetStatusLedConfig,
GetStatusLedConfig,
GetChipTemperature,
Reset,
WriteUid,
ReadUid,
GetIdentity,
CallbackDateTime,
CallbackAlarm,
}
impl From<RealTimeClockV2BrickletFunction> for u8 {
fn from(fun: RealTimeClockV2BrickletFunction) -> Self {
match fun {
RealTimeClockV2BrickletFunction::SetDateTime => 1,
RealTimeClockV2BrickletFunction::GetDateTime => 2,
RealTimeClockV2BrickletFunction::GetTimestamp => 3,
RealTimeClockV2BrickletFunction::SetOffset => 4,
RealTimeClockV2BrickletFunction::GetOffset => 5,
RealTimeClockV2BrickletFunction::SetDateTimeCallbackConfiguration => 6,
RealTimeClockV2BrickletFunction::GetDateTimeCallbackConfiguration => 7,
RealTimeClockV2BrickletFunction::SetAlarm => 8,
RealTimeClockV2BrickletFunction::GetAlarm => 9,
RealTimeClockV2BrickletFunction::GetSpitfpErrorCount => 234,
RealTimeClockV2BrickletFunction::SetBootloaderMode => 235,
RealTimeClockV2BrickletFunction::GetBootloaderMode => 236,
RealTimeClockV2BrickletFunction::SetWriteFirmwarePointer => 237,
RealTimeClockV2BrickletFunction::WriteFirmware => 238,
RealTimeClockV2BrickletFunction::SetStatusLedConfig => 239,
RealTimeClockV2BrickletFunction::GetStatusLedConfig => 240,
RealTimeClockV2BrickletFunction::GetChipTemperature => 242,
RealTimeClockV2BrickletFunction::Reset => 243,
RealTimeClockV2BrickletFunction::WriteUid => 248,
RealTimeClockV2BrickletFunction::ReadUid => 249,
RealTimeClockV2BrickletFunction::GetIdentity => 255,
RealTimeClockV2BrickletFunction::CallbackDateTime => 10,
RealTimeClockV2BrickletFunction::CallbackAlarm => 11,
}
}
}
pub const REAL_TIME_CLOCK_V2_BRICKLET_WEEKDAY_MONDAY: u8 = 1;
pub const REAL_TIME_CLOCK_V2_BRICKLET_WEEKDAY_TUESDAY: u8 = 2;
pub const REAL_TIME_CLOCK_V2_BRICKLET_WEEKDAY_WEDNESDAY: u8 = 3;
pub const REAL_TIME_CLOCK_V2_BRICKLET_WEEKDAY_THURSDAY: u8 = 4;
pub const REAL_TIME_CLOCK_V2_BRICKLET_WEEKDAY_FRIDAY: u8 = 5;
pub const REAL_TIME_CLOCK_V2_BRICKLET_WEEKDAY_SATURDAY: u8 = 6;
pub const REAL_TIME_CLOCK_V2_BRICKLET_WEEKDAY_SUNDAY: u8 = 7;
pub const REAL_TIME_CLOCK_V2_BRICKLET_ALARM_MATCH_DISABLED: i8 = -1;
pub const REAL_TIME_CLOCK_V2_BRICKLET_ALARM_INTERVAL_DISABLED: i32 = -1;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
pub const REAL_TIME_CLOCK_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
pub const REAL_TIME_CLOCK_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
pub const REAL_TIME_CLOCK_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
pub const REAL_TIME_CLOCK_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
pub const REAL_TIME_CLOCK_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct DateTime {
pub year: u16,
pub month: u8,
pub day: u8,
pub hour: u8,
pub minute: u8,
pub second: u8,
pub centisecond: u8,
pub weekday: u8,
pub timestamp: i64,
}
impl FromByteSlice for DateTime {
fn bytes_expected() -> usize { 17 }
fn from_le_byte_slice(bytes: &[u8]) -> DateTime {
DateTime {
year: <u16>::from_le_byte_slice(&bytes[0..2]),
month: <u8>::from_le_byte_slice(&bytes[2..3]),
day: <u8>::from_le_byte_slice(&bytes[3..4]),
hour: <u8>::from_le_byte_slice(&bytes[4..5]),
minute: <u8>::from_le_byte_slice(&bytes[5..6]),
second: <u8>::from_le_byte_slice(&bytes[6..7]),
centisecond: <u8>::from_le_byte_slice(&bytes[7..8]),
weekday: <u8>::from_le_byte_slice(&bytes[8..9]),
timestamp: <i64>::from_le_byte_slice(&bytes[9..17]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Alarm {
pub month: i8,
pub day: i8,
pub hour: i8,
pub minute: i8,
pub second: i8,
pub weekday: i8,
pub interval: i32,
}
impl FromByteSlice for Alarm {
fn bytes_expected() -> usize { 10 }
fn from_le_byte_slice(bytes: &[u8]) -> Alarm {
Alarm {
month: <i8>::from_le_byte_slice(&bytes[0..1]),
day: <i8>::from_le_byte_slice(&bytes[1..2]),
hour: <i8>::from_le_byte_slice(&bytes[2..3]),
minute: <i8>::from_le_byte_slice(&bytes[3..4]),
second: <i8>::from_le_byte_slice(&bytes[4..5]),
weekday: <i8>::from_le_byte_slice(&bytes[5..6]),
interval: <i32>::from_le_byte_slice(&bytes[6..10]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct DateTimeEvent {
pub year: u16,
pub month: u8,
pub day: u8,
pub hour: u8,
pub minute: u8,
pub second: u8,
pub centisecond: u8,
pub weekday: u8,
pub timestamp: i64,
}
impl FromByteSlice for DateTimeEvent {
fn bytes_expected() -> usize { 17 }
fn from_le_byte_slice(bytes: &[u8]) -> DateTimeEvent {
DateTimeEvent {
year: <u16>::from_le_byte_slice(&bytes[0..2]),
month: <u8>::from_le_byte_slice(&bytes[2..3]),
day: <u8>::from_le_byte_slice(&bytes[3..4]),
hour: <u8>::from_le_byte_slice(&bytes[4..5]),
minute: <u8>::from_le_byte_slice(&bytes[5..6]),
second: <u8>::from_le_byte_slice(&bytes[6..7]),
centisecond: <u8>::from_le_byte_slice(&bytes[7..8]),
weekday: <u8>::from_le_byte_slice(&bytes[8..9]),
timestamp: <i64>::from_le_byte_slice(&bytes[9..17]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AlarmEvent {
pub year: u16,
pub month: u8,
pub day: u8,
pub hour: u8,
pub minute: u8,
pub second: u8,
pub centisecond: u8,
pub weekday: u8,
pub timestamp: i64,
}
impl FromByteSlice for AlarmEvent {
fn bytes_expected() -> usize { 17 }
fn from_le_byte_slice(bytes: &[u8]) -> AlarmEvent {
AlarmEvent {
year: <u16>::from_le_byte_slice(&bytes[0..2]),
month: <u8>::from_le_byte_slice(&bytes[2..3]),
day: <u8>::from_le_byte_slice(&bytes[3..4]),
hour: <u8>::from_le_byte_slice(&bytes[4..5]),
minute: <u8>::from_le_byte_slice(&bytes[5..6]),
second: <u8>::from_le_byte_slice(&bytes[6..7]),
centisecond: <u8>::from_le_byte_slice(&bytes[7..8]),
weekday: <u8>::from_le_byte_slice(&bytes[8..9]),
timestamp: <i64>::from_le_byte_slice(&bytes[9..17]),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct SpitfpErrorCount {
pub error_count_ack_checksum: u32,
pub error_count_message_checksum: u32,
pub error_count_frame: u32,
pub error_count_overflow: u32,
}
impl FromByteSlice for SpitfpErrorCount {
fn bytes_expected() -> usize { 16 }
fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
SpitfpErrorCount {
error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Identity {
pub uid: String,
pub connected_uid: String,
pub position: char,
pub hardware_version: [u8; 3],
pub firmware_version: [u8; 3],
pub device_identifier: u16,
}
impl FromByteSlice for Identity {
fn bytes_expected() -> usize { 25 }
fn from_le_byte_slice(bytes: &[u8]) -> Identity {
Identity {
uid: <String>::from_le_byte_slice(&bytes[0..8]),
connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
position: <char>::from_le_byte_slice(&bytes[16..17]),
hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
}
}
}
#[derive(Clone)]
pub struct RealTimeClockV2Bricklet {
device: Device,
}
impl RealTimeClockV2Bricklet {
pub const DEVICE_IDENTIFIER: u16 = 2106;
pub const DEVICE_DISPLAY_NAME: &'static str = "Real-Time Clock Bricklet 2.0";
pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> RealTimeClockV2Bricklet {
let mut result = RealTimeClockV2Bricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::SetDateTime) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetDateTime) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetTimestamp) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::SetOffset) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetOffset) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::SetDateTimeCallbackConfiguration) as usize] =
ResponseExpectedFlag::True;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetDateTimeCallbackConfiguration) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::SetAlarm) as usize] = ResponseExpectedFlag::True;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetAlarm) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetSpitfpErrorCount) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::SetBootloaderMode) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetBootloaderMode) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
ResponseExpectedFlag::False;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::WriteFirmware) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::SetStatusLedConfig) as usize] =
ResponseExpectedFlag::False;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetStatusLedConfig) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetChipTemperature) as usize] =
ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
result.device.response_expected[u8::from(RealTimeClockV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
result
}
pub fn get_response_expected(&mut self, fun: RealTimeClockV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
self.device.get_response_expected(u8::from(fun))
}
pub fn set_response_expected(
&mut self,
fun: RealTimeClockV2BrickletFunction,
response_expected: bool,
) -> Result<(), SetResponseExpectedError> {
self.device.set_response_expected(u8::from(fun), response_expected)
}
pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
pub fn get_date_time_callback_receiver(&self) -> ConvertingCallbackReceiver<DateTimeEvent> {
self.device.get_callback_receiver(u8::from(RealTimeClockV2BrickletFunction::CallbackDateTime))
}
pub fn get_alarm_callback_receiver(&self) -> ConvertingCallbackReceiver<AlarmEvent> {
self.device.get_callback_receiver(u8::from(RealTimeClockV2BrickletFunction::CallbackAlarm))
}
pub fn set_date_time(
&self,
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
centisecond: u8,
weekday: u8,
) -> ConvertingReceiver<()> {
let mut payload = vec![0; 9];
payload[0..2].copy_from_slice(&<u16>::to_le_byte_vec(year));
payload[2..3].copy_from_slice(&<u8>::to_le_byte_vec(month));
payload[3..4].copy_from_slice(&<u8>::to_le_byte_vec(day));
payload[4..5].copy_from_slice(&<u8>::to_le_byte_vec(hour));
payload[5..6].copy_from_slice(&<u8>::to_le_byte_vec(minute));
payload[6..7].copy_from_slice(&<u8>::to_le_byte_vec(second));
payload[7..8].copy_from_slice(&<u8>::to_le_byte_vec(centisecond));
payload[8..9].copy_from_slice(&<u8>::to_le_byte_vec(weekday));
self.device.set(u8::from(RealTimeClockV2BrickletFunction::SetDateTime), payload)
}
pub fn get_date_time(&self) -> ConvertingReceiver<DateTime> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetDateTime), payload)
}
pub fn get_timestamp(&self) -> ConvertingReceiver<i64> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetTimestamp), payload)
}
pub fn set_offset(&self, offset: i8) -> ConvertingReceiver<()> {
let mut payload = vec![0; 1];
payload[0..1].copy_from_slice(&<i8>::to_le_byte_vec(offset));
self.device.set(u8::from(RealTimeClockV2BrickletFunction::SetOffset), payload)
}
pub fn get_offset(&self) -> ConvertingReceiver<i8> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetOffset), payload)
}
pub fn set_date_time_callback_configuration(&self, period: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
self.device.set(u8::from(RealTimeClockV2BrickletFunction::SetDateTimeCallbackConfiguration), payload)
}
pub fn get_date_time_callback_configuration(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetDateTimeCallbackConfiguration), payload)
}
pub fn set_alarm(&self, month: i8, day: i8, hour: i8, minute: i8, second: i8, weekday: i8, interval: i32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 10];
payload[0..1].copy_from_slice(&<i8>::to_le_byte_vec(month));
payload[1..2].copy_from_slice(&<i8>::to_le_byte_vec(day));
payload[2..3].copy_from_slice(&<i8>::to_le_byte_vec(hour));
payload[3..4].copy_from_slice(&<i8>::to_le_byte_vec(minute));
payload[4..5].copy_from_slice(&<i8>::to_le_byte_vec(second));
payload[5..6].copy_from_slice(&<i8>::to_le_byte_vec(weekday));
payload[6..10].copy_from_slice(&<i32>::to_le_byte_vec(interval));
self.device.set(u8::from(RealTimeClockV2BrickletFunction::SetAlarm), payload)
}
pub fn get_alarm(&self) -> ConvertingReceiver<Alarm> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetAlarm), payload)
}
pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetSpitfpErrorCount), payload)
}
pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
let mut payload = vec![0; 1];
payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
self.device.get(u8::from(RealTimeClockV2BrickletFunction::SetBootloaderMode), payload)
}
pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetBootloaderMode), payload)
}
pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
self.device.set(u8::from(RealTimeClockV2BrickletFunction::SetWriteFirmwarePointer), payload)
}
pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
let mut payload = vec![0; 64];
payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
self.device.get(u8::from(RealTimeClockV2BrickletFunction::WriteFirmware), payload)
}
pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
let mut payload = vec![0; 1];
payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
self.device.set(u8::from(RealTimeClockV2BrickletFunction::SetStatusLedConfig), payload)
}
pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetStatusLedConfig), payload)
}
pub fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetChipTemperature), payload)
}
pub fn reset(&self) -> ConvertingReceiver<()> {
let payload = vec![0; 0];
self.device.set(u8::from(RealTimeClockV2BrickletFunction::Reset), payload)
}
pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
let mut payload = vec![0; 4];
payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
self.device.set(u8::from(RealTimeClockV2BrickletFunction::WriteUid), payload)
}
pub fn read_uid(&self) -> ConvertingReceiver<u32> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::ReadUid), payload)
}
pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
let payload = vec![0; 0];
self.device.get(u8::from(RealTimeClockV2BrickletFunction::GetIdentity), payload)
}
}