예제 #1
0
파일: test.py 프로젝트: kqdmqx/LightGBM
def test_load_from_csc(filename, reference):
    data = []
    label = []
    inp = open(filename, 'r')
    for line in inp.readlines():
        data.append([float(x) for x in line.split('\t')[1:]])
        label.append(float(line.split('\t')[0]))
    inp.close()
    mat = np.array(data)
    label = np.array(label, dtype=np.float32)
    csr = sparse.csc_matrix(mat)
    handle = ctypes.c_void_p()
    ref = None
    if reference is not None:
        ref = reference

    LIB.LGBM_DatasetCreateFromCSC(
        c_array(ctypes.c_int, csr.indptr),
        dtype_int32,
        c_array(ctypes.c_int, csr.indices),
        csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)),
        dtype_float64,
        len(csr.indptr),
        len(csr.data),
        csr.shape[0],
        c_str('max_bin=15'),
        ref,
        ctypes.byref(handle))
    num_data = ctypes.c_long()
    LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data))
    num_feature = ctypes.c_long()
    LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature))
    LIB.LGBM_DatasetSetField(handle, c_str('label'), c_array(ctypes.c_float, label), len(label), 0)
    print('#data:%d #feature:%d' % (num_data.value, num_feature.value))
    return handle
예제 #2
0
파일: cephfs.py 프로젝트: Aorjoa/ceph
    def mds_command(self, mds_spec, args, input_data):
        """
        :return 3-tuple of output status int, output status string, output data
        """

        cmdarr = (c_char_p * len(args))(*args)

        outbufp = pointer(pointer(c_char()))
        outbuflen = c_long()
        outsp = pointer(pointer(c_char()))
        outslen = c_long()

        ret = self.libcephfs.ceph_mds_command(self.cluster, c_char_p(mds_spec),
                                              cmdarr, len(args),
                                              c_char_p(input_data),
                                              len(input_data), outbufp,
                                              byref(outbuflen), outsp,
                                              byref(outslen))

        my_outbuf = outbufp.contents[:(outbuflen.value)]
        my_outs = outsp.contents[:(outslen.value)]
        if outbuflen.value:
            self.libcephfs.ceph_buffer_free(outbufp.contents)
        if outslen.value:
            self.libcephfs.ceph_buffer_free(outsp.contents)

        return (ret, my_outbuf, my_outs)
예제 #3
0
파일: uid.py 프로젝트: yujiabe/mock
def setresuid(ruid=-1, euid=-1, suid=-1):
    ruid = ctypes.c_long(ruid)
    euid = ctypes.c_long(euid)
    suid = ctypes.c_long(suid)
    res = _libc.setresuid(ruid, euid, suid)
    if res:
        raise OSError(_errno.value, os.strerror(_errno.value))
예제 #4
0
 def get_tracking(self):
     yaw = c_long()
     pitch = c_long()
     roll = c_long()
     rc = self.lib.IWRGetTracking(byref(yaw), byref(pitch), byref(roll))
     self._check_error(rc)
     return yaw.value, pitch.value, roll.value
예제 #5
0
파일: uid.py 프로젝트: yujiabe/mock
def setresgid(rgid=-1, egid=-1, sgid=-1):
    rgid = ctypes.c_long(rgid)
    egid = ctypes.c_long(egid)
    sgid = ctypes.c_long(sgid)
    res = _libc.setresgid(rgid, egid, sgid)
    if res:
        raise OSError(_errno.value, os.strerror(_errno.value))
예제 #6
0
파일: uid.py 프로젝트: heysion/mock_clone
def setresgid(rgid=-1, egid=-1, sgid=-1):
    rgid = ctypes.c_long(rgid)
    egid = ctypes.c_long(egid)
    sgid = ctypes.c_long(sgid)
    res = _libc.setresgid(rgid, egid, sgid)
    if res:
        raise OSError(ctypes.get_errno(), os.strerror(ctypes.get_errno()))
예제 #7
0
파일: uid.py 프로젝트: heysion/mock_clone
def setresuid(ruid=-1, euid=-1, suid=-1):
    ruid = ctypes.c_long(ruid)
    euid = ctypes.c_long(euid)
    suid = ctypes.c_long(suid)
    res = _libc.setresuid(ruid, euid, suid)
    if res:
        raise OSError(ctypes.get_errno(), os.strerror(ctypes.get_errno()))
예제 #8
0
def _sendMouseEvent(ev, x, y, dwData=0):
    """The helper function that actually makes the call to the mouse_event()
    win32 function.

    Args:
      ev (int): The win32 code for the mouse event. Use one of the MOUSEEVENTF_*
      constants for this argument.
      x (int): The x position of the mouse event.
      y (int): The y position of the mouse event.
      dwData (int): The argument for mouse_event()'s dwData parameter. So far
        this is only used by mouse scrolling.

    Returns:
      None
    """
    assert x != None and y != None, 'x and y cannot be set to None'
    # TODO: ARG! For some reason, SendInput isn't working for mouse events. I'm switching to using the older mouse_event win32 function.
    #mouseStruct = MOUSEINPUT()
    #mouseStruct.dx = x
    #mouseStruct.dy = y
    #mouseStruct.mouseData = ev
    #mouseStruct.time = 0
    #mouseStruct.dwExtraInfo = ctypes.pointer(ctypes.c_ulong(0)) # according to https://stackoverflow.com/questions/13564851/generate-keyboard-events I can just set this. I don't really care about this value.
    #inputStruct = INPUT()
    #inputStruct.mi = mouseStruct
    #inputStruct.type = INPUT_MOUSE
    #ctypes.windll.user32.SendInput(1, ctypes.pointer(inputStruct), ctypes.sizeof(inputStruct))

    width, height = _size()
    convertedX = 65536 * x // width + 1
    convertedY = 65536 * y // height + 1
    ctypes.windll.user32.mouse_event(ev, ctypes.c_long(convertedX), ctypes.c_long(convertedY), dwData, 0)

    if ctypes.windll.kernel32.GetLastError() != 0:
        raise ctypes.WinError()
예제 #9
0
파일: MS11-080.py 프로젝트: dtctd/pwk
def findSysBase(drvname=None):
    ARRAY_SIZE            = 1024
    myarray               = c_ulong * ARRAY_SIZE
    lpImageBase           = myarray()
    cb                    = c_int(1024)
    lpcbNeeded            = c_long()
    drivername_size       = c_long()
    drivername_size.value = 48
    Psapi.EnumDeviceDrivers(byref(lpImageBase), cb, byref(lpcbNeeded))
    for baseaddy in lpImageBase:
        drivername = c_char_p("\x00"*drivername_size.value)
        if baseaddy:
            Psapi.GetDeviceDriverBaseNameA(baseaddy, drivername,
                            drivername_size.value)
            if drvname:
                if drivername.value.lower() == drvname:
                    print "[+] Retrieving %s info..." % drvname
                    print "[+] %s base address: %s" % (drvname, hex(baseaddy))
                    return baseaddy
            else:
                if drivername.value.lower().find("krnl") !=-1:
                    print "[+] Retrieving Kernel info..."
                    print "[+] Kernel version:", drivername.value
                    print "[+] Kernel base address: %s" % hex(baseaddy)
                    return (baseaddy, drivername.value)
    return None
예제 #10
0
        def test_function(*args, **named):
            major, minor = ctypes.c_long(), ctypes.c_long()
            display = eglGetDisplay(EGL_DEFAULT_DISPLAY)
            eglInitialize(display, major, minor)
            num_configs = ctypes.c_long()
            configs = (EGLConfig * 2)()
            api_constant = API_NAMES[api.lower()]
            local_attributes = attributes[:]
            local_attributes.extend([EGL_CONFORMANT, API_BITS[api.lower()], EGL_NONE])
            print("local_attributes", local_attributes)
            local_attributes = arrays.GLintArray.asArray(local_attributes)
            eglChooseConfig(display, local_attributes, configs, 2, num_configs)
            print("API", api_constant)
            eglBindAPI(api_constant)

            # now need to get a raw X window handle...
            pygame.init()
            pygame.display.set_mode(size)
            window = pygame.display.get_wm_info()["window"]
            surface = eglCreateWindowSurface(display, configs[0], window, None)

            ctx = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, None)
            if ctx == EGL_NO_CONTEXT:
                raise RuntimeError("Unable to create context")
            try:
                eglMakeCurrent(display, surface, surface, ctx)
                function(*args, **named)
                eglSwapBuffers(display, surface)
            finally:
                pygame.display.quit()
                pygame.quit()
예제 #11
0
파일: APTMotor.py 프로젝트: XavierDR/OCT
    def __init__(self, SerialNum=None, HWTYPE=31, verbose=False):
        '''
        HWTYPE_BSC001		11	// 1 Ch benchtop stepper driver
        HWTYPE_BSC101		12	// 1 Ch benchtop stepper driver
        HWTYPE_BSC002		13	// 2 Ch benchtop stepper driver
        HWTYPE_BDC101		14	// 1 Ch benchtop DC servo driver
        HWTYPE_SCC001		21	// 1 Ch stepper driver card (used within BSC102,103 units)
        HWTYPE_DCC001		22	// 1 Ch DC servo driver card (used within BDC102,103 units)
        HWTYPE_ODC001		24	// 1 Ch DC servo driver cube
        HWTYPE_OST001		25	// 1 Ch stepper driver cube
        HWTYPE_MST601		26	// 2 Ch modular stepper driver module
        HWTYPE_TST001		29	// 1 Ch Stepper driver T-Cube
        HWTYPE_TDC001		31	// 1 Ch DC servo driver T-Cube
        HWTYPE_LTSXXX		42	// LTS300/LTS150 Long Travel Integrated Driver/Stages
        HWTYPE_L490MZ		43	// L490MZ Integrated Driver/Labjack
        HWTYPE_BBD10X		44	// 1/2/3 Ch benchtop brushless DC servo driver
        '''
        self.verbose = verbose
        self.Connected = False
        self.aptdll = windll.LoadLibrary('C:\\Users\\OCT\\Documents\\spyderworkspace\\test\\ThorLabsMotor\\APT(1).dll')
        self.aptdll.EnableEventDlg(True)
        self.aptdll.APTInit()
        #print 'APT initialized'
        self.HWType = c_long(HWTYPE)
        self.blCorr = 0.10 #100um backlash correction
        if SerialNum is not None:
            if verbose: print "Serial is", SerialNum
            self.SerialNum = c_long(SerialNum)
            self.initializeHardwareDevice()
        # TODO : Error reporting to know if initialisation went sucessfully or not.

        else:
            if verbose: print "No serial, please setSerialNumber"
