Skip to content

IR receive data missing last two values of send data #71

Closed
@bradanlane

Description

@bradanlane

I have written a simple IR test based on the CPE tutorial. It sends a list of 4 values and processes received data.

I am using the RPi PICOS with the Adafruit LED Emitter and 38kHz Receiver.

I am observing two problems.

  1. the receive data is missing two values at the end. They represent the first value of a bit followed by the trail value. In my example, this is 600 and 0.
  2. the received data is the 1s compliment of the send data. i.e. sending 255 receives 0; sending 251, receives 4.

Note: the 1s complement issue is an error in the CPX learning guide. A separate issue has been opened for the guide. The code below has been updated to the correct format.

Workaoround for missing values: Set the trail value to something (eg 100 or 255). Now the receiving end gets a full message plus a bad pulse. Test the pulse count and skip processing if it is too small. Sample code below has been updated.

Here is the code:

import board
import time
import random
import pulseio
import adafruit_irremote

IR_HEADER = [9000, 4000]
IR_START = 600
IR_SHORT = 600
IR_LONG = 1600
IR_ZERO = [IR_START, IR_SHORT]
IR_ONE  = [IR_START, IR_LONG]

ir_rx = pulseio.PulseIn(board.GP1, maxlen=200, idle_state=True)
ir_tx = pulseio.PulseOut(board.GP0, frequency=38000, duty_cycle=2**15)

decoder = adafruit_irremote.GenericDecode()
encoder = adafruit_irremote.GenericTransmit(header=IR_HEADER, one=IR_ONE, zero=IR_ZERO, trail=255, debug=True)

def ir_received(ir):
    pulses = decoder.read_pulses(ir)
    count = len(pulses)
    if count < 4:
        return
    # KLUDGE to append missing data
    #pulses.append(600)
    #pulses.append(0)
    print("Received: ", pulses)
    try:
        code = decoder.decode_bits(pulses)
        print("Received:", code)
    except Exception as e:
        print("Failed to decode:", e)

def ir_transmit(ir, value):
    if type(value) is not list:
        value = [value]
    print("sending: ", value)
    encoder.transmit(ir, value)

timer = time.time() + random.randint(5, 15)
message = [255, 253, 251, 249]

while True:
    if timer < time.time():
        timer = time.time() + random.randint(5, 15)
        ir_rx.pause()
        ir_transmit(ir_tx, message)
        ir_rx.resume()
    if len(ir_rx) > 0:
        ir_received(ir_rx)
    time.sleep(0.05)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions