def readMetadata(self):
"""Read device identity and capabilities via ioctl()"""
buffer = "\0"*512
# Read the name
self.name = ioctl(self.fd, EVIOCGNAME_512, buffer)
self.name = self.name[:self.name.find("\0")]
# Read info on each absolute axis
absmap = Event.codeMaps['EV_ABS']
buffer = "\0" * struct.calcsize("iiiii")
self.absAxisInfo = {}
for name, number in absmap.nameMap.iteritems():
try:
values = struct.unpack("iiiii", ioctl(self.fd, EVIOCGABS_512 + number, buffer))
values = dict(zip(( 'value', 'min', 'max', 'fuzz', 'flat' ),values))
self.absAxisInfo[name] = values
except IOError:
continue
python类ioctl()的实例源码
def set_special_baudrate(port, baudrate):
# right size is 44 on x86_64, allow for some growth
import array
buf = array.array('i', [0] * 64)
try:
# get serial_struct
FCNTL.ioctl(port.fd, TCGETS2, buf)
# set custom speed
buf[2] &= ~TERMIOS.CBAUD
buf[2] |= BOTHER
buf[9] = buf[10] = baudrate
# set serial_struct
res = FCNTL.ioctl(port.fd, TCSETS2, buf)
except IOError, e:
raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))
def xfer(self, txbuff=None):
length = len(txbuff)
if PYTHON_MAJOR >= 3:
_txbuff = bytes(txbuff)
_txptr = ctypes.create_string_buffer(_txbuff)
else:
_txbuff = str(bytearray(txbuff))
_txptr = ctypes.create_string_buffer(_txbuff)
_rxptr = ctypes.create_string_buffer(length)
data = struct.pack("QQLLHBBL", #64 64 32 32 16 8 8 32 b = 32B
ctypes.addressof(_txptr),
ctypes.addressof(_rxptr),
length,
self.speed,
0, #delay
self.bits,
0, # cs_change,
0 # pad
)
fcntl.ioctl(self.fd, SPI_IOC_MESSAGE(len(data)), data)
_rxbuff = ctypes.string_at(_rxptr, length)
return bytearray(_rxbuff)
def _select_device(self, addr):
"""Set the address of the device to communicate with on the I2C bus."""
ioctl(self._device.fileno(), I2C_SLAVE, addr & 0x7F)
def read_byte_data(self, addr, cmd):
"""Read a single byte from the specified cmd register of the device."""
assert self._device is not None, 'Bus must be opened before operations are made against it!'
# Build ctypes values to marshall between ioctl and Python.
reg = c_uint8(cmd)
result = c_uint8()
# Build ioctl request.
request = make_i2c_rdwr_data([
(addr, 0, 1, pointer(reg)), # Write cmd register.
(addr, I2C_M_RD, 1, pointer(result)) # Read 1 byte as result.
])
# Make ioctl call and return result data.
ioctl(self._device.fileno(), I2C_RDWR, request)
return result.value
def read_block_data(self, addr, cmd):
"""Perform a block read from the specified cmd register of the device.
The amount of data read is determined by the first byte send back by
the device. Data is returned as a bytearray.
"""
# TODO: Unfortunately this will require calling the low level I2C
# access ioctl to trigger a proper read_block_data. The amount of data
# returned isn't known until the device starts responding so an I2C_RDWR
# ioctl won't work.
raise NotImplementedError()
def get_terminal_size():
"""Returns a tuple (x, y) representing the width(x) and the height(x)
in characters of the terminal window."""
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack(
'hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')
)
except:
return None
if cr == (0, 0):
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
def get_terminal_size():
"""Returns a tuple (x, y) representing the width(x) and the height(x)
in characters of the terminal window."""
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack(
'hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')
)
except:
return None
if cr == (0, 0):
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
def _get_terminal_size(fd):
try:
res = fcntl.ioctl(fd, termios.TIOCGWINSZ, b"\x00" * 4)
except IOError as e:
raise OSError(e)
lines, columns = struct.unpack("hh", res)
return terminal_size(columns, lines)
def get_terminal_size():
"""Returns a tuple (x, y) representing the width(x) and the height(x)
in characters of the terminal window."""
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return None
if cr == (0, 0):
return None
if cr == (0, 0):
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
def get_terminal_size():
"""Returns a tuple (x, y) representing the width(x) and the height(x)
in characters of the terminal window."""
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack(
'hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')
)
except:
return None
if cr == (0, 0):
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
def ioctl(self, fd, ioctlno):
self.pack()
rv = fcntl.ioctl(fd, ioctlno, self._buffer, True)
self.unpack()
return rv
def get_version(self, fd): return self.ioctl(fd, HIDIOCGVERSION)
def get_flags(self, fd): return self.ioctl(fd, HIDIOCGFLAG)
def set_flags(self, fd): return self.ioctl(fd, HIDIOCSFLAG)
def get(self, fd): return self.ioctl(fd, HIDIOCGDEVINFO)
def get_info(self, fd): return self.ioctl(fd, HIDIOCGREPORTINFO)
def get_info(self, fd): return self.ioctl(fd, HIDIOCGFIELDINFO)
def get_info(self, fd, index):
self.index = index
return self.ioctl(fd, HIDIOCGCOLLECTIONINFO)
def get_device_name(f):
a = array.array('B', [0]*1024)
fcntl.ioctl(f, HIDIOCGNAME(1024), a, True)
print a