import os
import platform
import sys
import time

if platform.system().lower() == "windows":
    sys.path.append(f"{os.getcwd()}\src\components")
from src.components.usb_586x import USB_586x
from lib.helpers import ConfigReader


test_config = ConfigReader(system_id="test-linux")

if "USB-5862" in test_config["digital_io"]["id"]:
    out_size = in_size = 16
if "USB-5860" in test_config["digital_io"]["id"]:
    out_size = in_size = 8

digital_io = USB_586x(test_config)
digital_io.start()
# READ CONFIG

out_num = 0
n_in_bytes = int(in_size/8)
while True:
    out_bit = out_num % 8
    out_byte = int(out_num/8)
    res = digital_io.set_bit(out_byte, out_bit, 1)
    time.sleep(0.5)
    res2 = digital_io.set_bit(out_byte, out_bit, 0)
    input_data = digital_io.get()
    if "--5862" in sys.argv:
        print("in:{} {} out:{}".format(bytes(input_data[0]),bytes(input_data[1]), out_num))
    else:
        print("in:{} out:{}".format(bytes(input_data[0]), out_num))
    time.sleep(0.5)
    out_num += 1
    if out_num == out_size:
        out_num = 0