Toggle Navigation
Hatchery
Eggs
i2cflasher
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import i2c, machine, time, neopixel, easydraw neopixel.enable() i2c = machine.I2C(sda=machine.Pin(12), scl=machine.Pin(33), freq=1000000) try: directory = '/'.join(__file__.split('/')[:-1]) except: directory = '/' print("Current directory: {}".format(directory)) with open(directory+"/data.bin", "rb") as f: data = f.read() while True: try: easydraw.msg("Waiting for badge to be plugged in...", "I2C Flasher", 1) leddata = [0x10, 0x10, 0x00, 0x00]*6 neopixel.send(bytes(leddata)) print("Waiting for badge to be plugged in...") while len(i2c.scan()) < 1: time.sleep(0.1) start = time.ticks_ms() easydraw.msg("Badge found, writing EEPROM...", "I2C Flasher", 0) leddata = [0x00, 0x00, 0x10, 0x00]*6 neopixel.send(bytes(leddata)) print("Badge found, writing EEPROM...") addr = 0x50 success = True # meh, eigenlijk verkeer om, maar nu even makkelijker print("Writing...") for i in range(512): if (i % 64 == 0): easydraw.msg("(Writing step {})".format(i), "I2C Flasher", 0) print("(Writing step {})".format(i)) block = data[64*i:64*i+64] i2c.writeto_mem(addr, 64*i, block, addrsize=16) time.sleep(0.003) check = i2c.readfrom_mem(addr, 64*i, 64, addrsize=16) if block == check: #print("OK") pass else: success = False leddata = [0x00, 0x20, 0x00, 0x00]*6 neopixel.send(bytes(leddata)) easydraw.msg("Failure", "FAILURE!", 0) #easydraw.msg(("FAIL @ {}".format(i)), "FAILURE!!!", 1) print("FAIL @ {}".format(i)) #easydraw.msg(("Expected:", block)), "FAILURE!!!", 0) print("Expected:", block) #easydraw.msg(("Read:", check)), "FAILURE!!!", 0) print("Read:", check) time.sleep(1) break print("Done") end = time.ticks_ms() total = end-start print("Operation took {} ms.".format(total)) time.sleep(0.1) leddata = ([0x15, 0x00, 0x00, 0x00] if success else [0x00, 0x20, 0x00, 0x00]) * 6 neopixel.send(bytes(leddata)) easydraw.msg("Waiting for badge to be removed...", "I2C Flasher", 0) print("Waiting for badge to be removed...") while len(i2c.scan()): time.sleep(0.1) except BaseException as e: easydraw.msg(("Error", e), "FAILURE!!!", 1) print("Error", e) time.sleep(0.5)