[{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eJust got a Fruit Jam and have been learning a lot - so many cool components wrapped into a neat little package.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eOne of the goodies included is an IR receiver that is available as \u003cstrong\u003eboard.IR\u003c/strong\u003e, so I thought it might be neat to send commands to the Fruit Jam with a remote control. This is a quick look receiving input from a remote control. But this article is also a lesson on the power of the Adafruit CircuitPython community.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eFirst, the parts for the project:\u003c/p\u003e\n      \n\n\n\n\n\n\n","metadata":{}},{"element_type":"product","content":"https://www.adafruit.com/product/6200","metadata":{}},{"element_type":"product","content":"https://www.adafruit.com/product/389","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n        \u003cp\u003eI used a couple of odd remotes I had laying around and in fact I have two remotes coded in.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eThe Fruit Jam Learn Guide doesn't address coding the IR receiver, but there are other guides for getting IR signals. I started with:\u003c/p\u003e\n      \n\n\n\n","metadata":{}},{"element_type":"guide","content":"https://learn.adafruit.com/adafruit-infrared-ir-remote-receiver/circuitpython-and-python","metadata":{"guide_id":"3982","page_id":"28425"}},{"element_type":"text","content":"\n        \u003cp\u003eI copied over the libraries and copy and pasted the code, modifying it to use the CircuitPython defined input - worked like a charm!\u003c/p\u003e\n\u003cp\u003eExcept... this code is blocking, meaning that it waits to receive an IR signal before other things in the while loop can proceed. But I want my Fruit Jam to do other stuff while waiting for IR signals...\u0026nbsp;\u003c/p\u003e\n\u003ch2\u003eA little help here...\u003c/h2\u003e\n\u003cp\u003eHere's where the power of the Adafruit CircuitPython community really shines. I posted the question looking for non-blocking sample code in the #\u003cem\u003ehelp-with-circuitpython\u003c/em\u003e channel in the Adafruit Discord. Within minutes I had a response directing me to the answer:\u003c/p\u003e\n      ","metadata":{}},{"element_type":"embed","content":"https://github.com/adafruit/Adafruit_CircuitPython_IRRemote/blob/main/examples/irremote_nonblocking.py"},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eI also got suggestions for other approaches, such as using asyncio - I will have to play with that as well. But this worked for now.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eHere is my code:\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"import pulseio\nimport adafruit_irremote\nimport board\n\nremote = { # Add tuples to map codes generated from the remote\n\t(0, 255, 10, 245): \"On/Off\",\n\t(0, 255, 16, 239): \"Select\",\n\n}\n\nir_pin = board.IR\npulsein = pulseio.PulseIn(ir_pin, maxlen=120, idle_state=True)\ndecoder = adafruit_irremote.NonblockingGenericDecode(pulsein)\n\nwhile True:\n\tfor message in decoder.read():\n\n\t\tif isinstance(message, adafruit_irremote.IRMessage):\n\t\t\tprint(\"Decoded:\", message.code)  # , remote[message.code])\n\t\t\tif message.code in remote:\n\t\t\t\tprint(\"Button:\", remote[message.code])\n\t\t\telse:\n\t\t\t\tprint(\"Unknown Button\")\n\t\telif isinstance(message, adafruit_irremote.NECRepeatIRMessage):\n\t\t\tprint(\"NEC repeat!\")\n\t\t\tpass\n\t\telif isinstance(message, adafruit_irremote.UnparseableIRMessage):\n\t\t\tprint(\"Failed to decode\", message.reason)\n\t\tprint(\"----------------------------\")","metadata":{"language":"auto","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eNote that you will need to map the codes from your remote and put them into the remote dictionary - run the code, press a button, copy the tuple from the REPL and paste it into the remote dictionary. So far I have mapped two different remotes and about 50 buttons into the code.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n","metadata":{}}]