topherfangio-lcdproc-ruby 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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,54 @@
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
+ # This class exists for the sole purpose of expansion. I cannot currently think of any reason why we would
30
+ # need more than a simple string to be sent to the server, however, in the future, it may make more sense to
31
+ # send the command and some arguments.
32
+ class Command
33
+ attr_reader :message
34
+
35
+ # Creates a new Command object.
36
+ #
37
+ # * <tt>message</tt> - The message/command to be sent to the server.
38
+ #
39
+ # Example:
40
+ #
41
+ # hello = Command.new( "hello" )
42
+ # client.send( hello )
43
+ #
44
+ # or
45
+ #
46
+ # client.send( Command.new( "info" ) )
47
+ #
48
+ def initialize( message )
49
+ @message = message
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,41 @@
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 Errors
30
+
31
+ # Raised when an invalid command has been issued
32
+ class InvalidCommand < RuntimeError
33
+ end
34
+
35
+ # Raised when a command is formatted incorrectly
36
+ class IncorrectlyFormattedCommand < RuntimeError
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,59 @@
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
+ class KeyEvent
30
+ attr_reader :key, :block
31
+
32
+ # Creates a new KeyEvent object. Note that this does not register it with a client, it only gives you a
33
+ # clean object to pass around.
34
+ #
35
+ # * <tt>key</tt> - The key identifier. Valid values are "Escape", "Enter", "Up", "Down", "Left", or "Right".
36
+ # * <tt>&block</tt> - The block that you would like to associate with this key.
37
+ #
38
+ # Example:
39
+ #
40
+ # ke = KeyEvent.new( 'Enter' ) { puts 'Enter was pressed!' }
41
+ def self.new( key, &block )
42
+
43
+ if block.nil?
44
+ return nil
45
+ else
46
+ super( key, &block )
47
+ end
48
+
49
+ end
50
+
51
+
52
+ def initialize( key, &block )
53
+ @key = key
54
+ @block = block
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,188 @@
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
+ class MenuItemStore
30
+ attr_accessor :menu_item, :menu_event, :added_to_screen
31
+
32
+ def initialize( menu_item, menu_event, added_to_screen )
33
+ @menu_item = menu_item
34
+ @menu_event = menu_event
35
+ @added_to_screen = added_to_screen
36
+ end
37
+
38
+ end
39
+
40
+ class Menu
41
+ attr_reader :client, :id, :items
42
+ attr_accessor :parent
43
+
44
+ @@menu_count = 0
45
+
46
+ # Create a new Menu object optionally attached to a client. Note that if you would like to create
47
+ # a sub menu which can be attached to a menu, you should use LCDProc::MenuItems::Submenu.
48
+ #
49
+ # Note that when a client is created, this function is called and and it becomes the default client's menu
50
+ #
51
+ # * <tt>client</tt> - The client to which you wish this menu to be attached. Defaults to nil
52
+ def initialize( client = nil )
53
+ @id = "".quotify
54
+ @parent = nil
55
+ @client = client
56
+ @stored_items = []
57
+ end
58
+
59
+
60
+ # Adds the specified menu item to this menu. Returns the menu item that was added.
61
+ #
62
+ # * <tt>menu_item</tt> - The menu item that you wish to add
63
+ # * <tt>&block</tt> - You may optionally pass a block to this menthod to register a menu event with the client
64
+ #
65
+ # Example:
66
+ #
67
+ # c = Client.new
68
+ # a = c.menu.add_item( MenuItem.new( :action, :id => 'AnAction' ) ) { |response| puts "AnAction was pressed!!!" }
69
+ #
70
+ # # a now contains the action menu item
71
+ def add_item( menu_item, &block )
72
+
73
+ # This slight hack allows for sub menus to correctly know to whom they belong
74
+ if menu_item.respond_to? :parent=
75
+ menu_item.send( :parent=, self )
76
+ end
77
+
78
+ @stored_items << MenuItemStore.new( menu_item, MenuEvent.new( menu_item, &block ), false )
79
+
80
+ update
81
+
82
+ return menu_item
83
+ end
84
+
85
+
86
+ # Attaches this menu to a particular client. Note that this will detach this menu from any existing clients.
87
+ #
88
+ # * <tt>client</tt> - The client to which you wish to be attached. Defaults to nil.
89
+ # * <tt>should_update</tt> - Whether or not we should go ahead and display all of our items on the screen. Defaults to true.
90
+ #
91
+ # Example:
92
+ #
93
+ # c1 = Client.new
94
+ # c2 = Client.new
95
+ # c2.detach
96
+ #
97
+ # c1.menu.attach_to( c2 )
98
+ def attach_to( client = nil, should_update = true )
99
+
100
+ self.detach
101
+
102
+ @client = client
103
+
104
+ if should_update
105
+ self.update
106
+ end
107
+ end
108
+
109
+
110
+ # Detaches this menu from the current client. Note that if this fails, it still detaches the client, so
111
+ # you might have extraneous menu items lying around (but you shouldn't so long as you don't bypass the
112
+ # add_item method somehow...)
113
+ def detach
114
+ errors = []
115
+
116
+ @stored_items.select{ |si| si.added_to_screen == true }.each do |visible_stored_item|
117
+ menu_item = visible_stored_item.menu_item
118
+
119
+ response = client.send_command( Command.new( "menu_del_item #{@id} #{menu_item.id.quotify}" ) )
120
+
121
+ if not response.successful?
122
+ errors << true
123
+ end
124
+ end
125
+
126
+ # Make sure that we don't still think that any are visible on the screen
127
+ @stored_items.each { |si| si.added_to_screen = false }
128
+
129
+ @client = nil
130
+
131
+ return errors.length == 0
132
+ end
133
+
134
+
135
+ # Adds any existing, but not currently added, menu items to the screen (including sub menus and their items).
136
+ # This may be called to ensure that all items are displayed in the menu and is called whenever a menu item
137
+ # is added to a menu. This technically allows you to create a menu object that is not attached to a
138
+ # client, later attach it to a client and then call the update method to display the menu and it's items
139
+ # on screen.
140
+ #
141
+ # Example
142
+ #
143
+ # c = Client.new
144
+ # c.menu.update
145
+ def update
146
+
147
+ if @client
148
+ client = @client
149
+ else
150
+ parent_menu = @parent
151
+
152
+ while not parent_menu.nil? and parent_menu.client.nil?
153
+ parent_menu = parent_menu.parent
154
+ end
155
+
156
+ client = parent_menu.client unless parent_menu.nil? or parent_menu.client.nil?
157
+ end
158
+
159
+ if client
160
+
161
+ @stored_items.select{ |mis| mis.added_to_screen == false }.each do |stored_item|
162
+ menu_item = stored_item.menu_item
163
+
164
+ response = client.send_command( Command.new( "menu_add_item #{@id} #{menu_item.id.quotify} #{menu_item.lcdproc_type} #{menu_item.lcdproc_options_as_string}" ) )
165
+
166
+ if not response.successful?
167
+ return nil
168
+ else
169
+ stored_item.added_to_screen = true
170
+ end
171
+
172
+ if not stored_item.menu_event.nil?
173
+ client.register_menu_event( stored_item.menu_event )
174
+ end
175
+
176
+ if menu_item.respond_to? :update
177
+ menu_item.send( :update )
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+
184
+ end
185
+
186
+ end
187
+
188
+ end
@@ -0,0 +1,49 @@
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
+ class MenuEvent
30
+ attr_reader :menu_item, :block
31
+
32
+ # Creates a new MenuEvent attached to the specified menu_item. If no block is passed, this function
33
+ # returns nil.
34
+ def self.new( menu_item, &block )
35
+ if block.nil?
36
+ return nil
37
+ else
38
+ super( menu_item, &block )
39
+ end
40
+ end
41
+
42
+ def initialize( menu_item, &block )
43
+ @menu_item = menu_item
44
+ @block = block
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,108 @@
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
+ # MenuItem is a factory for creating new menu items and registering them with the client so that when the
30
+ # menu item's event is processed, it can call the associated block.
31
+ module MenuItem
32
+ attr_reader :id, :is_hidden, :lcdproc_type, :lcdproc_event_type, :next, :parent_menu, :previous, :text
33
+ attr_accessor :lcdproc_options
34
+ @supported_types = {}
35
+
36
+ def MenuItem.new( type = :string, options = {}, lcdproc_options = {} )
37
+ if @supported_types.include? type
38
+ menu_item = @supported_types[ type ].new( options, lcdproc_options )
39
+
40
+ return menu_item
41
+ else
42
+ return nil
43
+ end
44
+
45
+ end
46
+
47
+ # Allows a particular type of menu item to inform the MenuItem module (which is a MenuItem factory) that it is
48
+ # able to support a particular type of menu item.
49
+ def MenuItem.add_support( type, menu_item_class )
50
+ @supported_types[type] = menu_item_class
51
+ end
52
+
53
+
54
+ # Returns an array of symbols listing the supported types of menu items.
55
+ def MenuItem.supported_types
56
+ @supported_types.keys
57
+ end
58
+
59
+
60
+ # Set's whether or not the menu item is hidden.
61
+ #
62
+ # * <tt>value</tt> - True or false
63
+ #
64
+ # Example:
65
+ #
66
+ # m = MenuItem.new( :action )
67
+ # m.is_hidden = true
68
+ #
69
+ def is_hidden= value
70
+ # If we have a client, go ahead and send the command; otherwise, just set the value.
71
+ if @parent_menu and @parent_menu.client
72
+ response = @parent_menu.client.send_command( Command.new( "menu_set_item \"#{@parent_menu}\" \"#{@id}\" -is_hidden #{value}" ) )
73
+
74
+ if response.successful?
75
+ @is_hidden = value
76
+ else
77
+ return nil
78
+ end
79
+ else
80
+ @is_hidden = value
81
+ end
82
+ end
83
+
84
+
85
+ # Returns a string representation of the lcdproc_options that can be sent directly to LCDd.
86
+ def lcdproc_options_as_string
87
+ options_as_lcdproc = []
88
+ @lcdproc_options.each{ |key,value| options_as_lcdproc << "-#{key} #{value}" }
89
+ options_as_lcdproc.join(" ")
90
+ end
91
+
92
+ def text= value
93
+ # If we have a client, go ahead and send the command; otherwise, just set the value.
94
+ if @parent_menu and @parent_menu.client
95
+ response = @parent_menu.client.send_command( Command.new( "menu_set_item \"#{@parent_menu}\" \"#{@id}\" -text #{value}" ) )
96
+
97
+ if response.successful?
98
+ @text = value
99
+ else
100
+ return nil
101
+ end
102
+ else
103
+ @text = value
104
+ end
105
+ end
106
+ end
107
+
108
+ end