#================================================================
#================================================================
# API-AIO(WDM)
# Counter sample
# Common function file
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import ctypes
import ctypes.wintypes
import threading
import subprocess
import caio
#----------------------------------------
# Declaration of global variables
#----------------------------------------
g_lock = threading.Lock()         # Mutex
g_info_flag = ctypes.c_short()    # A flag showing the compare match count, count value, status in the callback


#========================================
# Function name    isnum
# Parameter        str     String
# Parameter        base    Base
# Details          Function to checks if a string can be converted to a number
# Return value     True: Can be converted
#                  False: Can not be converted
#========================================
def isnum(str, base):
    try:
        if 10 == base:
            int(str, 10)
        else:
            float(str)
    except:
        return False
    return True


#========================================
# Function name    GetStatusString
# Parameter        cnt_status           Status
# Details          Function to get status string
# Return value     cnt_status_string    Status string
#========================================
def GetStatusString(cnt_status):
    #----------------------------------------
    # Create counter input status string
    #----------------------------------------
    #--------------------
    # When status 'Counter in operation' is ON
    #--------------------
    if (cnt_status.value & caio.CNTS_BUSY) == caio.CNTS_BUSY:
        cnt_status_string = "Operating"
    #--------------------
    # When status 'Counter in operation' is OFF
    #--------------------
    else:
        cnt_status_string = "Stop"
    #--------------------
    # When status 'Comparison count match' is ON
    #--------------------
    if (cnt_status.value & caio.CNTS_DATA_NUM) == caio.CNTS_DATA_NUM:
        cnt_status_string += ", Comparison count match"
    #--------------------
    # When status 'Overrun' is ON
    #--------------------
    if (cnt_status.value & caio.CNTS_ORERR) == caio.CNTS_ORERR:
        cnt_status_string += ", Overrun"
    #--------------------
    # When status 'Counter operation error' is ON
    #--------------------
    if (cnt_status.value & caio.CNTS_ERR) == caio.CNTS_ERR:
        cnt_status_string += ", Counter operation error"
    return cnt_status_string


#========================================
# Function name    PrintOnOneLine
# Parameter        buf    String to display
# Details          Display string in one line,
#                  move the cursor to a fixed position
# Return value     None
#========================================
def PrintOnOneLine(buf):
    global g_lock         # Mutex
    global g_info_flag    # A flag showing the compare match count, count value, status in the callback

    #----------------------------------------
    # Lock
    #----------------------------------------
    g_lock.acquire()
    #----------------------------------------
    # Specify the default shell
    #----------------------------------------
    subprocess.run('', shell=True)
    #----------------------------------------
    # Erase from cursor position to bottom right of screen
    #----------------------------------------
    print("\033[0J", end='', flush=True)
    #----------------------------------------
    # Save the cursor position
    #----------------------------------------
    print("\033[s", end='', flush=True)
    #----------------------------------------
    # Display the string
    #----------------------------------------
    print(buf, end='\r', flush=True)
    #----------------------------------------
    # Restore the cursor position
    #----------------------------------------
    print("\033[u", end='', flush=True)
    #----------------------------------------
    # Move cursor up one line
    #----------------------------------------
    if g_info_flag.value == 0:
        print("\033[1A", end='', flush=True)
    g_info_flag.value = 1
    #----------------------------------------
    # Unlock
    #----------------------------------------
    g_lock.release()