我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ctypes.c_ubyte()。
def simxReadVisionSensor(clientID, sensorHandle, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' detectionState = ct.c_ubyte() auxValues = ct.POINTER(ct.c_float)() auxValuesCount = ct.POINTER(ct.c_int)() ret = c_ReadVisionSensor(clientID, sensorHandle, ct.byref(detectionState), ct.byref(auxValues), ct.byref(auxValuesCount), operationMode) auxValues2 = [] if ret == 0: s = 0 for i in range(auxValuesCount[0]): auxValues2.append(auxValues[s:s+auxValuesCount[i+1]]) s += auxValuesCount[i+1] #free C buffers c_ReleaseBuffer(auxValues) c_ReleaseBuffer(auxValuesCount) return ret, bool(detectionState.value!=0), auxValues2
def simxReadForceSensor(clientID, forceSensorHandle, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' state = ct.c_ubyte() forceVector = (ct.c_float*3)() torqueVector = (ct.c_float*3)() ret = c_ReadForceSensor(clientID, forceSensorHandle, ct.byref(state), forceVector, torqueVector, operationMode) arr1 = [] for i in range(3): arr1.append(forceVector[i]) arr2 = [] for i in range(3): arr2.append(torqueVector[i]) #if sys.version_info[0] == 3: # state=state.value #else: # state=ord(state.value) return ret, state.value, arr1, arr2
def simxReadProximitySensor(clientID, sensorHandle, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' detectionState = ct.c_ubyte() detectedObjectHandle = ct.c_int() detectedPoint = (ct.c_float*3)() detectedSurfaceNormalVector = (ct.c_float*3)() ret = c_ReadProximitySensor(clientID, sensorHandle, ct.byref(detectionState), detectedPoint, ct.byref(detectedObjectHandle), detectedSurfaceNormalVector, operationMode) arr1 = [] for i in range(3): arr1.append(detectedPoint[i]) arr2 = [] for i in range(3): arr2.append(detectedSurfaceNormalVector[i]) return ret, bool(detectionState.value!=0), arr1, detectedObjectHandle.value, arr2
def simxGetAndClearStringSignal(clientID, signalName, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' signalLength = ct.c_int(); signalValue = ct.POINTER(ct.c_ubyte)() if (sys.version_info[0] == 3) and (type(signalName) is str): signalName=signalName.encode('utf-8') ret = c_GetAndClearStringSignal(clientID, signalName, ct.byref(signalValue), ct.byref(signalLength), operationMode) a = bytearray() if ret == 0: for i in range(signalLength.value): a.append(signalValue[i]) if sys.version_info[0] != 3: a=str(a) return ret, a
def simxReadStringStream(clientID, signalName, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' signalLength = ct.c_int(); signalValue = ct.POINTER(ct.c_ubyte)() if (sys.version_info[0] == 3) and (type(signalName) is str): signalName=signalName.encode('utf-8') ret = c_ReadStringStream(clientID, signalName, ct.byref(signalValue), ct.byref(signalLength), operationMode) a = bytearray() if ret == 0: for i in range(signalLength.value): a.append(signalValue[i]) if sys.version_info[0] != 3: a=str(a) return ret, a
def simxSetStringSignal(clientID, signalName, signalValue, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' sigV=signalValue if sys.version_info[0] == 3: if type(signalName) is str: signalName=signalName.encode('utf-8') if type(signalValue) is bytearray: sigV = (ct.c_ubyte*len(signalValue))(*signalValue) if type(signalValue) is str: signalValue=signalValue.encode('utf-8') sigV = (ct.c_ubyte*len(signalValue))(*signalValue) else: if type(signalValue) is bytearray: sigV = (ct.c_ubyte*len(signalValue))(*signalValue) if type(signalValue) is str: signalValue=bytearray(signalValue) sigV = (ct.c_ubyte*len(signalValue))(*signalValue) sigV=ct.cast(sigV,ct.POINTER(ct.c_ubyte)) # IronPython needs this return c_SetStringSignal(clientID, signalName, sigV, len(signalValue), operationMode)
def simxAppendStringSignal(clientID, signalName, signalValue, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' sigV=signalValue if sys.version_info[0] == 3: if type(signalName) is str: signalName=signalName.encode('utf-8') if type(signalValue) is bytearray: sigV = (ct.c_ubyte*len(signalValue))(*signalValue) if type(signalValue) is str: signalValue=signalValue.encode('utf-8') sigV = (ct.c_ubyte*len(signalValue))(*signalValue) else: if type(signalValue) is bytearray: sigV = (ct.c_ubyte*len(signalValue))(*signalValue) if type(signalValue) is str: signalValue=bytearray(signalValue) sigV = (ct.c_ubyte*len(signalValue))(*signalValue) sigV=ct.cast(sigV,ct.POINTER(ct.c_ubyte)) # IronPython needs this return c_AppendStringSignal(clientID, signalName, sigV, len(signalValue), operationMode)
def simxWriteStringStream(clientID, signalName, signalValue, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' sigV=signalValue if sys.version_info[0] == 3: if type(signalName) is str: signalName=signalName.encode('utf-8') if type(signalValue) is bytearray: sigV = (ct.c_ubyte*len(signalValue))(*signalValue) if type(signalValue) is str: signalValue=signalValue.encode('utf-8') sigV = (ct.c_ubyte*len(signalValue))(*signalValue) else: if type(signalValue) is bytearray: sigV = (ct.c_ubyte*len(signalValue))(*signalValue) if type(signalValue) is str: signalValue=bytearray(signalValue) sigV = (ct.c_ubyte*len(signalValue))(*signalValue) sigV=ct.cast(sigV,ct.POINTER(ct.c_ubyte)) # IronPython needs this return c_WriteStringStream(clientID, signalName, sigV, len(signalValue), operationMode)
def symlink_ms(source, linkname): """Python 2 doesn't have os.symlink on windows so we do it ourselfs :param source: sourceFile :type source: str :param linkname: symlink path :type linkname: str :raises: WindowsError, raises when it fails to create the symlink if the user permissions are incorrect """ import ctypes csl = ctypes.windll.kernel32.CreateSymbolicLinkW csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32) csl.restype = ctypes.c_ubyte flags = 1 if os.path.isdir(source) else 0 try: if csl(linkname, source.replace('/', '\\'), flags) == 0: raise ctypes.WinError() except WindowsError: raise WindowsError("Failed to create symbolicLink due to user permissions")
def decrypt_ige(cipher_text, key, iv): """ Decrypts the given text in 16-bytes blocks by using the given key and 32-bytes initialization vector. """ aeskey = AES_KEY() ckey = (ctypes.c_ubyte * len(key))(*key) cklen = ctypes.c_int(len(key)*8) cin = (ctypes.c_ubyte * len(cipher_text))(*cipher_text) ctlen = ctypes.c_size_t(len(cipher_text)) cout = (ctypes.c_ubyte * len(cipher_text))() civ = (ctypes.c_ubyte * len(iv))(*iv) _libssl.AES_set_decrypt_key(ckey, cklen, ctypes.byref(aeskey)) _libssl.AES_ige_encrypt( ctypes.byref(cin), ctypes.byref(cout), ctlen, ctypes.byref(aeskey), ctypes.byref(civ), AES_DECRYPT ) return bytes(cout)
def writer(ring, start, count): for i in range(start, start + count): data = os.urandom(random.randint(1, 1000)) time_micros = int(time.time() * 10**6) record = Record( write_number=i, timestamp_microseconds=time_micros, length=len(data)) # Note: You can't pass 'data' to the constructor without doing an # additional copy to convert the bytes type to a c_ubyte * 1000. So # instead, the constructor will initialize the 'data' field's bytes # to zero, and then this assignment overwrites the data-sized part. record.data[:len(data)] = data try: ring.try_write(record) except ringbuffer.WaitingForReaderError: print('Reader is too slow, dropping %d' % i) continue if i and i % 100 == 0: print('Wrote %d so far' % i) ring.writer_done() print('Writer is done')
def make_array(shape=(1,), dtype=np.float32, shared=False, fill_val=None): np_type_to_ctype = {np.float32: ctypes.c_float, np.float64: ctypes.c_double, np.bool: ctypes.c_bool, np.uint8: ctypes.c_ubyte, np.uint64: ctypes.c_ulonglong} if not shared: np_arr = np.empty(shape, dtype=dtype) else: numel = np.prod(shape) arr_ctypes = sharedctypes.RawArray(np_type_to_ctype[dtype], numel) np_arr = np.frombuffer(arr_ctypes, dtype=dtype, count=numel) np_arr.shape = shape if not fill_val is None: np_arr[...] = fill_val return np_arr
def simxReadForceSensor(clientID, forceSensorHandle, operationMode): ''' Please have a look at the function description/documentation in the V-REP user manual ''' state = ct.c_ubyte() forceVector = (ct.c_float*3)() torqueVector = (ct.c_float*3)() ret = c_ReadForceSensor(clientID, forceSensorHandle, ct.byref(state), forceVector, torqueVector, operationMode) arr1 = [] for i in range(3): arr1.append(forceVector[i]) arr2 = [] for i in range(3): arr2.append(torqueVector[i]) if sys.version_info[0] == 3: state=state.value else: state=state.value return ret, state, arr1, arr2
def SetCustomBatteryProfile(self, profile): d = [0x00] * 14 try: c = profile['capacity'] d[0] = c&0xFF d[1] = (c >> 8)&0xFF d[2] = int(round((profile['chargeCurrent'] - 550) / 75)) d[3] = int(round((profile['terminationCurrent'] - 50) / 50)) d[4] = int(round((profile['regulationVoltage'] - 3500) / 20)) d[5] = int(round(profile['cutoffVoltage'] / 20)) d[6] = ctypes.c_ubyte(profile['tempCold']).value d[7] = ctypes.c_ubyte(profile['tempCool']).value d[8] = ctypes.c_ubyte(profile['tempWarm']).value d[9] = ctypes.c_ubyte(profile['tempHot']).value B = profile['ntcB'] d[10] = B&0xFF d[11] = (B >> 8)&0xFF R = profile['ntcResistance'] / 10 d[12] = R&0xFF d[13] = (R >> 8)&0xFF except: return {'error':'BAD_ARGUMENT'} print d return self.interface.WriteDataVerify(self.BATTERY_PROFILE_CMD, d, 0.2)
def _generate_data(self, num_bytes, offset): if self._bytes_per_sample == 1: start = offset samples = num_bytes bias = 127 amplitude = 127 data = (ctypes.c_ubyte * samples)() else: start = offset >> 1 samples = num_bytes >> 1 bias = 0 amplitude = 32767 data = (ctypes.c_short * samples)() step = self.frequency * (math.pi * 2) / self.audio_format.sample_rate envelope = self._envelope_array env_offset = offset // self._bytes_per_sample for i in range(samples): data[i] = int(math.sin(step * (i + start)) * amplitude * envelope[i+env_offset] + bias) return data
def _generate_data(self, num_bytes, offset): # XXX TODO consider offset if self._bytes_per_sample == 1: samples = num_bytes value = 127 maximum = 255 minimum = 0 data = (ctypes.c_ubyte * samples)() else: samples = num_bytes >> 1 value = 0 maximum = 32767 minimum = -32768 data = (ctypes.c_short * samples)() step = (maximum - minimum) * self.frequency / self._sample_rate envelope = self._envelope_array env_offset = offset // self._bytes_per_sample for i in range(samples): value += step if value > maximum: value = minimum + (value % maximum) data[i] = int(value * envelope[i+env_offset]) return data
def _generate_data(self, num_bytes, offset): if self._bytes_per_sample == 1: start = offset samples = num_bytes bias = 127 amplitude = 127 data = (ctypes.c_ubyte * samples)() else: start = offset >> 1 samples = num_bytes >> 1 bias = 0 amplitude = 32767 data = (ctypes.c_short * samples)() self._advance(start) ring_buffer = self.ring_buffer decay = self.decay for i in range(samples): data[i] = int(ring_buffer[0] * amplitude + bias) ring_buffer.append(decay * (ring_buffer[0] + ring_buffer[1]) / 2) return data
def decode_bitfields(bits, r_mask, g_mask, b_mask, width, height, pitch, pitch_sign): r_shift1, r_shift2 = get_shift(r_mask) g_shift1, g_shift2 = get_shift(g_mask) b_shift1, b_shift2 = get_shift(b_mask) rgb_pitch = 3 * len(bits[0]) buffer = (ctypes.c_ubyte * (height * rgb_pitch))() i = 0 for row in bits: for packed in row: buffer[i] = (packed & r_mask) >> r_shift1 << r_shift2 buffer[i+1] = (packed & g_mask) >> g_shift1 << g_shift2 buffer[i+2] = (packed & b_mask) >> b_shift1 << b_shift2 i += 3 return ImageData(width, height, 'RGB', buffer, pitch_sign * rgb_pitch)
def _generate_data(self, bytes, offset): if self._bytes_per_sample == 1: start = offset samples = bytes bias = 127 amplitude = 127 data = (ctypes.c_ubyte * samples)() else: start = offset >> 1 samples = bytes >> 1 bias = 0 amplitude = 32767 data = (ctypes.c_short * samples)() step = self.frequency * (math.pi * 2) / self.audio_format.sample_rate for i in range(samples): data[i] = future_round(math.sin(step * (i + start)) * amplitude + bias) return data
def _generate_data(self, bytes, offset): # XXX TODO consider offset if self._bytes_per_sample == 1: samples = bytes value = 127 max = 255 min = 0 data = (ctypes.c_ubyte * samples)() else: samples = bytes >> 1 value = 0 max = 32767 min = -32768 data = (ctypes.c_short * samples)() step = (max - min) * 2 * self.frequency / self.audio_format.sample_rate for i in range(samples): value += step if value > max: value = max - (value - max) step = -step if value < min: value = min - (value - min) step = -step data[i] = future_round(value) return data
def _generate_data(self, bytes, offset): # XXX TODO consider offset if self._bytes_per_sample == 1: samples = bytes value = 0 amplitude = 255 data = (ctypes.c_ubyte * samples)() else: samples = bytes >> 1 value = -32768 amplitude = 65535 data = (ctypes.c_short * samples)() period = self.audio_format.sample_rate / self.frequency / 2 count = 0 for i in range(samples): count += 1 if count == period: value = amplitude - value count = 0 data[i] = future_round(value) return data
def marshal(self) : "serializes this Message into the wire protocol format and returns a bytes object." buf = ct.POINTER(ct.c_ubyte)() nr_bytes = ct.c_int() if not dbus.dbus_message_marshal(self._dbobj, ct.byref(buf), ct.byref(nr_bytes)) : raise CallFailed("dbus_message_marshal") #end if result = bytearray(nr_bytes.value) ct.memmove \ ( ct.addressof((ct.c_ubyte * nr_bytes.value).from_buffer(result)), buf, nr_bytes.value ) dbus.dbus_free(buf) return \ result #end marshal