Class: LCDProc::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/lcdproc/screen.rb

Constant Summary collapse

PRIORITIES =
[ :hidden, :background, :info, :foreground, :alert, :input ]
@@screen_count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = "Screen_" + @@screen_count.to_s) ⇒ Screen

Creates a new screen to which you may attach widgets.

Example Usage:

Screen.new( 'TestScreen' )


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lcdproc/screen.rb', line 41

def initialize( id = "Screen_" + @@screen_count.to_s )
  @id = id
  @client = nil
  @widgets = []
  @key_events = []
  @priority = :info
  
  @lcdproc_attributes = {}
  @lcdproc_attributes[:heartbeat] = :open
  
  @@screen_count += 1
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



32
33
34
# File 'lib/lcdproc/screen.rb', line 32

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



32
33
34
# File 'lib/lcdproc/screen.rb', line 32

def id
  @id
end

#key_eventsObject (readonly)

Returns the value of attribute key_events.



32
33
34
# File 'lib/lcdproc/screen.rb', line 32

def key_events
  @key_events
end

#lcdproc_attributesObject (readonly)

Returns the value of attribute lcdproc_attributes.



32
33
34
# File 'lib/lcdproc/screen.rb', line 32

def lcdproc_attributes
  @lcdproc_attributes
end

#priorityObject

Returns the value of attribute priority.



32
33
34
# File 'lib/lcdproc/screen.rb', line 32

def priority
  @priority
end

#widgetsObject (readonly)

Returns the value of attribute widgets.



32
33
34
# File 'lib/lcdproc/screen.rb', line 32

def widgets
  @widgets
end

Instance Method Details

#add_widget(widget) ⇒ Object

Add a widget to this screen



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lcdproc/screen.rb', line 56

def add_widget( widget )
  if @widgets.select{ |w| w == widget }.empty?
    
    if widget.attach_to( self )
      @widgets << widget
      
      if @client
        if widget.show
          return true
        else
          return false
        end
      else
        return true
      end
    else
      return false
    end
    
  else
    if @client
      @client.add_message( "Widget '#{widget.id}' is already added" )
    end
    
    return false
  end
end

#alert!Object

Sets the priority of this screen to “alert” status



86
87
88
# File 'lib/lcdproc/screen.rb', line 86

def alert!
  self.priority = :alert
end

#attach_to(client) ⇒ Object

Attaches the screen to the requested client



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lcdproc/screen.rb', line 92

def attach_to( client )
  response = client.send_command( Command.new( "screen_add #{id.to_s}" ) )
  
  if response.successful?
    client.add_message( "Screen '#{self.id}' attached to client '#{client.name}'" )
    @client = client
    
    return true
  else
    client.add_message( "Error: Failed to attach screen '#{self.id}' to client '#{client.name}' (#{response.message} : #{response.successful?})" )
    return false
  end
end

#backlight=(state) ⇒ Object

Turns on or off the screen’s backlight

  • state - A symbol representing the state in which you wish the backlight to be.Valid values are

    * :on - Always on.
    * :off - Always off.
    * :toggle - Toggle the current state.
    * :open - Use the client's setting.
    * :blink - Moderately striking backlight variation.
    * :flash - Very striking backlight variation.
    


143
144
145
146
147
# File 'lib/lcdproc/screen.rb', line 143

def backlight= state
  @lcdproc_attributes[:backlight] = state
  
  update_lcdproc_attribute( :backlight )
end

#detachObject

Detaches the screen from any client



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lcdproc/screen.rb', line 108

def detach
  if @client
    response = @client.send_command( Command.new( "screen_del #{self.id}" ) )
    
    if response.successful?
      @client.add_message( "Screen '#{self.id}' detached from client '#{@client.name}'" )
      @client = nil
      
      return true
    end
  else
    add_message "Error: Failed to detach screen '#{self.id}' from '#{@client.name}' (#{response.message})"
    return false
  end
end

#heartbeat=(state) ⇒ Object

Turns on or off the screen’s heartbeat

  • state - A symbol representing the state in which you wish the heartbeat to be. Valid values are :on, :off or :open.



128
129
130
131
132
# File 'lib/lcdproc/screen.rb', line 128

def heartbeat= state
  @lcdproc_attributes[:heartbeat] = state
  
  update_lcdproc_attribute( :heartbeat )
end

#process_key(key) ⇒ Object

Processed a particular key event



183
184
185
186
187
# File 'lib/lcdproc/screen.rb', line 183

def process_key( key )
  @key_events.select{ |ke| ke.key === key }.each do |ke|
    ke.block.call unless ke.block.nil?
  end
end

#register_key_event(key_event) ⇒ Object

Register a key event to be handled when the key is pressed on this screen. Note that you can register the same key multiple times. All actions that are registered will be performed!

Example:

c = Client.new
s = Screen.new
s.register_key_event( KeyEvent.new( 'Left' ) { puts "Left button was pressed!" } )


198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/lcdproc/screen.rb', line 198

def register_key_event( key_event )
  
  if key_event and @client and @client.valid_keys.include? key_event.key
    response = @client.send_command( Command.new( "client_add_key -shared #{key_event.key}" ) )
    
    if response.successful?
      @key_events << key_event
      return key_event
    end
  end
  
  return nil
end

#remove_widget(widget) ⇒ Object

Removes a widget from the screen



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/lcdproc/screen.rb', line 214

def remove_widget( widget )
  if @widgets.include? widget
    
    if widget.hide and widget.detach
      @widgets.delete( widget )
      return true
    else
      return false
    end
    
  else
    if @client
      @client.add_message( "Widget '#{widget.id}' is not attached to Screen '#{@id}'" )
    end
    
    return false
  end
end

#unregister_key_event(key_event) ⇒ Object

Unregisters a key event so that it will no longer be processed



235
236
237
238
239
240
241
242
# File 'lib/lcdproc/screen.rb', line 235

def unregister_key_event( key_event )
  if @key_events.include? key_event
    @key_events.delete key_event
    return key_event
  else
    return nil
  end
end

#updateObject

Updates the current contents of the screen by iterating through it’s widgets and calling each of their update methods as well as making sure that they are displayed on the screen by calling their show method.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/lcdproc/screen.rb', line 246

def update
  if @client
    passed = true
    
    @widgets.each{ |widget| widget.show; passed = widget.update; if not passed then break end }
    
    if passed
      return true
    else
      return false
    end
  else
    return false
  end
end

#update_lcdproc_attribute(attribute) ⇒ Object

Updates a particular LCDProc attribute



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/lcdproc/screen.rb', line 264

def update_lcdproc_attribute( attribute )
  if @client
    display = attribute.to_s.capitalize
    value = @lcdproc_attributes[attribute].to_s
    
    response = @client.send_command( Command.new( "screen_set #{@id} -#{attribute.to_s} #{value}" ) )
    
    if response.successful?
      @client.add_message( "#{display} set to '#{value}' for Screen '#{@id}'" )
      return true
    else
      @client.add_message( "Error: #{display} could not be set to '#{value}' for Screen '#{@id}' (#{response.message})" )
      return false
    end
  end
end