class MicroPythonBoard():
def __init__(self, port, baudrate):
self.serialPort=serial.Serial(port, baudrate)
def putCursorOnNewLine(self):
self.serialPort.write(b"\r")
def writeln(self, string):
""" Write a line to serial poart """
self.serialPort.write( bytes(string, "ascii"))
self.putCursorOnNewLine()
def write(self, string):
"""write a string to serial port """
self.serialPort.write( bytes(string, "ascii") )
def writeFile(self, pathToFile, fileName):
"""
pathToFile - path to file on your computer
fileName - Name of file on your MicroPython board
"""
with open(pathToFile) as file:
self.serialPort.write((chr(5).encode("ascii"))) # write <CTRL+E> (PASTE MODE)
self.serialPort.write((f'with(open("{fileName}", "w")) as f:').encode("ascii"))
self.putCursorOnNewLine()
self.serialPort.write(b" f.write(r'''")
self.putCursorOnNewLine()
for line in file.readlines():
self.serialPort.write(line.encode("ascii"))
self.putCursorOnNewLine()
self.serialPort.write(b"''')")
self.serialPort.write(chr(4).encode("ascii")) # write <CTRL+D> (CLOSE PASTE MODE)
print("DONE")
def readLastLine(self):
all_text=self.serialPort.read_all()
last_line=all_text.split(b"\r")[-1]
return last_line