from spidev import SpiDev

dev = SpiDev(0,0)
dev.max_speed_hz = 7800000

resp = dev.xfer([0xA3])

time_sec = 0x74749054

while True:
	resp = dev.xfer([0xA3])
	if resp == [0xA0] or resp == [0xA5]:
		print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()

		resp = dev.xfer([0xA2]);
		print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()


		# first_stage_hash - 256 bits
		resp = dev.xfer([0x09, 0xA0, 0xD1, 0x91, 0x92, 0xEF, 0x77, 0xC3, 0x04, 0xFE, 0x44,
		0x78, 0x88, 0xF9, 0xEF, 0x50, 0x69, 0xD6, 0x48, 0x46, 0x5A, 0x19, 0x14, 0x6F, 0xB7, 0x70,
		0x61, 0x97, 0x14, 0xD0, 0x89, 0x04]);
		print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()

		# input_M - 96 bits
		#resp = dev.xfer([0x45, 0xF4, 0x99, 0x2E, 0x74, 0x74, 0x90, 0x54, 0x74, 0x7B, 0x1B, 0x18]);
		resp = dev.xfer([0x45, 0xF4, 0x99, 0x2E])
		print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()

		# time
		print [time_sec >> i & 0xff for i in (24,16,8,0)]
		resp = dev.xfer([time_sec >> i & 0xff for i in (24,16,8,0)]);
		print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()

		# bits
		resp = dev.xfer([0x74, 0x7B, 0x1B, 0x18]);
		print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()

		# previous block - 256 bits
		resp = dev.xfer([
		0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0xA2, 0x94, 0x08, 0x84, 0xE0, 0xC3, 0xBC, 0x96,
		0x51, 0x0C, 0xAD, 0x11, 0x91, 0x2A, 0x52, 0x7E,
		0x9D, 0x15, 0xDF, 0x42, 0xF0, 0xE1, 0xD6, 0x72]);

		print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()

		while True:
			resp = dev.xfer([0xA3]);
			print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()
			if resp == [0xA5] or resp == [0xA6]:
				break

		if resp == [0xA6]:
			resp = dev.xfer([0xA4, 0xA4])
			print "%s" % ''.join(["0x%02X " % x for x in resp]).strip()

			resp = []
			for i in range(0, 36):
				resp += dev.xfer([0xA7])

			print "xxx: %s" % ''.join(["0x%02X " % x for x in resp]).strip()
		else:
			print "Done. Nothing found"
		
		time_sec = time_sec + 1 # increment unconditionally


