import can_iotv_v2_1 as iotv
import asyncio
from time import sleep

DIG_VERT_ADDR = "0100"
SIDE = "B"
led_DO7_on = True
if 'aturat' not in globals(): # 'aturat' variable exists only on Pyodide simulator. 'aturat' means 'stopped'
    aturat = False # Running on Rasperry Pi. It is not running on Pyodide simulator
    
async def main():
	global led_DO7_on
	
	print(f"DO7 blinking on {SIDE} rib at {DIG_VERT_ADDR} address vertebra")
	while not aturat:
		if led_DO7_on:
			iotv.doutbit(DIG_VERT_ADDR, "b", 7, 1)
		else:
			iotv.doutbit(DIG_VERT_ADDR, "b", 7, 0)
		led_DO7_on = not led_DO7_on
		await asyncio.sleep(0.5)

if __name__ == "__main__":
	try:
		iotv.can_on()
		
		# Execute the asynchronous function through the asyncio manager
		asyncio.run(main())

	except KeyboardInterrupt:
		iotv.dout(DIG_VERT_ADDR, 'B', 0x00)
		print("\nProgram stopped by user.")
		sleep(0.1)
		iotv.can_off()
		