예제 #12
0
def init(as_switcher):
    """
    Initialise the wavemeter
    """
    global lib

    # Boolean telling us whether the wlm is being used as a switcher as well
    global _SWITCHER
    _SWITCHER = as_switcher

    # Open the DLL
    lib = ctypes.WinDLL('C:\Windows\system32\wlmData.dll')

    lib.GetFrequencyNum.restype = ctypes.c_double
    lib.Instantiate.restype = ctypes.c_long
    lib.GetExposureRange.restype = ctypes.c_long

    # Turn off auto-switcher mode
    lib.SetSwitcherMode(ctypes.c_long(0))

    if _SWITCHER:
        # Turn off auto exposure in all channels (1-8)
        for i in range(8):
            lib.SetExposureModeNum(i+1, 0)
    else:
        lib.SetExposureModeNum(1, 0)

    # Set to measuring normally
    lib.Operation(cMeasurement)

    global EXP_MAX, EXP_MIN
    EXP_MIN = lib.GetExposureRange(ctypes.c_long(cExpoMin))
    EXP_MAX = lib.GetExposureRange(ctypes.c_long(cExpoMax))
예제 #13
0
    def stop_ui(self):
        """
        This method is called when the UI stop message arrives.
        It shuts down the web UI.
        """

        # Tell the consumer thread to stop.
        self.thread_continue = False

        # Tell the Django application to stop.
        try:
            self.bridge.send( ("stop",) )
        except:
            pass

        # Shut down the communication pipe.
        # This should wake up the consumer thread.
        self.bridge.close()

        # Wait for the consumer thread to stop.
        if self.thread.isAlive():
            self.thread.join(2.0)

        # If a timeout occurs...
        if self.thread.isAlive():

            # Forcefully kill the thread. Ignore errors.
            # http://stackoverflow.com/a/15274929/426293
            import ctypes
            exc = ctypes.py_object(KeyboardInterrupt)
            res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
                ctypes.c_long(self.thread.ident), exc)
            if res > 1:
                ctypes.pythonapi.PyThreadState_SetAsyncExc(
                    ctypes.c_long(self.thread.ident), None)
예제 #14
0
    def terminate(self):
        """
        Force the thread to terminate.

        .. warning: Don't use this except in extreme circumstances!
                    The terminated thread's code may not have a proper chance
                    to clean up its resources or free its locks, and this may
                    lead to memory leaks and/or deadlocks!
        """

        # For more details see:
        # http://stackoverflow.com/a/15274929/426293

        # Do nothing if the thread is already dead.
        if not self.isAlive():
            return

        # Inject a fake KeyboardInterrupt exception.
        # This is rather dangerous, since the exception may be raised anywhere,
        # and usually Python code doesn't expect that, even though it should.
        # This happens even within standard Python libraries!
        import ctypes
        exc = ctypes.py_object(KeyboardInterrupt)
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
            ctypes.c_long(self.ident), exc)
        if res == 0:
            raise SystemError("Nonexistent thread id?")
        elif res > 1:
            # If it returns a number greater than one, you're in trouble,
            # and you should call it again with exc=NULL to revert the effect.
            ctypes.pythonapi.PyThreadState_SetAsyncExc(
                ctypes.c_long(self.ident), None)
            raise SystemError("PyThreadState_SetAsyncExc() failed")
예제 #15
0
	def homeParams(self):
		direction = c_long()
		switch = c_long()
		velocity = c_float()
		zero_offset = c_float()
		self.ctrl.GetHomeParams(self.channel, byref(direction), byref(switch), byref(velocity), byref(zero_offset))
		return direction.value, switch.value, velocity.value, zero_offset.value
예제 #16
0
    def initServer(self):

        # load wavemeter dll file for use of API functions self.d and self.l
        # are dummy c_types for unused wavemeter functions

        dll_path = "C:\Windows\System32\wlmData.dll"
        self.wmdll = ctypes.windll.LoadLibrary(dll_path)
        self.d = ctypes.c_double(0)
        self.l = ctypes.c_long(0)
        self.b = ctypes.c_bool(0)

        self.set_pid_variables()

        # Getting the amplitude in the GetAmplitudeNum function can
        # return the max, min, and average of the interference pattern
        self.AmplitudeMin = ctypes.c_long(0)
        self.AmplitudeMax = ctypes.c_long(2)
        self.AmplitudeAvg = ctypes.c_long(4)

        self.set_dll_variables()
        self.WavemeterVersion = self.wmdll.GetWLMVersion(ctypes.c_long(1))

        self.measureChan()

        self.listeners = set()
 def getHardwareLimitSwitches(self, serialNumber):
     HWSerialNum = c_long(serialNumber)
     reverseLimitSwitch = c_long()
     forwardLimitSwitch = c_long()
     self.aptdll.MOT_GetHWLimSwitches(HWSerialNum, pointer(reverseLimitSwitch), pointer(forwardLimitSwitch))
     hardwareLimitSwitches = [reverseLimitSwitch.value, forwardLimitSwitch.value]
     return hardwareLimitSwitches
예제 #18
0
 def set_const_dt(self, c, dacPort, dt):
     """Activates the dt PID settings for a given DAC port. This makes each
     dt in the integration constant as opposed to oscillating values based
     on the system time, which changes when changing wm settings."""
     port_c = ctypes.c_long(dacPort)
     value = ctypes.c_long(dt)
     yield self.wmdll.SetPIDSetting(self.PIDConstdt, port_c, value, self.d)
예제 #19
0
    def set_channel_lock(self, c, dacPort, waveMeterChannel, lock):
        """Locks a wavemeter channel to a given DAC port."""
        port_c = ctypes.c_long(dacPort)
        chan_c = ctypes.c_long(waveMeterChannel)

        # Check to ensure a valid PID Course number is set, otherwise
        # trying to lock a channel turns off lock to main PID lock switch

        course_c = ctypes.create_string_buffer(1024)
        yield self.wmdll.GetPIDCourseNum(port_c, ctypes.pointer(course_c))
        course = float(course_c.value)
        if self.WavemeterVersion == 1312:
            returnValue("Version of WLM does not support individual locking")
        if course <= 0:
            returnValue("Set PID Course to a valid number")
        else:
            notified = self.getOtherListeners(c)
            if lock == 1:
                yield self.wmdll.SetPIDSetting(self.DeviationChannel, port_c,
                                               chan_c, self.d)

            elif lock == 0:
                yield self.wmdll.SetPIDSetting(self.DeviationChannel, port_c,
                                               ctypes.c_long(0), self.d)

            self.channellock((dacPort, waveMeterChannel, lock), notified)
예제 #20
0
파일: u12.py 프로젝트: maunsell/MWorks
 def listAll(self):
     """
     Name: U12.listAll()
     Args: See section 4.22 of the User's Guide
     Desc: Searches the USB for all LabJacks, and returns the serial number and local ID for each
     
     >>> dev = U12()
     >>> dev.listAll()
     >>> {'serialnumList': <u12.c_long_Array_127 object at 0x00E2AD50>, 'numberFound': 1, 'localIDList': <u12.c_long_Array_127 object at 0x00E2ADA0>}
     """
     
     # Create arrays and ctypes
     productIDList = listToCArray([0]*127, ctypes.c_long)
     serialnumList = listToCArray([0]*127, ctypes.c_long)
     localIDList = listToCArray([0]*127, ctypes.c_long)
     powerList = listToCArray([0]*127, ctypes.c_long)
     arr127_type = ctypes.c_long * 127 
     calMatrix_type = arr127_type * 20
     calMatrix = calMatrix_type() 
     reserved = ctypes.c_long()
     numberFound = ctypes.c_long()
     
     ecode = staticLib.ListAll(ctypes.byref(productIDList), ctypes.byref(serialnumList), ctypes.byref(localIDList), ctypes.byref(powerList), ctypes.byref(calMatrix), ctypes.byref(numberFound), ctypes.byref(reserved), ctypes.byref(reserved))
     if ecode != 0: raise U12Exception(ecode)
     
     return {"serialnumList": serialnumList, "localIDList":localIDList, "numberFound":numberFound.value}
예제 #21
0
def find_driver_base(driver=None):
	"""
	Get the base address of the specified driver or the NT Kernel if none is
	specified.

	:param str driver: The name of the driver to get the base address of.
	:return: The base address and the driver name.
	:rtype: tuple
	"""
	if platform.architecture()[0] == '64bit':
		lpImageBase = (ctypes.c_ulonglong * 1024)()
		lpcbNeeded = ctypes.c_longlong()
		ctypes.windll.psapi.GetDeviceDriverBaseNameA.argtypes = [ctypes.c_longlong, ctypes.POINTER(ctypes.c_char), ctypes.c_uint32]
	else:
		if process_is_wow64():
			raise RuntimeError('python running in WOW64 is not supported')
		lpImageBase = (ctypes.c_ulong * 1024)()
		lpcbNeeded = ctypes.c_long()
	driver_name_size = ctypes.c_long()
	driver_name_size.value = 48
	ctypes.windll.psapi.EnumDeviceDrivers(ctypes.byref(lpImageBase), ctypes.c_int(1024), ctypes.byref(lpcbNeeded))
	for base_addr in lpImageBase:
		driver_name = ctypes.c_char_p(b'\x00' * driver_name_size.value)
		if base_addr:
			ctypes.windll.psapi.GetDeviceDriverBaseNameA(base_addr, driver_name, driver_name_size.value)
			driver_name_value = driver_name.value.decode('utf-8')
			if driver is None and driver_name_value.lower().find("krnl") != -1:
				return base_addr, driver_name_value
			elif driver_name_value.lower() == driver:
				return base_addr, driver_name_value
	return None
예제 #22
0
파일: u12.py 프로젝트: maunsell/MWorks
    def aiStreamRead(self, numScans, localID=None, timeout=1):
        """
        Name: U12.aiStreamRead(numScans, localID=None, timeout=1)
        Args: See section 4.9 of the User's Guide
        Desc: Waits for a specified number of scans to be available and reads them.

        >>> dev = U12()
        >>> dev.aiStreamStart(1, [0], 200)
        >>> dev.aiStreamRead(10)
        {'overVoltage': 0, 'ljScanBacklog': 0, 'stateIOout': <u12.c_long_Array_4096 object at 0x00DF4AD0>, 'reserved': 0, 'voltages': <u12.c_float_Array_4096_Array_4 object at 0x00DF4B20>}
        """
        
        # Check to make sure that we are streaming
        if not self.streaming:
            raise U12Exception(-1, "Streaming has not started")
        
        # Check id number
        if localID is None:
            localID = self.id
            
        # Create arrays and other ctypes
        arr4096_type = ctypes.c_float * 4096 
        voltages_type = arr4096_type * 4 
        voltages = voltages_type() 
        stateIOout = (ctypes.c_long * 4096)()
        reserved = ctypes.c_long(0)
        ljScanBacklog = ctypes.c_long(99999)
        overVoltage = ctypes.c_long(999)
        
        ecode = staticLib.AIStreamRead(localID, numScans, timeout, ctypes.byref(voltages), ctypes.byref(stateIOout), ctypes.byref(reserved), ctypes.byref(ljScanBacklog), ctypes.byref(overVoltage))
        
        if ecode != 0: raise U12Exception(ecode) # TODO: Switch this out for exception
        
        return {"voltages":voltages, "stateIOout":stateIOout, "reserved":reserved.value, "ljScanBacklog":ljScanBacklog.value, "overVoltage":overVoltage.value}
