Skip to content

Commit

Permalink
Move to j4rs
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <[email protected]>
  • Loading branch information
seanyoung committed May 4, 2024
1 parent a94d63d commit 5b74967
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion irp/tests/rust-irptransmogrifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ publish = false
[dependencies]
irp = { path = "../.." }
rand = "0.8"
j4rs = "0.17"
j4rs = "0.18"
37 changes: 23 additions & 14 deletions irp/tests/rust-irptransmogrifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl<'a> IrpTransmogrifierRender<'a> {
pub fn new(jvm: &'a Jvm, irp: &str) -> Result<Self, J4RsError> {
let irp = jvm.create_instance("java.lang.String", &[InvocationArg::try_from(irp)?])?;

let protocol = jvm.create_instance("org.harctoolbox.irp.Protocol", &[irp.into()])?;
let protocol =
jvm.create_instance("org.harctoolbox.irp.Protocol", &[InvocationArg::from(irp)])?;

Ok(IrpTransmogrifierRender { protocol, jvm })
}
Expand All @@ -34,33 +35,41 @@ impl<'a> IrpTransmogrifierRender<'a> {
.java_map(JavaClass::String, JavaClass::Long, param)?;

self.jvm
.invoke(&self.protocol, "toIrSignal", &[jparam.into()])
.invoke(&self.protocol, "toIrSignal", &[InvocationArg::from(jparam)])
}

pub fn render_raw(&self, param: HashMap<String, i64>) -> Result<[Vec<u32>; 3], J4RsError> {
let res = self.render(param)?;

let intro: Vec<u32> = self
.jvm
.to_rust(self.jvm.invoke(&res, "getIntroInts", &[])?)?;
let intro: Vec<u32> = self.jvm.to_rust(self.jvm.invoke(
&res,
"getIntroInts",
InvocationArg::empty(),
)?)?;

let repeat: Vec<u32> = self
.jvm
.to_rust(self.jvm.invoke(&res, "getRepeatInts", &[])?)?;
let repeat: Vec<u32> = self.jvm.to_rust(self.jvm.invoke(
&res,
"getRepeatInts",
InvocationArg::empty(),
)?)?;

let ending: Vec<u32> = self
.jvm
.to_rust(self.jvm.invoke(&res, "getEndingInts", &[])?)?;
let ending: Vec<u32> = self.jvm.to_rust(self.jvm.invoke(
&res,
"getEndingInts",
InvocationArg::empty(),
)?)?;

Ok([intro, repeat, ending])
}

pub fn render_pronto(&self, param: HashMap<String, i64>) -> Result<String, J4RsError> {
let res = self.render(param)?;

let res =
self.jvm
.invoke_static("org.harctoolbox.ircore.Pronto", "toString", &[res.into()])?;
let res = self.jvm.invoke_static(
"org.harctoolbox.ircore.Pronto",
"toString",
&[InvocationArg::from(res)],
)?;

self.jvm.to_rust(res)
}
Expand Down
17 changes: 12 additions & 5 deletions irp/tests/transmogrifier_compare/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use irp::{InfraredData, Irp, Message, NFADecoder, Vartable};
use irp::{Decoder, InfraredData, Irp, Message, Options, Vartable};
use irptransmogrifier::{create_jvm, IrpTransmogrifierRender};
use itertools::Itertools;
use rand::Rng;
Expand Down Expand Up @@ -29,6 +29,13 @@ fn main() {
vars.set(param.name.to_owned(), value);
}

let options = Options {
aeps: 100,
eps: 3,
max_gap: 100000,
..Default::default()
};

// encode with irp crate
match irp.encode(vars.clone()) {
Ok(our) => {
Expand All @@ -42,9 +49,9 @@ fn main() {
assert!(compare_with_rounding(&our[i], &their[i]));
}

match irp.build_nfa() {
Ok(nfa) => {
let mut decoder = NFADecoder::new(100, 3, 100000);
match irp.compile(&options) {
Ok(dfa) => {
let mut decoder = Decoder::new(options);
let mut decoded = false;

for part in our {
Expand All @@ -53,7 +60,7 @@ fn main() {
let mut failed = false;

for i in ir {
decoder.input(i, &nfa, |ev, fields| {
decoder.dfa_input(i, &dfa, |ev, fields| {
println!(
"decode {i} {ev}: {}",
fields
Expand Down
2 changes: 1 addition & 1 deletion tests/lirc_compare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
cir = { path = "../.." }
irp = { path = "../../irp" }
liblircd = { path = "../../liblircd" }
liblircd = { path = "../../tests/liblircd" }
num-integer = "0.1"

[workspace]

0 comments on commit 5b74967

Please sign in to comment.