import ice40, display, buttons

# Find the directory this app resides in
try:
	directory = '/'.join(__file__.split('/')[:-1])
except:
	directory = ''

def draw(text, color):
	oneFourthWidth = display.width()//4
	width = display.getTextWidth(text, "ocra16")
	height = display.getTextHeight(text, "ocra16")
	display.drawFill(0x000000)
	display.drawText((oneFourthWidth*2) - ((width-4)//2), (display.height()-height)//2-2, text, color, "ocra16")
	display.flush()


draw("Loading bitstream...\n"+directory+"/chip.bin", 0xFFFF00)

try:
  with open(directory+"/chip.bin", "rb") as f:
	bitstream = f.read()
	try:
		ice40.load(bitstream)
		draw("Success", 0x00FF00)
	except:
	  draw("Failed", 0xFF0000)
except:
  draw("Failed to load bitstream!\n"+directory+"/chip.bin", 0xFF0000)