예제 #23
0
def list_available_devices():
    """
    Lists all devices connected to the computer.

    Returns
    -------
    out : list
        list of available devices. Each device is described by a tuple
        (hardware type, serial number)
    """
    # we have to check for all possible hardware types.
    # Unfortunately I couldn't find a list of all existing hardware types,
    # the list in the C header file is incomplete. Therefore we just check
    # the first 100 type values
    devices = []
    count = ctypes.c_long()
    for hwtype in range(100):
        if (_lib.GetNumHWUnitsEx(hwtype, ctypes.byref(count)) == 0):
            # found an existing hardware type
            if (count.value > 0):
                # devices are available!!
                # get their serial number
                serial_number = ctypes.c_long()
                for ii in range(count.value):
                    if (_lib.GetHWSerialNumEx(hwtype, ii, 
                        ctypes.byref(serial_number)) == 0):
                        devices.append((hwtype, serial_number.value))
    return devices
예제 #24
0
def call_sph_sdens(file_particles, Bsz, Nc, Np):

    dsx = ct.c_float(Bsz / Nc)
    Ngb = ct.c_long(32)
    xc1 = ct.c_float(0.0)
    xc2 = ct.c_float(0.0)
    xc3 = ct.c_float(0.0)
    mass_particle = ct.c_float(1e9)
    posx1 = np.zeros((Nc, Nc), dtype=ct.c_float)
    posx2 = np.zeros((Nc, Nc), dtype=ct.c_float)
    sdens = np.zeros((Nc, Nc), dtype=ct.c_float)

    sps.cal_sph_sdens(
        file_particles,
        ct.c_float(Bsz),
        ct.c_long(Nc),
        dsx,
        Ngb,
        ct.c_long(Np),
        xc1,
        xc2,
        xc3,
        mass_particle,
        posx1,
        posx2,
        sdens,
    )
    return sdens
예제 #25
0
    def get_hardware_limit_switches(self):
        """
        Returns hardware limit switch modes for reverse and forward direction.

        Returns
        -------
        out : tuple
            (reverse limit switch, forward limit switch)
            HWLIMSWITCH_IGNORE = 1 : Ignore limit switch (e.g. for stages
                with only one or no limit switches).
            HWLIMSWITCH_MAKES = 2	: Limit switch is activated when electrical
                continuity is detected.
            HWLIMSWITCH_BREAKS = 3 : Limit switch is activated when electrical
                continuity is broken.
            HWLIMSWITCH_MAKES_HOMEONLY = 4 : As per HWLIMSWITCH_MAKES except 
                switch is ignored other than when homing (e.g. to support 
                rotation stages).
            HWLIMSWITCH_BREAKS_HOMEONLY = 5 : As per HWLIMSWITCH_BREAKS except
                switch is ignored other than when homing (e.g. to support
                rotation stages).

        See also
        --------
        set_hardware_limit_switches
        """
        rev = ctypes.c_long()
        fwd = ctypes.c_long()
        err_code = _lib.MOT_GetHWLimSwitches(self._serial_number,
                ctypes.byref(rev), ctypes.byref(fwd))
        if (err_code != 0):
            raise Exception("Getting hardware limit switches failed: %s" %
                    _get_error_text(err_code))
        return (rev.value, fwd.value)
예제 #26
0
파일: two01.py 프로젝트: hooloong/gitpython
def tfcGetVersion():
    ma = C.c_long();
    mi = C.c_long();
    if TFC.tfcGetVersion(ma,mi):
        ver = "%i.%i" % (ma.value,mi.value)
    else:
        ver = "error!"
예제 #27
0
 def __init__(self):
     """Instantiate global vars"""
     support.glob = self
     # x11 reference to xlib library display and root window globals
     self.x11 = ctypes.CDLL(ctypes.util.find_library("X11"))
     self.x11.XOpenDisplay.restype = ctypes.c_void_p
     self.disp = ctypes.c_void_p(self.x11.XOpenDisplay(0))
     self.root = self.x11.XDefaultRootWindow(self.disp)
     # property atoms for moveresize
     # assigned once here so they are not recreated
     # every time moveresize is called
     self.fscreen_atom = self.x11.XInternAtom(self.disp, "_NET_WM_STATE_FULLSCREEN", False)
     self.maxv_atom = self.x11.XInternAtom(self.disp, "_NET_WM_STATE_MAXIMIZED_VERT", False)
     self.maxh_atom = self.x11.XInternAtom(self.disp, "_NET_WM_STATE_MAXIMIZED_HORZ", False)
     self.hidden_atom = self.x11.XInternAtom(self.disp, "_NET_WM_STATE_HIDDEN", False)
     self.sticky_atom = self.x11.XInternAtom(self.disp, "_NET_WM_STATE_STICKY", False)
     self.str_atom = self.x11.XInternAtom(self.disp, "UTF8_STRING", False)
     # GLOBAL returns for getwindowproperty
     self.ret_type = ctypes.c_long()
     self.ret_format = ctypes.c_long()
     self.num_items = ctypes.c_long()
     self.bytes_after = ctypes.c_long()
     self.ret_pointer = ctypes.pointer(ctypes.c_long())
     # xlib global "defines" for some standard atoms
     self.XA_CARDINAL = 6
     self.XA_WINDOW = 33
     self.XA_STRING = 31
     self.XA_ATOM = 4
     # GLOBAL size hints return
     self.size_hints_return = XSizeHints()
     self.screen_index = support.get_root_screen_index()
     self.str2_atom = self.x11.XInternAtom(self.disp, "STRING", False)
     self.num_monitors = gtk.gdk.screen_get_default().get_n_monitors()
     self.is_compiz_running = support.is_compiz_running()
     self.desktop_width, self.desktop_height = support.get_desktop_width_n_height()
예제 #28
0
    def get_dc_current_loop_parameters(self):
        """
        Returns DC current loop parameters.

        Returns
        -------
        out : tuple
            (proportional, integrator, integrator_limit, integrator_dead_band,
             fast_forward)
        """
        proportional = ctypes.c_long()
        integrator = ctypes.c_long()
        integrator_limit = ctypes.c_long()
        integrator_dead_band = ctypes.c_long()
        fast_forward = ctypes.c_long()
        err_code = _lib.MOT_GetDCCurrentLoopParams(self._serial_number,
                ctypes.byref(proportional),
                ctypes.byref(integrator),
                ctypes.byref(integrator_limit),
                ctypes.byref(integrator_dead_band),
                ctypes.byref(fast_forward))
        if (err_code != 0):
            raise Exception("Getting DC current loop parameters failed: %s" %
                    _get_error_text(err_code))
        return (proportional.value, integrator.value, integrator_limit.value,
                integrator_dead_band.value, fast_forward.value)
def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    if not inspect.isclass(exctype):
        raise TypeError("Only types can be raised (not instances)")

    if not issubclass(exctype, BaseException):
        raise ValueError("Only sub classes of BaseException can be raised")

    # PyThreadState_SetAsyncExc requires GIL to be held
    gil_state = ctypes.pythonapi.PyGILState_Ensure()
    try:
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid),
                                                         ctypes.py_object(exctype))
        if res == 0:
            # The thread is likely dead already.
            raise InvalidThreadIdError(tid)

        elif res != 1:
            # If more than one threads are affected (WTF?), we're in trouble, and
            # we try our best to revert the effect, although this may not work.
            ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), None)

            raise AsyncRaiseError("PyThreadState_SetAsyncExc failed", res)

    finally:
        ctypes.pythonapi.PyGILState_Release(gil_state)
예제 #30
0
파일: u12.py 프로젝트: maunsell/MWorks
    def counter(self, idNum=None, demo=0, resetCounter=0, enableSTB=1):
        """
        Name: U12.counter(idNum=None, demo=0, resetCounter=0, enableSTB=1)
        Args: See section 4.15 of the User's Guide
        Desc: Converts a voltage to it's 12-bit (0-4095) binary representation. No hardware communication is involved.

        >>> dev = U12()
        >>> dev.counter(0, 0, 3)
        >>> {'bits': 2662}
        """
        
        #Check id number
        if idNum is None:
            idNum = self.id
        idNum = ctypes.c_long(idNum)
        
        # Create ctypes
        stateD = ctypes.c_long(999)
        stateIO = ctypes.c_long(999)
        count = ctypes.c_ulong(999)
        
        print idNum
        ecode = staticLib.Counter(ctypes.byref(idNum), demo, ctypes.byref(stateD), ctypes.byref(stateIO), resetCounter, enableSTB, ctypes.byref(count))

        if ecode != 0: raise U12Exception(ecode)
        
        return {"idnum":idNum.value, "stateD": stateD.value, "stateIO":stateIO.value, "count":count.value}
예제 #31
0
    def SetTiming(self,
                  SequenceId=None,
                  illuminationTime=None,
                  pictureTime=None,
                  synchDelay=None,
                  synchPulseWidth=None,
                  triggerInDelay=None):
        '''
        Set the timing properties of the sequence to display.

        Usage:
            
        SetTiming( SequenceId = None, illuminationTime = None, pictureTime = None, synchDelay = None, \
                  synchPulseWidth = None, triggerInDelay = None)
            
        PARAMETERS
        ----------
        
        SequenceId : c_ulong, optional
                       Identified of the sequence. If not specified, set the last sequence allocated in the DMD board memory
        illuminationTime: c_ulong, optional
                           Display time of a single image of the sequence in microseconds. 
                           If not specified, use the highest possible value compatible with pictureTime.
        pictureTime : int, optional
                        Time between the start of two consecutive picture, up to 10^7 microseconds = 10 seconds.
                        With illuminationTime, it sets the display rate.
                        If not specified, the value is set to minimize the dark time according illuminationTime.
                        If illuminationTime is also not specified, set to a frame rate of 30Hz.
        synchDelay : Specifies the time delay between the start of the output sync pulse and the start of the display (master mode).
                       Value between 0 and 130,000 microseconds. Set to 0 if not specified.
        synchPulseWidth : Duration of the sync output pulse. 
                         By default equals synchDelay + illuminationTime in normal mode.
                         By default equals ALP_ILLUMINATION_TIME in binary uninterrupted mode.
        triggerInDelay : Length of the trigger signal in microseconds, set to 0 by default.
        
            
        SEE ALSO
        --------
        See ALPLib.AlpSeqAlloc in the ALP API description for more information.
        '''
        if (SequenceId == None) and (self._lastDDRseq):
            SequenceId = self._lastDDRseq
        if (SequenceId == None):
            self._raiseError('No sequence to display.')

        if (synchDelay == None):
            synchDelay = ALP_DEFAULT
        if (synchPulseWidth == None):
            synchPulseWidth = ALP_DEFAULT
        if (triggerInDelay == None):
            triggerInDelay = ALP_DEFAULT
        if (illuminationTime == None):
            illuminationTime = ALP_DEFAULT
        if (pictureTime == None):
            pictureTime = ALP_DEFAULT

        self._checkError(
            self._ALPLib.AlpSeqTiming(self.ALP_ID, SequenceId,
                                      ct.c_long(illuminationTime),
                                      ct.c_long(pictureTime),
                                      ct.c_long(synchDelay),
                                      ct.c_long(synchPulseWidth),
                                      ct.c_long(triggerInDelay)),
            'Cannot set timing.')
