Class: Lorraine::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/lorraine/message.rb

Constant Summary collapse

COMMAND_IDS =
{set_pixel: 1, refresh: 2, effect: 3}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#blueObject

Returns the value of attribute blue.



33
34
35
# File 'lib/lorraine/message.rb', line 33

def blue
  @blue
end

#commandObject

Returns the value of attribute command.



34
35
36
# File 'lib/lorraine/message.rb', line 34

def command
  @command
end

#greenObject

Returns the value of attribute green.



32
33
34
# File 'lib/lorraine/message.rb', line 32

def green
  @green
end

#pixelObject

Returns the value of attribute pixel.



35
36
37
# File 'lib/lorraine/message.rb', line 35

def pixel
  @pixel
end

#redObject

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

.formatObject

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

.from_packet(p) ⇒ Object



59
60
61
62
63
# File 'lib/lorraine/message.rb', line 59

def self.from_packet(p)
  m = Lorraine::Message.new
  m.packet = p
  m
end

Instance Method Details

#command_idObject



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

#packetObject



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_binaryObject



69
70
71
# File 'lib/lorraine/message.rb', line 69

def to_binary
  self.packet.pack(Lorraine::Message.format)
end

#to_jsonObject



65
66
67
# File 'lib/lorraine/message.rb', line 65

def to_json
  self.packet.to_json
end

#to_sObject



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