topherfangio-lcdproc-ruby 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. data/.document +5 -0
  2. data/.gitignore +6 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +9 -0
  5. data/Rakefile +70 -0
  6. data/TODO +18 -0
  7. data/VERSION +1 -0
  8. data/devices/crystalfontz/packet.rb +108 -0
  9. data/devices/devices.rb +41 -0
  10. data/examples/basic.rb +39 -0
  11. data/examples/clock.rb +78 -0
  12. data/examples/lights.rb +67 -0
  13. data/lcdproc.rb +43 -0
  14. data/lib/console.rb +39 -0
  15. data/lib/core_extensions/array.rb +37 -0
  16. data/lib/core_extensions/string.rb +37 -0
  17. data/lib/includes/lcdproc.rb +27 -0
  18. data/lib/lcdproc-ruby.rb +0 -0
  19. data/lib/lcdproc/client.rb +458 -0
  20. data/lib/lcdproc/command.rb +54 -0
  21. data/lib/lcdproc/errors.rb +41 -0
  22. data/lib/lcdproc/key_event.rb +59 -0
  23. data/lib/lcdproc/menu.rb +188 -0
  24. data/lib/lcdproc/menu_event.rb +49 -0
  25. data/lib/lcdproc/menu_item.rb +108 -0
  26. data/lib/lcdproc/menu_items/action.rb +72 -0
  27. data/lib/lcdproc/menu_items/alpha.rb +85 -0
  28. data/lib/lcdproc/menu_items/checkbox.rb +76 -0
  29. data/lib/lcdproc/menu_items/ip.rb +75 -0
  30. data/lib/lcdproc/menu_items/numeric.rb +75 -0
  31. data/lib/lcdproc/menu_items/ring.rb +75 -0
  32. data/lib/lcdproc/menu_items/slider.rb +83 -0
  33. data/lib/lcdproc/menu_items/submenu.rb +77 -0
  34. data/lib/lcdproc/response.rb +65 -0
  35. data/lib/lcdproc/screen.rb +283 -0
  36. data/lib/lcdproc/widget.rb +142 -0
  37. data/lib/lcdproc/widgets/graph.rb +185 -0
  38. data/lib/lcdproc/widgets/hbar.rb +101 -0
  39. data/lib/lcdproc/widgets/icon.rb +94 -0
  40. data/lib/lcdproc/widgets/num.rb +92 -0
  41. data/lib/lcdproc/widgets/scroller.rb +110 -0
  42. data/lib/lcdproc/widgets/string.rb +94 -0
  43. data/lib/lcdproc/widgets/title.rb +90 -0
  44. data/lib/lcdproc/widgets/vbar.rb +111 -0
  45. data/script/console.rb +28 -0
  46. data/script/telnet.rb +90 -0
  47. data/tasks/test/basic.rake +82 -0
  48. data/tasks/test/keys.rake +66 -0
  49. data/tasks/test/menus.rake +74 -0
  50. metadata +104 -0