예제 #32
0
파일: log.py 프로젝트: zero804/armory
WARN = 35
ERROR = 31

if platform.system() == "Windows":
    HAS_COLOR_SUPPORT = platform.release() == "10"

    if HAS_COLOR_SUPPORT:
        # Enable ANSI codes. Otherwise, the ANSI sequences might not be
        # evaluated correctly for the first colored print statement.
        import ctypes
        kernel32 = ctypes.windll.kernel32

        # -11: stdout
        handle_out = kernel32.GetStdHandle(-11)

        console_mode = ctypes.c_long()
        kernel32.GetConsoleMode(handle_out, ctypes.byref(console_mode))

        # 0b100: ENABLE_VIRTUAL_TERMINAL_PROCESSING, enables ANSI codes
        # see https://docs.microsoft.com/en-us/windows/console/setconsolemode
        console_mode.value |= 0b100
        kernel32.SetConsoleMode(handle_out, console_mode)
else:
    HAS_COLOR_SUPPORT = True

info_text = ''
num_warnings = 0


def clear(clear_warnings=False):
    global info_text, num_warnings
예제 #33
0
# -*- coding: utf-8 -*-
from ctypes import (byref, create_unicode_buffer, c_long, c_ulong, c_wchar_p,
                    windll)
from struct import unpack_from

STATUS_BUFFER_TO_SMALL = c_long(0xC0000023).value

FormatMessage = windll.kernel32.FormatMessageW
GetLastError = windll.kernel32.GetLastError
NtQuerySystemInformation = windll.ntdll.NtQuerySystemInformation
RtlNtStatusToDosError = windll.ntdll.RtlNtStatusToDosError

NtQuerySystemInformation.restype = c_long


def getlasterror(nts):
    msg = create_unicode_buffer(0x100)
    print('Unknown error has been occured.' if not FormatMessage(
        0x12FF, None,
        RtlNtStatusToDosError(nts) if 0 != nts else GetLastError(
        ), 1024, msg, len(msg), None) else msg.value)


def getsystemdrive():
    req = c_ulong()
    nts = NtQuerySystemInformation(99, None, None, byref(req))
    if STATUS_BUFFER_TO_SMALL != nts:
        getlasterror(nts)
        return
    buf = create_unicode_buffer(req.value)
    nts = NtQuerySystemInformation(99, buf, len(buf), None)
예제 #34
0
 def __enter__(self):
     """Disable the windows file system redirection in the with statement."""
     self.old_value = ctypes.c_long()
     self.success = self._disable(ctypes.byref(self.old_value))
예제 #35
0
파일: utils.py 프로젝트: zcddtb/labeless
                idx = len(guarded) - 1
            guarded[idx]['size'] += GRANULARITY
            gpoints[i] = idx

    ea = base + size - GRANULARITY
    if ea in gpoints:
        while True:
            ea += GRANULARITY
            if VirtualQueryEx(h_process, C.c_void_p(ea), C.byref(mbi), C.sizeof(mbi)) and\
                        mbi.Protect & D.PAGE_GUARD:
                guarded[-1]['size'] += GRANULARITY
            else:
                break

    # turn off page guard before read
    dummy = C.c_long()
    for g in guarded:
        for off in range(0, g['size'], GRANULARITY):
            g['ok'] = VirtualProtectEx(h_process, C.c_void_p(g['ea'] + off),
                                       GRANULARITY,
                                       C.c_long(g['p'] & ~D.PAGE_GUARD),
                                       C.byref(dummy))

    for i in long_xrange(base, base + size, GRANULARITY):
        p_addr = C.c_void_p(i)
        if VirtualQueryEx(h_process, p_addr, C.byref(mbi), C.sizeof(mbi)):
            if mbi.Protect & D.PAGE_GUARD:
                # TODO
                pass
            mem = unsafe_read_process_memory(i, GRANULARITY)
            if mem is None:
 def __enter__(self):
     self.old_value = ctypes.c_long()
     self.success = self._disable(ctypes.byref(self.old_value))
예제 #37
0
    def test_PyThreadState_SetAsyncExc(self):
        try:
            import ctypes
        except ImportError:
            self.skipTest('requires ctypes')

        set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc

        class AsyncExc(Exception):
            pass

        exception = ctypes.py_object(AsyncExc)

        # First check it works when setting the exception from the same thread.
        tid = thread.get_ident()

        try:
            result = set_async_exc(ctypes.c_long(tid), exception)
            # The exception is async, so we might have to keep the VM busy until
            # it notices.
            while True:
                pass
        except AsyncExc:
            pass
        else:
            # This code is unreachable but it reflects the intent. If we wanted
            # to be smarter the above loop wouldn't be infinite.
            self.fail("AsyncExc not raised")
        try:
            self.assertEqual(result, 1)  # one thread state modified
        except UnboundLocalError:
            # The exception was raised too quickly for us to get the result.
            pass

        # `worker_started` is set by the thread when it's inside a try/except
        # block waiting to catch the asynchronously set AsyncExc exception.
        # `worker_saw_exception` is set by the thread upon catching that
        # exception.
        worker_started = threading.Event()
        worker_saw_exception = threading.Event()

        class Worker(threading.Thread):
            def run(self):
                self.id = thread.get_ident()
                self.finished = False

                try:
                    while True:
                        worker_started.set()
                        time.sleep(0.1)
                except AsyncExc:
                    self.finished = True
                    worker_saw_exception.set()

        t = Worker()
        t.daemon = True  # so if this fails, we don't hang Python at shutdown
        t.start()
        if verbose:
            print "    started worker thread"

        # Try a thread id that doesn't make sense.
        if verbose:
            print "    trying nonsensical thread id"
        result = set_async_exc(ctypes.c_long(-1), exception)
        self.assertEqual(result, 0)  # no thread states modified

        # Now raise an exception in the worker thread.
        if verbose:
            print "    waiting for worker thread to get started"
        ret = worker_started.wait()
        self.assertTrue(ret)
        if verbose:
            print "    verifying worker hasn't exited"
        self.assertTrue(not t.finished)
        if verbose:
            print "    attempting to raise asynch exception in worker"
        result = set_async_exc(ctypes.c_long(t.id), exception)
        self.assertEqual(result, 1)  # one thread state modified
        if verbose:
            print "    waiting for worker to say it caught the exception"
        worker_saw_exception.wait(timeout=10)
        self.assertTrue(t.finished)
        if verbose:
            print "    all OK -- joining worker"
        if t.finished:
            t.join()
예제 #38
0
    def SeqPutEx(self,
                 imgData,
                 LineOffset,
                 LineLoad,
                 SequenceId=None,
                 PicOffset=0,
                 PicLoad=0,
                 dataFormat='Python'):
        '''
        Image data transfer using AlpSeqPut is based on whole DMD frames. Applications that only
        update small regions inside a frame suffer from overhead of this default behavior. An extended 
        ALP API function is available to reduce this overhead.

        The AlpSeqPutEx function offers the same functionality as the standard function (AlpSeqPut), 
        but in addition, it is possible to select a section within a sequence frame using the
        LineOffset and LineLoad parameters of the tAlpLinePut data-structure (see below) and update 
        only this section of the SDRAM-memory associated with the sequence for a range of
        sequence-pictures (selected via the PicOffset and PicLoad parameters of tAlpLinePut in 
        similarity to AlpSeqPut).

        This results in accelerated transfer-time of small image data updates (due to the fact that the
        amount of transferred data is reduced).

        Therefore, the user only passes the lines of the pictures he wants to update via the UserArrayPtr
        (that would be PicLoad*LineLoad lines in total).

        PARAMETERS
        ----------
        
        imgData : list, 1D array or 1D ndarray
                  Data stream corresponding to a sequence of nSizeX by nSizeX images.
                  Values has to be between 0 and 255.
        LineOffset : int
                     Defines the offset of the frame-section. The frame-data of this section is transferred
                     for each of the frames selected with PicOffset and PicLoad. The value of this 
                     parameter must be greater or equal to zero, otherwise ALP_PARM_INVALID is returned.
        LineLoad : int
                   Defines the size of the frame-section. If the value of the parameter is
                   less than zero or if LineOffset+LineLoad exceeds the number of lines
                   per sequence-frame, ALP_PARM_INVALID is returned. If LineLoad is
                   zero, this value is adjusted to include all lines of the frame, starting at
                   line LineOffset
        SequenceId : ctypes c_long
                     Sequence identifier. If not specified, set the last sequence allocated in the DMD board memory
        PicOffset : int, optional
                    Picture number in the sequence (starting at 0) where the data upload is 
                    started; the meaning depends upon ALP_DATA_FORMAT.
                    By default, PifOffset = 0.
        PicLoad : int, optional
                 number of pictures that are to be loaded into the sequence memory. 
                 Depends on ALP_DATA_FORMAT.
                 PicLoad = 0 correspond to a complete sequence.
                 By default, PicLoad = 0.
        dataFormat : string, optional
                 Specify the type of data sent as image.
                 Should be ' Python' or 'C'.
                 If the data is of Python format, it is converted into a C array before sending to the DMD via the dll.                 
                 By default dataFormat = 'Python'
        '''

        if not SequenceId:
            SequenceId = self._lastDDRseq

        LinePutParam = tAlpLinePut(ALP_PUT_LINES, ct.c_long(PicOffset),
                                   ct.c_long(PicLoad), ct.c_long(LineOffset),
                                   ct.c_long(LineLoad))

        if dataFormat == 'Python':
            pImageData = (ct.c_ubyte * imgData.size)()
            for ind, x in enumerate(imgData):
                pImageData[ind] = x
        elif dataFormat == 'C':
            pImageData = ct.cast(imgData, ct.c_void_p)

        self._checkError(
            self._ALPLib.AlpSeqPutEx(self.ALP_ID, SequenceId, LinePutParam,
                                     pImageData),
            'Cannot send image sequence to device.')
