]> vault307.fbx.one Git - micorpython_ir.git/blobdiff - ir_tx/__init__.py
README.md: Add reference to IRMP.
[micorpython_ir.git] / ir_tx / __init__.py
index ab166277ef59f8e2c2939b60671d8cd329ea9ddc..e791b4a961810d773a68300def1489d70110f68c 100644 (file)
@@ -1,14 +1,17 @@
 # __init__.py Nonblocking IR blaster
-# Runs on Pyboard D or Pyboard 1.x (not Pyboard Lite) and ESP32
+# Runs on Pyboard D or Pyboard 1.x (not Pyboard Lite), ESP32 and RP2
 
 # Released under the MIT License (MIT). See LICENSE.
 
-# Copyright (c) 2020 Peter Hinch
+# Copyright (c) 2020-2021 Peter Hinch
 from sys import platform
 ESP32 = platform == 'esp32'  # Loboris not supported owing to RMT
+RP2 = platform == 'rp2'
 if ESP32:
     from machine import Pin, PWM
     from esp32 import RMT
+elif RP2:
+    from .rp2_rmt import RP2_RMT
 else:
     from pyb import Pin, Timer  # Pyboard does not support machine.PWM
 
@@ -18,7 +21,6 @@ from time import ticks_us, ticks_diff
 # import micropython
 # micropython.alloc_emergency_exception_buf(100)
 
-# On ESP32 gate hardware design is led_on = rmt and carrier
 
 # Shared by NEC
 STOP = const(0)  # End of data
@@ -40,11 +42,11 @@ class IR:
 
     def __init__(self, pin, cfreq, asize, duty, verbose):
         if ESP32:
-            self._pwm = PWM(pin[0])  # Continuous 36/38/40KHz carrier
-            self._pwm.deinit()
-            # ESP32: 0 <= duty <= 1023
-            self._pwm.init(freq=cfreq, duty=round(duty * 10.23))
-            self._rmt = RMT(0, pin=pin[1], clock_div=80)  # 1μs resolution
+            self._rmt = RMT(0, pin=pin, clock_div=80, tx_carrier = (cfreq, duty, 1))
+            # 1μs resolution
+        elif RP2:  # PIO-based RMT-like device
+            self._rmt = RP2_RMT(pin_pulse=None, carrier=(pin, cfreq, duty))  # 1μs resolution
+            asize += 1  # Allow for possible extra space pulse
         else:  # Pyboard
             if not IR._active_high:
                 duty = 100 - duty
@@ -95,7 +97,10 @@ class IR:
     # Subclass interface
     def trigger(self):  # Used by NEC to initiate a repeat frame
         if ESP32:
-            self._rmt.write_pulses(tuple(self._mva[0 : self.aptr]), start = 1)
+            self._rmt.write_pulses(tuple(self._mva[0 : self.aptr]))
+        elif RP2:
+            self.append(STOP)
+            self._rmt.send(self._arr)
         else:
             self.append(STOP)
             self.aptr = 0  # Reset pointer