Posted by Robert Jordens on Mon 6 Aug 19:45
report spam | download | new post
- #!/usr/bin/python
- from optparse import OptionParser
- import sys, os, fcntl, struct, ctypes
- def io(d, t, n, s):
- return ((d<<(8+8+14) | (t<<8) | (s<<(8+8)) | (n<<0)))
- class hiddev_report_info(ctypes.Structure):
- _fields_ = [
- ("report_type", ctypes.c_uint32),
- ("report_id", ctypes.c_uint32),
- ("num_fields", ctypes.c_uint32),
- ]
- class hiddev_usage_ref(ctypes.Structure):
- _fields_ = [
- ("report_type", ctypes.c_uint32),
- ("report_id", ctypes.c_uint32),
- ("field_index", ctypes.c_uint32),
- ("usage_index", ctypes.c_uint32),
- ("usage_code", ctypes.c_uint32),
- ("value", ctypes.c_int32),
- ]
- HIDIOCINITREPORT = io(0, ord("H"), 0x05, 0)
- HIDIOCSUSAGE = io(1, ord("H"), 0x0c,
- ctypes.sizeof(hiddev_usage_ref))
- HIDIOCSREPORT = io(1, ord("H"), 0x08,
- ctypes.sizeof(hiddev_report_info))
- class mx610(object):
- states = {
- "off": 0x6, "on": 0x5,
- "pulse": 0x4, "flash": 0x3,
- "inston": 0x2, "instoff": 0x1,
- "nop": 0x0}
- pfx = "\x01\x80\x52"
- susage = hiddev_usage_ref(2, 0x10, 0, 0, 0xff000001, 0)
- sreport = hiddev_report_info(2, 0x10, 1)
- def __init__(self, dev):
- self._d = open(dev, "rb")
- self.ctl(HIDIOCINITREPORT)
- def ctl(self, op, dat=0):
- return fcntl.ioctl(self._d.fileno(), op, dat)
- def cmd(self, c):
- for i, v in enumerate(c):
- self.susage.usage_index = i
- self.susage.value = ord(v)
- self.ctl(HIDIOCSUSAGE, ctypes.string_at(ctypes.byref(
- self.susage), ctypes.sizeof(self.susage)))
- self.ctl(HIDIOCSREPORT, ctypes.string_at(ctypes.byref(
- self.sreport), ctypes.sizeof(self.sreport)))
- def state(self, s0="nop", s1="nop", s2="nop"):
- self.cmd(self.pfx+struct.pack("BBB", self.states[s0],
- self.states[s1], self.states[s2]))
- self.confirm()
- def confirm(self):
- self.cmd(self.pfx+"\x00"*3)
- def main():
- states = mx610.states.keys()
- p = OptionParser()
- p.add_option("-D", "--dev", type="string",
- help="device [%default]")
- p.add_option("-E", "--email", type="choice", choices=states,
- help="email light, one of %s, default: %%default" % states)
- p.add_option("-I", "--im", type="choice", choices=states,
- help="instant messaging light, one of %s, default: %%default" % states)
- p.set_defaults(dev="/dev/hiddev0", email="nop", im="nop")
- (o, a) = p.parse_args()
- m = mx610(o.dev)
- m.state(o.email, o.im)
- if __name__ == "__main__":
- main()
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.