예제 #39
0
파일: _objects.py 프로젝트: jinxuan/dill
a['FormatterType'] = logging.Formatter() # pickle ok
a['FilterType'] = logging.Filter() # pickle ok
a['LogRecordType'] = logging.makeLogRecord(_dict) # pickle ok
a['OptionParserType'] = _oparser = optparse.OptionParser() # pickle ok
a['OptionGroupType'] = optparse.OptionGroup(_oparser,"foo") # pickle ok
a['OptionType'] = optparse.Option('--foo') # pickle ok
if HAS_CTYPES:
    a['CCharType'] = _cchar = ctypes.c_char()
    a['CWCharType'] = ctypes.c_wchar() # fail == 2.6
    a['CByteType'] = ctypes.c_byte()
    a['CUByteType'] = ctypes.c_ubyte()
    a['CShortType'] = ctypes.c_short()
    a['CUShortType'] = ctypes.c_ushort()
    a['CIntType'] = ctypes.c_int()
    a['CUIntType'] = ctypes.c_uint()
    a['CLongType'] = ctypes.c_long()
    a['CULongType'] = ctypes.c_ulong()
    a['CLongLongType'] = ctypes.c_longlong()
    a['CULongLongType'] = ctypes.c_ulonglong()
    a['CFloatType'] = ctypes.c_float()
    a['CDoubleType'] = ctypes.c_double()
    a['CSizeTType'] = ctypes.c_size_t()
    a['CLibraryLoaderType'] = ctypes.cdll
    a['StructureType'] = _Struct
    a['BigEndianStructureType'] = ctypes.BigEndianStructure()
#NOTE: also LittleEndianStructureType and UnionType... abstract classes
#NOTE: remember for ctypesobj.contents creates a new python object
#NOTE: ctypes.c_int._objects is memberdescriptor for object's __dict__
#NOTE: base class of all ctypes data types is non-public _CData

try: # python 2.6
예제 #40
0
    def SeqPut(self,
               imgData,
               SequenceId=None,
               PicOffset=0,
               PicLoad=0,
               dataFormat='Python'):
        '''
        This  function  allows  loading user  supplied  data  via  the  USB  connection  into  the  ALP  memory  of  a 
        previously allocated sequence (AlpSeqAlloc) or a part of such a sequence. The loading operation can 
        run  concurrently to  the  display  of  other sequences.  Data  cannot be  loaded  into  sequences that  are 
        currently started for display. Note: This protection can be disabled by ALP_SEQ_PUT_LOCK.
        
        The function loads PicNum pictures into the ALP memory reserved for the specified sequence starting 
        at picture PicOffset. The calling program is suspended until the loading operation is completed.
        
        The  ALP  API  compresses  image  data  before  sending  it  over  USB.  This  results  in  a  virtual 
        improvement of data transfer speed. Compression ratio is expected to vary depending on image data. 
        Incompressible data do not cause overhead delays.
        
        Usage:
        SeqPut(imgData, nbImg = 1, bitDepth = 1)
        
        PARAMETERS
        ----------
        
        imgData : list, 1D array or 1D ndarray
                  Data stream corresponding to a sequence of nSizeX by nSizeX images.
                  Values has to be between 0 and 255.
        SequenceId : ctypes c_long
                     Sequence identifier. If not specified, set the last sequence allocated in the DMD board memory
        PicOffset : int, optional
                    Picture number in the sequence (starting at 0) where the data upload is 
                    started; the meaning depends upon ALP_DATA_FORMAT.
                    By default, PifOffset = 0.
        PicLoad : int, optional
                 number of pictures that are to be loaded into the sequence memory. 
                 Depends on ALP_DATA_FORMAT.
                 PicLoad = 0 correspond to a complete sequence.
                 By default, PicLoad = 0.
        dataFormat : string, optional
                 Specify the type of data sent as image.
                 Should be ' Python' or 'C'.
                 If the data is of Python format, it is converted into a C array before sending to the DMD via the dll.                 
                 By default dataFormat = 'Python'
                
        SEE ALSO
        --------
        
        See ALPLib.AlpSeqPut in the ALP API description for more information.
        '''

        if not SequenceId:
            SequenceId = self._lastDDRseq

        if dataFormat == 'Python':
            pImageData = (ct.c_ubyte * imgData.size)()
            for ind, x in enumerate(imgData):
                pImageData[ind] = x
        elif dataFormat == 'C':
            pImageData = ct.cast(imgData, ct.c_void_p)

        self._checkError(
            self._ALPLib.AlpSeqPut(self.ALP_ID,
                                   SequenceId, ct.c_long(PicOffset),
                                   ct.c_long(PicLoad), pImageData),
            'Cannot send image sequence to device.')
예제 #41
0
 def get_steps_remaining(self):
     steps = c_long()
     self._libfli.FLIGetStepsRemaining(self._dev, byref(steps))
     return steps.value
예제 #42
0
ALP_FLAG_RSVD0 = ct.c_ulong(16)  # reserved


# for AlpSeqPutEx():
class tAlpLinePut(ct.Structure):
    _fields_ = [
        ("TransferMode", ct.c_long
         ),  # common first member of AlpSeqPutEx' UserStructPtr argument
        ("PicOffset", ct.c_long),
        ("PicLoad", ct.c_long),
        ("LineOffset", ct.c_long),
        ("LineLoad", ct.c_long)
    ]


ALP_PUT_LINES = ct.c_long(
    1)  # not ulong; need to be long in the tAlpLinePut struct

