Beispiel #1
0
def recordCodes(deviceName):
    """Ask the user what the button press should be called and return as a dict of 
    name:code key/value pairs.  Similar to the Lirc irrecord tool"""

    serialDevice = serial.Serial(deviceName)
    toy = IrToy(serialDevice)
    codes = {}
    while True:

        try:
            input = raw_input
        except NameError:
            pass

        label = input("Please enter a name for the button you will press, or Enter to finish:\n")

        if label == "":
            break
        else:
            toy.reset()
            print("press the remote control button once for the %s button:" % label)
            codes[label] = toy.receive()

    serialDevice.close()

    return codes
Beispiel #2
0
def recordCodes(deviceName):
    '''Ask the user what the button press should be called and return as a dict of 
    name:code key/value pairs.  Similar to the Lirc irrecord tool'''

    serialDevice = serial.Serial(deviceName)
    toy = IrToy(serialDevice)
    codes = {}
    while True:
        
        try:
            input = raw_input
        except NameError:
            pass

        label = input('Please enter a name for the button you will press, or Enter to finish:\n')
        
        if label == '':
            break
        else:
            toy.reset()
            print('press the remote control button once for the %s button:' % label)
            codes[label] = toy.receive()

    serialDevice.close()

    return codes
Beispiel #3
0
class TestIrToy(unittest.TestCase):

    def setUp(self):

        self.serialMock = SerialMock()
        self.toy = IrToy(self.serialMock)
        self.toy.sleepTime = 0

    def testTransmit(self):

        # length of code sent must be even
        self.assertRaises(ValueError, self.toy.transmit, [10])
        
        # set the expected results from read(): protocol version, handshake ('>' converts to 62),
        # byte count transmitted, and completion code.
        self.serialMock.setReadCode([bytearray([62]), bytearray([62]), bytearray([0,0,0,1]), b'C', b'S01'])
        
        self.toy.transmit([10,10])
        
        # when transmitting, we expect a reset (five 0x00), 'S' to enter sampling mode, 0x26 for enable handshake,
        # 0x25 to enable notify on transmit, 0x24 to enable transmit byte count, and 0x03 to start the transmission,
        # then the list of codes to transmit (always ending with 0xff, 0xff), and another reset.  See DP link at top of this file for more info.
        expectedHistory = [[0x00, 0x00, 0x00, 0x00, 0x00],
                           'v',
                           [0x00, 0x00, 0x00, 0x00, 0x00],
                           'v',
                           [0x00, 0x00, 0x00, 0x00, 0x00],
                           b'S', [0x26], [0x25], [0x24], [0x03],
                           [10, 10, 0xff, 0xff],
                           [0x00, 0x00, 0x00, 0x00, 0x00],
                           b'S']

        self.assertEqual(self.serialMock.writeHistory, expectedHistory)

    def testReceive(self):

        # pretend to be receiving the following signals from the IR Toy, must end with 0xff,0xff (same as 255, means no signal) 
        # or the code will keep recording indefinitely.  First element is for the protocol version, since receive() resets the toy
        self.serialMock.setReadCode([bytearray([62]), bytearray([1]), bytearray([2]), bytearray([0xff]), bytearray([0xff])])
        readSignal = self.toy.receive()

        self.assertEqual(readSignal, [1, 2, 0xff, 0xff])

    def XtestMultipleTransmits(self):
        # tracking down a bug that shows itself when we transmit using the same object for more than one transmit.  Method
        # name starts with X to avoid running with other tests, this one is run manually since you need the IR Toy hardware
        # and the pySerial module which isn't necessarily installed by default.

        # IR code that causes an issue, any code would probably work for this purpose.
        irCode=[1, 159, 0, 212, 0, 26, 0, 25, 0, 26, 0, 78, 0, 26, 0, 25, 0, 26, 0, 25, 0, 26, 0, 78, 0, 26, 0, 
                25, 0, 27, 0, 77, 0, 26, 0, 77, 0, 26, 0, 78, 0, 26, 0, 26, 0, 26, 0, 78, 0, 26, 0, 78, 0, 26,
                0, 25, 0, 26, 0, 78, 0, 26, 0, 78, 0, 26, 0, 24, 0, 28, 0, 76, 0, 27, 0, 25, 0, 27, 0, 77, 0, 27, 
                0, 25, 0, 27, 0, 24, 0, 27, 0, 24, 0, 28, 0, 24, 0, 28, 0, 24, 0, 28, 0, 24, 0, 27, 0, 76, 0, 28, 
                0, 24, 0, 27, 0, 77, 0, 27, 0, 76, 0, 27, 0, 76, 0, 28, 0, 76, 0, 27, 0, 76, 0, 28, 7, 15, 1, 160, 
                0, 107, 0, 27, 255, 255] #Onkyo TX-SR508 mute
        
        import serial
        serialDevice = serial.Serial('/dev/ttyACM0')
        #serialDevice = self.serialMock

        # re-using a single object with multiple transmits
        toy = IrToy(serialDevice)
        for i in range(5):
            toy.transmit(irCode)
            
        # using a new object for each transmit
        for i in range(5):
            toy2 = IrToy(serialDevice)
            toy2.transmit(irCode)
