Skip to content

Commit

Permalink
Support Arduino/IRremote/IRrecvDumpV2 for capturing. Resolves #15.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtmartensson committed Sep 14, 2020
1 parent ef4cb7d commit 7b4ba23
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
156 changes: 156 additions & 0 deletions src/main/java/org/harctoolbox/harchardware/ir/IRrecvDumpV2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
Copyright (C) 2020 Bengt Martensson.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/.
*/

package org.harctoolbox.harchardware.ir;

import java.io.Closeable;
import java.io.IOException;
import java.util.logging.Logger;
import org.harctoolbox.harchardware.HarcHardwareException;
import org.harctoolbox.harchardware.IHarcHardware;
import org.harctoolbox.harchardware.comm.LocalSerialPort;
import org.harctoolbox.harchardware.comm.LocalSerialPortBuffered;
import org.harctoolbox.ircore.IrCoreUtils;
import org.harctoolbox.ircore.IrSequence;
import org.harctoolbox.ircore.OddSequenceLengthException;

public class IRrecvDumpV2 implements IHarcHardware, IReceive, Closeable {

private static final Logger logger = Logger.getLogger(IRrecvDumpV2.class.getName());

private static final int DEFAULT_BAUD = 115200;
private static final String DEFAULT_PORTNAME = "arduino";
private static final String RAWDATADECL = "unsigned int rawData";
private static final int DUMMY_ENDING_GAP = (int) IrCoreUtils.DEFAULT_MINIMUM_LEADOUT;

static IrSequence parse(String str) throws OddSequenceLengthException {
if (!str.startsWith(RAWDATADECL))
return null;

String[] arr = str.split("\\{");
if (arr.length != 2)
return null;
String[] arr1 = arr[1].split("\\}");
if (arr1.length != 2)
return null;

String[] data = arr1[0].split(",\\s*");
int[] micros = new int[data.length + 1];
for (int i = 0; i < data.length; i++)
micros[i] = Integer.parseInt(data[i]);

micros[data.length] = DUMMY_ENDING_GAP;
IrSequence irSequence = new IrSequence(micros);
return irSequence;
}

private final LocalSerialPortBuffered serialPort;
private boolean stopRequested;
private int beginTimeout = DEFAULT_BEGIN_TIMEOUT;

public IRrecvDumpV2(LocalSerialPortBuffered hardware) throws HarcHardwareException, IOException {
this.serialPort = hardware;
}

public IRrecvDumpV2(String portName, boolean verbose, Integer timeout, Integer baud) throws IOException, HarcHardwareException {
this(new LocalSerialPortBuffered(portName, verbose, timeout, baud));
}

public IRrecvDumpV2(String portName, boolean verbose, Integer timeout) throws IOException, HarcHardwareException {
this(new LocalSerialPortBuffered(LocalSerialPort.canonicalizePortName(portName, DEFAULT_PORTNAME), verbose, timeout, DEFAULT_BAUD));
}

@Override
public void close() throws IOException {
serialPort.close();
}

@Override
public String getVersion() throws IOException {
return null;
}

@Override
public void setVerbose(boolean verbose) {
serialPort.setVerbose(verbose);
}

@Override
public void setBeginTimeout(int beginTimeout) throws IOException {
serialPort.setTimeout(beginTimeout);
this.beginTimeout = beginTimeout;
}

@Override
public boolean isValid() {
return serialPort.isValid();
}

@Override
public void open() throws IOException, HarcHardwareException {
serialPort.open();
}

@Override
public IrSequence receive() throws HarcHardwareException, IOException, OddSequenceLengthException {
if (stopRequested)
return null;

if (!isValid())
throw new HarcHardwareException("Port not initialized");

serialPort.setTimeout(beginTimeout);
serialPort.flushInput();
IrSequence irSequence = null;
do {
String str = serialPort.readString(true);
if (str == null)
return null; // timeout

irSequence = parse(str);
} while (irSequence == null);
return irSequence;
}


@Override
public boolean stopReceive() {
stopRequested = true;
return true;
}

public void reset() throws IOException, HarcHardwareException {
serialPort.dropDTR(100);
}

@Override
public void setDebug(int debug) {
}

@Override
public void setTimeout(int timeout) throws IOException {
serialPort.setTimeout(timeout);
}

@Override
public void setCaptureMaxSize(int integer) {
}

@Override
public void setEndingTimeout(int endingTimeout) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.harctoolbox.harchardware.ir;

import org.harctoolbox.ircore.IrSequence;
import static org.testng.Assert.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
*
* @author bengt
*/
public class IRrecvDumpV2NGTest {

private static final String testStr
= "unsigned int rawData[67] = {8950,4450, 600,500, 600,500, 600,550, 600,500, 600,500, 600,550, 550,550, 600,1650, 550,1650, 600,1650, 600,1650, 600,1600, 600,1650, 600,1650, 550,1650, 600,1650, 600,1650, 550,1650, 600,1650, 600,1650, 550,1650, 600,550, 550,550, 600,500, 600,500, 600,550, 600,500, 600,500, 600,550, 600,1600, 600,1650, 600,1650, 550}; // NEC 1FFF807";
private static final String expected = "[8950,4450,600,500,600,500,600,550,600,500,600,500,600,550,550,550,600,1650,550,1650,600,1650,600,1650,600,1600,600,1650,600,1650,550,1650,600,1650,600,1650,550,1650,600,1650,600,1650,550,1650,600,550,550,550,600,500,600,500,600,550,600,500,600,500,600,550,600,1600,600,1650,600,1650,550,20000]";

public IRrecvDumpV2NGTest() {
}

@BeforeClass
public static void setUpClass() throws Exception {
}

@AfterClass
public static void tearDownClass() throws Exception {
}

@BeforeMethod
public void setUpMethod() throws Exception {
}

@AfterMethod
public void tearDownMethod() throws Exception {
}

/**
* Test of parse method, of class IRrecvDumpV2.
*/
@Test
public void testParse() throws Exception {
System.out.println("parse");
IrSequence result = IRrecvDumpV2.parse(expected);
assertNull(result);

result = IRrecvDumpV2.parse(testStr);
assertEquals(result.toString(), expected);
// TODO review the generated test code and remove the default call to fail.

}

}

0 comments on commit 7b4ba23

Please sign in to comment.