ALP_ERRORS = {
    1001: 'The specified ALP device has not been found or is not ready.',
    1002: 'The ALP device is not in idle state.',
    1003: 'The specified ALP device identifier is not valid.',
    1004: 'The specified ALP device is already allocated.',
    1005: 'One of the parameters is invalid.',
    1006: 'Error accessing user data.',
    1007: 'The requested memory is not available (full?).',
    1008: 'The sequence specified is currently in use.',
    1009:
    'The ALP device has been stopped while image data transfer was active.',
    1010: 'Initialization error.',
    1011: 'Communication error.',
    1012: 'The specified ALP has been removed.',
예제 #43
0
 def NuiCameraElevationGetAngle(self):
     res = ctypes.c_long()
     _NuiInstance._NuiCameraElevationGetAngle(self, ctypes.byref(res))
     return res.value
예제 #44
0
 def get_stepper_position(self):
     pos = c_long()
     self._libfli.FLIGetStepperPosition(self._dev, byref(pos))
     self.stepper_position = pos.value
     return pos.value
예제 #45
0
 def _time():
     if _clock_gettime(ctypes.c_long(_clockid),
                       ctypes.pointer(tv)) != 0:
         _ernno = ctypes.get_errno()
         raise OSError(_ernno, strerror(_ernno))
     return tv.tv_sec + (tv.tv_usec * 0.000000001)
예제 #46
0
 def __init__(self, dev_name, model):
     USBDevice.__init__(self, dev_name=dev_name, model=model)
     self.stepper_position = None
     extent = c_long()
     self._libfli.FLIGetFocuserExtent(self._dev, byref(extent))
     self.stepper_max_extent = extent.value
예제 #47
0
파일: guzik.py 프로젝트: hgrollnt/pyHegel
 def config(self, channels=None, n_S_ch=1024, bits_16=True, gain_dB=0.):
     """
     if channels is None, it returns information about the current config.
     channels needs be a list of integer that represent the channels (1-4).
     It can also be a single integer
     bits_16 when False, returns 8 bit data.
     n_S_ch is the number of Sample per ch to read.
     To free the memory, delete any user variable that remembers a previous result,
     than call config with a new size.
     """
     if channels is None:
         return self._read_config()
     if not isinstance(channels, (np.ndarray, list, tuple)):
         channels = [channels]
     channels = sorted(set(channels)) # Make list of unique values and sorted.
     if not all([1<=c<=4 for c in channels]):
         raise ValueError(self.perror('Invalid channel number. Needs to be a number from 1 to 4.'))
     Nch = len(channels)
     if Nch == 0:
         raise RuntimeError(self.perror('Invalid number of channels'))
     if n_S_ch > 52.5*GiS:
         # This is only for 1 channels or 2 interleaved (1,3 or 2,3, or 2,4 or 1,4)
         # There is 64 GiB of ECC memory with 15/16 used. The 10 bit of a sample
         # is packed as 7 words of 10 bits into 72 bits of ECC (8 words of 9 bits)
         # So the packing is 7 samples (70 bits) into 8 words (72 bits)
         # 64/16.*15*7/8 = 52.5
         raise RuntimeError(self.perror('Maximum hardware request is 52.5 GiS'))
     S2B = 2 if bits_16 else 1
     if n_S_ch*Nch*S2B > (self._computer_memsize -  5*GiS):
         raise RuntimeError(self.perror('You are requesting more memory than is available (with a reserve of 5 GiB)'))
     self._destroy_op()
     self._gsa_Nch = Nch
     channels_list = ','.join(['CH%i'%i for i in channels])
     conf = c_long()
     SDK = self._gsasdk
     board_index = self._board_index
     if SDK.GSA_ReadChBestConfigGet(board_index, SDK.GSA_READ_CH_INP1, channels_list, byref(conf)) == SDK.GSA_FALSE:
         raise RuntimeError(self.perror('Unable to obtain best config.'))
     self._gsa_conf = conf.value
     # now obtain the conf
     chrarg = SDK.GSA_READ_CH_CFG_INFO_ARG(version=SDK.GSA_SDK_VERSION, rc_conf=conf)
     chrres = SDK.GSA_READ_CH_CFG_INFO_RES()
     if SDK.GSA_ReadChCfgInfoGet(chrarg, chrres) == SDK.GSA_FALSE:
         raise RuntimeError(self.perror('Unable to read best config.'))
     self._gsa_conf_ch = chrres
     # Now setup acquisition
     hdr_default = SDK.GSA_ARG_HDR(version=SDK.GSA_SDK_VERSION)
     arg = SDK.GSA_Data_ARG(hdr=hdr_default)
     res_arr = (SDK.GSA_Data_RES*4)() # array of 4 RES
     if SDK.GSA_Data_Multi_Info(arg, Nch, res_arr, None) == SDK.GSA_FALSE:
         raise RuntimeError(self.perror('Unable to initialize acq structures.'))
     arg.common.rc_idx = 0
     arg.common.rc_conf = conf
     arg.common.input_labels_list = channels_list
     arg.common.acq_len = int(n_S_ch)
     #arg.common.acq_time_ns = 1000
     arg.common.sectors_num = 1
     #arg.common.sector_time_ns = 1000
     #arg.common.acq_timeout = 0 # in us. -1 for infinite
     arg.common.acq_adjust_up = SDK.GSA_TRUE
     arg.common.trigger_mode = SDK.GSA_DP_TRIGGER_MODE_IMMEDIATE
     arg.common.gain_dB = gain_dB
     ts = [np.zeros(10, np.uint) for i in range(4)]
     tf = [np.zeros(10, np.uint64) for i in range(4)]
     self._gsa_data_res_ts = ts # timestamp in seconds
     self._gsa_data_res_tf = tf # timestamp in femtoseconds
     for i in range(4):
         res_arr[i].common.timestamp_seconds.size = len(ts[i])
         res_arr[i].common.timestamp_seconds.arr = ts[i].ctypes.data_as(POINTER(c_uint))
         res_arr[i].common.timestamp_femtoseconds.size = len(tf[i])
         res_arr[i].common.timestamp_femtoseconds.arr = tf[i].ctypes.data_as(POINTER(c_uint64))
     if bits_16:
         arg.common.data_type = SDK.GSA_DATA_TYPE_INT15BIT
     else:
         arg.common.data_type = SDK.GSA_DATA_TYPE_SHIFTED8BIT
     arg.hdr.op_command = SDK.GSA_OP_CONFIGURE
     if SDK.GSA_Data_Multi(arg, Nch, res_arr) == SDK.GSA_FALSE:
         raise RuntimeError(self.perror('Unable to finish initializing acq structure.'))
     self._gsa_data_arg = arg
     self._gsa_data_res_arr = res_arr
     # free previous data memory
     self.fetch.setcache(None)
     self._gsa_data = None
     N = res_arr[0].common.data_len
     for i in range(Nch):
         if res_arr[i].common.data_len != N:
             # if we see this exception then the algo below will need to change.
             raise RuntimeError(self.perror('Some channels are not expecting the same data length.'))
     if Nch > 1:
         dims = (Nch, N)
     else:
         dims = N
     if bits_16:
         data = np.empty(dims, np.int16)
     else:
         data = np.empty(dims, np.uint8)
     data_2d = data if Nch>1 else data.reshape((1, -1))
     for i in range(Nch):
         res_arr[i].common.data.arr = data_2d[i].ctypes.data_as(POINTER(c_ubyte))
         res_arr[i].common.data.size = data_2d[i].nbytes
     self._gsa_data = data
def stdapi_sys_process_get_processes_via_windll(request, response):
    TH32CS_SNAPPROCESS = 2
    PROCESS_QUERY_INFORMATION = 0x0400
    PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
    PROCESS_VM_READ = 0x10
    TOKEN_QUERY = 0x0008
    TokenUser = 1
    k32 = ctypes.windll.kernel32
    pe32 = PROCESSENTRY32()
    pe32.dwSize = ctypes.sizeof(PROCESSENTRY32)
    proc_snap = k32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    result = k32.Process32First(proc_snap, ctypes.byref(pe32))
    if not result:
        return ERROR_FAILURE, response
    while result:
        proc_h = k32.OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ),
                                 False, pe32.th32ProcessID)
        if not proc_h:
            proc_h = k32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False,
                                     pe32.th32ProcessID)
        exe_path = (ctypes.c_char * 1024)()
        success = False
        if hasattr(ctypes.windll.psapi, 'GetModuleFileNameExA'):
            success = ctypes.windll.psapi.GetModuleFileNameExA(
                proc_h, 0, exe_path, ctypes.sizeof(exe_path))
        elif hasattr(k32, 'GetModuleFileNameExA'):
            success = k32.GetModuleFileNameExA(proc_h, 0, exe_path,
                                               ctypes.sizeof(exe_path))
        if not success and hasattr(k32, 'QueryFullProcessImageNameA'):
            dw_sz = ctypes.c_uint32()
            dw_sz.value = ctypes.sizeof(exe_path)
            success = k32.QueryFullProcessImageNameA(proc_h, 0, exe_path,
                                                     ctypes.byref(dw_sz))
        if not success and hasattr(ctypes.windll.psapi,
                                   'GetProcessImageFileNameA'):
            success = ctypes.windll.psapi.GetProcessImageFileNameA(
                proc_h, exe_path, ctypes.sizeof(exe_path))
        if success:
            exe_path = ctypes.string_at(exe_path)
        else:
            exe_path = ''
        complete_username = ''
        tkn_h = ctypes.c_long()
        tkn_len = ctypes.c_uint32()
        if ctypes.windll.advapi32.OpenProcessToken(proc_h, TOKEN_QUERY,
                                                   ctypes.byref(tkn_h)):
            ctypes.windll.advapi32.GetTokenInformation(tkn_h, TokenUser,
                                                       None, 0,
                                                       ctypes.byref(tkn_len))
            buf = (ctypes.c_ubyte * tkn_len.value)()
            if ctypes.windll.advapi32.GetTokenInformation(
                    tkn_h, TokenUser, ctypes.byref(buf), ctypes.sizeof(buf),
                    ctypes.byref(tkn_len)):
                user_tkn = SID_AND_ATTRIBUTES()
                ctypes.memmove(ctypes.byref(user_tkn), buf,
                               ctypes.sizeof(user_tkn))
                username = (ctypes.c_char * 512)()
                domain = (ctypes.c_char * 512)()
                u_len = ctypes.c_uint32()
                u_len.value = ctypes.sizeof(username)
                d_len = ctypes.c_uint32()
                d_len.value = ctypes.sizeof(domain)
                use = ctypes.c_ulong()
                use.value = 0
                ctypes.windll.advapi32.LookupAccountSidA(
                    None, user_tkn.Sid, username, ctypes.byref(u_len), domain,
                    ctypes.byref(d_len), ctypes.byref(use))
                complete_username = ctypes.string_at(
                    domain) + '\\' + ctypes.string_at(username)
            k32.CloseHandle(tkn_h)
        parch = windll_GetNativeSystemInfo()
        is_wow64 = ctypes.c_ubyte()
        is_wow64.value = 0
        if hasattr(k32, 'IsWow64Process'):
            if k32.IsWow64Process(proc_h, ctypes.byref(is_wow64)):
                if is_wow64.value:
                    parch = PROCESS_ARCH_X86
        pgroup = ''
        pgroup += tlv_pack(TLV_TYPE_PID, pe32.th32ProcessID)
        pgroup += tlv_pack(TLV_TYPE_PARENT_PID, pe32.th32ParentProcessID)
        pgroup += tlv_pack(TLV_TYPE_USER_NAME, complete_username)
        pgroup += tlv_pack(TLV_TYPE_PROCESS_NAME, pe32.szExeFile)
        pgroup += tlv_pack(TLV_TYPE_PROCESS_PATH, exe_path)
        pgroup += tlv_pack(TLV_TYPE_PROCESS_ARCH, parch)
        response += tlv_pack(TLV_TYPE_PROCESS_GROUP, pgroup)
        result = k32.Process32Next(proc_snap, ctypes.byref(pe32))
        k32.CloseHandle(proc_h)
    k32.CloseHandle(proc_snap)
    return ERROR_SUCCESS, response
예제 #49
0
def djb_hash(s):
    import ctypes
    seed = 5381
    for i in s:
        seed = ((seed << 5) + seed) + ord(i)
    return ctypes.c_long(seed).value
예제 #50
0
    def SKIP_test_PyThreadState_SetAsyncExc(self):
        try:
            import ctypes
        except ImportError:
            if verbose:
                print("test_PyThreadState_SetAsyncExc can't import ctypes")
            return  # can't do anything

        set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc

        class AsyncExc(Exception):
            pass

        exception = ctypes.py_object(AsyncExc)

        # `worker_started` is set by the thread when it's inside a try/except
        # block waiting to catch the asynchronously set AsyncExc exception.
        # `worker_saw_exception` is set by the thread upon catching that
        # exception.
        worker_started = threading.Event()
        worker_saw_exception = threading.Event()

        class Worker(threading.Thread):
            def run(self):
                self.id = thread.get_ident()
                self.finished = False

                try:
                    while True:
                        worker_started.set()
                        time.sleep(0.1)
                except AsyncExc:
                    self.finished = True
                    worker_saw_exception.set()

        t = Worker()
        t.daemon = True  # so if this fails, we don't hang Python at shutdown
        t.start()
        if verbose:
            print("    started worker thread")

        # Try a thread id that doesn't make sense.
        if verbose:
            print("    trying nonsensical thread id")
        result = set_async_exc(ctypes.c_long(-1), exception)
        self.assertEqual(result, 0)  # no thread states modified

        # Now raise an exception in the worker thread.
        if verbose:
            print("    waiting for worker thread to get started")
        worker_started.wait()
        if verbose:
            print("    verifying worker hasn't exited")
        self.assert_(not t.finished)
        if verbose:
            print("    attempting to raise asynch exception in worker")
        result = set_async_exc(ctypes.c_long(t.id), exception)
        self.assertEqual(result, 1)  # one thread state modified
        if verbose:
            print("    waiting for worker to say it caught the exception")
        worker_saw_exception.wait(timeout=10)
        self.assert_(t.finished)
        if verbose:
            print("    all OK -- joining worker")
        if t.finished:
            t.join()
예제 #51
0
    coord = feature['geometry']['coordinates']
    while isinstance(coord[0], list):
        coord = coord[0]
    latlng = s2sphere.LatLng.from_degrees(coord[1], coord[0])
    print 'latitude: ', coord[1], '\tlongitude: ', coord[0]

    # Best Solution?
    cell = s2sphere.CellId.from_lat_lng(latlng)
    cell_lv19 = s2sphere.CellId.from_lat_lng(latlng).parent(19)
    cell_lv16 = s2sphere.CellId.from_lat_lng(latlng).parent(16)
    cell_lv15 = s2sphere.CellId.from_lat_lng(latlng).parent(15)
    cell_lv13 = s2sphere.CellId.from_lat_lng(latlng).parent(13)
    cell_lv11 = s2sphere.CellId.from_lat_lng(latlng).parent(11)

    # Convert Magic Python Type to Signed 64-bit Int
    cellID = ctypes.c_long(cell.id()).value
    cellID_11 = ctypes.c_long(cell_lv11.id()).value
    cellID_13 = ctypes.c_long(cell_lv13.id()).value
    cellID_15 = ctypes.c_long(cell_lv15.id()).value
    cellID_16 = ctypes.c_long(cell_lv16.id()).value
    cellID_19 = ctypes.c_long(cell_lv19.id()).value

    session.execute(ps_node_lv19, (cellID, cellID_19, tuid, jsonFeature))
    session.execute(ps_node_lv15,
                    (cellID, cellID_15, cellID_16, tuid, jsonFeature))
    session.execute(ps_node_lv11,
                    (cellID, cellID_11, cellID_13, tuid, jsonFeature))

