<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def switch_mode(self, new_mode):
 """ Explicitly switch the Bus Pirate mode :param new_mode: The mode to switch to. Use the buspirate.MODE_* constants """

        packet = bytearray()
        packet.append(new_mode)
        self.device.write(packet)
        possible_responses = {
            self.MODE_I2C: b'I2C1',
            self.MODE_RAW: b'BBIO1',
            self.MODE_SPI: b'API1',
            self.MODE_UART: b'ART1',
            self.MODE_ONEWIRE: b'1W01'
        }
        expected = possible_responses[new_mode]
        response = self.device.read(4)
        if response != expected:
            raise Exception('Could not switch mode')
        self.mode = new_mode
        self.set_peripheral()
        if self.i2c_speed:
            self._set_i2c_speed(self.i2c_speed)