#================================================================
#================================================================
# API-AIO(WDM)
# Timer 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 notification count in the callback


#========================================
# Function name    isnum
# Parameter        str     String
# Parameter        base    Base
# Details          Function to check 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    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 notification count 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 the 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()