]> vault307.fbx.one Git - micorpython_ir.git/blobdiff - ir_tx/test.py
Merge pull request #8 from jetannenbaum/master
[micorpython_ir.git] / ir_tx / test.py
index bb54329affaf379001b6876ab6eccf6da660abd0..c9e3366e35482f57d76e87c6935018770f728fb1 100644 (file)
@@ -1,4 +1,4 @@
-# ir_tx_test.py Test for nonblocking NEC/SONY/RC-5/RC-6 mode 0 IR blaster.
+# ir_tx.test Test for nonblocking NEC/SONY/RC-5/RC-6 mode 0 IR blaster.
 
 # Released under the MIT License (MIT). See LICENSE.
 
 
 # Released under the MIT License (MIT). See LICENSE.
 
@@ -6,13 +6,16 @@
 
 # Implements a 2-button remote control on a Pyboard with auto repeat.
 from sys import platform
 
 # Implements a 2-button remote control on a Pyboard with auto repeat.
 from sys import platform
-ESP32 = platform == 'esp32' or platform == 'esp32_LoBo'
-if ESP32:
+ESP32 = platform == 'esp32'
+RP2 = platform == 'rp2'
+PYBOARD = platform == 'pyboard'
+if ESP32 or RP2:
     from machine import Pin
 else:
     from pyb import Pin, LED
 import uasyncio as asyncio
     from machine import Pin
 else:
     from pyb import Pin, LED
 import uasyncio as asyncio
-from aswitch import Switch, Delay_ms
+from primitives.switch import Switch
+from primitives.delay_ms import Delay_ms
 # Import all implemented classes
 from ir_tx.nec import NEC
 from ir_tx.sony import SONY_12, SONY_15, SONY_20
 # Import all implemented classes
 from ir_tx.nec import NEC
 from ir_tx.sony import SONY_12, SONY_15, SONY_20
@@ -20,20 +23,24 @@ from ir_tx.philips import RC5, RC6_M0
 
 loop = asyncio.get_event_loop()
 
 
 loop = asyncio.get_event_loop()
 
+# If button is held down normal behaviour is to retransmit
+# but most NEC models send a REPEAT code
 class Rbutton:
     toggle = 1  # toggle is ignored in NEC mode
 class Rbutton:
     toggle = 1  # toggle is ignored in NEC mode
-    def __init__(self, irb, pin, addr, data, rep_code=False):
+    def __init__(self, irb, pin, addr, data, proto):
         self.irb = irb
         self.sw = Switch(pin)
         self.addr = addr
         self.data = data
         self.irb = irb
         self.sw = Switch(pin)
         self.addr = addr
         self.data = data
-        self.rep_code = rep_code
+        self.proto = proto
+
         self.sw.close_func(self.cfunc)
         self.sw.open_func(self.ofunc)
         self.tim = Delay_ms(self.repeat)
 
     def cfunc(self):  # Button push: send data
         self.sw.close_func(self.cfunc)
         self.sw.open_func(self.ofunc)
         self.tim = Delay_ms(self.repeat)
 
     def cfunc(self):  # Button push: send data
-        self.irb.transmit(self.addr, self.data, Rbutton.toggle)
+        tog = 0 if self.proto < 3 else Rbutton.toggle  # NEC, sony 12, 15: toggle==0
+        self.irb.transmit(self.addr, self.data, tog, True)  # Test validation
         # Auto repeat. The Sony protocol specifies 45ms but this is tight.
         # In 20 bit mode a data burst can be upto 39ms long.
         self.tim.trigger(108)
         # Auto repeat. The Sony protocol specifies 45ms but this is tight.
         # In 20 bit mode a data burst can be upto 39ms long.
         self.tim.trigger(108)
@@ -46,59 +53,80 @@ class Rbutton:
         await asyncio.sleep(0)  # Let timer stop before retriggering
         if not self.sw():  # Button is still pressed: retrigger
             self.tim.trigger(108)
         await asyncio.sleep(0)  # Let timer stop before retriggering
         if not self.sw():  # Button is still pressed: retrigger
             self.tim.trigger(108)
-            if self.rep_code:
+            if self.proto == 0:
                 self.irb.repeat()  # NEC special case: send REPEAT code
             else:
                 self.irb.repeat()  # NEC special case: send REPEAT code
             else:
-                self.irb.transmit(self.addr, self.data, Rbutton.toggle)
+                tog = 0 if self.proto < 3 else Rbutton.toggle  # NEC, sony 12, 15: toggle==0
+                self.irb.transmit(self.addr, self.data, tog, True)  # Test validation
 
 async def main(proto):
 
 async def main(proto):
