class Test_DigitalInputOuput(unittest.TestCase): @classmethod def setUpClass(self): self.value = True self.dio = DigitalInputOutput(bank, [DIOChannel.DIO2, DIOChannel.DIO3, DIOChannel.DIO4, DIOChannel.DIO8, DIOChannel.DIO9, DIOChannel.DIO10, DIOChannel.DIO11, DIOChannel.DIO12]) @classmethod def tearDownClass(self): self.dio.close() def test_WriteAndReadFourChannels(self): self.dio.write(self.value, [DIOChannel.DIO2, DIOChannel.DIO3, DIOChannel.DIO4, DIOChannel.DIO8]) data = self.dio.read([DIOChannel.DIO9, DIOChannel.DIO10, DIOChannel.DIO11, DIOChannel.DIO12]) self.assertEqual(data, [1, 1, 1, 1]) def test_WriteAndReadTwoChannels(self): self.dio.write(self.value, [DIOChannel.DIO2, DIOChannel.DIO4]) data = self.dio.read([DIOChannel.DIO9, DIOChannel.DIO11]) self.assertEqual(data, [1, 1]) def test_WriteAndReadSingleChannel(self): self.dio.write(not self.value, [DIOChannel.DIO3]) data = self.dio.read([DIOChannel.DIO10]) self.assertEqual(data, [0])
class Test_DigitalInputOuput_Assertion(unittest.TestCase): @classmethod def setUpClass(self): self.value = True self.channel = [DIOChannel.DIO1] self.dio = DigitalInputOutput(bank, [DIOChannel.DIO2, DIOChannel.DIO3, DIOChannel.DIO4, DIOChannel.DIO8, DIOChannel.DIO9, DIOChannel.DIO10, DIOChannel.DIO11, DIOChannel.DIO12]) @classmethod def tearDownClass(self): self.dio.close() def test_writeInvalidValue_ShowAssertion(self): with self.assertRaises(AssertionError): self.dio.write(200, self.channel) def test_writeInvalidChannel_ShowAssertion(self): with self.assertRaises(AssertionError): self.dio.write(self.value, [20]) def test_readInvalidChannel_ShowAssertion(self): with self.assertRaises(AssertionError): self.dio.read([20])
class Test_SPI(unittest.TestCase): @classmethod def setUpClass(self): self.cs = DigitalInputOutput(bank, [cs_channel]) self.spi = SPI(frequency, bank, clock_phase, clock_polarity, data_direction, frame_length) @classmethod def tearDownClass(self): self.spi.close() self.cs.close() def __writeAndRead(self): self.cs.write(False, [cs_channel]) self.spi.write([0x80]) value_array = self.spi.read(1) self.cs.write(True, [cs_channel]) return value_array def __writeread(self): self.cs.write(False, [cs_channel]) value_array = self.spi.writeread([0x80]) self.cs.write(True, [cs_channel]) return value_array def test_WriteAndRead_ReturnExpectedReadback(self): value_array = self.__writeAndRead() self.assertEqual(value_array[0], 'e5') def test_WriteRead_ReturnExpectedReadback(self): value_array = self.__writeread() self.assertEqual(value_array[0], 'e5')
# configure the chip select channel cs = DigitalInputOutput(bank, [cs_channel]) ############################################################################## # Section 1: # You use the write function and the read function to write to and read from # the SPI channel. ############################################################################## # configure an SPI session with SPI(frequency, bank, clock_phase, clock_polarity, data_direction, frame_length) as spi: # specify the bytes to write to the SPI channel data_to_write = [0x80] # set the chip select of SPI to low cs.write(False, [cs_channel]) # write data to the SPI channel spi.write(data_to_write) # set the chip select of SPI to high after writing cs.write(True, [cs_channel]) # specify the number of frame (int) to read from the SPI channel number_frames = 1 # set the chip select of SPI to low cs.write(False, [cs_channel]) # read one byte of data from SPI channel value_array = spi.read(number_frames) # set the chip select of SPI to high after reading cs.write(True, [cs_channel]) # print the data print("value read from SPI.read: ", value_array[0])