Exemplo n.º 1
0
    def test_GivenBankAAI0AI1AndBankBAI0AI1_Write_ReturnExpectedReadBack(self):
        def __read(number_of_times_to_read, read_object, number_of_samples_to_read, timeout, expected_value):
            title = {'A': 'Bank A AI0 - AO0 and AI1 - AO3', 'B': 'Bank B AI0 - AO1 and AI0 - AO3'}
            result = {'A': [[],[]], 'B': [[],[]]}
            for i in range(0, number_of_times_to_read):
                value_array = read_object.read(number_of_samples_to_read, timeout)
                # print(value_array)
                bank_A_values = value_array[0]
                bank_B_values = value_array[1]
                result['A'][0].append(bank_A_values[0])
                result['A'][1].append(bank_A_values[1])
                result['B'][0].append(bank_B_values[0])
                result['B'][1].append(bank_B_values[1])

            def __print(bank):
                print(title[bank])
                for index, channel in enumerate(result[bank]):
                    for values_per_channel in channel:
                        values = [ round(value) for value in values_per_channel ]
                        print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value[bank][index]))

            __print(Bank.A.value)
            __print(Bank.B.value)
        
        AO = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0},
                          {'bank': Bank.B, 'channel': AOChannel.AO1},
                          {'bank': bank, 'channel': AOChannel.AO0},
                          {'bank': bank, 'channel': AOChannel.AO1})
        AI = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0},
                         {'bank': Bank.B, 'channel': AIChannel.AI3},
                         {'bank': bank, 'channel': AIChannel.AI0},
                         {'bank': bank, 'channel': AIChannel.AI3})

        timeout = -1
        sample_rate = 1000
        input_value = [[1,2,3,4,5,6,7,8,9,10], [10,9,8,7,6,5,4,3,2,1], [2,3,4,5,6,2,3,4,5,6], [7,6,5,4,3,7,6,5,4,3]]
        number_of_samples_to_read = 5
        expected_value = {
            'A': [self.__convert_list_to_string(input_value[2]), self.__convert_list_to_string(input_value[3])],
            'B': [self.__convert_list_to_string(input_value[0]), self.__convert_list_to_string(input_value[1])]
            }
        irq_thread = threading.Thread(target=self.__writing_thread, args=(AO, input_value, timeout))
        
        AO.start_continuous_mode(input_value, sample_rate, timeout)
        self.__start_thread(irq_thread)
        
        AI.start_continuous_mode(sample_rate)
        
        __read(2, AI, number_of_samples_to_read, timeout, expected_value)
        
        self.__close_thread(irq_thread)

        AO.close()
        AI.close()
Exemplo n.º 2
0
    def test_GivenBankAAI0AI1AndBankBAI0_WriteWithDifferentValuesLength_ReturnExpectedReadBack(self):
        def __read(number_of_times_to_read, read_object, number_of_samples_to_read, timeout, expected_value):
            # result = [A: [AO0, AO1], B: [AO0]]
            result = [[[],[]], [[],[]]]
            for i in range(0, number_of_times_to_read):
                value_array = read_object.read(number_of_samples_to_read, timeout)
                bank_A_values = value_array[0]
                bank_B_values = value_array[1]
                result[0][0].append(bank_A_values[0])
                result[0][1].append(bank_A_values[1])
                result[1][0].append(bank_B_values[0])

            def __print(channels, bank):
                for channel in channels:
                    for index, values_per_channel in enumerate(channel):
                        values = [ round(value) for value in values_per_channel ]
                        if bank == Bank.A.value:
                            print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value[index]))
                        else:
                            print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value[2]))

            print('Bank A AI0 - AO0 and AI1 - AO3')
            __print(result[0], Bank.A.value)
            print('Bank B AI0 - AO0')
            __print(result[1], Bank.B.value)
        
        AO = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0},
                          {'bank': bank, 'channel': AOChannel.AO0},
                          {'bank': bank, 'channel': AOChannel.AO1})
        AI = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0},
                         {'bank': bank, 'channel': AIChannel.AI0},
                         {'bank': bank, 'channel': AIChannel.AI3})

        timeout = -1
        sample_rate = 1000
        input_value = [[0,1,2,3,4,5,0,1,2,3,4,5], [7,8,9,10,7,8,9,10,7,8,9,10], [1,2,3,4,5,1,2,3,4,5]]
        number_of_samples_to_read = 4
        expected_value = [self.__convert_list_to_string(input_value[1]), self.__convert_list_to_string(input_value[2]), self.__convert_list_to_string(input_value[0])]
        irq_thread = threading.Thread(target=self.__writing_thread, args=(AO, input_value, timeout))
        
        AO.start_continuous_mode(input_value, sample_rate, timeout)
        self.__start_thread(irq_thread)
        
        AI.start_continuous_mode(sample_rate)
        
        __read(2, AI, number_of_samples_to_read, timeout, expected_value)
        
        self.__close_thread(irq_thread)

        AO.close()
        AI.close()