Beispiel #4
0
class TestIrToy(unittest.TestCase):
    def setUp(self):

        self.serialMock = SerialMock()
        self.toy = IrToy(self.serialMock)
        self.toy.sleepTime = 0

    def testTransmit(self):

        # length of code sent must be even
        self.assertRaises(ValueError, self.toy.transmit, [10])

        # set the expected results from read(): protocol version, handshake ('>' converts to 62),
        # byte count transmitted, and completion code.
        self.serialMock.setReadCode([
            bytearray([62]),
            bytearray([62]),
            bytearray([0, 0, 0, 1]), b'C', b'S01'
        ])

        self.toy.transmit([10, 10])

        # when transmitting, we expect a reset (five 0x00), 'S' to enter sampling mode, 0x26 for enable handshake,
        # 0x25 to enable notify on transmit, 0x24 to enable transmit byte count, and 0x03 to start the transmission,
        # then the list of codes to transmit (always ending with 0xff, 0xff), and another reset.  See DP link at top of this file for more info.
        expectedHistory = [[0x00, 0x00, 0x00, 0x00, 0x00], 'v',
                           [0x00, 0x00, 0x00, 0x00, 0x00], 'v',
                           [0x00, 0x00, 0x00, 0x00, 0x00], b'S', [0x26],
                           [0x25], [0x24], [0x03], [10, 10, 0xff, 0xff],
                           [0x00, 0x00, 0x00, 0x00, 0x00], b'S']

        self.assertEqual(self.serialMock.writeHistory, expectedHistory)

    def testReceive(self):

        # pretend to be receiving the following signals from the IR Toy, must end with 0xff,0xff (same as 255, means no signal)
        # or the code will keep recording indefinitely.  First element is for the protocol version, since receive() resets the toy
        self.serialMock.setReadCode([
            bytearray([62]),
            bytearray([1]),
            bytearray([2]),
            bytearray([0xff]),
            bytearray([0xff])
        ])
        readSignal = self.toy.receive()

        self.assertEqual(readSignal, [1, 2, 0xff, 0xff])

    def XtestMultipleTransmits(self):
        # tracking down a bug that shows itself when we transmit using the same object for more than one transmit.  Method
        # name starts with X to avoid running with other tests, this one is run manually since you need the IR Toy hardware
        # and the pySerial module which isn't necessarily installed by default.

        # IR code that causes an issue, any code would probably work for this purpose.
        irCode = [
            1, 159, 0, 212, 0, 26, 0, 25, 0, 26, 0, 78, 0, 26, 0, 25, 0, 26, 0,
            25, 0, 26, 0, 78, 0, 26, 0, 25, 0, 27, 0, 77, 0, 26, 0, 77, 0, 26,
            0, 78, 0, 26, 0, 26, 0, 26, 0, 78, 0, 26, 0, 78, 0, 26, 0, 25, 0,
            26, 0, 78, 0, 26, 0, 78, 0, 26, 0, 24, 0, 28, 0, 76, 0, 27, 0, 25,
            0, 27, 0, 77, 0, 27, 0, 25, 0, 27, 0, 24, 0, 27, 0, 24, 0, 28, 0,
            24, 0, 28, 0, 24, 0, 28, 0, 24, 0, 27, 0, 76, 0, 28, 0, 24, 0, 27,
            0, 77, 0, 27, 0, 76, 0, 27, 0, 76, 0, 28, 0, 76, 0, 27, 0, 76, 0,
            28, 7, 15, 1, 160, 0, 107, 0, 27, 255, 255
        ]  #Onkyo TX-SR508 mute

        import serial
        serialDevice = serial.Serial('/dev/ttyACM0')
        #serialDevice = self.serialMock

        # re-using a single object with multiple transmits
        toy = IrToy(serialDevice)
        for i in range(5):
            toy.transmit(irCode)

        # using a new object for each transmit
        for i in range(5):
            toy2 = IrToy(serialDevice)
            toy2.transmit(irCode)