#    else:
#       a = 0
#       print 'found a complex structure!'
예제 #52
0
def async_raise(thread_obj, exception):
    """Raise an exception in another thread.

    thread_obj: `threading.Thread` object
        The target thread to inject the exception into. Must be running.
    exception: ``Exception``
        The exception to be raised. As with regular `raise`, this may be
        an exception instance or an exception class object.

    No return value. Normal return indicates success.

    If the specified `threading.Thread` is not active, or the thread's ident
    was not accepted by the interpreter, raises `ValueError`.

    If the raise operation failed internally, raises `SystemError`.

    If not supported for the Python implementation we're currently running on,
    raises `NotImplementedError`.

    **NOTE**: This currently works only in CPython, because there is no Python-level
    API to achieve what this function needs to do, and PyPy3's C API emulation layer
    `cpyext` doesn't currently (January 2020) implement the function required to do
    this (and the C API functions in `cpyext` are not exposed to the Python level
    anyway, unlike CPython's `ctypes.pythonapi`).

    **CAUTION**: This is **potentially dangerous**. If the async raise
    operation fails, the interpreter may be left in an inconsistent state.

    **NOTE**: The term `async` here has nothing to do with `async`/`await`;
    instead, it refers to an asynchronous exception such as `KeyboardInterrupt`.
        https://en.wikipedia.org/wiki/Exception_handling#Exception_synchronicity

    In a nutshell, a *synchronous* exception (i.e. the usual kind of exception)
    has an explicit `raise` somewhere in the code that the thread that
    encountered the exception is running. In contrast, an *asynchronous*
    exception **doesn't**, it just suddenly magically materializes from the outside.
    As such, it can in principle happen *anywhere*, with absolutely no hint about
    it in any obvious place in the code.

    **Hence, use this function very, very sparingly, if at all.**

    For example, `unpythonic` only uses this to support remotely injecting a
    `KeyboardInterrupt` into a REPL session running in another thread. So this
    may be interesting mainly if you're developing your own REPL server/client
    pair.

    (Incidentally, that's **not** how `KeyboardInterrupt` usually works.
    Rather, the OS sends a SIGINT, which is then trapped by an OS signal
    handler that runs in the main thread. At that point the magic has already
    happened: the control of the main thread is now inside the signal handler,
    as if the signal handler was called from the otherwise currently innermost
    point on the call stack. All the handler needs to do is to perform a regular
    `raise`, and the exception will propagate correctly.

    REPL sessions running in other threads can't use the standard mechanism,
    because in CPython, OS signal handlers only run in the main thread, and even
    in PyPy3, there is no guarantee *which* thread gets the signal even if you
    use `with __pypy__.thread.signals_enabled` to enable OS signal trapping in
    some of your other threads. Only one thread (including the main thread, plus
    any currently dynamically within a `signals_enabled`) will see the signal;
    which one, is essentially random and not even reproducible.)

    See also:
        https://vorpus.org/blog/control-c-handling-in-python-and-trio/

    The function necessary to perform this magic is actually mentioned right
    there in the official CPython C API docs, but it's not very well known:
        https://docs.python.org/3/c-api/init.html#c.PyThreadState_SetAsyncExc

    Original detective work by Federico Ficarelli and LIU Wei:
        https://gist.github.com/nazavode/84d1371e023bccd2301e
        https://gist.github.com/liuw/2407154
    """
    if not ctypes or not PyThreadState_SetAsyncExc:
        raise NotImplementedError("async_raise not supported on this Python interpreter.")  # pragma: no cover

    if not hasattr(thread_obj, "ident"):
        raise TypeError(f"Expected a thread object, got {type(thread_obj)} with value '{thread_obj}'")

    target_tid = thread_obj.ident
    if target_tid not in {thread.ident for thread in threading.enumerate()}:
        raise ValueError("Invalid thread object, cannot find its ident among currently active threads.")

    affected_count = PyThreadState_SetAsyncExc(ctypes.c_long(target_tid), ctypes.py_object(exception))
    if affected_count == 0:
        raise ValueError("PyThreadState_SetAsyncExc did not accept the thread ident, even though it was among the currently active threads.")  # pragma: no cover

    # TODO: check CPython source code if this case can actually ever happen.
    #
    # The API docs seem to hint that 0 or 1 are the only possible return values.
    # If so, we can remove this `SystemError` case and the "potentially dangerous" caution.
    elif affected_count > 1:  # pragma: no cover
        # Clear the async exception, targeting the same thread identity, and hope for the best.
        PyThreadState_SetAsyncExc(ctypes.c_long(target_tid), ctypes.c_long(0))
        raise SystemError("PyThreadState_SetAsyncExc failed, broke the interpreter state.")