Exemplo n.º 3
0
    def test_GivenBankBAI0AndBankAAI1_Write_ReturnExpectedReadBack(self):
        def __read(number_of_times_to_read, read_object, number_of_samples_to_read, timeout, expected_value_for_bank_A, expected_value_for_bank_B):
            result = [[], []]
            for i in range(0, number_of_times_to_read):
                value_array = read_object.read(number_of_samples_to_read, timeout)
                result[0].append(value_array[0])
                result[1].append(value_array[1])

            def __print(channels, expected_value):
                for channel in channels:
                    for values_per_channel in channel:
                        values = [ round(value) for value in values_per_channel ]
                        print('continuous: %s should almost equal to %s and continuous from the last read' % (values, expected_value))

            print('Bank A AI3 - AO1')
            __print(result[0], expected_value_for_bank_A)
            print('Bank B AI0 - AO0')
            __print(result[1], expected_value_for_bank_B)
        
        AO = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0},
                          {'bank': bank, 'channel': AOChannel.AO1})
        AI = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0},
                         {'bank': bank, 'channel': AIChannel.AI3})

        timeout = -1
        sample_rate = 1000
        input_value = [[1,2,3,4,5,6,7,8,9,10], [10,9,8,7,6,5,4,3,2,1]]
        number_of_samples_to_read = 5
        expected_value_for_bank_B = self.__convert_list_to_string(input_value[0])
        expected_value_for_bank_A = self.__convert_list_to_string(input_value[1])
        irq_thread = threading.Thread(target=self.__writing_thread, args=(AO, input_value, timeout))
        
        AO.start_continuous_mode(input_value, sample_rate, timeout)
        self.__start_thread(irq_thread)

        AI.start_continuous_mode(sample_rate)
        
        __read(2, AI, number_of_samples_to_read, timeout, expected_value_for_bank_A, expected_value_for_bank_B)
            
        self.__close_thread(irq_thread)

        AO.close()
        AI.close()
Exemplo n.º 4
0
        def __write_and_read(input_value):
            AO_single_channel = AnalogOutput({'bank': Bank.B, 'channel': AOChannel.AO0})
            AI_single_channel = AnalogInput({'bank': Bank.B, 'channel': AIChannel.AI0})

            timeout = -1
            sample_rate = 1000
            number_of_samples_to_read = 10
            expected_value = self.__convert_list_to_string(input_value)
            irq_thread = threading.Thread(target=self.__writing_thread, args=(AO_single_channel, input_value, timeout))
            
            AO_single_channel.start_continuous_mode(input_value, sample_rate, timeout)
            self.__start_thread(irq_thread)

            AI_single_channel.start_continuous_mode(sample_rate)
            
            self.__read(2, AI_single_channel, number_of_samples_to_read, timeout, expected_value)
                
            self.__close_thread(irq_thread)

            AO_single_channel.close()
            AI_single_channel.close()
