Class: DreamCheeky::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/dreamcheeky/notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device) ⇒ Notifier

Returns a new instance of Notifier.



10
11
12
13
# File 'lib/dreamcheeky/notifier.rb', line 10

def initialize(device)
  @device = device
  reset_device
end

Class Method Details

.allObject



6
7
8
# File 'lib/dreamcheeky/notifier.rb', line 6

def self.all
  @@devices ||= LIBUSB::Context.new.devices(idVendor: 0x1d34, idProduct: 4).sort.map {|device| new (device)}
end

Instance Method Details

#colour!(colour) ⇒ Object Also known as: colour



15
16
17
# File 'lib/dreamcheeky/notifier.rb', line 15

def colour!(colour)
  send("%c%c%c\x00\x00\x00\x00\x05" % [colour.r, colour.g, colour.b].map {|x| x*64 })
end

#reset_deviceObject

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



35
36
37
38
39
# File 'lib/dreamcheeky/notifier.rb', line 35

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

#send(data) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/dreamcheeky/notifier.rb', line 25

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

#turn_offObject



21
22
23
# File 'lib/dreamcheeky/notifier.rb', line 21

def turn_off
  colour!(Colour.new(0, 0, 0))
end