-    # Test uses a 38KHz carrier. Some Philips systems use 36KHz.
-    # If button is held down normal behaviour is to retransmit
-    # but most NEC models send a REPEAT code
-    rep_code = proto == 0  # Rbutton constructor requires False for RC-X. NEC protocol only.
-    pin = Pin(23, Pin.OUT) if ESP32 else Pin('X1')
+    # Test uses a 38KHz carrier.
+    if ESP32:  # Pins for IR LED gate
+        pin = Pin(23, Pin.OUT, value = 0)
+    elif RP2:
+        pin = Pin(17, Pin.OUT, value = 0)
+    else:
+        pin = Pin('X1')
     classes = (NEC, SONY_12, SONY_15, SONY_20, RC5, RC6_M0)
     irb = classes[proto](pin, 38000)  # My decoder chip is 38KHz
     classes = (NEC, SONY_12, SONY_15, SONY_20, RC5, RC6_M0)
     irb = classes[proto](pin, 38000)  # My decoder chip is 38KHz
+    # Uncomment the following to print transmit timing
+    # irb.timeit = True
 
     b = []  # Rbutton instances
 
     b = []  # Rbutton instances
-    px3 = Pin(18, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X3', Pin.IN, Pin.PULL_UP)
-    px4 = Pin(19, Pin.IN, Pin.PULL_UP) if ESP32 else Pin('X4', Pin.IN, Pin.PULL_UP)
-    b.append(Rbutton(irb, px3, 0x1, 0x7, rep_code))
-    b.append(Rbutton(irb, px4, 0x10, 0xb, rep_code))
+    px3 = Pin('X3', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(18, Pin.IN, Pin.PULL_UP)
+    px4 = Pin('X4', Pin.IN, Pin.PULL_UP) if PYBOARD else Pin(19, Pin.IN, Pin.PULL_UP)
+    b.append(Rbutton(irb, px3, 0x1, 0x7, proto))
+    b.append(Rbutton(irb, px4, 0x10, 0xb, proto))
     if ESP32:
         while True:
             print('Running')
             await asyncio.sleep(5)
     if ESP32:
         while True:
             print('Running')
             await asyncio.sleep(5)
+    elif RP2:
+        led = Pin(25, Pin.OUT)
+        while True:
+            await asyncio.sleep_ms(500)  # Obligatory flashing LED.
+            led(not led())
     else:
         led = LED(1)
         while True:
             await asyncio.sleep_ms(500)  # Obligatory flashing LED.
             led.toggle()
 
     else:
         led = LED(1)
         while True:
             await asyncio.sleep_ms(500)  # Obligatory flashing LED.
             led.toggle()
 
+# Greeting strings. Common:
 s = '''Test for IR transmitter. Run:
 s = '''Test for IR transmitter. Run:
-from ir_tx_test import test
+from ir_tx.test import test
 test() for NEC protocol
 test(1) for Sony SIRC 12 bit
 test(2) for Sony SIRC 15 bit
 test() for NEC protocol
 test(1) for Sony SIRC 12 bit
 test(2) for Sony SIRC 15 bit
-test(3) for Sony SIRC 20 bit'''
-spb = '''
-test(5) for Philips RC-5 protocol
-test(6) for Philips RC-6 mode 0.
+test(3) for Sony SIRC 20 bit
+test(4) for Philips RC-5 protocol
+test(5) for Philips RC-6 mode 0.
+'''
 
 
+# Pyboard:
+spb = '''
 IR LED on pin X1
 Ground pin X3 to send addr 1 data 7
 Ground pin X4 to send addr 0x10 data 0x0b.'''
 IR LED on pin X1
 Ground pin X3 to send addr 1 data 7
 Ground pin X4 to send addr 0x10 data 0x0b.'''
+
+# ESP32
 sesp = '''
 sesp = '''
+IR LED gate on pin 23
+Ground pin 18 to send addr 1 data 7
+Ground pin 19 to send addr 0x10 data 0x0b.'''
 
 
-IR LED on pin 23
+# RP2
+srp2 = '''
+IR LED gate on pin 17
 Ground pin 18 to send addr 1 data 7
 Ground pin 19 to send addr 0x10 data 0x0b.'''
 Ground pin 18 to send addr 1 data 7
 Ground pin 19 to send addr 0x10 data 0x0b.'''
+
 if ESP32:
 if ESP32:
-    s = ''.join((s, sesp))
+    print(''.join((s, sesp)))
+elif RP2:
+    print(''.join((s, srp2)))
 else:
 else:
-    s = ''.join((s, spb))
-print(s)
-
+    print(''.join((s, spb)))
 
 def test(proto=0):
     loop.run_until_complete(main(proto))
 
 def test(proto=0):
     loop.run_until_complete(main(proto))