示例#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.speaker = Speaker(**self.mock_kwargs)

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

        self.speaker._set_property = mock.Mock(side_effect=eval_set_property)
        self.speaker._get_property = mock.Mock()
        self.speaker._msg_send_q = mock.Mock()
示例#3
0
 def test_get_tune(self):
     """Test get_tune method with none input."""
     _ = self.speaker.tune
     sent_messages = []
     while not self.send_q.empty():
         sent_messages.append(self.send_q.get_nowait())
     self.assertTrue(
         Speaker.request_property(
             -1, Speaker.PropertyType.FREQUENCY) in sent_messages)
     self.assertTrue(
         Speaker.request_property(
             -1, Speaker.PropertyType.VOLUME) in sent_messages)
示例#4
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
示例#5
0
 def test_set_off(self):
     """Test set_off method"""
     expected_values = (0, 0)
     self.speaker.tune = expected_values
     sent_messages = []
     while not self.send_q.empty():
         sent_messages.append(self.send_q.get_nowait())
     self.assertTrue(
         Speaker.request_property(
             -1, Speaker.PropertyType.FREQUENCY) in sent_messages)
     self.assertTrue(
         Speaker.request_property(
             -1, Speaker.PropertyType.VOLUME) in sent_messages)
     self.assertTrue(
         parse_message(
             0x04, 16, -1, parse_data(
                 expected_values, 'float')) in sent_messages)
示例#6
0
 def test_set_tune(self):
     """Test set_tune method."""
     expected_values = frequency, volume = (
         self.speaker.Scale.F_RA_6.value, 30
     )
     self.speaker.tune = expected_values
     sent_messages = []
     while not self.send_q.empty():
         sent_messages.append(self.send_q.get_nowait())
     self.assertTrue(
         Speaker.request_property(
             -1, Speaker.PropertyType.FREQUENCY) in sent_messages)
     self.assertTrue(
         Speaker.request_property(
             -1, Speaker.PropertyType.VOLUME) in sent_messages)
     self.assertTrue(
         parse_message(
             0x04, 16, -1, parse_data(
                 expected_values, 'float')) in sent_messages)
示例#7
0
 def test_get_volume(self):
     """Test get_volume method with none input."""
     _ = self.speaker.volume
     self.assertEqual(
         self.send_q.get(),
         Speaker.request_property(-1, Speaker.PropertyType.VOLUME))
示例#8
0
 def test_get_frequency(self):
     """Test get_frequency method with none input."""
     _ = self.speaker.frequency
     self.assertEqual(
         self.send_q.get(),
         Speaker.request_property(-1, Speaker.PropertyType.FREQUENCY))
示例#9
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.speaker = Speaker(**self.mock_kwargs)
示例#10
0
class TestSpeaker(unittest.TestCase):
    """Tests for 'Speaker' class."""

    def setUp(self):
        """Set up test fixtures, if any."""
        self.mock_kwargs = {"id_": -1, "uuid": -1, "msg_send_q": None}
        self.speaker = Speaker(**self.mock_kwargs)

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

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

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

    @mock.patch.object(Speaker, "set_volume")
    @mock.patch.object(Speaker, "set_frequency")
    def test_set_tune(self, mock_set_frequency, mock_set_volume):
        """Test set_tune method."""
        expected_values = frequency, volume = (
            self.speaker.Scale.F_RA_6.value, 30
        )
        self.speaker.set_tune(*expected_values)

        expected_tune_params = (
            self.mock_kwargs["id_"],
            self.speaker.CommandType.SET_TUNE.value,
            expected_values,
            self.speaker.PropertyDataType.FLOAT,
        )
        self.speaker._set_property.assert_called_once_with(
            *expected_tune_params)
        self.speaker._msg_send_q.put.assert_called_once_with(
            self.speaker.CommandType.SET_TUNE.value
        )

        mock_set_frequency.assert_called_once_with()
        mock_set_volume.assert_called_once_with()

    @mock.patch.object(Speaker, "set_volume")
    @mock.patch.object(Speaker, "set_frequency")
    def test_set_tune_with_none(self, mock_set_frequency, mock_set_volume):
        """Test set_tune method with none input."""
        self.speaker.set_tune()

        mock_set_frequency.assert_called_once_with()
        mock_set_volume.assert_called_once_with()

    @mock.patch.object(Speaker, "set_tune")
    def test_set_frequency(self, mock_set_tune):
        """Test set_frequency method."""
        expeceted_frequency = 50
        self.speaker.set_frequency(frequency_value=expeceted_frequency)
        mock_set_tune.assert_called_once_with(
            frequency_value=expeceted_frequency)

    def test_set_frequency_with_none(self):
        """Test set_frequency method with none input."""
        self.speaker.set_frequency(frequency_value=None)
        self.speaker._get_property.assert_called_once_with(
            self.speaker.PropertyType.FREQUENCY
        )

    @mock.patch.object(Speaker, "set_tune")
    def test_set_volume(self, mock_set_tune):
        """Test set_volume method."""
        expeceted_volume = 50
        self.speaker.set_volume(volume_value=expeceted_volume)
        mock_set_tune.assert_called_once_with(volume_value=expeceted_volume)

    def test_set_volume_with_none(self):
        """Test set_volume method with none input."""
        self.speaker.set_volume(volume_value=None)
        self.speaker._get_property.assert_called_once_with(
            self.speaker.PropertyType.VOLUME
        )

    @mock.patch.object(Speaker, "set_tune")
    def test_set_off(self, mock_set_tune):
        """Test set_off method"""
        self.speaker.set_off()

        expeceted_values = frequency, volume = 0, 0
        mock_set_tune.assert_called_once_with(*expeceted_values)
示例#11
0
 def setUp(self):
     """Set up test fixtures, if any."""
     self.conn = MockConn()
     self.mock_kwargs = [-1, -1, self.conn]
     self.speaker = Speaker(*self.mock_kwargs)