From b880cbc86f9f834023fdf91e64fbd162a92c17b5 Mon Sep 17 00:00:00 2001 From: Pete Nicholls Date: Thu, 26 Dec 2013 14:23:40 +1300 Subject: [PATCH] Support RGB, hex values, and add known colours to Dream Cheeky Webmail Notifier --- .../dream_cheeky/webmail_notifier.rb | 61 ++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/lib/device_recipes/dream_cheeky/webmail_notifier.rb b/lib/device_recipes/dream_cheeky/webmail_notifier.rb index c2d50ea..3e37049 100644 --- a/lib/device_recipes/dream_cheeky/webmail_notifier.rb +++ b/lib/device_recipes/dream_cheeky/webmail_notifier.rb @@ -1,39 +1,56 @@ module Blinky module DreamCheeky module WebmailNotifier + COLOURS = { + :black => "000000", + :white => "ffffff", + :green => "00ff00", + :red => "ff0000", + :blue => "0000ff", + :orange => "ff2a00", + :teal => "00ffff", + :yellow => "ffff00", + :pink => "ff00ff" + } + + COLOURS.each do |name, hex_code| + define_method(name) do + colour_hex!(hex_code) + end + end + + alias :success! :green + alias :failure! :red + alias :building! :blue + alias :warning! :orange + alias :off! :black - def success! - colour!("\x00\xFF\x00") - end - - def failure! - colour!("\xFF\x00\x00") - end - - def building! - colour!("\x00\x00\xFF") + def init + send_data "\x1f\x02\x00\x2e\x00\x00\x2b\x03" + send_data "\x00\x02\x00\x2e\x00\x00\x2b\x04" + send_data "\x00\x00\x00\x2e\x00\x00\x2b\x05" end - def warning! - colour!("\xFF\x2A\x00") + def colour!(colour) + send_data(colour + "\x00\x00\x00\x00\x05") end - def off! - colour!("\x00\x00\x00") + def colour_rgb!(red, green, blue) + hex_sequence = [red, green, blue].map(&:chr).join + colour!(hex_sequence) end - def init - 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 + def colour_hex!(hex_code) + rgb_values = hex_code.scan(/\h{2}/).take(3).map do |n| + n.to_i(16) + end - def colour!(colour) - send(colour + "\x00\x00\x00\x00\x05") + colour_rgb!(*rgb_values) end private - def send(data) + + def send_data(data) @handle.usb_control_msg(0x21, 0x09, (3 << 8) | 1, 0, data, 0) end