serdisplib-ruby 0.5.0-x86-linux

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.
@@ -0,0 +1,260 @@
1
+ #!/usr/bin/ruby -w
2
+ #
3
+ # File:: serdisp_test.rb
4
+ # Description:: Testing serdisplib Ruby extension.
5
+ #
6
+ # Author:: David Muir <dhm@davidmuir.co.uk>
7
+ # Date:: 7 June 2008
8
+ #
9
+ # Based off of testserdisp.c by Wolfgang Astleitner.
10
+ # Portions Copyright (C) 2003-2007 Wolfgang Astleitner.
11
+ # ----------------------------------------------------------------------------
12
+ # This program is free software; you can redistribute it and/or modify
13
+ # it under the terms of the GNU General Public License as published by
14
+ # the Free Software Foundation; either version 2 of the License, or (at
15
+ # your option) any later version.
16
+ #
17
+ # This program is distributed in the hope that it will be useful, but
18
+ # WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
+ # General Public License for more details.
21
+ #
22
+ # You should have received a copy of the GNU General Public License
23
+ # along with this program; if not, write to the Free Software
24
+ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25
+ # 02111-1307, USA. Or, point your browser to
26
+ # http://www.gnu.org/copyleft/gpl.html
27
+ #-----------------------------------------------------------------------------
28
+
29
+ #-----------------------------------------------------------------------------
30
+ # Uses
31
+ #-----------------------------------------------------------------------------
32
+ require 'rubygems'
33
+ require 'serdisp'
34
+ include Serdisp
35
+
36
+ require 'getoptlong'
37
+
38
+ #-----------------------------------------------------------------------------
39
+ # Constants
40
+ #-----------------------------------------------------------------------------
41
+
42
+ SERDISP_TEST_RB_NAME = 'serdisp_test.rb'
43
+ SERDISP_TEST_RB_VERSION = '0.0.1'
44
+ SERDISP_TEST_RB_OPTIONS = GetoptLong.new(
45
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
46
+ [ '--version', '-V', GetoptLong::NO_ARGUMENT ],
47
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
48
+ [ '--name', '-n', GetoptLong::REQUIRED_ARGUMENT ],
49
+ [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT ],
50
+ [ '--options', '-o', GetoptLong::REQUIRED_ARGUMENT ],
51
+ [ '--debug', '-d', GetoptLong::OPTIONAL_ARGUMENT ]
52
+ )
53
+
54
+ #-----------------------------------------------------------------------------
55
+ # Functions
56
+ #-----------------------------------------------------------------------------
57
+
58
+ #
59
+ # Print program usage information to stdout.
60
+ #
61
+ def usage()
62
+ puts "usage: #{SERDISP_TEST_RB_NAME} -n <display name> [<options>]\n\n"
63
+ puts " Options: (default values in square brackets)"
64
+ puts " -n name display name"
65
+ puts " -p dev|port output device or port"
66
+ puts " -o options options for driver, semicolon-separated key-value pairs"
67
+ puts " eg: -o \"WIRING=1;CONTRAST=2;BACKLIGHT=ON\""
68
+ puts " -v verbose output"
69
+ puts " -d [0] debug level (0 .. no debugging, 2 .. max. debugging)"
70
+ puts " -V version information\n\n"
71
+ puts " Examples:"
72
+ puts " #{SERDISP_TEST_RB_NAME} -n PCD8544 -p \"/dev/parport0\""
73
+ puts " #{SERDISP_TEST_RB_NAME} -n PCD8544 -p \"0x378\" ## direct io"
74
+ end
75
+
76
+ #
77
+ # Print program version information to stdout.
78
+ #
79
+ def version()
80
+ print " #{SERDISP_TEST_RB_NAME} version #{SERDISP_TEST_RB_VERSION}"
81
+ print " (using serdisplib version #{SERDISP_VERSION_MAJOR}.#{SERDISP_VERSION_MINOR})\n"
82
+ puts " Portions (C) 2003-2007 Wolfgang Astleitner"
83
+ puts " (C) 2008 David Muir"
84
+ puts " Based on testserdisp (part of the serdisplib package) by Wolfgang Astleitner"
85
+ puts " Ruby extension and this Ruby test script written by David Muir"
86
+ end
87
+
88
+ #
89
+ # Print interpreter usage information to stdout.
90
+ #
91
+ def interpreter_usage()
92
+ puts "\n\n"
93
+ puts "exit | quit exit this program"
94
+ puts "close exit this program but don't shut down display"
95
+ puts "clear | cls clear display"
96
+ puts "redraw | re redraw display"
97
+ puts "update | upd update display"
98
+ puts "invert | inv toggle display inversion"
99
+ puts "rotate | rot rotate display 180 degrees"
100
+ puts "reset re-init the display"
101
+ puts "fullreset resets device and re-inits the display"
102
+ puts "blink <x> blink x-times (if no x given: blink once)"
103
+ puts "sp <W>x<H> set a pixel at WxH. eg: sp 1x1"
104
+ puts "cp <W>x<H> clear a pixel at WxH. eg: cp 1x1"
105
+ puts "gp <W>x<H> get pixel at WxH. eg: gp 1x1"
106
+ puts "p [<x>, 0 <= x <= 9] draw a test pattern (p w/o digit for more info)"
107
+ puts "sl <x> shift display to left x times"
108
+ puts "sr <x> shift display to right x times\n\n"
109
+ end
110
+
111
+ #-----------------------------------------------------------------------------
112
+ # Entry-Point
113
+ #-----------------------------------------------------------------------------
114
+
115
+ if ( __FILE__ == $0 ) then
116
+
117
+ # Parse options
118
+ g_name = ''
119
+ g_device = ''
120
+ g_options = ''
121
+ g_loop = true
122
+ g_shutdown_lcd = true
123
+ g_fgcolour = SD_COL_BLACK
124
+ g_bgcolour = SD_COL_WHITE
125
+ SERDISP_TEST_RB_OPTIONS.each do |opt, arg|
126
+ case opt
127
+ when '--help'
128
+ usage()
129
+ g_loop = false
130
+ when '--version'
131
+ version()
132
+ g_loop = false
133
+ when '--name'
134
+ g_name = arg
135
+ puts "Set LCD display: #{arg}"
136
+ when '--port'
137
+ g_device = arg
138
+ puts "Set LCD device: #{arg}"
139
+ when '-options'
140
+ g_options = arg
141
+ when '--debug'
142
+ sd_debuglevel = arg.to_i
143
+ end
144
+ end
145
+
146
+ if ( '' == g_device and '' == g_name and g_loop )
147
+ g_loop = false
148
+ usage()
149
+ end
150
+
151
+ begin
152
+ if ( g_loop ) then
153
+ sdcd = SDCONN_open( g_device )
154
+ raise RuntimeError.new( "Failed to initialise device: #{g_device}" ) \
155
+ if ( nil == sdcd )
156
+ dd = serdisp_init( sdcd, g_name, g_options )
157
+ raise RuntimeError.new( "Failed to initilaise LCD: #{g_name}" ) \
158
+ if ( nil == dd )
159
+
160
+ puts "\nEnter 'help' to get help\n\n"
161
+ serdisp_clear( dd )
162
+
163
+ # User-command processing loop
164
+ while ( g_loop )
165
+ print "> "
166
+ cmd = gets.chomp
167
+ case cmd
168
+ # LCD commands
169
+ when 'clear', 'cls'
170
+ serdisp_clear( dd )
171
+ when 'redraw', 're'
172
+ serdisp_rewrite( dd )
173
+ when 'update', 'upd'
174
+ serdisp_update( dd )
175
+ when 'invert', 'inv'
176
+ serdisp_setoption( dd, 'INVERT', SD_OPTION_TOGGLE )
177
+ when /rotate( [0-9]+)?/, /rot( [0-9]+)?/
178
+ rc = ( nil == $1 ) ? SD_OPTON_TOGGLE : $1.to_i
179
+ serdisp_setoption( dd, 'ROTATE', rc )
180
+ when 'reset'
181
+ rc = serdisp_reset( dd )
182
+ puts "reset was #{rc ? '' : 'un'}successful"
183
+ when 'fullreset'
184
+ dd = serdisp_fullreset( dd )
185
+ puts "fullreset was #{dd ? '' : 'un'}successful"
186
+ g_loop = false if ( nil == dd )
187
+ when /blink( [0-9]+)?/
188
+ serdisp_blink( dd, 0, $1.to_i, 100 )
189
+ when /sp ([0-9]+)x([0-9]+)/
190
+ x = $1.to_i
191
+ y = $2.to_i
192
+ puts "set pixel at: #{x}/#{y}"
193
+ serdisp_setcolour( dd, x, y, g_fgcolour )
194
+ serdisp_update( dd )
195
+ when /gp ([0-9]+)x([0-9]+)/
196
+ x = $1.to_i
197
+ y = $2.to_i
198
+ printf( "pixel at: %d/%d: %d\n\n", x, y, serdisp_getcolour( dd, x, y ) )
199
+ serdisp_setcolour( dd, x, y, g_fgcolour )
200
+ serdisp_update( dd )
201
+ when /cp ([0-9]+)x([0-9]+)/
202
+ x = $1.to_i
203
+ y = $2.to_i
204
+ puts "clear pixel at: #{x}/#{y}"
205
+ serdisp_setcolour( dd, x, y, g_bgcolour )
206
+ serdisp_update( dd )
207
+ when /p ([0-9])/
208
+ pattern = $1.to_i
209
+ serdisp_clear( dd )
210
+ case pattern
211
+ when 1
212
+ h = serdisp_getheight( dd )
213
+ w = serdisp_getwidth( dd )
214
+ px = 0
215
+ py = 0
216
+ while ( py < h )
217
+ while ( px < w )
218
+ serdisp_setcolour( dd, px, py, g_fgcolour )
219
+ px += 2
220
+ end
221
+ py += 2
222
+ end
223
+ serdisp_update( dd )
224
+ else
225
+ puts "Pattern #{pattern} not currently implemented.\n\n"
226
+ end # pattern
227
+ # Interpreter commands
228
+ when 'help'
229
+ interpreter_usage()
230
+ when 'quit', 'exit'
231
+ g_loop = false
232
+ when 'close'
233
+ g_loop = false
234
+ g_shutdown_lcd = false
235
+ else
236
+ puts "Unknown/unimplemented command. Enter 'help' to get help.\n\n"
237
+ end
238
+ end
239
+
240
+ if ( g_shutdown_lcd ) then
241
+ serdisp_quit( dd )
242
+ else
243
+ serdisp_close( dd )
244
+ end
245
+ end
246
+
247
+ rescue RuntimeError => ex
248
+ puts "Exception: #{ex.message}"
249
+ puts "Exiting."
250
+
251
+ rescue Exception => ex
252
+ puts "Unhandled exception: #{ex.message}"
253
+ puts "Exiting."
254
+ end
255
+
256
+ #Serdisp.methods.each do |m| puts m; end
257
+ #Serdisp.constants.each do |c| puts c; end
258
+ end
259
+
260
+ # End of serdisp_test.rb
@@ -0,0 +1,218 @@
1
+ #!/usr/bin/ruby -w
2
+ #
3
+ # File:: uptime.rb
4
+ # Description:: Sample uptime application for serdisp-ruby Gem.
5
+ #
6
+ # Author:: David Muir <dhm@davidmuir.co.uk>
7
+ # Date:: 12 June 2008
8
+ #
9
+
10
+ #-----------------------------------------------------------------------------
11
+ # Uses
12
+ #-----------------------------------------------------------------------------
13
+ require 'rubygems'
14
+ require 'getoptlong'
15
+ require 'serdisp'
16
+ include Serdisp
17
+ require 'imlib2_util'
18
+
19
+ #-----------------------------------------------------------------------------
20
+ # Constants
21
+ #-----------------------------------------------------------------------------
22
+
23
+ UPTIME_NAME = 'uptime.rb'
24
+ UPTIME_VERSION = '0.0.1'
25
+ UPTIME_OPTIONS = GetoptLong.new(
26
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
27
+ [ '--version', '-V', GetoptLong::NO_ARGUMENT ],
28
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
29
+ [ '--name', '-n', GetoptLong::REQUIRED_ARGUMENT ],
30
+ [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT ],
31
+ [ '--options', '-o', GetoptLong::REQUIRED_ARGUMENT ],
32
+ [ '--debug', '-d', GetoptLong::OPTIONAL_ARGUMENT ],
33
+ [ '--refresh', '-r', GetoptLong::REQUIRED_ARGUMENT ]
34
+ )
35
+
36
+ #-----------------------------------------------------------------------------
37
+ # Globals
38
+ #-----------------------------------------------------------------------------
39
+
40
+ #-----------------------------------------------------------------------------
41
+ # Functions
42
+ #-----------------------------------------------------------------------------
43
+
44
+ #
45
+ # Print program usage information to stdout.
46
+ #
47
+ def usage()
48
+ puts "usage: #{UPTIME_NAME} -n <display name> [<options>]\n\n"
49
+ puts " Options: (default values in square brackets)"
50
+ puts " -n name display name"
51
+ puts " -p dev|port output device or port"
52
+ puts " -o options options for driver, semicolon-separated key-value pairs"
53
+ puts " eg: -o \"WIRING=1;CONTRAST=2;BACKLIGHT=ON\""
54
+ puts " -v verbose output"
55
+ puts " -d [0] debug level (0 .. no debugging, 2 .. max. debugging)"
56
+ puts " -V version information"
57
+ puts " -r <N> refresh delay in seconds\n\n"
58
+ puts " Examples:"
59
+ puts " #{UPTIME_NAME} -n PCD8544 -p \"/dev/parport0\""
60
+ puts " #{UPTIME_NAME} -n PCD8544 -p \"0x378\" ## direct io"
61
+ end
62
+
63
+ #
64
+ # Print program version information to stdout.
65
+ #
66
+ def version()
67
+ print " #{UPTIME_NAME} version #{UPTIME_VERSION}"
68
+ print " (using serdisplib version #{SERDISP_VERSION_MAJOR}.#{SERDISP_VERSION_MINOR})\n"
69
+ puts " Portions (C) 2003-2007 Wolfgang Astleitner"
70
+ puts " (C) 2008 David Muir"
71
+ puts " Simple serdisp-ruby test application which displays system uptime"
72
+ puts " and load usage on the LCD. Refreshing the data every 2 seconds."
73
+ end
74
+
75
+ #
76
+ # Print interpreter usage to stdout.
77
+ #
78
+ def print_interpreter_usage( )
79
+ puts "\n\n"
80
+ puts "exit | quit | q exit this program"
81
+ puts "help | h display this usage information"
82
+ puts "delay | d <[0-9]+> update refresh delay in seconds (default: 2)\n\n"
83
+ end
84
+
85
+ #-----------------------------------------------------------------------------
86
+ # Entry-Point
87
+ #-----------------------------------------------------------------------------
88
+
89
+ if ( __FILE__ == $0 ) then
90
+
91
+ # Parse options
92
+ g_name = ''
93
+ g_device = ''
94
+ g_options = ''
95
+ g_loop = true
96
+ g_delay = 2
97
+ g_fgcolour = SD_COL_BLACK
98
+ g_bgcolour = SD_COL_WHITE
99
+ UPTIME_OPTIONS.each do |opt, arg|
100
+ case opt
101
+ when '--help'
102
+ usage()
103
+ g_loop = false
104
+ when '--version'
105
+ version()
106
+ g_loop = false
107
+ when '--name'
108
+ g_name = arg
109
+ puts "Set LCD display: #{arg}"
110
+ when '--port'
111
+ g_device = arg
112
+ puts "Set LCD device: #{arg}"
113
+ when '-options'
114
+ g_options = arg
115
+ when '--debug'
116
+ sd_debuglevel = arg.to_i
117
+ when '--refresh'
118
+ g_delay = arg.to_i
119
+ end
120
+ end
121
+
122
+ if ( '' == g_device and '' == g_name and g_loop )
123
+ usage( )
124
+ g_loop = false
125
+ end
126
+
127
+ begin
128
+ if ( g_loop ) then
129
+ sdcd = SDCONN_open( g_device )
130
+ raise RuntimeError.new( "Failed to initialise device: #{g_device}" ) \
131
+ if ( nil == sdcd )
132
+ dd = serdisp_init( sdcd, g_name, g_options )
133
+ raise RuntimeError.new( "Failed to initilaise LCD: #{g_name}" ) \
134
+ if ( nil == dd )
135
+
136
+ puts "\nEnter 'help' to get help, 'quit' to quit\n\n"
137
+ serdisp_clear( dd )
138
+
139
+ # Initialise our refresh thread.
140
+ refresh_thread = Thread.new do
141
+ begin
142
+ h = serdisp_getheight( dd )
143
+ w = serdisp_getwidth( dd )
144
+ im = Imlib2::Image.new( w, h )
145
+ Imlib2::Font.add_path '/home/dhm/projects/library.ruby/trunk/gem/serdisp/examples/fonts'
146
+ font = Imlib2::Font.new( 'yudit/10' )
147
+ text = "Testing: UPTIME INFO"
148
+ fw, fh = font.size text
149
+ fx, fy = (im.width - fw) / 2, (im.height / 2 - fh ) / 2
150
+ im.draw_text font, text, fx, fy, Imlib2::Color::RED
151
+ im.save( 'test.png' )
152
+
153
+ while ( g_loop )
154
+
155
+ uptime_output = `uptime`
156
+ puts "output: #{uptime_output}"
157
+
158
+ lcd_data = Imlib2Util.imlib_image_to_serdisp_data( im )
159
+ im.width.times do |x|
160
+ im.height.times do |y|
161
+
162
+ serdisp_setcolour( dd, x, y, lcd_data[y][x] )
163
+ puts "Value: #{lcd_data[y][x]}" unless ( 0 == lcd_data[y][x] )
164
+ end
165
+ end
166
+ puts "Done setting LCD."
167
+
168
+ serdisp_update( dd )
169
+ sleep( g_delay )
170
+ end
171
+ ensure
172
+
173
+ im.delete!( true ) unless ( nil == im )
174
+ end
175
+ end
176
+
177
+ while ( g_loop )
178
+ print "> "
179
+ cmd = gets.chomp
180
+ case cmd
181
+ when 'h', 'help'
182
+ print_interpreter_usage( )
183
+
184
+ when 'q', 'quit', 'exit'
185
+ g_loop = false
186
+
187
+ when /delay ([0-9]+)?/, /d ([0-9]+)?/
188
+ puts "Setting delay time to #{$1.to_i} seconds"
189
+ g_delay = $1.to_i
190
+ end
191
+ end
192
+
193
+ refresh_thread.join()
194
+ end # if g_loop
195
+
196
+ rescue RuntimeError => ex
197
+ puts "Exception: #{ex.message}"
198
+ puts "Stacktrace:"
199
+ ex.backtrace.each do |m| puts m; end
200
+ puts "Exiting."
201
+
202
+ rescue Exception => ex
203
+ puts "Unhandled exception: #{ex.message}"
204
+ puts "Stacktrace:"
205
+ ex.backtrace.each do |m| puts m; end
206
+ puts "Exiting."
207
+
208
+ ensure
209
+ puts "Shutting down..."
210
+ g_loop = false
211
+ refresh_thread.join()
212
+ serdisp_quit( dd )
213
+
214
+ end
215
+ end
216
+
217
+ # End of uptime.rb
218
+