示例#1
0
def initialize(display: Display, led: Led, speaker: Speaker,
               dial: Dial) -> int:
    """Initialize the movment of the ball

    :param display: Display module
    :param led:  Led module
    :param speaker: Speaker module
    :param dial: Dial module
    :return: Score
    """
    ball_pos = (20, 30)
    ball_vel = (1, -1)
    led.rgb = 0, 50, 0
    score = 0
    while True:
        bar_pos = int(50 * dial.degree / 100)
        ball_pos, ball_vel = update_screen(ball_pos, ball_vel, bar_pos,
                                           display)
        time.sleep(0.02)
        if ball_pos[1] > 55 and (ball_pos[0] > bar_pos + 10
                                 or ball_pos[0] < bar_pos - 10):
            led.rgb = 50, 0, 0
            break
        elif ball_pos[1] > 55:
            speaker.tune = 700, 100
            time.sleep(0.1)
            speaker.volume = 0
            score += 1
        display.clear()
    return score
示例#2
0
    def setUp(self):
        """Set up test fixtures, if any."""
        self.mock_kwargs = {"id_": -1, "uuid": -1, "msg_send_q": None}
        self.led = Led(**self.mock_kwargs)

        def eval_set_property(id, command_type, data):
            return command_type

        self.led._set_property = mock.Mock(side_effect=eval_set_property)
        self.led._get_property = mock.Mock()
        self.led._msg_send_q = mock.Mock()
示例#3
0
    def setUp(self):
        """Set up test fixtures, if any."""
        mock_input_values = (-1, -1, None)
        mock_modules = [
            SetupModule(*mock_input_values),
            InputModule(*mock_input_values),
            OutputModule(*mock_input_values),
            Button(*mock_input_values),
            Dial(*mock_input_values),
            Env(*mock_input_values),
            Gyro(*mock_input_values),
            Ir(*mock_input_values),
            Mic(*mock_input_values),
            Ultrasonic(*mock_input_values),
            Display(*mock_input_values),
            Led(*mock_input_values),
            Motor(*mock_input_values),
            Speaker(*mock_input_values),
        ]

        self.modi = MODI(nb_modules=len(mock_modules), test=True)

        self.modi._ser_proc = mock.Mock()
        self.modi._exe_thrd = mock.Mock()
        self.modi._modules = mock_modules
示例#4
0
 def test_set_green(self):
     """Test set_green method."""
     expected_color = (0, 20, 0)
     set_message = parse_message(0x04, 16, -1, parse_data(
         expected_color, 'int'))
     sent_messages = []
     self.led.green = 20
     while not self.send_q.empty():
         sent_messages.append(self.send_q.get_nowait())
     self.assertTrue(Led.request_property(
         -1, Led.PropertyType.RED) in sent_messages)
     self.assertTrue(Led.request_property(
         -1, Led.PropertyType.GREEN) in sent_messages)
     self.assertTrue(Led.request_property(
         -1, Led.PropertyType.BLUE) in sent_messages)
     self.assertTrue(set_message in sent_messages)
示例#5
0
 def test_set_rgb(self):
     """Test set_rgb method with user-defined inputs."""
     expected_color = (10, 100, 200)
     self.led.rgb = expected_color
     set_message = parse_message(0x04, 16, -1, parse_data(
         expected_color, 'int'))
     sent_messages = []
     while not self.send_q.empty():
         sent_messages.append(self.send_q.get_nowait())
     self.assertTrue(Led.request_property(
         -1, Led.PropertyType.RED) in sent_messages)
     self.assertTrue(Led.request_property(
         -1, Led.PropertyType.GREEN) in sent_messages)
     self.assertTrue(Led.request_property(
         -1, Led.PropertyType.BLUE) in sent_messages)
     self.assertTrue(set_message in sent_messages)
