def test_get_serial_no_port(): """ An IOError should be raised if no micro:bit is found. """ with mock.patch('microfs.find_microbit', return_value=(None, None)): with pytest.raises(IOError) as ex: microfs.get_serial() assert ex.value.args[0] == 'Could not find micro:bit.'
def test_get_serial(): """ Ensure that if a port is found then PySerial is used to create a connection to the device. """ mock_serial = mock.MagicMock() with mock.patch('microfs.find_microbit', return_value='/dev/ttyACM3'), \ mock.patch('microfs.Serial', return_value=mock_serial): result = microfs.get_serial() assert result == mock_serial
def test_get_serial(): """ Ensure that if a port is found then PySerial is used to create a connection to the device. """ mock_serial = mock.MagicMock() mock_result = ('/dev/ttyACM3', '9900000031864e45003c10070000006e0000000097969901') with mock.patch('microfs.find_microbit', return_value=mock_result), \ mock.patch('microfs.Serial', return_value=mock_serial): result = microfs.get_serial() assert result == mock_serial
class SerialSystem: ser = microfs.get_serial() ser.baudrate = 115200 ser.close() microbitpath = uflash.find_microbit() before = { 'Buttons': { 'A': False, 'B': False }, 'Temp': 20, 'CompassHeading': 369, 'Accelerometer': { 'Y': 0, 'X': 0, 'Z': 0 }, 'Brightness': 0 } def close(self): self.ser.close() def open(self): try: self.ser.open() except serial.serialutil.SerialException as e: print(str(e)) # FIXA def readyMicroBit(self, printb=False): # shutil.copyfile(os.getcwd() + "\\src\\" + "microbitserialsystem.hex", self.microbitpath+"SerialSystem.hex") # shutil.copy if printb: print("Downloading HEX") url = readymicrobithexurl r = requests.get(url) if printb: print("Downloaded HEX") print("Fixing HEX") content = "" # contentb = r.content # contentb = str(contentb) # contentb = contentb[:-1] # contentb = contentb[2:] # contentsplit = contentb.split("\\n") # for i in contentsplit: # content = content + i + "\n" content = r.content.decode("UTF-8") if printb: print("Fixed HEX\n" + content) print("Moving HEX to microbit") try: file = open("SerialSystem.hex", "w") file.write(content) file.close() shutil.move("SerialSystem.hex", uflash.find_microbit() + "SerialSystem.hex") except shutil.Error as e: print(e) print("SerialSystem hex already installed") os.remove(os.getcwd() + "\\SerialSystem.hex") if printb: print("Moved HEX to microbit") def display(self, screen=[[5, 6, 7, 6, 5], [6, 7, 8, 7, 6], [7, 8, 9, 8, 7], [6, 7, 8, 7, 6], [5, 6, 7, 6, 5]]): fixedscreen = [0, 0, 0, 0, 0] yn = 0 for y in screen: tempstring = "" xn = 0 for x in y: tempstring = tempstring + str(screen[yn][xn]) xn = xn + 1 if y != 4: tempstring = tempstring + ":" fixedscreen[yn] = tempstring yn = yn + 1 try: self.ser.open() except serial.serialutil.SerialException as e: pass # print(str(e)) self.ser.write(bytes("?" + str(fixedscreen).replace(" ", ""), 'utf-8')) def readRaw(self): try: self.ser.open() except serial.serialutil.SerialException as e: pass # print(str(e)) data_raw = self.ser.readline() try: if data_raw != b'': return (data_raw.decode("utf-8")) except UnicodeEncodeError as e: print("Skipping because of error: " + str(e)) def read(self): try: self.ser.open() except serial.serialutil.SerialException as e: pass # print(str(e)) try: mbd = self.ser.readline().decode("UTF-8") mbd = mbd.replace("'", '"') except serial.serialutil.SerialException: return {"Error": "Can not find MicroBit"} except UnicodeDecodeError as e: mbd = "" time.sleep(0.5) print(e) except IndexError: return {"Error": "Unknown error"} if mbd.startswith("?"): mbds = mbd.split("?") try: result = json.loads(mbds[1]) if result["Buttons"]["A"] == "true": result["Buttons"]["A"] = True elif result["Buttons"]["A"] == "false": result["Buttons"]["A"] = False if result["Buttons"]["B"] == "true": result["Buttons"]["B"] = True elif result["Buttons"]["B"] == "false": result["Buttons"]["B"] = False except json.decoder.JSONDecodeError as e: print(mbds[1]) print(e) result = { "Error": "Cant convert Json (MicroBit probably is using wrong firmware)" } else: result = self.before self.before = result return result