(freq: u32)
| 1 | use anyhow::Result; |
| 2 | |
| 3 | pub fn encode_freq(freq: u32) -> Result<[u8; 3]> { |
| 4 | let mut freq = freq; |
| 5 | // Support LoRaWAN 2.4GHz, in which case the stepping is 200Hz: |
| 6 | // See Frequency Encoding in MAC Commands |
| 7 | // https://lora-developers.semtech.com/documentation/tech-papers-and-guides/physical-layer-proposal-2.4ghz/ |
| 8 | if freq >= 2400000000 { |
| 9 | freq /= 2; |
| 10 | } |
| 11 | |
| 12 | if freq / 100 >= (1 << 24) { |
| 13 | return Err(anyhow!("max freq value is 2^24 - 1")); |
| 14 | } |
| 15 | if freq % 100 != 0 { |
| 16 | return Err(anyhow!("freq must be multiple of 100")); |
| 17 | } |
| 18 | |
| 19 | let mut b = [0; 3]; |
| 20 | b[0..3].copy_from_slice(&(freq / 100).to_le_bytes()[0..3]); |
| 21 | Ok(b) |
| 22 | } |
| 23 | |
| 24 | pub fn decode_freq(b: &[u8]) -> Result<u32> { |
| 25 | if b.len() != 3 { |
no test coverage detected