示例#6
0
class TestLed(unittest.TestCase):
    """Tests for 'Led' class."""
    @staticmethod
    def to_true_color(color):
        return tuple(map(lambda c: c * 100 // 255, color))

    def setUp(self):
        """Set up test fixtures, if any."""
        self.conn = MockConn()
        self.mock_kwargs = -1, -1, self.conn
        self.led = Led(*self.mock_kwargs)

    def tearDown(self):
        """Tear down test fixtures, if any."""
        del self.led

    def test_set_rgb(self):
        """Test set_rgb method with user-defined inputs."""
        expected_color = (10, 20, 255)
        self.led.rgb = expected_color
        set_message = parse_message(
            0x04, 16, -1, parse_data(self.to_true_color(expected_color),
                                     'int'))
        sent_messages = []
        while self.conn.send_list:
            sent_messages.append(self.conn.send_list.pop())
        self.assertTrue(set_message in sent_messages)

    def test_on(self):
        """Test on method."""
        expected_color = (255, 255, 255)
        self.led.rgb = expected_color
        set_message = parse_message(
            0x04, 16, -1, parse_data(self.to_true_color(expected_color),
                                     'int'))
        sent_messages = []
        while self.conn.send_list:
            sent_messages.append(self.conn.send_list.pop())
        self.assertTrue(set_message in sent_messages)

    def test_off(self):
        """Test off method."""
        expected_color = (0, 0, 0)
        self.led.turn_on()
        self.led.turn_off()
        set_message = parse_message(
            0x04, 16, -1, parse_data(self.to_true_color(expected_color),
                                     'int'))
        sent_messages = []
        while self.conn.send_list:
            sent_messages.append(self.conn.send_list.pop())
        self.assertTrue(set_message in sent_messages)

    def test_set_red(self):
        """Test set_red method."""
        expected_color = (20, 0, 0)
        set_message = parse_message(
            0x04, 16, -1, parse_data(self.to_true_color(expected_color),
                                     'int'))
        sent_messages = []
        self.led.red = 20
        while self.conn.send_list:
            sent_messages.append(self.conn.send_list.pop())
        self.assertTrue(set_message in sent_messages)

    def test_get_red(self):
        """Test get_red method with none input."""
        _ = self.led.red
        self.assertEqual(
            self.conn.send_list[0],
            parse_message(0x03, 0, -1,
                          (Led.RED, None, self.led.prop_samp_freq, None)))

    def test_set_green(self):
        """Test set_green method."""
        expected_color = (0, 20, 0)
        set_message = parse_message(
            0x04, 16, -1, parse_data(self.to_true_color(expected_color),
                                     'int'))
        sent_messages = []
        self.led.green = 20
        while self.conn.send_list:
            sent_messages.append(self.conn.send_list.pop())
        self.assertTrue(set_message in sent_messages)

    def test_get_green(self):
        """Test set_green method with none input."""
        _ = self.led.green
        self.assertEqual(
            self.conn.send_list[0],
            parse_message(0x03, 0, -1,
                          (Led.GREEN, None, self.led.prop_samp_freq, None)))

    def test_set_blue(self):
        """Test blue method."""
        expected_color = (0, 0, 20)
        set_message = parse_message(
            0x04, 16, -1, parse_data(self.to_true_color(expected_color),
                                     'int'))
        sent_messages = []
        self.led.blue = 20
        while self.conn.send_list:
            sent_messages.append(self.conn.send_list.pop())
        self.assertTrue(set_message in sent_messages)

    def test_get_blue(self):
        """Test get blue method with none input."""
        _ = self.led.blue
        self.assertEqual(
            self.conn.send_list[0],
            parse_message(0x03, 0, -1,
                          (Led.BLUE, None, self.led.prop_samp_freq, None)))
示例#7
0
 def setUp(self):
     """Set up test fixtures, if any."""
     self.conn = MockConn()
     self.mock_kwargs = -1, -1, self.conn
     self.led = Led(*self.mock_kwargs)
示例#8
0
class TestLed(unittest.TestCase):
    """Tests for 'Led' class."""
    def setUp(self):
        """Set up test fixtures, if any."""
        self.mock_kwargs = {"id_": -1, "uuid": -1, "msg_send_q": None}
        self.led = Led(**self.mock_kwargs)

        def eval_set_property(id, command_type, data):
            return command_type

        self.led._set_property = mock.Mock(side_effect=eval_set_property)
        self.led._get_property = mock.Mock()
        self.led._msg_send_q = mock.Mock()

    def tearDown(self):
        """Tear down test fixtures, if any."""
        del self.led

    @mock.patch.object(Led, "set_blue")
    @mock.patch.object(Led, "set_green")
    @mock.patch.object(Led, "set_red")
    def test_set_rgb(self, mock_set_red, mock_set_green, mock_set_blue):
        """Test set_rgb method with user-defined inputs."""
        expected_color = (10, 100, 200)
        self.led.set_rgb(*expected_color)

        expected_rgb_params = (
            self.mock_kwargs["id_"],
            self.led.CommandType.SET_RGB.value,
            expected_color,
        )
        self.led._set_property.assert_called_once_with(*expected_rgb_params)
        self.led._msg_send_q.put.assert_called_once_with(
            self.led.CommandType.SET_RGB.value)

        mock_set_red.assert_called_once_with()
        mock_set_green.assert_called_once_with()
        mock_set_blue.assert_called_once_with()

    @mock.patch.object(Led, "set_blue")
    @mock.patch.object(Led, "set_green")
    @mock.patch.object(Led, "set_red")
    def test_set_rgb_with_none(self, mock_set_red, mock_set_green,
                               mock_set_blue):
        """Test set_rgb method with none input"""
        self.led.set_rgb()

        # TODO: Ensure set_property and q.put have NOT been called
        # self.led._set_property.assert_not_called_once_with()
        # self.led._serial_write_q.put.assert_not_called_once_with()

        mock_set_red.assert_called_once_with()
        mock_set_green.assert_called_once_with()
        mock_set_blue.assert_called_once_with()

    @mock.patch.object(Led, "set_rgb")
    def test_on(self, mock_set_rgb):
        """Test on method."""
        self.led.set_on()

        expected_color = (255, 255, 255)
        mock_set_rgb.assert_called_once_with(*expected_color)

    @mock.patch.object(Led, "set_rgb")
    def test_off(self, mock_set_rgb):
        """Test off method."""
        self.led.set_off()

        expected_color = (0, 0, 0)
        mock_set_rgb.assert_called_once_with(*expected_color)

    @mock.patch.object(Led, "set_rgb")
    def test_set_red(self, mock_set_rgb):
        """Test set_red method."""
        expected_color = self.led.PropertyType.RED.value
        self.led.set_red(red=expected_color)
        mock_set_rgb.assert_called_once_with(red=expected_color)

    def test_set_red_with_none(self):
        """Test set_red method with none input."""
        self.led.set_red(red=None)
        self.led._get_property.assert_called_once_with(
            self.led.PropertyType.RED)

    @mock.patch.object(Led, "set_rgb")
    def test_get_green(self, mock_set_rgb):
        """Test set_green method."""
        expected_color = self.led.PropertyType.GREEN.value
        self.led.set_green(green=expected_color)
        mock_set_rgb.assert_called_once_with(green=expected_color)

    def test_get_green_with_none(self):
        """Test set_green method with none input."""
        self.led.set_green(green=None)
        self.led._get_property.assert_called_once_with(
            self.led.PropertyType.GREEN)

    @mock.patch.object(Led, "set_rgb")
    def test_get_blue(self, mock_set_rgb):
        """Test blue method."""
        expected_color = self.led.PropertyType.BLUE.value
        self.led.set_blue(blue=expected_color)
        mock_set_rgb.assert_called_once_with(blue=expected_color)

    def test_get_blue_with_none(self):
        """Test blue method with none input."""
        self.led.set_blue(blue=None)
        self.led._get_property.assert_called_once_with(
            self.led.PropertyType.BLUE)
示例#9
0
 def test_get_red(self):
     """Test get_red method with none input."""
     _ = self.led.red
     self.assertEqual(
         self.send_q.get(),
         Led.request_property(-1, Led.PropertyType.RED))
示例#10
0
 def test_get_blue(self):
     """Test get blue method with none input."""
     _ = self.led.blue
     self.assertEqual(
         self.send_q.get(),
         Led.request_property(-1, Led.PropertyType.BLUE))
示例#11
0
 def test_get_green(self):
     """Test set_green method with none input."""
     _ = self.led.green
     self.assertEqual(
         self.send_q.get(),
         Led.request_property(-1, Led.PropertyType.GREEN))
示例#12
0
 def setUp(self):
     """Set up test fixtures, if any."""
     self.send_q = Queue()
     self.mock_kwargs = {"id_": -1, "uuid": -1, "msg_send_q": self.send_q}
     self.led = Led(**self.mock_kwargs)