Skip to content

Commit

Permalink
Add trailing space to raw decoding
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <[email protected]>
  • Loading branch information
seanyoung committed Mar 30, 2024
1 parent 5240a08 commit 4286373
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion irp/src/build_nfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion src/lircd_conf/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions src/lircd_conf/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message, String> {
debug!("encoding name={}", raw_code.name);
Expand Down
6 changes: 3 additions & 3 deletions tests/lircd_conf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 4286373

Please sign in to comment.