def set_time(self, t = None, format = None): if t == None: t = time.localtime() self.write_symbol('date', tools.mcutime(t, format).get())
def connect(self, port = None, rate = None, meta = None, sw = None): try: unpickled = pickle.load(open("gum.cache", 'rb')) except IOError: pass except EOFError: pass else: #if not port: port = unpickled['port'] if not rate: rate = unpickled['rate'] if not meta: meta = unpickled['meta'] if port == None: ports = ( '/dev/' + p for p in os.listdir('/dev/') if p.startswith('ttyUSB') or p.startswith('ttyS')) else: if type(port) == str: ports = ( port, ) else: try: ports = iter(port) except TypeError: ports = ( port, ) if not ports: raise Exception("No port found!") ports = tuple(ports) rates = ( 38400, 230400, 1000000, 115200, 9600 ) #rates = rates + tuple(reversed(sorted(tuple(set(uart.uart.BAUDRATES) - set(rates))))) if rate: rates = filter(lambda x: x != rate, rates) rates = tuple([rate]) + tuple(rates) try: for uart.uart.port in ports: build = False try: for uart.uart.baudrate in rates: uart.uart.open() try: build = uart.access(0, 0x100, bytearray(8)) break; except uart.ProtocolErr as inst: print("Failed:", uart.uart.port, uart.uart.baudrate, "reason:", inst) uart.uart.close() if uart.uart.baudrate == rates[-1]: raise else: continue except uart.ProtocolErr: if uart.uart.port == ports[-1]: raise else: continue if build: break except uart.ProtocolErr: print("Failed to autoconnect, defaulting to the first listed ...") uart.uart.port = ports[0] uart.uart.baudrate = rates[0] # # Meta # def extract_build(meta): return bytearray([ meta['macros']["BUILD_SEC" ], meta['macros']["BUILD_MIN" ], meta['macros']["BUILD_HOUR" ], meta['macros']["BUILD_WEEKDAY"], meta['macros']["BUILD_DAY" ], meta['macros']["BUILD_MONTH" ]] + list(tools.to_bytes(meta['macros']["BUILD_YEAR" ], 2))) sws = filter(lambda x: x[0:6] == "update" and x[-8:] == ".tar.bz2", os.listdir(".")) sws = sorted(sws, key=lambda x: os.stat(x).st_mtime) sws = list(reversed(sws)) if sw: sws = filter(lambda x: x == sw, sws) sws = tuple([sw]) + tuple(sws) def sw2meta(sw): pickled_meta = packer.getfile(sw, 'meta') if pickled_meta: return pickle.load(pickled_meta) else : return None meta_latest = None build_latest = bytearray(8) metafs = tuple([lambda: meta]) + tuple(map(lambda x: lambda: sw2meta(x), sws)) for metaf in metafs: meta_new = metaf() if meta_new: build_curr = extract_build(meta_new) if meta_new and build_curr == build: break elif bytearray(reversed(build_curr)) > bytearray(reversed(build_latest)): meta_latest = meta_new build_latest = build_curr if metaf == metafs[-1]: if not meta_latest: raise Exception("No meta found") print("Failed to find appropriate meta (", tools.mcutime(build), "). Failing back to latest found (", tools.mcutime(build_latest), ").") meta_new = meta_latest build = build_latest Gum.meta = meta_new uart.max_write_len = Gum.meta['symbols']['rx_buf']['size'] print("Connected to", uart.uart.port, " rate", uart.uart.baudrate, tools.mcutime(build)) if port and port != uart.uart.port: print("Port changed (", port , "->", uart.uart.port , ")") if rate and rate != uart.uart.baudrate: print("Rate changed (", rate , "->", uart.uart.baudrate , ")") if meta and meta != Gum.meta: print("Meta changed (", tools.mcutime(extract_build(meta)), "->", tools.mcutime(extract_build(Gum.meta)), ")")
#!/usr/bin/python3 import os import sys import subprocess import pickle import urllib.request from datetime import datetime import objdump import macrodump import xml_parse import tools import packer build_time = tools.mcutime().get() defines = { # "F_CPU" : 9216000, # "BAUD" : 230400, "F_CPU": 16000000, "BAUD": 38400, # "NDEBUG" : "", "__ASSERT_USE_STDERR": "", # I have my own assert right now anyway "__DELAY_BACKWARD_COMPATIBLE__": "", # compile error with 4.7.2 "PLAIN_CONSOLE": 0, "BUILD_SEC": build_time[0], "BUILD_MIN": build_time[1], "BUILD_HOUR": build_time[2], "BUILD_WEEKDAY": build_time[3], "BUILD_DAY": build_time[4],
def flash(self, fw_bin = None, bootloader_bin = None, update = None): body = el('body') #form = el("form") #form.text = "Firmware:" #form.attrib['method'] = 'post' #form.attrib['enctype'] = 'multipart/form-data' #file_input = el("input") #file_input.attrib['type'] = 'file' #file_input.attrib['name'] = 'fw_bin' #form.append(file_input) #submit = el("input") #submit.attrib['type'] = 'submit' #form.append(submit) #body.append(form) # #if fw_bin: # body.text = "Flashed fw with " + fw_bin.filename # gumi.flash_fw(fw_bin.file) # #form = el("form") #form.text = "Bootloader:" #form.attrib['method'] = 'post' #form.attrib['enctype'] = 'multipart/form-data' #file_input = el("input") #file_input.attrib['type'] = 'file' #file_input.attrib['name'] = 'bootloader_bin' #form.append(file_input) #submit = el("input") #submit.attrib['type'] = 'submit' #form.append(submit) #body.append(form) # #if bootloader_bin: # body.text = "Flashed bootloader with " + bootloader_bin.filename # gumi.flash_bootloader(bootloader_bin.file) if update: if update.filename[-8:] == ".tar.bz2": body.text = "Applying " + update.filename fn = update.filename[0:-8] + str(tools.mcutime()) + update.filename[-8:] sf = open(fn, mode='wb') sf.write(update.file.read()) sf.close() def update_thread(): packer.update(fn) time.sleep(1) os.execv(sys.argv[0], sys.argv) threading.Thread(target = update_thread).start() else: body.text = "File " + update.filename + " is not a valid update." else: form = el("form") form.text = "Update:" form.attrib['method'] = 'post' form.attrib['enctype'] = 'multipart/form-data' file_input = el("input") file_input.attrib['type'] = 'file' file_input.attrib['name'] = 'update' form.append(file_input) submit = el("input") submit.attrib['type'] = 'submit' form.append(submit) body.append(form) return self.skeleton(body)
#!/usr/bin/python3 import os import sys import subprocess import pickle import urllib.request from datetime import datetime import objdump import macrodump import xml_parse import tools import packer build_time = tools.mcutime().get() defines = { # "F_CPU" : 9216000, # "BAUD" : 230400, "F_CPU" : 16000000, "BAUD" : 38400, # "NDEBUG" : "", "__ASSERT_USE_STDERR" : "", # I have my own assert right now anyway "__DELAY_BACKWARD_COMPATIBLE__" : "", # compile error with 4.7.2 "PLAIN_CONSOLE" : 0, "BUILD_SEC" : build_time[0], "BUILD_MIN" : build_time[1], "BUILD_HOUR" : build_time[2], "BUILD_WEEKDAY" : build_time[3],