forked from thekua/email-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.rb
70 lines (55 loc) · 1.65 KB
/
notifier.rb
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
require 'libusb'
class Colour
attr_reader :red, :green, :blue
def initialize(red, green, blue)
@red = red
@green = green
@blue = blue
end
def hex_string
hexify(@red, @green, @blue)
end
def max_val
[@red, @green, @blue].max
end
private
def hexify(*items)
items.collect! { |x| x.chr }.join
end
end
WHITE = Colour.new(255, 255, 255)
RED = Colour.new(255, 0, 0)
GREEN = Colour.new(0, 255, 0)
BLUE = Colour.new(0, 0, 255)
OFF = Colour.new(0, 0, 0)
class EmailNotifier
def initialize
usb_context = LIBUSB::Context.new
# The hex value for Dream Cheeky on the device. Assuming only the email notifier is attached
@device = usb_context.devices(:idVendor => 0x1d34).first
reset_device
end
def colour!(colour)
@colour = colour
send(colour.hex_string + "\x00\x00\x00\x00\x05") # The 5 extra bytes apparently need to be sent
end
alias :colour :colour!
def turn_off
colour!(Colour.new(0, 0, 0))
end
private
def send(data)
@device.open do |handle|
request_type = LIBUSB::REQUEST_TYPE_CLASS | LIBUSB::RECIPIENT_INTERFACE # flags to send data to device
# other special flags that I don't know what they do
handle.control_transfer(:bmRequestType => request_type, :bRequest => 0x09, :wValue => 0x200, :wIndex => 0x00, :dataOut => data)
end
end
# Special codes that apparently need to be sent to initialise the device.
# The first three values of the last line also represent the RGB colours on initialisation
def reset_device
send "\x1f\x02\x00\x2e\x00\x00\x2b\x03"
send "\x00\x02\x00\x2e\x00\x00\x2b\x04"
send "\x00\x00\x00\x2e\x00\x00\x2b\x05"
end
end