Ejemplo n.º 1
0
def parse_and_report(rawdata_str,
                     hertz=38000,
                     end_usecs=100000,
                     use_initial=False,
                     generate_code=False,
                     verbose=False,
                     output=sys.stdout):
    """Analyse the rawdata c++ definition of a IR message."""

    # Parse the input.
    rawdata = convert_rawdata(rawdata_str)
    if verbose:
        output.write("Found %d timing entries.\n" % len(rawdata))

    # Do we need to pad out the rawdata to make it even in length?
    if end_usecs > 0 and len(rawdata) % 2 == 1:
        rawdata.append(end_usecs)

    result = ["0000"]
    # Work out the frequency code.
    pronto_freq = int(1000000.0 / (hertz * 0.241246))
    if verbose:
        output.write("Pronto frequency is %X (%d Hz).\n" %
                     (pronto_freq, hertz))
    result.append("%04X" % pronto_freq)
    period = 1000000.0 / max(1, hertz)
    if verbose:
        output.write("Pronto period is %f uSecs.\n" % period)
    # Add the lengths to the code.
    if use_initial:
        result.append("%04x" %
                      int(len(rawdata) / 2))  # Initial burst code length
        result.append("%04x" % 0)  # No Repeat code length
    else:
        result.append("%04x" % 0)  # No Initial burst code length
        result.append("%04x" % int(len(rawdata) / 2))  # Repeat code length

    # Add the data.
    if verbose:
        output.write("Raw data: %s " % rawdata)
    for i in rawdata:
        result.append("%04x" % int(i / period))
    if generate_code:
        output.write("uint16_t pronto[%d] = {0x%s};\n" %
                     (len(result), ", 0x".join(result)))
    else:
        output.write("Pronto code = '%s'\n" % " ".join(result))
Ejemplo n.º 2
0
def parse_and_report(rawdata_str,
                     hertz=38000,
                     end_usecs=100000,
                     use_initial=False,
                     generate_code=False,
                     verbose=False,
                     output=sys.stdout):
    """Analyse the rawdata c++ definition of a IR message."""

    # Parse the input.
    rawdata = convert_rawdata(rawdata_str)
    if verbose:
        output.write(f"Found {len(rawdata)} timing entries.\n")

    # Do we need to pad out the rawdata to make it even in length?
    if end_usecs > 0 and len(rawdata) % 2 == 1:
        rawdata.append(end_usecs)

    result = ["0000"]
    # Work out the frequency code.
    pronto_freq = int(1000000.0 / (hertz * 0.241246))
    if verbose:
        output.write(f"Pronto frequency is {pronto_freq:X} ({hertz} Hz).\n")
    result.append(f"{pronto_freq:04X}")
    period = 1000000.0 / max(1, hertz)
    if verbose:
        output.write(f"Pronto period is {period} uSecs.\n")
    # Add the lengths to the code.
    if use_initial:
        result.append(
            f"{int(len(rawdata) / 2):04x}")  # Initial burst code length
        result.append("0000")  # No Repeat code length
    else:
        result.append("0000")  # No Initial burst code length
        result.append(f"{int(len(rawdata) / 2):04x}")  # Repeat code length

    # Add the data.
    if verbose:
        output.write(f"Raw data: {rawdata} ")
    for i in rawdata:
        result.append(f"{int(i / period):04x}")
    if generate_code:
        output.write(f"uint16_t pronto[{len(result)}] = "
                     f"{{0x{', 0x'.join(result)}}};\n")
    else:
        output.write(f"Pronto code = '{' '.join(result)}'\n")
Ejemplo n.º 3
0
  def test_convert_rawdata(self):
    """Tests for the convert_rawdata() function."""
    # trivial cases
    self.assertEqual(analyse.convert_rawdata("0"), [0])
    with self.assertRaises(ValueError) as context:
      analyse.convert_rawdata("")
    self.assertEqual(context.exception.message,
                     "Raw Data contains a non-numeric value of ''.")

    # Single parenthesis
    self.assertEqual(analyse.convert_rawdata("foo {10"), [10])
    self.assertEqual(analyse.convert_rawdata("20} bar"), [20])

    # No parentheses
    self.assertEqual(analyse.convert_rawdata("10,20 ,  30"), [10, 20, 30])

    # Dual parentheses
    self.assertEqual(analyse.convert_rawdata("{10,20 ,  30}"), [10, 20, 30])
    self.assertEqual(analyse.convert_rawdata("foo{10,20}bar"), [10, 20])

    # Many parentheses
    self.assertEqual(analyse.convert_rawdata("foo{10,20}{bar}"), [10, 20])
    self.assertEqual(analyse.convert_rawdata("foo{10,20}{bar}}{"), [10, 20])

    # Bad parentheses
    with self.assertRaises(ValueError) as context:
      analyse.convert_rawdata("}10{")
    self.assertEqual(context.exception.message,
                     "Raw Data not parsible due to parentheses placement.")

    # Non base-10 values
    with self.assertRaises(ValueError) as context:
      analyse.convert_rawdata("10, 20, foo, bar, 30")
    self.assertEqual(context.exception.message,
                     "Raw Data contains a non-numeric value of 'foo'.")

    # A messy usual "good" case.
    input_str = """uint16_t rawbuf[6] = {
    9008, 4496, 644,
    1660, 676,

    530}
    ;"""
    self.assertEqual(
        analyse.convert_rawdata(input_str), [9008, 4496, 644, 1660, 676, 530])
  def test_convert_rawdata(self):
    """Tests for the convert_rawdata() function."""
    # trivial cases
    self.assertEqual(analyse.convert_rawdata("0"), [0])
    with self.assertRaises(ValueError) as context:
      analyse.convert_rawdata("")
    self.assertEqual(context.exception.message,
                     "Raw Data contains a non-numeric value of ''.")

    # Single parenthesis
    self.assertEqual(analyse.convert_rawdata("foo {10"), [10])
    self.assertEqual(analyse.convert_rawdata("20} bar"), [20])

    # No parentheses
    self.assertEqual(analyse.convert_rawdata("10,20 ,  30"), [10, 20, 30])

    # Dual parentheses
    self.assertEqual(analyse.convert_rawdata("{10,20 ,  30}"), [10, 20, 30])
    self.assertEqual(analyse.convert_rawdata("foo{10,20}bar"), [10, 20])

    # Many parentheses
    self.assertEqual(analyse.convert_rawdata("foo{10,20}{bar}"), [10, 20])
    self.assertEqual(analyse.convert_rawdata("foo{10,20}{bar}}{"), [10, 20])

    # Bad parentheses
    with self.assertRaises(ValueError) as context:
      analyse.convert_rawdata("}10{")
    self.assertEqual(context.exception.message,
                     "Raw Data not parsible due to parentheses placement.")

    # Non base-10 values
    with self.assertRaises(ValueError) as context:
      analyse.convert_rawdata("10, 20, foo, bar, 30")
    self.assertEqual(context.exception.message,
                     "Raw Data contains a non-numeric value of 'foo'.")

    # A messy usual "good" case.
    input_str = """uint16_t rawbuf[6] = {
    9008, 4496, 644,
    1660, 676,

    530}
    ;"""
    self.assertEqual(
        analyse.convert_rawdata(input_str), [9008, 4496, 644, 1660, 676, 530])