Class: Lorraine::Message
- Inherits:
-
Object
- Object
- Lorraine::Message
- Defined in:
- lib/lorraine/message.rb
Constant Summary collapse
- COMMAND_IDS =
{set_pixel: 1, refresh: 2, effect: 3}
Instance Attribute Summary collapse
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#command ⇒ Object
Returns the value of attribute command.
-
#green ⇒ Object
Returns the value of attribute green.
-
#pixel ⇒ Object
Returns the value of attribute pixel.
-
#red ⇒ Object
Returns the value of attribute red.
Class Method Summary collapse
- .decode(binary_string) ⇒ Object
-
.format ⇒ Object
Command ID - 16 bit unsigned integer (S) Address - 16 bit unsigned integer (S) Red value - 16 bit unsigned integer (S) Green value - 16 bit unsigned integer (S) Blue value - 16 bit unsigned integer (S).
- .from_json(json) ⇒ Object
- .from_packet(p) ⇒ Object
Instance Method Summary collapse
- #command_id ⇒ Object
- #command_id=(id) ⇒ Object
-
#initialize(command = 0, pixel = 0, red = 0, green = 0, blue = 0) ⇒ Message
constructor
A new instance of Message.
- #packet ⇒ Object
- #packet=(new_packet) ⇒ Object
- #to_binary ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(command = 0, pixel = 0, red = 0, green = 0, blue = 0) ⇒ Message
Returns a new instance of Message.
17 18 19 20 21 22 23 |
# File 'lib/lorraine/message.rb', line 17 def initialize(command = 0, pixel = 0, red = 0, green = 0, blue = 0) self.command = command self.pixel = pixel self.red = red self.green = green self.blue = blue end |
Instance Attribute Details
#blue ⇒ Object
Returns the value of attribute blue.
33 34 35 |
# File 'lib/lorraine/message.rb', line 33 def blue @blue end |
#command ⇒ Object
Returns the value of attribute command.
34 35 36 |
# File 'lib/lorraine/message.rb', line 34 def command @command end |
#green ⇒ Object
Returns the value of attribute green.
32 33 34 |
# File 'lib/lorraine/message.rb', line 32 def green @green end |
#pixel ⇒ Object
Returns the value of attribute pixel.
35 36 37 |
# File 'lib/lorraine/message.rb', line 35 def pixel @pixel end |
#red ⇒ Object
Returns the value of attribute red.
31 32 33 |
# File 'lib/lorraine/message.rb', line 31 def red @red end |
Class Method Details
.decode(binary_string) ⇒ Object
25 26 27 28 29 |
# File 'lib/lorraine/message.rb', line 25 def self.decode(binary_string) m = Lorraine::Message.new m.command_id, m.pixel, m.red, m.green, m.blue = binary_string.unpack(Lorraine::Message.format) m end |
.format ⇒ Object
Command ID - 16 bit unsigned integer (S) Address - 16 bit unsigned integer (S) Red value - 16 bit unsigned integer (S) Green value - 16 bit unsigned integer (S) Blue value - 16 bit unsigned integer (S)
13 14 15 |
# File 'lib/lorraine/message.rb', line 13 def self.format "nnnnn" end |
.from_json(json) ⇒ Object
55 56 57 |
# File 'lib/lorraine/message.rb', line 55 def self.from_json(json) Lorraine::Message.from_packet JSON.parse(json) end |
Instance Method Details
#command_id ⇒ Object
39 40 41 |
# File 'lib/lorraine/message.rb', line 39 def command_id COMMAND_IDS[self.command] end |
#command_id=(id) ⇒ Object
43 44 45 |
# File 'lib/lorraine/message.rb', line 43 def command_id=(id) self.command = COMMAND_IDS.invert[id] end |
#packet ⇒ Object
47 48 49 |
# File 'lib/lorraine/message.rb', line 47 def packet [self.command_id, self.pixel.to_i, self.red.to_i, self.green.to_i, self.blue.to_i] end |
#packet=(new_packet) ⇒ Object
51 52 53 |
# File 'lib/lorraine/message.rb', line 51 def packet=(new_packet) self.command_id, self.pixel, self.red, self.green, self.blue = new_packet end |
#to_binary ⇒ Object
69 70 71 |
# File 'lib/lorraine/message.rb', line 69 def to_binary self.packet.pack(Lorraine::Message.format) end |
#to_json ⇒ Object
65 66 67 |
# File 'lib/lorraine/message.rb', line 65 def to_json self.packet.to_json end |
#to_s ⇒ Object
73 74 75 |
# File 'lib/lorraine/message.rb', line 73 def to_s "#<Lorraine::Message command=#{command} pixel=#{pixel} r=#{red} g=#{green} b=#{blue} bytes=#{to_binary.length}>" end |