class TestDebouncer(unittest.TestCase): def setUp(self): # set up the device simulation self.v = Vlab(working_directory=".", print_uart=True, token="demo-token-for-ci") self.v.load("BUILD/STM32_Button_Debounce.bin") self.v.on_pin_level_event(self.pin_listener) # running for a while to get the BSP done self.v.run_for_ms(10) self.is_led_on = False self.times_pressed = 0 def tearDown(self): self.v.stop() def pin_listener(self, pin_number, pin_level): if pin_number == 5 and pin_level == 1: self.is_led_on = True self.times_pressed = self.times_pressed + 1 def test_debouncer(self): self.v.UserButton.on() self.v.run_for_ms(10) self.v.UserButton.off() self.v.run_for_ms(10) self.v.UserButton.on() self.v.run_for_ms(10) self.v.UserButton.off() self.v.run_for_ms(10) self.v.UserButton.on() self.v.UserButton.off() self.v.run_for_ms(280) self.assertEqual(self.is_led_on, True) self.assertEqual(self.times_pressed, 1)
class TestAppButton(unittest.TestCase): def pins_listener(self, pin_number, pin_level): if pin_number == 17 and pin_level == 0: self.is_led_on = True def setUp(self): self.vlab = Vlab(working_directory=dir) self.vlab.load(fw_bin) self.vlab.on_pin_level_event(self.pins_listener) self.vlab.run_for_ms(500) print('Virtual device is running') self.is_led_on = False def tearDown(self): self.vlab.stop() def test_should_ignore_button_noise(self): self.vlab.BUTTON1.on() self.vlab.run_for_ms(1) self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) self.assertFalse(self.is_led_on) def test_led_should_turn_on_on_button_push(self): self.vlab.BUTTON1.on() self.vlab.run_for_ms(60) self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) self.assertTrue(self.is_led_on)
class TestClass(unittest.TestCase): _FAIL = False _PASS = True def setUp(self): ########################## # change these arguments # ########################## self.fw = 'BUILD/STM32_Button_Debounce.elf' # TODO: YOUR FW HERE self.timeout = 10000 self.timeout_result = self._FAIL self.uart_test = True self.uart_test_value = 'mBed boot done' self.uart_test_result = self._PASS self.gpio_test = False self.gpio_test_pin = None self.gpio_test_value = None self.gpio_test_result = None self._is_pass = False # set up the device simulation self.v = Vlab(working_directory=".", print_uart=True, token="demo-token-for-ci") self.v.load(self.fw) self.v.on_pin_level_event(self.pin_listener) # running for a while to get the BSP done self.v.start(0) self.uart_data = '' self._condition_found = False self.v.stop_after_ms(self.timeout) self.v.resume() def tearDown(self): self.v.stop() def pin_listener(self, pin_number, pin_level): if self.gpio_test and self.gpio_test_pin == pin_number and self.gpio_test_value == pin_level: self._is_pass = True if self.gpio_test_result == self._PASS else False self._condition_found = True def read_from_uart(self): self.uart_data += self.v.uart.read() if self.uart_test and self.uart_test_value in self.uart_data: self._is_pass = True if self.uart_test_result == self._PASS else False self._condition_found = True def test_debouncer(self): while self.v.get_state() == 'running' and not self._condition_found: self.read_from_uart() sleep(0.1) self.assertEqual(self._is_pass, True)
class TestAppButton(unittest.TestCase): def setUp(self): print(self.id().split('.')[-1]) # test name self.vlab = Vlab(working_directory=dir) self.vlab.load(fw_bin) self.vlab.on_pin_level_event(self.pins_listener) self.vlab.run_for_ms(500) print('Virtual device is running') self.is_led_on = False def pins_listener(self, pin_number, pin_level): if pin_number == 17 and pin_level == 0: self.is_led_on = True def tearDown(self): self.vlab.stop() def print_device_time(self): print('Device time: ' + str(self.vlab.get_device_time_ms()) + ' ms') def test_led_should_turn_on_on_button_push(self): self.print_device_time() print('Button on') self.vlab.BUTTON1.on() self.vlab.run_for_ms(60) self.print_device_time() print('Button off') self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) # in this case we are going to poll for the pin level self.assertEqual(self.vlab.get_pin_level(17), 0) def test_should_ignore_button_noise(self): self.print_device_time() print('Button on') self.vlab.BUTTON1.on() self.vlab.run_for_ms(1) self.print_device_time() print('Button off') self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) self.assertFalse(self.is_led_on)
class TestBq24160(unittest.TestCase): def setUp(self): self.vlab = Vlab(working_directory=dir) self.vlab.load(fw_bin) self.uart = self.vlab.uart self.vlab.run_for_ms(500) self.vlab.on_pin_level_event(self.pins_listener) print('Virtual device is running') self.success = False self.need_to_be_on = False self.pin_number_should_toggle = 6 def pins_listener(self, pin_number, pin_level): if self.pin_number_should_toggle == pin_number: self.success = True def tearDown(self): self.vlab.stop() def test_should_raise_interrupt_bq24160(self): print("raise watchdog interrupt") self.pin_number_should_toggle = 3 self.success = False self.vlab.bq24160.interrupt( self.vlab.bq24160.WATCHDOG_EXPIRATION_INTERRUPT) self.vlab.run_for_ms(0.5) self.assertTrue(self.success) self.pin_number_should_toggle = 7 self.success = False self.vlab.run_for_ms(1.2) self.assertTrue(self.success) def test_should_charge_discharge_bq24160(self): print("charge usb") self.pin_number_should_toggle = 6 self.success = False self.vlab.bq24160.charge("usb") self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Charging from usb") self.pin_number_should_toggle = 8 self.success = False print("discharge") self.vlab.bq24160.discharge() self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Stopped charging") self.pin_number_should_toggle = 5 self.success = False print("charge in") self.success = False self.vlab.bq24160.charge("in") self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Charging from in") self.pin_number_should_toggle = 8 self.success = False print("discharge") self.vlab.bq24160.discharge() self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Stopped charging")