Archives USB邮件提醒器
Post
Cancel

USB邮件提醒器

买了个山寨版的这个玩意儿,软件不爽,随即hack之。libusb+pyusb,搞定。不过Mac下面还不行,kernel的module直接claim设备,不知道怎么unclaim。

然后封装了一个简单的库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
 
from Queue import Queue
from threading import Thread
 
import usb.core
import usb.util
 
class Singleton(type):
    def __init__(cls, name, bases, dict):
        super(Singleton, cls).__init__(name, bases, dict)
        cls.instance = None
 
    def __call__(cls, *args, **kw):
        if cls.instance is None:
            cls.instance = super(Singleton, cls).__call__(*args, **kw)
 
        return cls.instance
 
class Lamp(object):
    GREEN = 0x01
    RED = 0x02
    BLUE = 0x03
    PURPLE = 0x04
    X = 0x05
    Y = 0x06
    WHITE = 0x07
    __QUIT = -1
 
    __metaclass__ = Singleton
 
    def __init__(self):
        self.t = None
        self.q = None
 
        self.dev = usb.core.find(idVendor = 0x1294, idProduct = 0x1320)
 
        import os
 
        if os.uname()[] == 'Linux':
            self.dev.detach_kernel_driver()
 
        self.dev.set_configuration()
 
    def __worker(self):
        item = self.q.get()
        while item != Lamp.__QUIT:
            if item >= Lamp.GREEN and item <= Lamp.WHITE:
                print item
                data = (item, 0x04, 0x04, 0x04, 0x04)
                self.dev.write(2, data, )
 
            self.q.task_done()
            item = self.q.get()
 
    def plug(self):
        if self.q is None:
            self.q = Queue()
            self.t = Thread(target=self.__worker)
            self.t.start()
 
    def unplug(self):
        if self.q is not None:
            self.q.put(Lamp.__QUIT)
            self.t.join()
            self.q = None
            self.t = None
 
    def on(self, color):
        self.q.put(color)
 
    def off(self):
        self.q.put(0x00)
 
if __name__ == '__main__':
    lamp1 = Lamp()
    lamp2 = Lamp()
    lamp3 = Lamp()
 
    lamp1.plug()
    lamp2.plug()
    lamp3.plug()
 
    lamp1.on(Lamp.GREEN)
    lamp1.on(Lamp.RED)
    lamp1.on(Lamp.WHITE)
    lamp1.on(100)
 
    lamp1.unplug()
    lamp2.unplug()
    lamp3.unplug()

忘记还有其它什么颜色了,暂时用X、Y代替吧。

This post is licensed under CC BY 4.0 by the author.

强化部与我的成长

APE解码器MAC在Snow Leopard上的问题

Comments powered by Disqus.