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()[0] == 'Linux':
            self.dev.detach_kernel_driver(0)
 
        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, 0)
 
            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代替吧。

处理flac和ape的过程

Linux和Mac上差不多。有个shntool,可以根据cue文件将ape或者flac进行分割,同时可以读出cue里面的唱片名、作者、tracknumber之类的信息,但问题是这些信息不是很方便传给lame,所以就这样处理一下:

  1. shntool split -f $cue -t ‘%n–@%p–@%a–@%t’ -o ‘cust ext=mp3 lame -b 320 -s 48 -quiet – %f’ -d $dir $audio (简单来说,将信息放到文件名里)
  2. python ~/bin/id3.py $dir (这是个自己写的python程序,从文件名里分离所有的信息,然后写到mp3文件里,用到了一个python的module:mutagen)

这样就不需要在iTunes里面再手工写这些信息了,唯一还需要填一下的就是总的曲目数、唱片出版年份、唱片类型。