@@ -0,0 +1,142 @@
1
+ #-------------------------------------------------------------------------------
2
+ # Copyright (c) 2008 Topher Fangio
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person
5
+ # obtaining a copy of this software and associated documentation
6
+ # files (the "Software"), to deal in the Software without
7
+ # restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the
10
+ # Software is furnished to do so, subject to the following
11
+ # conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
+ # OTHER DEALINGS IN THE SOFTWARE.
24
+ #-------------------------------------------------------------------------------
25
+
26
+
27
+ module LCDProc
28
+
29
+ # *NOTE*: You may only have one (1) type of the following per screen: hbar, vbar or num. They currently conflict
30
+ # with each other and will not draw properly. You *MAY* have as many of each type per screen as you like. For
31
+ # instance, you *MAY* have twenty (20) vertical bars per screen or four (4) horizontal bars on a screen, but
32
+ # you cannot mix and match!
33
+
34
+ module Widget
35
+ attr_reader :id, :screen, :type, :visible
36
+ @supported_types = {}
37
+
38
+ # Finds the first widget that is of type _type_ and returns a new instance of it.
39
+ #
40
+ # * <tt>type</tt> - A symbol representing the type of widget you wish to create.
41
+ # * <tt>id</tt> - A unique string by which you can indentify the widget. Defaults to the widget type +
42
+ # "Widget_" + a sequence number
43
+ #
44
+ # Example:
45
+ #
46
+ # w = Widget.new( :string, 'Test' )
47
+ def Widget.new( type = :string, id = nil )
48
+ if @supported_types.include? type
49
+ @supported_types[ type ].new( id )
50
+ else
51
+ return nil
52
+ end
53
+ end
54
+
55
+ # Returns an array of symbols listing the supported types of widgets.
56
+ def Widget.supported_types
57
+ @supported_types.keys
58
+ end
59
+
60
+ # Allows a particular type of widget to inform the Widget module (which is a Widget factory) that it is
61
+ # able to support a particular type of widget.
62
+ def Widget.add_support( type, widget_class )
63
+ @supported_types[type] = widget_class
64
+ end
65
+
66
+ # Allows a widget to be attached to a screen.
67
+ def attach_to( screen )
68
+ @screen = screen
69
+
70
+ return true
71
+ end
72
+
73
+ # Allows a widget to be dettached from a screen.
74
+ def detach
75
+ attach_to nil
76
+ end
77
+
78
+
79
+ # Sends the command to the LCDd server to remove the widget from the screen unless you
80
+ # pass false, in which case it does everything but send the command
81
+ def hide( send_command = true )
82
+
83
+ if not send_command
84
+ @visible = false
85
+ @screen.client.add_message( "Widget '#{@id}' is now hidden" )
86
+ return true
87
+ end
88
+
89
+ # Only if we are visible, attempt to hide ourselves
90
+ if @visible
91
+ response = @screen.client.send_command( Command.new( "widget_del #{@screen.id} #{self.id}" ) )
92
+
93
+ if response.successful?
94
+ @visible = false
95
+ @screen.client.add_message( "Widget '#{@id}' is now hidden" )
96
+ return true
97
+ else
98
+ @visible = true
99
+ @screen.client.add_message( "Error: Widget '#{@id}' could NOT be hidden (#{response.message})" )
100
+ return false
101
+ end
102
+ else
103
+ return true
104
+ end
105
+
106
+ end
107
+
108
+ # Sends the command to the LCDd server to show the widget on the screen unless you
109
+ # pass false, in which case it does everything but send the command
110
+ def show( send_command = true )
111
+
112
+ if not send_command
113
+ @visible = true
114
+ @screen.client.add_message( "Widget '#{@id}' is now visible" )
115
+ return true
116
+ end
117
+
118
+ # If we aren't already visible, attempt to make oursevles visible
119
+ if not @visible
120
+ response = @screen.client.send_command( Command.new( "widget_add #{@screen.id} #{self.id} #{@type.to_s}" ) )
121
+
122
+ if response.successful?
123
+ @visible = true
124
+ @screen.client.add_message( "Widget '#{@id}' is now visible" )
125
+
126
+ self.update
127
+
128
+ return true
129
+ else
130
+ @visible = false
131
+ @screen.client.add_message( "Error: Widget '#{@id}' could NOT be displayed (#{response.message})" )
132
+ return false
133
+ end
134
+ else
135
+ return true
136
+ end
137
+ end
138
+
139
+
140
+ end
141
+
142
+ end
@@ -0,0 +1,185 @@
1
+ #-------------------------------------------------------------------------------
2
+ # Copyright (c) 2008 Topher Fangio
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person
5
+ # obtaining a copy of this software and associated documentation
6
+ # files (the "Software"), to deal in the Software without
7
+ # restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the
10
+ # Software is furnished to do so, subject to the following
11
+ # conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
+ # OTHER DEALINGS IN THE SOFTWARE.
24
+ #-------------------------------------------------------------------------------
25
+
26
+
27
+ module LCDProc
28
+
29
+ module Widgets
30
+
31
+ class Graph
32
+ include LCDProc::Widget
33
+ LCDProc::Widget.add_support( :graph, self )
34
+
35
+ attr_accessor :bars, :x, :y
36
+
37
+ # Note that the lenght is not changeable after you have added a graph.
38
+ attr_reader :length
39
+
40
+ @@widget_count = 0
41
+
42
+
43
+ # Creates a new Graph widget which can then be displayed anywhere on the screen.
44
+ #
45
+ # * <tt>id</tt> - A unique string which identifies this widget. Defaults to "GraphWidget_" + a sequence number.
46
+ # * <tt>col</tt> - The beginning column in which you wish to display the graph on screen. Defaults to 1.
47
+ # * <tt>row</tt> - The beginning row in which you wish to display the text on screen. Defaults to 1.
48
+ # * <tt>length</tt> - The length of the graph (how many vertical bars to show). Defaults to 21 - x for a 20x4 screen.
49
+ #
50
+ # Example:
51
+ #
52
+ # w = Graph.new
53
+ #
54
+ # or
55
+ #
56
+ # w = Graph.new( 'Test', 'This is a graph', 1, 5, 10 )
57
+ #
58
+ # *NOTE*: The length of the graph is not changeable after it has been created
59
+ def initialize( id = "GraphWidget_" + @@widget_count.to_s, col = 1, row = 1, length = 21 - col)
60
+ if id.nil?
61
+ id = "GraphWidget_#{@@widget_count}"
62
+ end
63
+
64
+ @id = id
65
+ @type = :graph
66
+ @screen = nil
67
+ @visible = false
68
+
69
+ @x = col
70
+ @y = row
71
+ @length = length
72
+
73
+ @bars = []
74
+ @length.times{ |i| @bars << VBar.new( "#{self.id}_Bar_#{i}", 1, @x + i, @y ) }
75
+
76
+ @@widget_count += 1
77
+ end
78
+
79
+
80
+ # Actually sends the command to the LCDd server to remove the widget from the screen
81
+ def hide
82
+
83
+ # Only if we are visible, attempt to hide ourselves
84
+ if @visible
85
+ passed = true
86
+
87
+ @bars.each do |bar|
88
+
89
+ # If at any point we fail, bail out and return false
90
+ if not bar.hide or not bar.detach
91
+ passed = false
92
+ break
93
+ end
94
+
95
+ end
96
+
97
+ if passed
98
+ @visible = false
99
+ @screen.client.add_message( "Widget '#{@id}' is now hidden" )
100
+ return true
101
+ else
102
+ @visible = true
103
+ @screen.client.add_message( "Error: Widget '#{@id}' could NOT be hidden (#{response.message})" )
104
+ return false
105
+ end
106
+ else
107
+ return true
108
+ end
109
+
110
+ end
111
+
112
+
113
+ # Actually sends the command to the LCDd server to add the widget to the screen
114
+ def show
115
+
116
+ # If we aren't already visible, attempt to make oursevles visible
117
+ if not @visible
118
+ commands = []
119
+ passed = true
120
+
121
+ @bars.each do |bar|
122
+
123
+ # If at any point we fail, bail out and return false
124
+ if not bar.attach_to( @screen ) or not bar.show( false )
125
+ passed = false
126
+ break
127
+ end
128
+
129
+ end
130
+
131
+ @bars.each do |bar|
132
+ commands << "widget_add #{@screen.id} #{bar.id} #{bar.type.to_s}"
133
+ end
134
+
135
+ response = @screen.client.send_command( Command.new( commands ) )
136
+
137
+ if response.successful? and passed
138
+ @visible = true
139
+ @screen.client.add_message( "Widget '#{@id}' is now visible" )
140
+
141
+ self.update
142
+
143
+ return true
144
+ else
145
+ @visible = false
146
+ @screen.client.add_message( "Error: Widget '#{@id}' could NOT be displayed (#{response.message})" )
147
+ return false
148
+ end
149
+ else
150
+ return true
151
+ end
152
+
153
+ end
154
+
155
+
156
+ # Sends to command to the LCDd server to update the widget on screen
157
+ def update
158
+
159
+ if @screen
160
+ commands = []
161
+
162
+ @bars.each do |bar|
163
+ commands << "widget_set #{@screen.id} #{bar.id} #{bar.x} #{bar.y} #{bar.height}"
164
+ end
165
+
166
+ response = @screen.client.send_command( Command.new( commands ) )
167
+
168
+ if response.successful?
169
+ @screen.client.add_message( "Widget '#{@id}' was successfully updated" )
170
+ return true
171
+ else
172
+ @screen.client.add_message( "Error: Widget '#{@id}' was NOT successfully updated (#{response.message})" )
173
+ return true
174
+ end
175
+ else
176
+ @screen.client.add_message( "Error: Cannot update Widget '#{@id}' until it is attached to a screen" )
177
+ return false
178
+ end
179
+ end
180
+
181
+ end
182
+
183
+ end
184
+
185
+ end
@@ -0,0 +1,101 @@
1
+ #-------------------------------------------------------------------------------
2
+ # Copyright (c) 2008 Topher Fangio
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person
5
+ # obtaining a copy of this software and associated documentation
6
+ # files (the "Software"), to deal in the Software without
7
+ # restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the
10
+ # Software is furnished to do so, subject to the following
11
+ # conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
+ # OTHER DEALINGS IN THE SOFTWARE.
24
+ #-------------------------------------------------------------------------------
25
+
26
+
27
+ module LCDProc
28
+
29
+ module Widgets
30
+
31
+ class HBar
32
+ include LCDProc::Widget
33
+ LCDProc::Widget.add_support( :hbar, self )
34
+
35
+ attr_accessor :x, :y, :length
36
+
37
+ @@widget_count = 0
38
+
39
+
40
+ # Creates a new HBar widget which can then be displayed anywhere on the screen.
41
+ #
42
+ # * <tt>:id</tt> - A unique string which identifies this widget. Defaults to "HBarWidget_" + a sequence number.
43
+ # * <tt>:bar_length</tt> - The length of the bar. Default to 120 (fills a 20x4 character screen).
44
+ # * <tt>:row</tt> - The row in which the bar should be positioned. Defaults to 1.
45
+ # * <tt>:col</tt> - The column in which the bar should be positioned. Defaults to 1.
46
+ #
47
+ # Example:
48
+ #
49
+ # w = HBar.new
50
+ #
51
+ # or
52
+ #
53
+ # w = HBar.new( 'TestBar', 15, 1, 5 )
54
+ def initialize( id = "HBarWidget_#{@@widget_count}", bar_length = 120, row = 1, col = 1 )
55
+ if id.nil?
56
+ id = "HBarWidget_#{@@widget_count}"
57
+ end
58
+
59
+ @id = id
60
+ @type = :hbar
61
+ @screen = nil
62
+ @visible = false
63
+
64
+ @length = bar_length
65
+ @x = row
66
+ @y = col
67
+
68
+ @@widget_count += 1
69
+ end
70
+
71
+
72
+ # Sends to command to the LCDd server to update the widget on screen
73
+ def update
74
+ if @screen
75
+
76
+ bar_length = @length
77
+
78
+ if @length < 1
79
+ bar_length = @length * 120
80
+ end
81
+
82
+ response = @screen.client.send_command( Command.new( "widget_set #{@screen.id} #{self.id} #{@x} #{@y} \"#{bar_length}\"" ) )
83
+
84
+ if response.successful?
85
+ @screen.client.add_message( "Widget '#{@id}' was successfully updated" )
86
+ return true
87
+ else
88
+ @screen.client.add_message( "Error: Widget '#{@id}' was NOT successfully updated (#{response.message})" )
89
+ return true
90
+ end
91
+ else
92
+ @screen.client.add_message( "Error: Cannot update Widget '#{@id}' until it is attached to a screen" )
93
+ return false
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,94 @@
1
+ #-------------------------------------------------------------------------------
2
+ # Copyright (c) 2008 Topher Fangio
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person
5
+ # obtaining a copy of this software and associated documentation
6
+ # files (the "Software"), to deal in the Software without
7
+ # restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the
10
+ # Software is furnished to do so, subject to the following
11
+ # conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
+ # OTHER DEALINGS IN THE SOFTWARE.
24
+ #-------------------------------------------------------------------------------
25
+
26
+
27
+ module LCDProc
28
+
29
+ module Widgets
30
+
31
+ class Icon
32
+ include LCDProc::Widget
33
+ LCDProc::Widget.add_support( :icon, self )
34
+
35
+ attr_accessor :x, :y, :name
36
+
37
+ @@widget_count = 0
38
+
39
+
40
+ # Creates a new Icon widget which can then be displayed anywhere on the screen.
41
+ #
42
+ # * <tt>:id</tt> - A unique string which identifies this widget. Defaults to "IconWidget_" + a sequence number.
43
+ # * <tt>:icon_name</tt> - The name of the icon that you wish to display on the screen. Default to "".
44
+ # * <tt>:col</tt> - The column in which you wish to display the text on screen. Defaults to 1.
45
+ # * <tt>:row</tt> - The row in which you wish to display the text on screen. Defaults to 1.
46
+ #
47
+ # Example:
48
+ #
49
+ # w = Icon.new
50
+ #
51
+ # or
52
+ #
53
+ # w = Icon.new( 'Test', 'This is a icon', 1, 5 )
54
+ def initialize( id = "IconWidget_#{@@widget_count}", icon_name = "", col = 1, row = 1 )
55
+ if id.nil?
56
+ id = "IconWidget_#{@@widget_count}"
57
+ end
58
+
59
+ @id = id
60
+ @type = :icon
61
+ @screen = nil
62
+ @visible = false
63
+
64
+ @name = icon_name
65
+ @x = col
66
+ @y = row
67
+
68
+ @@widget_count += 1
69
+ end
70
+
71
+
72
+ # Sends to command to the LCDd server to update the widget on screen
73
+ def update
74
+ if @screen
75
+ response = @screen.client.send_command( Command.new( "widget_set #{@screen.id} #{self.id} #{@x} #{@y} \"#{@text}\"" ) )
76
+
77
+ if response.successful?
78
+ @screen.client.add_message( "Widget '#{@id}' was successfully updated" )
79
+ return true
80
+ else
81
+ @screen.client.add_message( "Error: Widget '#{@id}' was NOT successfully updated (#{response.message})" )
82
+ return true
83
+ end
84
+ else
85
+ @screen.client.add_message( "Error: Cannot update Widget '#{@id}' until it is attached to a screen" )
86
+ return false
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end