Exemplo n.º 5
0
    def test_Given7PointsWith1000SampleRateOnBankA_WriteThenCloseWithoutStop_ReturnExpectedReadBack(self):
        AO_single_channel = AnalogOutput({'bank': bank, 'channel': AOChannel.AO0})
        AI_single_channel = AnalogInput({'bank': bank, 'channel': AIChannel.AI0})

        timeout = -1
        sample_rate = 2000
        input_value = [[9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0]]
        number_of_samples_to_read = 7
        expected_value = self.__convert_list_to_string(input_value)
        irq_thread = threading.Thread(target=self.__writing_thread, args=(AO_single_channel, input_value, timeout))
        
        AO_single_channel.start_continuous_mode(input_value, sample_rate, timeout)
        self.__start_thread(irq_thread)

        AI_single_channel.start_continuous_mode(sample_rate)
        
        self.__read(3, AI_single_channel, number_of_samples_to_read, timeout, expected_value)
            
        self.__close_thread(irq_thread)

        AO_single_channel.close()
        AI_single_channel.close()
Exemplo n.º 6
0
def writeWaveformToAO(p_waveform, p_sampleRate, p_channelRef=None):
    bank = Bank.B
    channel = AOChannel.AO0
    isNewChannel = p_channelRef is None

    if isNewChannel:
        p_channelRef = AnalogOutput({
            'bank': bank,
            'channel': channel
        })

    # printString('朗读歌名...')
    timeout = -1
    MAX_SINGLE_WRITE = 200000
    totalSize = len(p_waveform)

    # limit the waveform length of single write
    numberOfChunks = math.ceil(totalSize / MAX_SINGLE_WRITE)
    for i in range(numberOfChunks):
        startOffset = i * MAX_SINGLE_WRITE
        endOffset = min(totalSize, (i + 1) * MAX_SINGLE_WRITE - 1)
        #print('[DBG] write range: %d:%d' % (startOffset, endOffset))

        toWrite = [p_waveform[startOffset:endOffset]]
        if i == 0:
            p_channelRef.start_continuous_mode(toWrite, p_sampleRate, timeout)

        p_channelRef.write(toWrite, p_sampleRate)

    time.sleep(5)

    # printString('结束播放')
    p_channelRef.stop_continuous_mode()

    if isNewChannel:
        p_channelRef.close()
Exemplo n.º 7
0
class Test_AnalogInput_StartAssertion(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.expectedInput = [[0, 1]]
        self.expectedSampleRate = 1000
        self.expectedTimeout = -1

    def setUp(self):
        self.AO_single_channel = AnalogOutput({
            'bank': bank,
            'channel': AOChannel.AO0
        })
        self.AO_multiple_channels = AnalogOutput(
            {
                'bank': bank,
                'channel': AOChannel.AO0
            }, {
                'bank': bank,
                'channel': AOChannel.AO1
            })

    def tearDown(self):
        self.AO_single_channel.close()
        self.AO_multiple_channels.close()

    def test_PassInvalidInputValues_ShowAssertion(self):
        wrong_values = [0, [], [[]]]

        for wrong_value in wrong_values:
            with self.assertRaises(AssertionError):
                self.AO_single_channel.start_continuous_mode(
                    wrong_value, self.expectedSampleRate, self.expectedTimeout)

    def test_PassInvalidNumberOfInputValues_ShowAssertion(self):
        wrong_values = [[1], [[1]], [[1], ['a']]]

        for wrong_value in wrong_values:
            with self.assertRaises(AssertionError):
                self.AO_multiple_channels.start_continuous_mode(
                    wrong_value, self.expectedSampleRate, self.expectedTimeout)

    def test_PassInvalidSampleRateToSingleChannel_ShowAssertion(self):
        limits = {'SampleRate': {'min': 1000, 'max': 1600000}}
        wrong_sample_rates = [
            limits['SampleRate']['min'] - 1, limits['SampleRate']['max'] + 1
        ]

        for wrong_sample_rate in wrong_sample_rates:
            with self.assertRaises(AssertionError):
                self.AO_single_channel.start_continuous_mode(
                    self.expectedInput, wrong_sample_rate,
                    self.expectedTimeout)