A RPi Pico powered Lightning Detector
1use core::num::TryFromIntError;
2
3use alloc::collections::TryReserveError;
4use embassy_rp::rtc::RtcError;
5use sachy_sntp::SntpError;
6
7#[derive(Debug)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9pub enum PicoError {
10 InvalidSntpTime(SntpError),
11 InvalidRtcTime,
12 AllocationError,
13 NoisePayloadTooBig,
14 TcpError(embassy_net::tcp::Error),
15 NoiseProtoError,
16}
17
18impl core::fmt::Display for PicoError {
19 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20 match self {
21 Self::InvalidSntpTime(reason) => write!(f, "Invalid SNTP time: {reason:?}"),
22 Self::InvalidRtcTime => write!(f, "Invalid RTC time: Year is out of bounds"),
23 Self::AllocationError => write!(f, "Failed to allocate memory"),
24 Self::NoisePayloadTooBig => write!(f, "Invalid payload size"),
25 Self::TcpError(error) => write!(f, "Network error: {error:?}"),
26 Self::NoiseProtoError => write!(f, "Noise protocol failure"),
27 }
28 }
29}
30
31impl core::error::Error for PicoError {}
32
33impl From<SntpError> for PicoError {
34 fn from(value: SntpError) -> Self {
35 Self::InvalidSntpTime(value)
36 }
37}
38
39impl From<RtcError> for PicoError {
40 fn from(_value: RtcError) -> Self {
41 Self::InvalidRtcTime
42 }
43}
44
45impl From<TryReserveError> for PicoError {
46 fn from(_value: TryReserveError) -> Self {
47 Self::AllocationError
48 }
49}
50
51impl From<TryFromIntError> for PicoError {
52 fn from(_value: TryFromIntError) -> Self {
53 Self::NoisePayloadTooBig
54 }
55}
56
57impl From<embassy_net::tcp::Error> for PicoError {
58 fn from(value: embassy_net::tcp::Error) -> Self {
59 Self::TcpError(value)
60 }
61}
62
63impl From<snow::Error> for PicoError {
64 fn from(_value: snow::Error) -> Self {
65 Self::NoiseProtoError
66 }
67}
68