blinkenlights 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,5 @@
1
- 2005-10-18 * Some small bugs fixed
2
- * flash method added
3
- * additional example
1
+ 2006-06-08 - 0.0.3 * Added support for Windows. The relevant code parts were
2
+ contributed by Dave Burt <dave@burt.id.au>. Thanks a lot!
3
+ 2005-10-18 - 0.0.2 * Some small bugs fixed
4
+ * flash method added
5
+ * additional example
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,7 +5,7 @@ include BlinkenLights::Constants
5
5
 
6
6
  proc_loadavg = '/proc/loadavg'
7
7
 
8
- bl = BlinkenLights.new
8
+ bl = BlinkenLights.new(ARGV.shift || '/dev/tty8')
9
9
  trap(:INT) do
10
10
  bl.close if bl
11
11
  exit
@@ -19,6 +19,8 @@ loop do
19
19
  when 0.25..0.50 then bl.digital = LED_LEFT
20
20
  when 0.50..0.75 then bl.digital = LED_LEFT | LED_MIDDLE
21
21
  when 0.75..1.00 then bl.digital = LED_LEFT | LED_MIDDLE | LED_RIGHT
22
+ when 1.00..2.00 then bl.off ; bl.digital = LED_RIGHT
23
+ when 2.00..3.00 then bl.off ; bl.digital = LED_RIGHT | LED_MIDDLE
22
24
  else bl.flash
23
25
  end
24
26
  end
@@ -6,7 +6,7 @@ include BlinkenLights::Constants
6
6
  proc_net_dev = '/proc/net/dev'
7
7
  dev = ARGV.shift || 'eth0'
8
8
 
9
- bl = BlinkenLights.new
9
+ bl = BlinkenLights.new(ARGV.shift || '/dev/tty8')
10
10
  trap(:INT) do
11
11
  bl.close if bl
12
12
  exit
@@ -20,7 +20,7 @@ loop do
20
20
  parts = line[0].scan(/\s+\d+/)
21
21
  rx, tx = parts[0].to_i, parts[8].to_i
22
22
  bl.digital = case [ rx != old_rx, tx != old_tx ]
23
- when [ true, true ] then LED_LEFT | LED_RIGHT
23
+ when [ true, true ] then LED_ALL
24
24
  when [ false, true ] then LED_RIGHT
25
25
  when [ true, false ] then LED_LEFT
26
26
  else 0
@@ -1,3 +1,8 @@
1
+ begin
2
+ require 'Win32API'
3
+ rescue LoadError
4
+ end
5
+
1
6
  # = blinkenlights.rb - Controlling the keyboard LEDs from Ruby
2
7
  #
3
8
  # == Author
@@ -28,6 +33,10 @@
28
33
  # It enables you to control the LEDs on your keyboard to signal digital numbers
29
34
  # between 0 and 7, events like received/transmitted network packets, or just
30
35
  # let them blink in visually pleasing patterns.
36
+ #
37
+ # *Beware*: If you use BlinkenLights under Windows, not only the leds are
38
+ # switched on/off but also the related keys are pressed/unpressed! This could
39
+ # be quite confusing. ;)
31
40
  #
32
41
  # == Examples
33
42
  #
@@ -46,10 +55,10 @@
46
55
  # 100.times { bl.random }
47
56
  # bl.close
48
57
  #
49
- # There's also two short examples examples/netblinker.rb and
58
+ # There are also two short examples examples/netblinker.rb and
50
59
  # examples/loadbar.rb in the distribution directory of this library, that show
51
60
  # how to let the lights blink if network packets are received/transmitted on
52
- # your host or how high the cpu load average is.
61
+ # your host or to indicate how high the cpu load average is.
53
62
  class BlinkenLights
54
63
 
55
64
  # Module to hold the BlinkenLights constants.
@@ -57,9 +66,9 @@ class BlinkenLights
57
66
  # The default tty. It happens to be the one, I run X on. ;)
58
67
  DEF_TTY = '/dev/tty8'
59
68
 
60
- # DEF_DELAY is the default standard delay in seconds, that is slept everytime
61
- # the LED state is changed. If it is too small your keyboard may become
62
- # confused about its LEDs' status.
69
+ # DEF_DELAY is the default standard delay in seconds, that is slept
70
+ # everytime the LED state is changed. If it is too small your keyboard may
71
+ # become confused about its LEDs' status.
63
72
  DEF_DELAY = 0.1
64
73
 
65
74
  # Scroll Lock LED (from /usr/include/linux/kd.h)
@@ -101,19 +110,42 @@ class BlinkenLights
101
110
 
102
111
  # All of the LEDs
103
112
  LED_ALL = 7
113
+
114
+ if defined? ::Win32API
115
+ # Mapping from Unix LEDs to Windows LEDs
116
+ WINDOWS_LEDS = { LED_SCR => 0x91, LED_NUM => 0x90, LED_CAP => 0x14 }
117
+
118
+ # Windows key released
119
+ WIN_KEY_UP = 0x26
120
+
121
+ GetKeyState = ::Win32API.new("user32", "GetKeyState", ["i"], "i")
122
+ Keybd_event = ::Win32API.new("user32", "keybd_event", %w[i i i i], "v")
123
+ end
104
124
  end
105
125
  include Constants
106
126
 
