IRremoteESP8266 icon indicating copy to clipboard operation
IRremoteESP8266 copied to clipboard

IRremoteESP8266 in a python library

Open frawau opened this issue 4 months ago • 11 comments

I wonder if you would accept this pull request.

It is quite a hack, I never used C++ and I am lazy so I took the path of least resistance.

What this PR does is add the ability to add IRremoteESP8266 to Python. I use it in my library pyhvac to generate HVAC IR codes with Python.

To avoid too many modifications to the original C++ code I hacked things this way:

  • Only expose IRac
  • Create a global vector
  • Modify "mark" and "space" of IRSend to concatenate the values passed to them to the global vector
  • Add 2 methods to the IRac class: -- getTiming to retrieve the value of the global vector -- resetTiming to reset the vector

All this is controlled by the preprocessor variable PYTHONLIB

I initially planned to generate Mark and Space pulses in Python (that is why the timing info is present in pyhvac) but that would have required a lot of ifdef/else in the C++ code.

I also thought about having the vector as a member of the IRac class, but this too would have required a lot of changes.

Using a global vector means that in some instances, access to the object needs to be serialized in Python, but I can live with that.

To generate the Python lib, I use SWIG and one must use the latest version (4.2.0 at the time of this writing) to generate the library.

Once the library is created you can use it like this:

import irhvac
ac=irhvac.IRac(4)
ac.next.protocol = irhvac.ELECTRA_AC
ac.next.model = irhvac.DG11J13A
ac.next.mode = irhvac.opmode_t_kCool
ac.next.degrees = 23.5
ac.next.swingv = irhvac.swingv_t_kAuto
ac.sendAc()
print(ac.getTiming())
ac.resetTiming()

Since SWIG is used, it should be quite simple to add support for other languages.

I did run the tests. They all passed on x86_64. The Lego test failed on ARM64, but this is not an A/C IR and I guess is more a mishandled endianess problem.

frawau avatar Feb 18 '24 07:02 frawau