Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bradanlane opened this issue May 2, 2024 · 2 comments
Open

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

bradanlane opened this issue May 2, 2024 · 2 comments

Comments

@bradanlane
Copy link

bradanlane commented May 2, 2024

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)
@bradanlane bradanlane changed the title IR receive is 1s compliment of IR send data IR receive data missing last two values of send data May 2, 2024
@tkomde
Copy link
Contributor

tkomde commented May 3, 2024

@bradanlane
I sent a PR two weeks ago ( #68 ) that addresses this issue. The samples in this repository should be fine, but I think the CPX learning guide is still intact. Sorry, but I don't know how to request a fix.

@bradanlane
Copy link
Author

My apologies for not checking the PRs. Thank you for adding it to this issue.

As to the CPX documentation, I have already submitted an issue to that repository with the requisite correction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants