From 428637397ecdab012e4c7715f9662bc43d443be8 Mon Sep 17 00:00:00 2001 From: Sean Young Date: Sat, 30 Mar 2024 14:41:19 +0000 Subject: [PATCH] Add trailing space to raw decoding Signed-off-by: Sean Young --- irp/src/build_nfa.rs | 2 +- src/lircd_conf/decode.rs | 3 ++- src/lircd_conf/encode.rs | 53 +++++++++++++++++++++++++++++++++++++++ tests/lircd_conf_tests.rs | 6 ++--- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/irp/src/build_nfa.rs b/irp/src/build_nfa.rs index dfa678bd..2719912c 100644 --- a/irp/src/build_nfa.rs +++ b/irp/src/build_nfa.rs @@ -88,7 +88,7 @@ impl NFA { /// Add nfa states for parsing raw IR pub fn add_raw(&mut self, raw: &[u32], event: Event, code: i64) { assert_ne!(raw.len(), 0); - assert_eq!(raw.len() % 2, 1); + assert_eq!(raw.len() % 2, 0); if self.verts.is_empty() { self.verts.push(Vertex::default()); diff --git a/src/lircd_conf/decode.rs b/src/lircd_conf/decode.rs index 75d0f01f..f2552604 100644 --- a/src/lircd_conf/decode.rs +++ b/src/lircd_conf/decode.rs @@ -28,7 +28,8 @@ impl Remote { let mut nfa = NFA::default(); for (i, raw) in self.raw_codes.iter().enumerate() { - nfa.add_raw(&raw.rawir, irp::Event::Down, u32::MAX as i64 + i as i64); + let message = self.encode_once(raw); + nfa.add_raw(&message.raw, irp::Event::Down, u32::MAX as i64 + i as i64); } nfa diff --git a/src/lircd_conf/encode.rs b/src/lircd_conf/encode.rs index db12236e..129881cd 100644 --- a/src/lircd_conf/encode.rs +++ b/src/lircd_conf/encode.rs @@ -151,6 +151,59 @@ impl Remote { } } + pub fn encode_once(&self, raw_code: &RawCode) -> Message { + // remove trailing space + let length = if raw_code.rawir.len().is_even() { + raw_code.rawir.len() - 1 + } else { + raw_code.rawir.len() + }; + + let mut raw = raw_code.rawir[..length].to_vec(); + + let gap = if self.gap2 != 0 && self.gap2 < self.gap { + self.gap2 + } else { + self.gap + }; + + let mut gap = if self.flags.contains(Flags::CONST_LENGTH) { + let total_length: u32 = raw.iter().sum(); + + if (total_length as u64) < gap { + gap as u32 - total_length + } else { + gap as u32 + } + } else { + gap as u32 + }; + + if gap == 0 { + gap = 20000; + } + + raw.push(gap); + + let carrier = if self.frequency != 0 { + Some(self.frequency as i64) + } else { + None + }; + + let duty_cycle = if self.duty_cycle != 0 { + Some(self.duty_cycle as u8) + } else { + None + }; + + Message { + carrier, + duty_cycle, + raw, + } + } + /// Encode raw code for this remote, with the given repeats pub fn encode_raw(&self, raw_code: &RawCode, repeats: u64) -> Result { debug!("encoding name={}", raw_code.name); diff --git a/tests/lircd_conf_tests.rs b/tests/lircd_conf_tests.rs index d3aca93f..9029b623 100644 --- a/tests/lircd_conf_tests.rs +++ b/tests/lircd_conf_tests.rs @@ -76,11 +76,11 @@ fn lircd_encode_decode(path: &Path) { } }; - let mut message = our_remote.encode_raw(our_code, 0).unwrap(); + let message = our_remote.encode_raw(our_code, 0).unwrap(); - message.raw.pop(); + let raw = &message.raw[0..message.raw.len() - 1]; - if lircd != message.raw { + if lircd != raw { let testdata = Message::from_raw_slice(&lircd); println!("lircd {}", testdata.print_rawir());