예제 #53
0
by Di2mot
'''
VERSION = 1.6

from time import perf_counter, monotonic, strftime
from os import system, name
from sys import stdout
from array import array
from getpass import getuser

if name == 'nt':

    from msvcrt import getch, kbhit
    import ctypes
    from ctypes import c_long, c_wchar_p, c_ulong, c_void_p
    gHandle = ctypes.windll.kernel32.GetStdHandle(c_long(-11))
else:
    # for UNIX systems
    import curses
    stdscr = curses.initscr()
    curses.cbreak()
    stdscr.keypad(True)
    curses.echo()
    stdscr.nodelay(True)

import argparse

# Size of the game FIELD
# Размер игрового поля
WIDTH = 100
HIGHT = 20
예제 #54
0
파일: run.py 프로젝트: sefcom/run-cgc-povs
def run(pov_path, target_path, *, flag=None, result=None):
    if result is None:
        result = {}

    if not flag:
        flag = os.urandom(4096)
    assert len(flag) == 4096
    flag_fd = os.memfd_create('flag')
    flag_path = f'/proc/{os.getpid()}/fd/{flag_fd}'
    os.write(flag_fd, flag)

    result['flag'] = flag.decode('latin')

    child_conn, parent_conn = multiprocessing.Pipe(duplex=True)

    def dup_child_3():
        os.dup2(child_conn.fileno(), 3, inheritable=True)

    pov_seed = str(int.from_bytes(os.urandom(3), 'little'))
    pov_popen = subprocess.Popen(
        ['qemu-cgc/i386-linux-user/qemu-i386', '-seed', pov_seed, pov_path],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL,
        pass_fds=(3, ),
        preexec_fn=dup_child_3)

    pov_type = b''.join(os.read(parent_conn.fileno(), 1) for _ in range(4))
    pov_type = int.from_bytes(pov_type, 'little')
    assert pov_type == 2
    os.write(parent_conn.fileno(), TYPE_2_DATA)

    def trace_me():
        libc.ptrace(PTRACE['TRACEME'], 0, 0, 0)

    target_seed = str(int.from_bytes(os.urandom(3), 'little'))
    target_popen = subprocess.Popen([
        'qemu-cgc/i386-linux-user/qemu-i386', '-magicpregen', flag_path,
        '-seed', target_seed, target_path
    ],
                                    stdin=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.DEVNULL,
                                    preexec_fn=trace_me)

    result['interaction'] = []

    while True:
        pid, status = os.wait()

        if pid == target_popen.pid:
            sig = os.WSTOPSIG(status)
            if sig and sig != signal.SIGTRAP:
                result['signal'] = sig
                break
            if not os.WIFSTOPPED(status):
                break

        else:
            continue

        regs = user_regs_struct()
        libc.ptrace(PTRACE['GETREGS'], pid, 0, ctypes.byref(regs))

        syscall = SYSCALL_NAME[regs.orig_rax]
        syscall_start = ctypes.c_long(regs.rax).value == -errno.ENOSYS

        reading = SYSCALL_NAME[regs.orig_rax] == 'read' and regs.rdi == 0
        writing = SYSCALL_NAME[regs.orig_rax] == 'write' and regs.rdi == 1

        try:
            if reading and syscall_start:
                count = regs.rdx
                data = pov_popen.stdout.read1(
                    min(count, io.DEFAULT_BUFFER_SIZE))
                target_popen.stdin.write(data)
                target_popen.stdin.flush()
                result['interaction'].append(
                    ('read', count, data.decode('latin')))
                if not data:
                    break

            elif writing and not syscall_start:
                count = regs.rdx
                data = target_popen.stdout.read(count)
                pov_popen.stdin.write(data)
                pov_popen.stdin.flush()
                result['interaction'].append(
                    ('write', count, data.decode('latin')))

        except BrokenPipeError:
            break

        libc.ptrace(PTRACE['SYSCALL'], pid, 0, 0)

    pov_answer = b''.join(os.read(parent_conn.fileno(), 1) for _ in range(4))
    result['pov_answer'] = pov_answer.decode('latin')
    result['pov_answer_correct'] = pov_answer in flag
예제 #55
0
def cancelThread(*threads, exception=EscapeException):
    'Raise exception on another thread.'
    for t in threads:
        ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(t.ident), ctypes.py_object(exception))
예제 #56
0
    def set_target_position(self, target, absolute, immediately):
        #print('check #1')

        nodeID = ctypes.wintypes.WORD(0)
        buf = ctypes.wintypes.DWORD(0)
        # First, set enabled state

        # print('#5 Motor current: {}'.format(self.get_motor_current()))
        # print('#5 Motor current: {}'.format(self.get_motor_current()))
        # print('#5 Motor current: {}'.format(self.get_motor_current()))
        # print('#5 Motor current: {}'.format(self.get_motor_current()))
        # print('#5 Motor current: {}'.format(self.get_motor_current()))

        ret = eposlib.VCS_SetEnableState(self._keyhandle, nodeID,
                                         ctypes.byref(buf))
        #print('Enable state ret %s buf %s' % (ret, buf.value))

        # print('#6 Motor current: {}'.format(self.get_motor_current()))
        # print('#6 Motor current: {}'.format(self.get_motor_current()))
        # print('#6 Motor current: {}'.format(self.get_motor_current()))
        # print('#6 Motor current: {}'.format(self.get_motor_current()))
        # print('#6 Motor current: {}'.format(self.get_motor_current()))

        pTarget = ctypes.c_long(target)
        pAbsolute = ctypes.wintypes.BOOL(absolute)
        pImmediately = ctypes.wintypes.BOOL(immediately)

        eposlib.VCS_MoveToPosition.argtypes = [
            ctypes.wintypes.HANDLE, ctypes.wintypes.WORD, ctypes.c_long,
            ctypes.wintypes.BOOL, ctypes.wintypes.BOOL,
            ctypes.POINTER(ctypes.wintypes.DWORD)
        ]
        eposlib.VCS_MoveToPosition.restype = ctypes.wintypes.BOOL

        #print('check #2')

        #print('About to set motor position')
        #print('Current motor position is %d' % (self.get_motor_position()))

        ret = eposlib.VCS_MoveToPosition(self._keyhandle, nodeID, pTarget,
                                         pAbsolute, pImmediately,
                                         ctypes.byref(buf))
        # print('#7 Motor current: {}'.format(self.get_motor_current()))
        # print('#7 Motor current: {}'.format(self.get_motor_current()))
        # print('#7 Motor current: {}'.format(self.get_motor_current()))
        # print('#7 Motor current: {}'.format(self.get_motor_current()))
        # print('#7 Motor current: {}'.format(self.get_motor_current()))

        #print('set motor position ret %s' % ret)
        #print('set motor position buf %s' % buf.value)

        steps_per_second = 14494.0  # hardcoded, estimated roughly, unused now

        nchecks = 0
        #print('check #3')
        while nchecks < 1000:
            # get the movement state. a movement state of 1 indicates the motor
            # is done moving
            # print('')
            # print('check #4')

            #print('Motor current: {}'.format(self.get_motor_current()))
            print('Motor position: {}'.format(self.get_motor_position()))
            #print('Motor offset: {}'.format(self.get_offset()))

            self._offset = self.get_offset()
            #print('Motor offset is %s' % self._offset)

            pMovementState = ctypes.pointer(ctypes.wintypes.BOOL())
            # print(pMovementState.contents.value)

            eposlib.VCS_GetMovementState.argtypes = [
                ctypes.wintypes.HANDLE, ctypes.wintypes.WORD,
                ctypes.POINTER(ctypes.wintypes.BOOL),
                ctypes.POINTER(ctypes.wintypes.DWORD)
            ]
            eposlib.VCS_GetMovementState.restype = ctypes.wintypes.BOOL
            # print('Getting movement state')
            ret = eposlib.VCS_GetMovementState(self._keyhandle,
                                               nodeID, pMovementState,
                                               ctypes.byref(buf))

            # print('set motor position ret %s' % ret)
            # print('set motor position buf %s' % buf.value)
            # print('Movement state is %s' % pMovementState.contents.value)
            if pMovementState.contents.value == 1:
                break
            nchecks = nchecks + 1
            # print('Current motor position is %d' % self.get_motor_position())
            # print('check #5')
            # print(nchecks)
            # print('')
            time.sleep(0.01)
        # Now set disabled state
        ret = eposlib.VCS_SetDisableState(self._keyhandle, nodeID,
                                          ctypes.byref(buf))
        #print('check #6')
        #print('Disable state ret %s buf %s' % (ret, buf.value))
        #print('Final motor position is %d' % (self.get_motor_position()))
        #print('check #7')
        return ret
예제 #57
0
def hlsvd(signals, nsv_sought, dwell_time, libhlsvd=None):
    """
    Input --
    signals:
        An iterable (e.g. list) of complex numbers. A numpy array should
        work too. The list must have <= MAX_DATA_POINTS elements.

    nsv_sought:
        The number of singular values sought. The function will return a
        maximum of this many singular values.

        Must be <= MAX_SINGULAR_VALUES.

    dwell_time:
        Dwell time in milliseconds

    libhlsvd:
        None (the default) or a string.
        If None, this function will try to find the library on its own.
        If a string, it should be a file name (fully qualified or not,
        e.g. "hlsvd.dll" or "/home/philip/lib/libhlsvd.dylib") that
        this function can pass to ctypes.CDLL().

    Output is a 6-tuple of --
     0) nsv_found: the number of singular values found (<= nsv_sought)
     1) A list of floats representing the singular values
     2) A list of floats representing the frequencies (in kilohertz)
     3) A list of floats representing the damping factors (in milliseconds?)
     4) A list of floats representing the amplitudes (in arbitrary units)
     5) A list of floats representing the phases (in degrees)

    Each list's length == nsv_found. The five lists are correlated (element N
    of one list is associated with element N in the other four lists) and are
    sorted by singular value with the largest (strongest signal) first.

    The frequencies and damping factors have been adjusted by the dwell time.
    """
    libhlsvd = _load_the_library(libhlsvd)

    # At this point libhlsvd should be a valid reference to the hlsvd library.
    # I test that assertion here as best as I can by generating a throwaway
    # reference to the function I'm going to call.
    f = libhlsvd.hlsvdpw_python_

    # OK, all is well. I create and populate the variables I need as parameters
    # to the function.
    n_data_points = len(signals)
    print("RMSE = %.2f" % (n_data_points))
    InputDoubleArrayType = ctypes.c_double * MAX_DATA_POINTS
    OutputDoubleArrayType = ctypes.c_double * MAX_SINGULAR_VALUES

    # In-line comments refer to the names of the corresponding variables
    # in hlsvdpro.f.

    # Input params
    real_signals = InputDoubleArrayType()
    imaginary_signals = InputDoubleArrayType()
     
    for i, signal in enumerate(signals):
        real_signals[i] = signal.real                   # signal_r
        imaginary_signals[i] = signal.imag              # signal_i

  
    nsv_sought = ctypes.c_long(nsv_sought)              # kuser

    # I copied the forumula for calculating the size of the Hankel matrix
    # from the Fortran code.
    mcol = n_data_points // 2
    lrow = n_data_points - mcol + 1
    mcol = ctypes.c_long(mcol)                          # mcoL
    lrow = ctypes.c_long(lrow)                          # Lrow
   
    n_data_points = ctypes.c_long(n_data_points)        # ndp
   
    # Output params
    frequencies = OutputDoubleArrayType()               # freq
    damping_factors = OutputDoubleArrayType()           # damp
    amplitudes = OutputDoubleArrayType()                # ampl
    phases = OutputDoubleArrayType()                    # fase
    singular_values = OutputDoubleArrayType()           # Lsinval
    nsv_found = ctypes.c_long()                         # kfit

    libhlsvd.hlsvdpw_python_(real_signals,
                             imaginary_signals,
                             ctypes.pointer(n_data_points),
                             ctypes.pointer(lrow),
                             ctypes.pointer(mcol),
                             ctypes.pointer(nsv_sought),
                             ctypes.pointer(nsv_found),
                             ctypes.pointer(singular_values),
                             ctypes.pointer(amplitudes),
                             ctypes.pointer(phases),
                             ctypes.pointer(damping_factors),
                             ctypes.pointer(frequencies),
                             )

    # I tease the returned variables into Python types before passing them
    # back to the caller. (Slicing a ctypes array returns a list.) The Fortran
    # code has already sorted them the way we like (largest singular value
    # first).
    nsv_found       = nsv_found.value
    singular_values = singular_values[:nsv_found]
    frequencies     = frequencies[:nsv_found]
    damping_factors = damping_factors[:nsv_found]
    amplitudes      = amplitudes[:nsv_found]
    phases          = phases[:nsv_found]

    damping_factors = [1 / df for df in damping_factors]
    damping_factors = [df * dwell_time for df in damping_factors]

    frequencies = [frequency / dwell_time for frequency in frequencies]

    phases = [phase * RADIANS_TO_DEGREES for phase in phases]

    return (nsv_found, singular_values, frequencies, damping_factors, amplitudes, phases)
예제 #58
0
 def go_live(self, um):
     self._chk(self.pxd_goLivePair(um, c_long(1), c_long(2)))
예제 #59
0
def safe_read_chunked_memory_region_as_one(base, size):
    mbi = D.MEMORY_BASIC_INFORMATION()
    VirtualQueryEx = C.windll.kernel32.VirtualQueryEx
    VirtualProtectEx = C.windll.kernel32.VirtualProtectEx
    GRANULARITY = 0x1000

    h_process = wintypes.HANDLE(oa.Plugingetvalue(oa.VAL_HPROCESS))
    rv = bytearray(size)

    guarded = list()
    gpoints = dict()
    protect = 0

    queried = VirtualQueryEx(h_process, C.c_void_p(base), C.byref(mbi),
                             C.sizeof(mbi))
    if queried:
        protect = mbi.Protect
    else:
        print >> sys.stderr, 'safe_read_chunked_memory_region_as_one: VirtualQueryEx() failed'
    if queried and mbi.Protect & D.PAGE_GUARD:
        g = {'ea': base, 'size': GRANULARITY, 'p': mbi.Protect}
        gpoints[base] = 0
        ea = base
        while True:
            ea -= GRANULARITY
            if VirtualQueryEx(h_process, C.c_void_p(ea), C.byref(mbi), C.sizeof(mbi)) and\
                    (mbi.Protect & D.PAGE_GUARD) != 0 and g['p'] == mbi.Protect:
                g['ea'] -= GRANULARITY
                g['size'] += GRANULARITY
            else:
                break

        guarded.append(g)

    for i in range(base + GRANULARITY, base + size, GRANULARITY):
        p_addr = C.c_void_p(i)
        if VirtualQueryEx(h_process, p_addr, C.byref(mbi), C.sizeof(mbi)) and\
                        mbi.Protect & D.PAGE_GUARD:
            prevaddr = i - GRANULARITY
            if prevaddr in gpoints and guarded[
                    gpoints[prevaddr]]['p'] == mbi.Protect:
                idx = gpoints[prevaddr]
            else:
                guarded.append({'ea': i, 'size': 0, 'p': mbi.Protect})
                idx = len(guarded) - 1
            guarded[idx]['size'] += GRANULARITY
            gpoints[i] = idx

    ea = base + size - GRANULARITY
    if ea in gpoints:
        while True:
            ea += GRANULARITY
            if VirtualQueryEx(h_process, C.c_void_p(ea), C.byref(mbi), C.sizeof(mbi)) and\
                        mbi.Protect & D.PAGE_GUARD:
                guarded[-1]['size'] += GRANULARITY
            else:
                break

    # turn off page guard before read
    dummy = C.c_long()
    for g in guarded:
        for off in range(0, g['size'], GRANULARITY):
            g['ok'] = VirtualProtectEx(h_process, C.c_void_p(g['ea'] + off),
                                       GRANULARITY,
                                       C.c_long(g['p'] & ~D.PAGE_GUARD),
                                       C.byref(dummy))

    for i in range(base, base + size, GRANULARITY):
        p_addr = C.c_void_p(i)
        if VirtualQueryEx(h_process, p_addr, C.byref(mbi), C.sizeof(mbi)):
            if mbi.Protect & D.PAGE_GUARD:
                # TODO
                pass
            mem = unsafe_read_process_memory(i, GRANULARITY)
            if mem is None:
                continue
            mem = mem[1]
            if mem:
                off = i - base
                rv[off:off + GRANULARITY] = mem

    for g in guarded:
        for off in range(0, g['size'], GRANULARITY):
            if not g['ok']:
                continue
            if not VirtualProtectEx(h_process, C.c_void_p(g['ea'] + off),
                                    GRANULARITY, C.c_long(g['p']),
                                    C.byref(dummy)):
                print >> sys.stderr, 'VirtualProtectEx(ptr 0x%08X, size 0x%08X, protect 0x%08X) failed' %\
                                     (g['ea'] + off, GRANULARITY, g['p'])
    if rv and len(rv) > size:
        rv = rv[:size]
    return size, rv, protect
                while (True):
                    thread = experimentThread(Configure.allDeviceNum,
                                              Configure.dataRatio,
                                              Configure.errorDistribution,
                                              _errorParameter,
                                              Configure.dataDistribution,
                                              _dataParameter, id)

                    thread.start()
                    thread.join(64)
                    if thread.success:
                        break
                    else:
                        if thread.is_alive():
                            ctypes.pythonapi.PyThreadState_SetAsyncExc(
                                ctypes.c_long(thread.ident))
                # experiment(Configure.allDeviceNum, Configure.dataRatio, Configure.errorDistribution,
                #            _errorParameter, Configure.dataDistribution, _dataParameter, id)
            id += 1
        if Configure.mulThread <= 0:
            temp = np.zeros((len(Configure.errorParameter), 3))
            temp[:, 0] = np.array(
                errorRateInformationConvex[id -
                                           len(Configure.errorParameter):id]
            )[:, 1]
            temp[:, 1] = np.array(
                errorRateInformationGreedy[id -
                                           len(Configure.errorParameter):id]
            )[:, 1]
            temp[:, 2] = np.array(
                errorRateInformationLinear[id -