# Use pip install VBMBPyLib.whl to install the VBMB Python Library
from VBMBPyLib import VTC
import time

# It is assumed that the VTC is already connected and the serial port is not held by another application
# If multiple VTC are connected, first available VTC port will be opened
vtc = VTC.VTC()

# On successful reset, the VTC LEDs will turn off and on again
#print(vtc.Reset())

# Uncomment commands as necessary. All commands are blocking.

# ----------------------Printing VTC system info--------------------------
print(vtc.GetFWVersion().retval)
print(vtc.GetModbusID().retval)
print(vtc.GetHardwareID().retval)
print(vtc.GetSystemStatus().retval)
print(vtc.GetDriverStatus(123).retval)
print(vtc.GetLEDStatus().retval)
# ------------------------------------------------------------------------

# ---------------------Enable/Disable VTC channels-----------------------
# print(vtc.ControlChannel(channel= 1, state=True).status)
# print(vtc.ControlChannel(channel=2, state=False).status)
# print(vtc.ControlChannel(channel=3, state=False).status)
# print(vtc.ControlChannel(channel=4, state=False).status)
# print(vtc.ControlChannel(channel=5, state=False).status)
# print(vtc.ControlChannel(channel=6, state=False).status)
# ------------------------------------------------------------------------

# ---------------------Constant Voltage Mode------------------------------------------------------------------------
# Setting the VTC Channel 1 voltage setpoint in Constant voltage mode, reading the setpoint and
# polling for channel voltage, current and temperature every second

# print(vtc.SetTargetVoltage(channel=1, voltage=0).status)
# print(vtc.GetTargetVoltage(channel=1).retval)
# while True:
#        print(vtc.GetActualTemp(channel=1).retval)
#        print(vtc.GetActualVoltage(channel=1).retval)
#        print(vtc.GetActualCurrent(channel=1).retval)
#        time.sleep(1)
# ------------------------------------------------------------------------------------------------------------------

# ---------------------Constant Current Mode------------------------------------------------------------------------
# Same as above. Only current setpoint is set instead for Constant current mode

# print(vtc.SetTargetCurrent(channel=1, current=0).status)
# print(vtc.GetTargetCurrent(channel=1).retval)
# ------------------------------------------------------------------------------------------------------------------

# ---------------------Constant Temperature Mode--------------------------------------------------------------------
# Same as above. Only temperature and ramp setpoint is set instead for Constant Temperature mode

# print(vtc.SetTargetTemp(channel=1, temp=45, ramp=1).status)
# print(vtc.GetTargetTemp(channel=1).retval)
# print(vtc.GetTargetRamp(channel=1).retval)
# print(vtc.GetActualTemp(channel=1).retval)
# print(vtc.SetAutoOffTimer(channel=1,timeout=50).status)
# ------------------------------------------------------------------------------------------------------------------

# ---------------------Creating a new profile number 5--------------------------------------------------------------
# Created profile will be profile number 5 and will have the name "Test Profile"
# Profile structure would be as follows:
# Step 0 --> Step 1 --> Step 2 --> Step 3 --> Step 2 --> Step 3 --> Step 2 --> Step 3 --> Step 4

#print(vtc.TPReset(profile=5).status)
#print(vtc.TPSetName(profile=5, name="Test Profile").status)
#print(vtc.TPSetHCTemp(profile=5, temp=50).retval)
#print(vtc.TPGetHCTemp(profile=5).retval)
#print(vtc.TPSetStepData(profile=5, step=0, tm0=10, tm1=10, ramp=30, duration=10).status)
#print(vtc.TPSetStepData(profile=5, step=1, tm0=40, tm1=40, ramp=4, duration=35).status)
#print(vtc.TPSetStepData(profile=5, step=2, tm0=10, tm1=10, ramp=1, duration=50).status)
#print(vtc.TPSetStepData(profile=5, step=3, tm0=40, tm1=40, ramp=1, duration=50).status)
#print(vtc.TPSetStepData(profile=5, step=4, tm0=10, tm1=10, ramp=2, duration=40).status)
#print(vtc.TPSetRepeat(profile=5, start=2, end=3, repeat=2).status)
# ------------------------------------------------------------------------------------------------------------------

# -----------------------Get Thermal Profile data-------------------------------------------------------------------
# Get the details of Thermal Profile 5 as a python dictionary
#
# print(vtc.TPGetStepData(profile=5,step=0).retval)
# print(vtc.TPGetStepData(profile=5, step=1).retval)
# print(vtc.TPGetStepData(profile=5, step=2).retval)
# print(vtc.TPGetStepData(profile=5, step=3).retval)
# print(vtc.TPGetStepData(profile=5, step=4).retval)
# print(vtc.TPGetAllSteps(profile=5).retval)
# print(vtc.TPGetInfo(profile=5).retval)
# ------------------------------------------------------------------------------------------------------------------

# ------------------------Thermal Profile Mode----------------------------------------------------------------------
# Run Thermal profile 5 on channel 1 and poll for status every second

# print(vtc.SetThermalProfile(channel=1, profile=5))
# print(vtc.ExecuteThermalProfile(profile=5, action=0))
# print(vtc.GetThermalProfile(channel = 1))
# while True:
#   print(vtc.TPGetStatus(profile=1).retval)
#   time.sleep(1)
# ------------------------------------------------------------------------------------------------------------------

# ------------------------Autopush Data-----------------------------------------------------------------------------
# Configure the VTC to push all 6 channel TEC data every second.
# Since this command is blocking, it is best to handle the response in a separate thread with appropriate
# thread synchronization or turn off Autopush before trying to run another command
#
# print(vtc.SetDataMonitoring(data_mask=63, interval=1000).status)
# while True:
#    print(vtc.receive())
# ------------------------------------------------------------------------------------------------------------------

# ------------------------Fan control-------------------------------------------------------------------------------
# Enable all 3 VTC fans
#
# print(vtc.ControlFan(fan_id=1, enable=True).status)
# print(vtc.ControlFan(fan_id=2, enable=True).status)
# print(vtc.ControlFan(fan_id=3, enable=True).status)
# ------------------------------------------------------------------------------------------------------------------


# ------------------------PID Params-------------------------------------------------------------------------------
# Set PID params for channel 2, read the new values and save them to the EEPROM
#
# print(vtc.PIDSetParams(channel=2, param_id=1, value = 0.5).status)
# print(vtc.PIDSetParams(channel=2, param_id=2, value = 0).status)
# print(vtc.PIDSetParams(channel=2, param_id=3, value = 0.5).status)

# print(vtc.PIDGetParams(channel=2, param_id=1).retval)
# print(vtc.PIDSetParams(channel=2, param_id=1).retval)
# print(vtc.PIDSetParams(channel=2, param_id=1).retval)
#
# print(vtc.PIDSaveToEEPROM(channel=2).status)
# ------------------------------------------------------------------------------------------------------------------
