#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy3 Experiment Builder (v2024.2.4),
    on March 05, 2025, at 11:49
If you publish work using this script the most relevant publication is:

    Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019) 
        PsychoPy2: Experiments in behavior made easy Behav Res 51: 195. 
        https://doi.org/10.3758/s13428-018-01193-y

"""

# --- Import packages ---
from psychopy import locale_setup
from psychopy import prefs
from psychopy import plugins
plugins.activatePlugins()
from psychopy import sound, gui, visual, core, data, event, logging, clock, colors, layout, hardware
from psychopy.tools import environmenttools
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
                                STOPPED, FINISHED, PRESSED, RELEASED, FOREVER, priority)

import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
                   sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle, choice as randchoice
import os  # handy system and path functions
import sys  # to get file system encoding

import psychopy.iohub as io
from psychopy.hardware import keyboard

# --- Setup global variables (available in all functions) ---
# create a device manager to handle hardware (keyboards, mice, mirophones, speakers, etc.)
deviceManager = hardware.DeviceManager()
# ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
# store info about the experiment session
psychopyVersion = '2024.2.4'
expName = 'TPad serial readline long timeout v36'  # from the Builder filename that created this script
# information about this experiment
expInfo = {
    'participant': f"{randint(0, 999999):06.0f}",
    'session': '001',
    'date|hid': data.getDateStr(),
    'expName|hid': expName,
    'psychopyVersion|hid': psychopyVersion,
}

# --- Define some variables which will change depending on pilot mode ---
'''
To run in pilot mode, either use the run/pilot toggle in Builder, Coder and Runner, 
or run the experiment with `--pilot` as an argument. To change what pilot 
#mode does, check out the 'Pilot mode' tab in preferences.
'''
# work out from system args whether we are running in pilot mode
PILOTING = core.setPilotModeFromArgs()
# start off with values from experiment settings
_fullScr = True
_winSize = [2560, 1440]
# if in pilot mode, apply overrides according to preferences
if PILOTING:
    # force windowed mode
    if prefs.piloting['forceWindowed']:
        _fullScr = False
        # set window size
        _winSize = prefs.piloting['forcedWindowSize']

def showExpInfoDlg(expInfo):
    """
    Show participant info dialog.
    Parameters
    ==========
    expInfo : dict
        Information about this experiment.
    
    Returns
    ==========
    dict
        Information about this experiment.
    """
    # show participant info dialog
    dlg = gui.DlgFromDict(
        dictionary=expInfo, sortKeys=False, title=expName, alwaysOnTop=True
    )
    if dlg.OK == False:
        core.quit()  # user pressed cancel
    # return expInfo
    return expInfo


def setupData(expInfo, dataDir=None):
    """
    Make an ExperimentHandler to handle trials and saving.
    
    Parameters
    ==========
    expInfo : dict
        Information about this experiment, created by the `setupExpInfo` function.
    dataDir : Path, str or None
        Folder to save the data to, leave as None to create a folder in the current directory.    
    Returns
    ==========
    psychopy.data.ExperimentHandler
        Handler object for this experiment, contains the data to save and information about 
        where to save it to.
    """
    # remove dialog-specific syntax from expInfo
    for key, val in expInfo.copy().items():
        newKey, _ = data.utils.parsePipeSyntax(key)
        expInfo[newKey] = expInfo.pop(key)
    
    # data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
    if dataDir is None:
        dataDir = _thisDir
    filename = u'data/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])
    # make sure filename is relative to dataDir
    if os.path.isabs(filename):
        dataDir = os.path.commonprefix([dataDir, filename])
        filename = os.path.relpath(filename, dataDir)
    
    # an ExperimentHandler isn't essential but helps with data saving
    thisExp = data.ExperimentHandler(
        name=expName, version='',
        extraInfo=expInfo, runtimeInfo=None,
        originPath='D:\\PsychoPy TPad\\FINAL 3\\TPad serial readline long timeout v36_lastrun.py',
        savePickle=True, saveWideText=True,
        dataFileName=dataDir + os.sep + filename, sortColumns='time'
    )
    thisExp.setPriority('thisRow.t', priority.CRITICAL)
    thisExp.setPriority('expName', priority.LOW)
    # return experiment handler
    return thisExp


def setupLogging(filename):
    """
    Setup a log file and tell it what level to log at.
    
    Parameters
    ==========
    filename : str or pathlib.Path
        Filename to save log file and data files as, doesn't need an extension.
    
    Returns
    ==========
    psychopy.logging.LogFile
        Text stream to receive inputs from the logging system.
    """
    # set how much information should be printed to the console / app
    if PILOTING:
        logging.console.setLevel(
            prefs.piloting['pilotConsoleLoggingLevel']
        )
    else:
        logging.console.setLevel('warning')
    # save a log file for detail verbose info
    logFile = logging.LogFile(filename+'.log')
    if PILOTING:
        logFile.setLevel(
            prefs.piloting['pilotLoggingLevel']
        )
    else:
        logFile.setLevel(
            logging.getLevel('error')
        )
    
    return logFile


def setupWindow(expInfo=None, win=None):
    """
    Setup the Window
    
    Parameters
    ==========
    expInfo : dict
        Information about this experiment, created by the `setupExpInfo` function.
    win : psychopy.visual.Window
        Window to setup - leave as None to create a new window.
    
    Returns
    ==========
    psychopy.visual.Window
        Window in which to run this experiment.
    """
    if PILOTING:
        logging.debug('Fullscreen settings ignored as running in pilot mode.')
    
    if win is None:
        # if not given a window to setup, make one
        win = visual.Window(
            size=_winSize, fullscr=_fullScr, screen=0,
            winType='pyglet', allowGUI=False, allowStencil=False,
            monitor='testMonitor', color=[-1.0000, -1.0000, -1.0000], colorSpace='rgb',
            backgroundImage='', backgroundFit='none',
            blendMode='avg', useFBO=True,
            units='height',
            checkTiming=False  # we're going to do this ourselves in a moment
        )
    else:
        # if we have a window, just set the attributes which are safe to set
        win.color = [-1.0000, -1.0000, -1.0000]
        win.colorSpace = 'rgb'
        win.backgroundImage = ''
        win.backgroundFit = 'none'
        win.units = 'height'
    if expInfo is not None:
        # get/measure frame rate if not already in expInfo
        if win._monitorFrameRate is None:
            win._monitorFrameRate = win.getActualFrameRate(infoMsg='Attempting to measure frame rate of screen, please wait...')
        expInfo['frameRate'] = win._monitorFrameRate
    win.hideMessage()
    # show a visual indicator if we're in piloting mode
    if PILOTING and prefs.piloting['showPilotingIndicator']:
        win.showPilotingIndicator()
    
    return win


def setupDevices(expInfo, thisExp, win):
    """
    Setup whatever devices are available (mouse, keyboard, speaker, eyetracker, etc.) and add them to 
    the device manager (deviceManager)
    
    Parameters
    ==========
    expInfo : dict
        Information about this experiment, created by the `setupExpInfo` function.
    thisExp : psychopy.data.ExperimentHandler
        Handler object for this experiment, contains the data to save and information about 
        where to save it to.
    win : psychopy.visual.Window
        Window in which to run this experiment.
    Returns
    ==========
    bool
        True if completed successfully.
    """
    # --- Setup input devices ---
    ioConfig = {}
    
    # Setup iohub keyboard
    ioConfig['Keyboard'] = dict(use_keymap='psychopy')
    
    # Setup iohub experiment
    ioConfig['Experiment'] = dict(filename=thisExp.dataFileName)
    
    # Start ioHub server
    ioServer = io.launchHubServer(window=win, **ioConfig)
    
    # store ioServer object in the device manager
    deviceManager.ioServer = ioServer
    
    # create a default keyboard (e.g. to check for escape)
    if deviceManager.getDevice('defaultKeyboard') is None:
        deviceManager.addDevice(
            deviceClass='keyboard', deviceName='defaultKeyboard', backend='iohub'
        )
    if deviceManager.getDevice('key_resp') is None:
        # initialise key_resp
        key_resp = deviceManager.addDevice(
            deviceClass='keyboard',
            deviceName='key_resp',
        )
    if deviceManager.getDevice('key_resp_1') is None:
        # initialise key_resp_1
        key_resp_1 = deviceManager.addDevice(
            deviceClass='keyboard',
            deviceName='key_resp_1',
        )
    # return True if completed successfully
    return True

def pauseExperiment(thisExp, win=None, timers=[], playbackComponents=[]):
    """
    Pause this experiment, preventing the flow from advancing to the next routine until resumed.
    
    Parameters
    ==========
    thisExp : psychopy.data.ExperimentHandler
        Handler object for this experiment, contains the data to save and information about 
        where to save it to.
    win : psychopy.visual.Window
        Window for this experiment.
    timers : list, tuple
        List of timers to reset once pausing is finished.
    playbackComponents : list, tuple
        List of any components with a `pause` method which need to be paused.
    """
    # if we are not paused, do nothing
    if thisExp.status != PAUSED:
        return
    
    # start a timer to figure out how long we're paused for
    pauseTimer = core.Clock()
    # pause any playback components
    for comp in playbackComponents:
        comp.pause()
    # make sure we have a keyboard
    defaultKeyboard = deviceManager.getDevice('defaultKeyboard')
    if defaultKeyboard is None:
        defaultKeyboard = deviceManager.addKeyboard(
            deviceClass='keyboard',
            deviceName='defaultKeyboard',
            backend='ioHub',
        )
    # run a while loop while we wait to unpause
    while thisExp.status == PAUSED:
        # check for quit (typically the Esc key)
        if defaultKeyboard.getKeys(keyList=['escape']):
            endExperiment(thisExp, win=win)
        # sleep 1ms so other threads can execute
        clock.time.sleep(0.001)
    # if stop was requested while paused, quit
    if thisExp.status == FINISHED:
        endExperiment(thisExp, win=win)
    # resume any playback components
    for comp in playbackComponents:
        comp.play()
    # reset any timers
    for timer in timers:
        timer.addTime(-pauseTimer.getTime())


def run(expInfo, thisExp, win, globalClock=None, thisSession=None):
    """
    Run the experiment flow.
    
    Parameters
    ==========
    expInfo : dict
        Information about this experiment, created by the `setupExpInfo` function.
    thisExp : psychopy.data.ExperimentHandler
        Handler object for this experiment, contains the data to save and information about 
        where to save it to.
    psychopy.visual.Window
        Window in which to run this experiment.
    globalClock : psychopy.core.clock.Clock or None
        Clock to get global time from - supply None to make a new one.
    thisSession : psychopy.session.Session or None
        Handle of the Session object this experiment is being run from, if any.
    """
    # mark experiment as started
    thisExp.status = STARTED
    # make sure window is set to foreground to prevent losing focus
    win.winHandle.activate()
    # make sure variables created by exec are available globally
    exec = environmenttools.setExecEnvironment(globals())
    # get device handles from dict of input devices
    ioServer = deviceManager.ioServer
    # get/create a default keyboard (e.g. to check for escape)
    defaultKeyboard = deviceManager.getDevice('defaultKeyboard')
    if defaultKeyboard is None:
        deviceManager.addDevice(
            deviceClass='keyboard', deviceName='defaultKeyboard', backend='ioHub'
        )
    eyetracker = deviceManager.getDevice('eyetracker')
    # make sure we're running in the directory for this experiment
    os.chdir(_thisDir)
    # get filename from ExperimentHandler for convenience
    filename = thisExp.dataFileName
    frameTolerance = 0.001  # how close to onset before 'same' frame
    endExpNow = False  # flag for 'escape' or other condition => quit the exp
    # get frame duration from frame rate in expInfo
    if 'frameRate' in expInfo and expInfo['frameRate'] is not None:
        frameDur = 1.0 / round(expInfo['frameRate'])
    else:
        frameDur = 1.0 / 60.0  # could not measure, so guess
    
    # Start Code - component code to be run after the window creation
    
    # --- Initialize components for Routine "welcome" ---
    welcome_text = visual.TextStim(win=win, name='welcome_text',
        text='To test your T-Pad is functioning correctly this example will take 1,000 samples from the serial port when the serial stream is active and convert the Credrus XID format to more meaningful information.\n\nNote the screen is only updated when a complete XID report from the T-Pad has been received. As the screen is updated, i.e. flipped, samples may be missed.\n\nPress space to continue\n',
        font='Courier New',
        pos=(0, 0), draggable=False, height=0.03, wrapWidth=None, ori=0.0, 
        color='white', colorSpace='rgb', opacity=None, 
        languageStyle='LTR',
        depth=0.0);
    key_resp = keyboard.Keyboard(deviceName='key_resp')
    
    # --- Initialize components for Routine "trial" ---
    # Run 'Begin Experiment' code from code
    #the goal of this example script is to show you how to read key press and releases from the T-pad when working as a standard keyboard
    #where each button press down is sent as a standard USB keyboard key press and each button release as a standard USB keyboard key up.
    #
    
    from psychopy import core
    import time
    from datetime import datetime
    import serial
    
    kb = keyboard.Keyboard()                                #setup keyboard
    
    ser = serial.Serial("COM7", 115200, timeout=0.01)       #open your T-Pad VCP serial port and have a very low timeout value to stop blocking, i.e. 0.01 seconds
    ser.write("X".encode())                                 #stop doing whatever it is doing and return to MOD0 or command mode
    core.wait(0.500)                                        #have a small delay to allow the T-Pad to go into Mode 0
    ser.write("MOD5\n".encode())                            #set to MOD5, i.e. XiD mode - flashing green LED on the back of the T-Pad
    core.wait(0.500)                                        #you can also get to this mode by pressing the button on the rear of the T-Pad 5 times until it flashes green
    print("START", flush=True)                              #debugging if needed
    
    # --- Initialize components for Routine "goodbye" ---
    goodbye_text = visual.TextStim(win=win, name='goodbye_text',
        text='THANK YOU\n\nfor using\n\nThe Black Box ToolKit\n\nT Pad\n\nPress SPACE to quit.',
        font='Open Sans',
        pos=(0, 0), draggable=False, height=0.05, wrapWidth=None, ori=0.0, 
        color='white', colorSpace='rgb', opacity=None, 
        languageStyle='LTR',
        depth=0.0);
    key_resp_1 = keyboard.Keyboard(deviceName='key_resp_1')
    
    # create some handy timers
    
    # global clock to track the time since experiment started
    if globalClock is None:
        # create a clock if not given one
        globalClock = core.Clock()
    if isinstance(globalClock, str):
        # if given a string, make a clock accoridng to it
        if globalClock == 'float':
            # get timestamps as a simple value
            globalClock = core.Clock(format='float')
        elif globalClock == 'iso':
            # get timestamps in ISO format
            globalClock = core.Clock(format='%Y-%m-%d_%H:%M:%S.%f%z')
        else:
            # get timestamps in a custom format
            globalClock = core.Clock(format=globalClock)
    if ioServer is not None:
        ioServer.syncClock(globalClock)
    logging.setDefaultClock(globalClock)
    # routine timer to track time remaining of each (possibly non-slip) routine
    routineTimer = core.Clock()
    win.flip()  # flip window to reset last flip timer
    # store the exact time the global clock started
    expInfo['expStart'] = data.getDateStr(
        format='%Y-%m-%d %Hh%M.%S.%f %z', fractionalSecondDigits=6
    )
    
    # --- Prepare to start Routine "welcome" ---
    # create an object to store info about Routine welcome
    welcome = data.Routine(
        name='welcome',
        components=[welcome_text, key_resp],
    )
    welcome.status = NOT_STARTED
    continueRoutine = True
    # update component parameters for each repeat
    # create starting attributes for key_resp
    key_resp.keys = []
    key_resp.rt = []
    _key_resp_allKeys = []
    # store start times for welcome
    welcome.tStartRefresh = win.getFutureFlipTime(clock=globalClock)
    welcome.tStart = globalClock.getTime(format='float')
    welcome.status = STARTED
    thisExp.addData('welcome.started', welcome.tStart)
    welcome.maxDuration = None
    # keep track of which components have finished
    welcomeComponents = welcome.components
    for thisComponent in welcome.components:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    frameN = -1
    
    # --- Run Routine "welcome" ---
    welcome.forceEnded = routineForceEnded = not continueRoutine
    while continueRoutine:
        # get current time
        t = routineTimer.getTime()
        tThisFlip = win.getFutureFlipTime(clock=routineTimer)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *welcome_text* updates
        
        # if welcome_text is starting this frame...
        if welcome_text.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            welcome_text.frameNStart = frameN  # exact frame index
            welcome_text.tStart = t  # local t and not account for scr refresh
            welcome_text.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(welcome_text, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'welcome_text.started')
            # update status
            welcome_text.status = STARTED
            welcome_text.setAutoDraw(True)
        
        # if welcome_text is active this frame...
        if welcome_text.status == STARTED:
            # update params
            pass
        
        # *key_resp* updates
        waitOnFlip = False
        
        # if key_resp is starting this frame...
        if key_resp.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            key_resp.frameNStart = frameN  # exact frame index
            key_resp.tStart = t  # local t and not account for scr refresh
            key_resp.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(key_resp, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'key_resp.started')
            # update status
            key_resp.status = STARTED
            # keyboard checking is just starting
            waitOnFlip = True
            win.callOnFlip(key_resp.clock.reset)  # t=0 on next screen flip
            win.callOnFlip(key_resp.clearEvents, eventType='keyboard')  # clear events on next screen flip
        if key_resp.status == STARTED and not waitOnFlip:
            theseKeys = key_resp.getKeys(keyList=['space'], ignoreKeys=["escape"], waitRelease=False)
            _key_resp_allKeys.extend(theseKeys)
            if len(_key_resp_allKeys):
                key_resp.keys = _key_resp_allKeys[-1].name  # just the last key pressed
                key_resp.rt = _key_resp_allKeys[-1].rt
                key_resp.duration = _key_resp_allKeys[-1].duration
                # a response ends the routine
                continueRoutine = False
        
        # check for quit (typically the Esc key)
        if defaultKeyboard.getKeys(keyList=["escape"]):
            thisExp.status = FINISHED
        if thisExp.status == FINISHED or endExpNow:
            endExperiment(thisExp, win=win)
            return
        # pause experiment here if requested
        if thisExp.status == PAUSED:
            pauseExperiment(
                thisExp=thisExp, 
                win=win, 
                timers=[routineTimer], 
                playbackComponents=[]
            )
            # skip the frame we paused on
            continue
        
        # check if all components have finished
        if not continueRoutine:  # a component has requested a forced-end of Routine
            welcome.forceEnded = routineForceEnded = True
            break
        continueRoutine = False  # will revert to True if at least one component still running
        for thisComponent in welcome.components:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # --- Ending Routine "welcome" ---
    for thisComponent in welcome.components:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    # store stop times for welcome
    welcome.tStop = globalClock.getTime(format='float')
    welcome.tStopRefresh = tThisFlipGlobal
    thisExp.addData('welcome.stopped', welcome.tStop)
    # check responses
    if key_resp.keys in ['', [], None]:  # No response was made
        key_resp.keys = None
    thisExp.addData('key_resp.keys',key_resp.keys)
    if key_resp.keys != None:  # we had a response
        thisExp.addData('key_resp.rt', key_resp.rt)
        thisExp.addData('key_resp.duration', key_resp.duration)
    thisExp.nextEntry()
    # the Routine "welcome" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()
    
    # --- Prepare to start Routine "trial" ---
    # create an object to store info about Routine trial
    trial = data.Routine(
        name='trial',
        components=[],
    )
    trial.status = NOT_STARTED
    continueRoutine = True
    # update component parameters for each repeat
    # Run 'Begin Routine' code from code
    strSerialData = ""                          #used to store raw serial data received
    strDecodedSerialData = ""                   #decoded serial data in utf-8
    intCounter = 0 #counter that increments by +1 each time there is a change on anything on the T-pad, that could be a button going down or up. multiple changes count as 1 sample or a report
    KeysPressed = "" #array string to store which keys are pressed
    booProcessSerialReceived = 0 #boolean flag as to whether there has been a change that needed to be processed, i.e. displayed on screen
    strSerialData = ""
    strDecodedSerialData= ""
    booXiDButton1Down= 0 #the boolean status of each input, i.e. 0 for button up or off and 1 for button down or on
    booXiDButton2Down= 0
    booXiDButton3Down= 0
    booXiDButton4Down= 0
    booXiDButton5Down= 0
    booXiDButton6Down= 0
    booXiDButton7Down= 0
    booXiDButton8Down= 0
    booXiDButton9Down= 0
    booXiDButton10Down= 0
    booXiDOpto1Active = 0
    booXiDOpto2Active = 0
    booXiDVKActive = 0
    booXiDTTLActive = 0
    strXiDChannel = "" #variable to store channel from XiD command sent over serial
    strXiDPressRelease = "" #variable to store Press or Release  from XiD command sent over serial
    strXiDSensorNo = "" #variable to store button or sensor number
    strXiDmSTimer = "" #variable to store the running external hardware mS timer built into the T-Pad
    strXiDStatusMsg = "" #variable to store the status of each button or sensor built up from receiving XiD commands over serial
    
    outputlength = 0 #variable to store the length of the last serial bytes sent from the T-Pad
    
    #setup status text
    text = visual.TextStim(win=win, name='text',
    text='',
    font='Courier New',
    pos=(0, 0), height=0.03, wrapWidth=None, ori=0, 
    color='white', colorSpace='rgb', opacity=1, 
    languageStyle='LTR',
    depth=0.0, alignHoriz='center')
    
    #flush serial buffer and keyboard
    ser.flush()
    ser.reset_input_buffer()
    kb.clearEvents()
    
    #initial status text to display on the first run before there is any keyboard activity all buttons should be up, optos off and voice key silent and no TTL input
    if intCounter == 0:
        text.text = "B1 B2 B3 B4 B5 B6 B7 B8 B9 10 O1 O2 VK TL\n"
        text.text += "UP UP UP UP UP UP UP UP UP UP OF OF OF 0V\n\n"
        text.text += "Channel / Press|Release / Sensor or Button / Running mS Timer\n\n"
        text.text += "Last Raw XiD: 0 0 0 0\n\n"
        text.text += "Sample: 0000\n\n"
        text.text += "Last Activity:\n\n"
        text.text += "None\n\n"
        text.text += "Press 'q' to quit"
        text.draw() #prepare the status text
        win.flip() #show the status text on screen
    
    #The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows.
    #Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window.
    #Usually only the first thread creates windows. Windows places messages into that queue whenever mouse activity occurs on that thread's
    #window, whenever keyboard activity occurs while that window has focus, and at other times. A process can also add messages to its own queue.
    #To accept user input, and for other reasons, each thread with a window must continuously retrieve messages from its queue, and act on them.
    #A programmer makes the process do that by writing a loop that calls GetMessage (which blocks for a message and retrieves it), and then calls
    #DispatchMessage (which dispatches the message), and repeats indefinitely. This is the message loop. There usually is a message loop in the
    #main program, which runs on the main thread, and additional message loop in each created modal dialog. Messages for every window of the
    #process pass through its message queue, and are handled by its message loop.A message loop is one kind of event loop.
    #
    #From: https://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows#:~:text=A%20programmer%20makes%20the%20process,This%20is%20the%20message%20loop.
    #
    #In this case we are checking for key presses and releases. So we are doing multiple things in the main messaging loop until we quit.
    #This is done to make processing non blocking, i.e. in a pseudo multi-tasking way.
    
    while (intCounter < 1000): #keep looping until we hit a 1,000 samples in a non blocking messaging loop or until the [q] key is pressed on the keyboard to quit
        KeysPressed = kb.getKeys(['q'], waitRelease=False, clear=True) #check which keys have been pressed
        core.wait(0.000001) #wait a microsecond to give PsychoPy time to process the message queue!!!
    
        if KeysPressed: #if there has been a key release then check which key it is?
            core.wait(0.000001) #wait a microsecond to give PsychoPy time to process the message queue!!!
            print("Pressed: ", [(k.rt, k.name, k.duration) for k in KeysPressed])
            for k in KeysPressed:
                if k.name == 'q':
                    ser.write("X".encode()) #stop doing whatever and return the T-pad to MOD0, i.e. mode 0 or command mode
                    core.wait(0.500) #add a small delay for the T-Pad to come out of whatever mode it is in
                    #flush serial buffer and keyboard
                    ser.flush()
                    ser.reset_input_buffer()
                    ser.close()
                    kb.clearEvents()
                    print("END", flush=True) #debugging if needed
                    core.quit() #quit PsychoPy
    
        #core.wait(0.001) #wait a millisecond to give PsychoPy time to process the message queue!!!
        #core.wait(0.000001) #wait a microsecond to give PsychoPy time to process the message queue!!!
    
        strSerialData = ser.readline() #read a '\n' terminated line from the serial stream, if there is no return from the T-Pad carry on as using non blocking serial
        strDecodedSerialData = strSerialData.decode() #decode into string
    
        #outputlength = len(strDecodedSerialData) # work out how long the complete serial string is including CR + LF, i.e. plus 2 invisiable bytes at the end
        
        if "\n" in strDecodedSerialData: #and booSettingWhiteOptoSensitivity == 1: #check if there has been a return message from the T-Pad by looking for a terminating \n in the serial stream
            datRTC = datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f')[:-3] #get the PC date and time we have detected there has been a change on the keyboard we need to report
            booProcessSerialReceived = 1 #flag that serial has been received and we need to process XiD later on in the messaging loop, i.e. display status of each button, opto etc.
    
            strXiDChannel = "" + strDecodedSerialData[0] #channel
            strXiDPressRelease = "" + strDecodedSerialData[2] #press|release
            strXiDSensorNo = "" + strDecodedSerialData[4] #sensor
            strXiDmSTimer = "" + strDecodedSerialData[6:] #running mS timer
    
            if strXiDChannel == 'A': #channel A is for buttons 1 to 10
                if strXiDPressRelease == 'P': #P is for button press/button down
                    if strXiDSensorNo == '1': #check the button number, i.e. button 1 in this case
                        booXiDButton1Down = 1 #flag button 1 as being pressed/down
                    if strXiDSensorNo == '2':
                        booXiDButton2Down = 1
                    if strXiDSensorNo == '3':
                        booXiDButton3Down = 1
                    if strXiDSensorNo == '4':
                        booXiDButton4Down = 1
                    if strXiDSensorNo == '5':
                        booXiDButton5Down = 1
                    if strXiDSensorNo == '6':
                        booXiDButton6Down = 1
                    if strXiDSensorNo == '7':
                        booXiDButton7Down = 1
                    if strXiDSensorNo == '8':
                        booXiDButton8Down = 1
                    if strXiDSensorNo == '9':
                        booXiDButton9Down = 1
                    if strXiDSensorNo == '0':
                        booXiDButton10Down = 1
                if strXiDPressRelease == 'R': #R is for button release/button up
                    if strXiDSensorNo == '1': #check the button number, i.e. button 1 in this case
                        booXiDButton1Down = 0 #flag button 1 as being released/up
                    if strXiDSensorNo == '2':
                        booXiDButton2Down = 0
                    if strXiDSensorNo == '3':
                        booXiDButton3Down = 0
                    if strXiDSensorNo == '4':
                        booXiDButton4Down = 0
                    if strXiDSensorNo == '5':
                        booXiDButton5Down = 0
                    if strXiDSensorNo == '6':
                        booXiDButton6Down = 0
                    if strXiDSensorNo == '7':
                        booXiDButton7Down = 0
                    if strXiDSensorNo == '8':
                        booXiDButton8Down = 0
                    if strXiDSensorNo == '9':
                        booXiDButton9Down = 0
                    if strXiDSensorNo == '0':
                        booXiDButton10Down = 0
    
            if strXiDChannel == 'C': #channel C is for optos 1 & 2
                if strXiDPressRelease == 'P': #P is for opto active/on
                    if strXiDSensorNo == '1': #check the opto number, i.e. opto 1 in this case
                        booXiDOpto1Active = 1
                    if strXiDSensorNo == '2':
                        booXiDOpto2Active = 1
                if strXiDPressRelease == 'R': #R is for opto non active/off
                    if strXiDSensorNo == '1':
                        booXiDOpto1Active = 0
                    if strXiDSensorNo == '2':
                        booXiDOpto2Active = 0
    
            if strXiDChannel == 'M': #channel M is for voice key or mic
                if strXiDPressRelease == 'P': #P is for microphone active/on
                    if strXiDSensorNo == '1': #micriohone 1
                        booXiDVKActive = 1
                if strXiDPressRelease == 'R':  #R is for microphone non active/off
                    if strXiDSensorNo == '1':
                        booXiDVKActive = 0
    
            if strXiDChannel == 'B': #channel B is for TTL in
                if strXiDPressRelease == 'P': #P is for TTL in active 5V/on
                    if strXiDSensorNo == '1':
                        booXiDTTLActive = 1
                if strXiDPressRelease == 'R': #R is for TTL in non active 0V/off
                    if strXiDSensorNo == '1':
                        booXiDTTLActive = 0
    
        if booProcessSerialReceived == 1: #check if there has been a change on any key and if we need to update the status of each key on screen
            #datRTC = datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f')[:-3] #get the PC date and time we have detected there has been a change on the keyboard we need to report
            strXiDStatusMsg = "" #get ready to produce a status message to show the status of every line built up via receiving XiD commands over serial
            if booXiDButton1Down == 1: #if button 1 is down and an [a] key has been returned as pressed then display it as down on the status screen.
                strXiDStatusMsg += "DN " #this could also be no change since the last display and another key has triggered the update
            else: #if button 1 is up and an [a] key has been returned as released then display it as down on the status screen
                strXiDStatusMsg += "UP " #this could also be no change since the last display and another key has triggered the update
            if booXiDButton2Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton3Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton4Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton5Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton6Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton7Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton8Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton9Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
            if booXiDButton10Down == 1:
                strXiDStatusMsg += "DN "
            else:
                strXiDStatusMsg += "UP "
    
            if booXiDOpto1Active == 1:
                strXiDStatusMsg += "ON "
            else:
                strXiDStatusMsg += "OF "
            if booXiDOpto2Active == 1:
                strXiDStatusMsg += "ON "
            else:
                strXiDStatusMsg += "OF "
    
            if booXiDVKActive == 1:
                strXiDStatusMsg += "ON "
            else:
                strXiDStatusMsg += "OF "
                
            if booXiDTTLActive == 1:
                strXiDStatusMsg += "ON"
            else:
                strXiDStatusMsg += "OF"
    
            booProcessSerialReceived = 0 #flag that there is no more serial XiD commands to process
            intCounter = intCounter + 1 #increment the sample count by +1
    
            #update the status message to show the status of every line built up via receiving XiD commands over serial
            text.text = "B1 B2 B3 B4 B5 B6 B7 B8 B9 10 O1 O2 VK TL\n"
            text.text += strXiDStatusMsg + "\n\n"
            text.text += "Channel / Press|Release / Sensor or Button / Running mS Timer\n\n"
            text.text += "Last Raw XiD: " + strXiDChannel + " " + strXiDPressRelease + " " + strXiDSensorNo + " " + strXiDmSTimer + " " + "\n"
            text.text += "Sample: {:04d}\n\n".format(intCounter) #the sample number, i.e. number of keyboard changes. multiple changes still count as 1 sample
            text.text += "Last Activity:\n\n"
            text.text += "(PC Clock: " + datRTC + ")\n\n" #show the timestamp that any keyboard change was detected
            text.text += "Press 'q' to quit"
            text.draw() #prepare the status text
            win.flip() #show the status text on screen
    
    # store start times for trial
    trial.tStartRefresh = win.getFutureFlipTime(clock=globalClock)
    trial.tStart = globalClock.getTime(format='float')
    trial.status = STARTED
    thisExp.addData('trial.started', trial.tStart)
    trial.maxDuration = None
    # keep track of which components have finished
    trialComponents = trial.components
    for thisComponent in trial.components:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    frameN = -1
    
    # --- Run Routine "trial" ---
    trial.forceEnded = routineForceEnded = not continueRoutine
    while continueRoutine:
        # get current time
        t = routineTimer.getTime()
        tThisFlip = win.getFutureFlipTime(clock=routineTimer)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # check for quit (typically the Esc key)
        if defaultKeyboard.getKeys(keyList=["escape"]):
            thisExp.status = FINISHED
        if thisExp.status == FINISHED or endExpNow:
            endExperiment(thisExp, win=win)
            return
        # pause experiment here if requested
        if thisExp.status == PAUSED:
            pauseExperiment(
                thisExp=thisExp, 
                win=win, 
                timers=[routineTimer], 
                playbackComponents=[]
            )
            # skip the frame we paused on
            continue
        
        # check if all components have finished
        if not continueRoutine:  # a component has requested a forced-end of Routine
            trial.forceEnded = routineForceEnded = True
            break
        continueRoutine = False  # will revert to True if at least one component still running
        for thisComponent in trial.components:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # --- Ending Routine "trial" ---
    for thisComponent in trial.components:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    # store stop times for trial
    trial.tStop = globalClock.getTime(format='float')
    trial.tStopRefresh = tThisFlipGlobal
    thisExp.addData('trial.stopped', trial.tStop)
    thisExp.nextEntry()
    # the Routine "trial" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()
    
    # --- Prepare to start Routine "goodbye" ---
    # create an object to store info about Routine goodbye
    goodbye = data.Routine(
        name='goodbye',
        components=[goodbye_text, key_resp_1],
    )
    goodbye.status = NOT_STARTED
    continueRoutine = True
    # update component parameters for each repeat
    # create starting attributes for key_resp_1
    key_resp_1.keys = []
    key_resp_1.rt = []
    _key_resp_1_allKeys = []
    # store start times for goodbye
    goodbye.tStartRefresh = win.getFutureFlipTime(clock=globalClock)
    goodbye.tStart = globalClock.getTime(format='float')
    goodbye.status = STARTED
    thisExp.addData('goodbye.started', goodbye.tStart)
    goodbye.maxDuration = None
    # keep track of which components have finished
    goodbyeComponents = goodbye.components
    for thisComponent in goodbye.components:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    frameN = -1
    
    # --- Run Routine "goodbye" ---
    goodbye.forceEnded = routineForceEnded = not continueRoutine
    while continueRoutine:
        # get current time
        t = routineTimer.getTime()
        tThisFlip = win.getFutureFlipTime(clock=routineTimer)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *goodbye_text* updates
        
        # if goodbye_text is starting this frame...
        if goodbye_text.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            goodbye_text.frameNStart = frameN  # exact frame index
            goodbye_text.tStart = t  # local t and not account for scr refresh
            goodbye_text.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(goodbye_text, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'goodbye_text.started')
            # update status
            goodbye_text.status = STARTED
            goodbye_text.setAutoDraw(True)
        
        # if goodbye_text is active this frame...
        if goodbye_text.status == STARTED:
            # update params
            pass
        
        # *key_resp_1* updates
        waitOnFlip = False
        
        # if key_resp_1 is starting this frame...
        if key_resp_1.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            key_resp_1.frameNStart = frameN  # exact frame index
            key_resp_1.tStart = t  # local t and not account for scr refresh
            key_resp_1.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(key_resp_1, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'key_resp_1.started')
            # update status
            key_resp_1.status = STARTED
            # keyboard checking is just starting
            waitOnFlip = True
            win.callOnFlip(key_resp_1.clock.reset)  # t=0 on next screen flip
            win.callOnFlip(key_resp_1.clearEvents, eventType='keyboard')  # clear events on next screen flip
        if key_resp_1.status == STARTED and not waitOnFlip:
            theseKeys = key_resp_1.getKeys(keyList=['space'], ignoreKeys=["escape"], waitRelease=False)
            _key_resp_1_allKeys.extend(theseKeys)
            if len(_key_resp_1_allKeys):
                key_resp_1.keys = _key_resp_1_allKeys[-1].name  # just the last key pressed
                key_resp_1.rt = _key_resp_1_allKeys[-1].rt
                key_resp_1.duration = _key_resp_1_allKeys[-1].duration
                # a response ends the routine
                continueRoutine = False
        
        # check for quit (typically the Esc key)
        if defaultKeyboard.getKeys(keyList=["escape"]):
            thisExp.status = FINISHED
        if thisExp.status == FINISHED or endExpNow:
            endExperiment(thisExp, win=win)
            return
        # pause experiment here if requested
        if thisExp.status == PAUSED:
            pauseExperiment(
                thisExp=thisExp, 
                win=win, 
                timers=[routineTimer], 
                playbackComponents=[]
            )
            # skip the frame we paused on
            continue
        
        # check if all components have finished
        if not continueRoutine:  # a component has requested a forced-end of Routine
            goodbye.forceEnded = routineForceEnded = True
            break
        continueRoutine = False  # will revert to True if at least one component still running
        for thisComponent in goodbye.components:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # --- Ending Routine "goodbye" ---
    for thisComponent in goodbye.components:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    # store stop times for goodbye
    goodbye.tStop = globalClock.getTime(format='float')
    goodbye.tStopRefresh = tThisFlipGlobal
    thisExp.addData('goodbye.stopped', goodbye.tStop)
    # check responses
    if key_resp_1.keys in ['', [], None]:  # No response was made
        key_resp_1.keys = None
    thisExp.addData('key_resp_1.keys',key_resp_1.keys)
    if key_resp_1.keys != None:  # we had a response
        thisExp.addData('key_resp_1.rt', key_resp_1.rt)
        thisExp.addData('key_resp_1.duration', key_resp_1.duration)
    thisExp.nextEntry()
    # the Routine "goodbye" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()
    # Run 'End Experiment' code from code
    ser.write("X".encode()) #stop doing whatever and return the T-pad to MOD0, i.e. mode 0 or command mode
    core.wait(0.500) #add a small delay for the T-Pad to come out of whatever mode it is in
    #flush serial buffer and keyboard
    ser.flush()
    ser.reset_input_buffer()
    ser.close()
    kb.clearEvents()
    print("END", flush=True) #debugging if needed
    # Run 'End Experiment' code from code_1
    ser.flush()
    ser.write("X".encode())
    ser.flush()
    ser.close()
    
    # mark experiment as finished
    endExperiment(thisExp, win=win)


def saveData(thisExp):
    """
    Save data from this experiment
    
    Parameters
    ==========
    thisExp : psychopy.data.ExperimentHandler
        Handler object for this experiment, contains the data to save and information about 
        where to save it to.
    """
    filename = thisExp.dataFileName
    # these shouldn't be strictly necessary (should auto-save)
    thisExp.saveAsWideText(filename + '.csv', delim='auto')
    thisExp.saveAsPickle(filename)


def endExperiment(thisExp, win=None):
    """
    End this experiment, performing final shut down operations.
    
    This function does NOT close the window or end the Python process - use `quit` for this.
    
    Parameters
    ==========
    thisExp : psychopy.data.ExperimentHandler
        Handler object for this experiment, contains the data to save and information about 
        where to save it to.
    win : psychopy.visual.Window
        Window for this experiment.
    """
    if win is not None:
        # remove autodraw from all current components
        win.clearAutoDraw()
        # Flip one final time so any remaining win.callOnFlip() 
        # and win.timeOnFlip() tasks get executed
        win.flip()
    # return console logger level to WARNING
    logging.console.setLevel(logging.WARNING)
    # mark experiment handler as finished
    thisExp.status = FINISHED
    logging.flush()


def quit(thisExp, win=None, thisSession=None):
    """
    Fully quit, closing the window and ending the Python process.
    
    Parameters
    ==========
    win : psychopy.visual.Window
        Window to close.
    thisSession : psychopy.session.Session or None
        Handle of the Session object this experiment is being run from, if any.
    """
    thisExp.abort()  # or data files will save again on exit
    # make sure everything is closed down
    if win is not None:
        # Flip one final time so any remaining win.callOnFlip() 
        # and win.timeOnFlip() tasks get executed before quitting
        win.flip()
        win.close()
    logging.flush()
    if thisSession is not None:
        thisSession.stop()
    # terminate Python process
    core.quit()


# if running this experiment as a script...
if __name__ == '__main__':
    # call all functions in order
    expInfo = showExpInfoDlg(expInfo=expInfo)
    thisExp = setupData(expInfo=expInfo)
    logFile = setupLogging(filename=thisExp.dataFileName)
    win = setupWindow(expInfo=expInfo)
    setupDevices(expInfo=expInfo, thisExp=thisExp, win=win)
    run(
        expInfo=expInfo, 
        thisExp=thisExp, 
        win=win,
        globalClock='float'
    )
    saveData(thisExp=thisExp)
    quit(thisExp=thisExp, win=win)