107
- # Creates a BlinkenLights instance for _tty_, a full pathname like
108
- # '/dev/tty8' to control the LEDs. _delay_ is the standard delay in seconds,
109
- # that is slept everytime the LED state is changed. If _delay_ is too small
110
- # your keyboard may become confused about its LEDs' status.
111
- def initialize(tty = DEF_TTY, delay = DEF_DELAY)
112
- @tty = File.new(tty, File::RDWR)
113
- @delay = delay
114
- @old_leds = get
127
+ if defined? ::Win32API
128
+ def initialize(ignored = nil, delay = DEF_DELAY) # :nodoc:
129
+ @tty = File.new('NUL', File::RDWR)
130
+ @delay = delay
131
+ @old_leds = get
132
+ end
133
+ else
134
+ # Creates a BlinkenLights instance for _tty_, a full pathname like
135
+ # '/dev/tty8' to control the LEDs. This parameter is ignored under the
136
+ # Windows operating system.
137
+ #
138
+ # _delay_ is the standard delay in seconds,
139
+ # that is slept everytime the LED state is changed. If _delay_ is too small
140
+ # your keyboard may become confused about its LEDs' status.
141
+ def initialize(tty = DEF_TTY, delay = DEF_DELAY)
142
+ @tty = File.new(tty, File::RDWR)
143
+ @delay = delay
144
+ @old_leds = get
145
+ end
115
146
  end
116
147
 
148
+
117
149
  # The standard delay of this BlinkenLights instance.
118
150
  attr_accessor :delay
119
151
 
@@ -385,18 +417,37 @@ class BlinkenLights
385
417
  self
386
418
  end
387
419
 
388
- # Set the state of the LEDs to integer _number_. (Quite low level)
389
- def set(number)
390
- @tty.ioctl(KDSETLED, number)
391
- sleep @delay
392
- number
393
- end
420
+ if defined? ::Win32API
421
+ def set(number) # :nodoc:
422
+ WINDOWS_LEDS.each do |nix, win|
423
+ if (number & nix != 0) ^ (GetKeyState.call(win) != 0)
424
+ Keybd_event.call(win, 0, 0, 0)
425
+ Keybd_event.call(win, 0, WIN_KEY_UP, 0)
426
+ end
427
+ end
428
+ sleep @delay
429
+ number
430
+ end
394
431
 
395
- # Return the state of the LEDs as an integer _number_. (Quite low level)
396
- def get
397
- char = [0].pack('C')
398
- @tty.ioctl(KDGETLED, char)
399
- char.unpack('C')[0]
432
+ def get # :nodoc:
433
+ WINDOWS_LEDS.inject(0) do |sum, (nix, win)|
434
+ sum | ((GetKeyState.call(win) != 0) ? nix : 0)
435
+ end
436
+ end
437
+ else
438
+ # Set the state of the LEDs to integer _number_. (Quite low level)
439
+ def set(number)
440
+ @tty.ioctl(KDSETLED, number)
441
+ sleep @delay
442
+ number
443
+ end
444
+
445
+ # Return the state of the LEDs as an integer _number_. (Quite low level)
446
+ def get
447
+ char = [0].pack('C')
448
+ @tty.ioctl(KDGETLED, char)
449
+ char.unpack('C')[0]
450
+ end
400
451
  end
401
452
 
402
453
  # Return a string representation of this BlinkenLights instance, showing
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- system "rdoc -m BlinkenLights lib/blinkenlights.rb"
4
- # vim: set et sw=4 ts=4:
3
+ require 'rbconfig'
4
+ bindir = Config::CONFIG['bindir']
5
5
 
6
+ system "#{bindir}/ruby #{bindir}/rdoc -m BlinkenLights lib/blinkenlights.rb"
7
+ # vim: set et sw=4 ts=4:
metadata CHANGED
@@ -3,52 +3,57 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: blinkenlights
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2005-10-19 00:00:00 +02:00
6
+ version: 0.0.3
7
+ date: 2006-04-08 00:00:00 +02:00
8
8
  summary: Control the Blinkenlights on your keyboard from Ruby
9
9
  require_paths:
10
- - lib
10
+ - lib
11
11
  email: flori@ping.de
12
12
  homepage: http://blinkenlights.rubyforge.org
13
13
  rubyforge_project: blinkenlights
14
- description: ''
14
+ description: ""
15
15
  autorequire: blinkenlights
16
16
  default_executable:
17
17
  bindir: bin
18
18
  has_rdoc: true
19
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
20
20
  requirements:
21
- -
22
- - ">"
23
- - !ruby/object:Gem::Version
24
- version: 0.0.0
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
25
24
  version:
26
25
  platform: ruby
27
26
  signing_key:
28
27
  cert_chain:
29
28
  authors:
30
- - Florian Frank
29
+ - Florian Frank
31
30
  files:
32
- - examples
33
- - install.rb
34
- - GPL
35
- - Rakefile
36
- - VERSION
37
- - CHANGES
38
- - lib
39
- - make_doc.rb
40
- - README.en
41
- - examples/netblinker.rb
42
- - examples/loadbar.rb
43
- - lib/blinkenlights.rb
31
+ - examples
32
+ - install.rb
33
+ - GPL
34
+ - Rakefile
35
+ - VERSION
36
+ - CHANGES
37
+ - lib
38
+ - make_doc.rb
39
+ - README.en
40
+ - examples/netblinker.rb
41
+ - examples/loadbar.rb
42
+ - lib/blinkenlights.rb
44
43
  test_files: []
44
+
45
45
  rdoc_options:
46
- - "--title"
47
- - BlinkenLights in Ruby
48
- - "--main"
49
- - BlinkenLights
46
+ - --title
47
+ - BlinkenLights in Ruby
48
+ - --main
49
+ - BlinkenLights
50
50
  extra_rdoc_files: []
51
+
51
52
  executables: []
53
+
52
54
  extensions: []
55
+
53
56
  requirements: []
54
- dependencies: []
57
+
58
+ dependencies: []
59
+