betabrite 0.0.2 → 1.0.0

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 (45) hide show
  1. data/.autotest +7 -0
  2. data/EXAMPLES +28 -73
  3. data/History.txt +16 -0
  4. data/Manifest.txt +35 -0
  5. data/{README → README.txt} +58 -52
  6. data/Rakefile +11 -0
  7. data/lib/betabrite.rb +6 -120
  8. data/lib/betabrite/autotest.rb +43 -0
  9. data/lib/betabrite/base.rb +86 -0
  10. data/lib/betabrite/files.rb +4 -0
  11. data/lib/betabrite/files/dots.rb +24 -0
  12. data/lib/betabrite/files/file_dsl.rb +18 -0
  13. data/lib/betabrite/files/string.rb +22 -0
  14. data/lib/betabrite/files/text.rb +122 -0
  15. data/lib/{memory → betabrite}/memory.rb +27 -5
  16. data/lib/betabrite/serial.rb +28 -0
  17. data/lib/{string.rb → betabrite/string.rb} +41 -15
  18. data/lib/betabrite/usb.rb +64 -0
  19. data/script/alloc.rb +21 -0
  20. data/script/clear_memory.rb +16 -0
  21. data/script/client.rb +39 -0
  22. data/script/dots_file.rb +35 -0
  23. data/script/server.rb +12 -0
  24. data/script/sign_test.rb +17 -0
  25. data/script/stock_alloc.rb +81 -0
  26. data/script/stock_client.rb +40 -0
  27. data/script/stockdata.rb +65 -0
  28. data/test/helper.rb +6 -0
  29. data/test/{tc_many_mem.rb → test_many_mem.rb} +6 -10
  30. data/test/{tc_memory.rb → test_memory.rb} +11 -24
  31. data/test/{tc_set_string.rb → test_set_string.rb} +5 -9
  32. data/test/test_string.rb +34 -0
  33. data/test/test_text_file.rb +19 -0
  34. data/test/test_usb_betabrite.rb +94 -0
  35. metadata +94 -60
  36. data/CHANGELOG +0 -10
  37. data/NOTES +0 -29
  38. data/lib/bb_version.rb +0 -3
  39. data/lib/files/dotsfile.rb +0 -24
  40. data/lib/files/stringfile.rb +0 -22
  41. data/lib/files/textfile.rb +0 -114
  42. data/test/bb_override.rb +0 -5
  43. data/test/tc_string.rb +0 -20
  44. data/test/tc_text_file.rb +0 -18
  45. data/test/ts_bb.rb +0 -7
@@ -0,0 +1,64 @@
1
+ module BetaBrite
2
+ class USB < Base
3
+ # USB Device Codes
4
+ PRODUCT_ID = 0x1234
5
+ VENDOR_ID = 0x8765
6
+ INTERFACE = 0x00
7
+ RECV_LENGTH = 0x40 # Max Packet Size of 64
8
+ SEND_LENGTH = 0x40 # Max Packet Size of 64
9
+ WRITE_ENDPOINT = 0x02
10
+ READ_ENDPOINT = 0x82
11
+ WRITE_TIMEOUT = 5000
12
+ READ_TIMEOUT = 5000
13
+
14
+ def reset!
15
+ write_memory!
16
+ end
17
+
18
+ def write_memory!
19
+ device = usb_device()
20
+ handle = usb_interface(device)
21
+ handle.usb_bulk_write(WRITE_ENDPOINT, memory_message, WRITE_TIMEOUT)
22
+ handle.usb_bulk_write(WRITE_ENDPOINT, memory_message, WRITE_TIMEOUT)
23
+ handle.usb_close
24
+ end
25
+
26
+ def write!
27
+ device = usb_device()
28
+ handle = usb_interface(device)
29
+ handle.usb_bulk_write(WRITE_ENDPOINT, message, WRITE_TIMEOUT)
30
+ handle.usb_bulk_write(WRITE_ENDPOINT, message, WRITE_TIMEOUT)
31
+ handle.usb_close
32
+ end
33
+
34
+ private
35
+ def usb_device
36
+ ::USB.devices.find { |d|
37
+ d.idProduct == PRODUCT_ID && d.idVendor == VENDOR_ID
38
+ } || raise("Unable to locate the BETABrite.")
39
+ end
40
+
41
+ def usb_interface(device)
42
+ handle = device.usb_open
43
+ raise "Unable to obtain a handle to the device." if handle.nil?
44
+
45
+ retries = 0
46
+ begin
47
+ error_code = handle.usb_claim_interface(INTERFACE)
48
+ raise unless error_code.nil?
49
+ rescue
50
+ handle.usb_detach_kernel_driver_np(INTERFACE);
51
+ if retries.zero?
52
+ retries += 1
53
+ retry
54
+ else
55
+ raise "Unable to claim the device interface."
56
+ end
57
+ end
58
+
59
+ raise "Unable to set alternative interface." unless handle.set_altinterface(0).nil?
60
+
61
+ handle
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,21 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'betabrite'
4
+ require 'serialport'
5
+
6
+ sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
7
+
8
+ sign = BetaBrite.new
9
+
10
+ string = BetaBrite::Memory::String.new('0', 32)
11
+ dots = BetaBrite::Memory::Dots.new('1', 7, 7)
12
+ text = BetaBrite::Memory::Text.new('A', 256)
13
+
14
+ sign.add text
15
+ sign.add dots
16
+ sign.add string
17
+
18
+ sign.allocate { |text|
19
+ sp.write text
20
+ }
21
+
@@ -0,0 +1,16 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ # = Synopsis
4
+ # This script shows an example of clearing the memory on the BetaBrite sign.
5
+ require 'betabrite'
6
+ require 'serialport'
7
+
8
+ sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
9
+
10
+ sign = BetaBrite.new
11
+ sign.add BetaBrite::Memory::Clear.new
12
+
13
+ sign.allocate { |text|
14
+ sp.write text
15
+ }
16
+
@@ -0,0 +1,39 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ # = Synopsis
4
+ # This script shows an example of setting a volitile StringFile, and
5
+ # referencing the StringFile from the TextFile.
6
+
7
+ require 'betabrite'
8
+ require 'serialport'
9
+
10
+ sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
11
+
12
+ sign = BetaBrite.new
13
+
14
+ # Create a new TextFile with the COMPRESSED_ROTATE mode.
15
+ tf = BetaBrite::TextFile.new { |a|
16
+ a.mode = BetaBrite::TextFile::Mode::COMPRESSED_ROTATE
17
+ }
18
+
19
+ # Set up a BetaBrite::String
20
+ hello_string = BetaBrite::String.new('James',
21
+ :color => BetaBrite::String::Color::AMBER
22
+ )
23
+ # Create a StringFile which can be modified without making the sign blink.
24
+ sf = BetaBrite::StringFile.new('0', hello_string)
25
+
26
+ # Create a dots file
27
+ dots = BetaBrite::DotsFile.new('1', 7, 7, [])
28
+
29
+ # Set the TextFile message and reference the StringFile and DotsFile
30
+ tf.message = BetaBrite::String.new("#{dots.id} Aaron #{sf.id} Patterson")
31
+
32
+
33
+ sign.add tf
34
+ sign.add sf
35
+
36
+ sign.write { |text|
37
+ sp.write text
38
+ }
39
+
@@ -0,0 +1,35 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'betabrite'
4
+ require 'serialport'
5
+
6
+ def rand_string(len = 7)
7
+ chars = ("1".."3").to_a
8
+ 3.times { chars << "0" }
9
+ string = ""
10
+ 1.upto(len) { |i| string << chars[rand(chars.size)] }
11
+ string
12
+ end
13
+
14
+ sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
15
+
16
+ sign = BetaBrite.new
17
+
18
+ pic = [
19
+ rand_string,
20
+ rand_string,
21
+ rand_string,
22
+ rand_string,
23
+ rand_string,
24
+ rand_string,
25
+ rand_string,
26
+ ]
27
+
28
+ dots = BetaBrite::DotsFile.new('1', 7, 7, pic)
29
+
30
+ sign.add dots
31
+
32
+ sign.write_dots { |text|
33
+ sp.write text
34
+ }
35
+
@@ -0,0 +1,12 @@
1
+ require 'drb'
2
+ require 'serialport'
3
+
4
+ class TestServer
5
+ def tty
6
+ sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
7
+ end
8
+ end
9
+
10
+ server = TestServer.new
11
+ DRb.start_service('druby://localhost.org:9000', server)
12
+ DRb.thread.join
@@ -0,0 +1,17 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'serialport'
4
+ require 'betabrite'
5
+
6
+ sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
7
+
8
+ sign = BetaBrite.new
9
+ tf = BetaBrite::TextFile.new
10
+ tf.message = ARGV[0]
11
+
12
+ sign.add tf
13
+
14
+ sign.write { |text|
15
+ sp.write text
16
+ }
17
+
@@ -0,0 +1,81 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'betabrite'
4
+ require 'usb'
5
+
6
+ class UsbBetabrite
7
+ # USB Device Codes
8
+ PRODUCT_ID = 0x1234
9
+ VENDOR_ID = 0x8765
10
+ INTERFACE = 0x00
11
+ RECV_LENGTH = 0x40 # Max Packet Size of 64
12
+ SEND_LENGTH = 0x40 # Max Packet Size of 64
13
+ WRITE_ENDPOINT = 0x02
14
+ READ_ENDPOINT = 0x82
15
+ WRITE_TIMEOUT = 5000
16
+ READ_TIMEOUT = 5000
17
+
18
+ # Creates a new interface between ruby and the Betabrite
19
+ def initialize
20
+ @device = get_usb_device()
21
+ @handle = get_usb_interface(@device)
22
+ end
23
+
24
+ def write(bytes)
25
+ # write twice to force past the 'data toggle bit' check
26
+ #@handle.usb_bulk_write(WRITE_ENDPOINT, bytes.pack('C*'), WRITE_TIMEOUT)
27
+ @handle.usb_bulk_write(WRITE_ENDPOINT, bytes, WRITE_TIMEOUT)
28
+ end
29
+
30
+ private
31
+
32
+ def get_usb_device
33
+ device = USB.devices.find {|d| d.idProduct == PRODUCT_ID and d.idVendor == VENDOR_ID }
34
+ raise "Unable to locate the BETABrite." if device.nil?
35
+ return device
36
+ end
37
+
38
+ def get_usb_interface(device)
39
+ handle = device.open
40
+ raise "Unable to obtain a handle to the device." if handle.nil?
41
+
42
+ retries = 0
43
+ begin
44
+ error_code = handle.usb_claim_interface(INTERFACE)
45
+ raise unless error_code.nil?
46
+ rescue
47
+ handle.usb_detach_kernel_driver_np(INTERFACE);
48
+ if retries.zero?
49
+ retries += 1
50
+ retry
51
+ else
52
+ raise "Unable to claim the device interface."
53
+ end
54
+ end
55
+
56
+ raise "Unable to set alternative interface." unless handle.set_altinterface(0).nil?
57
+
58
+ handle
59
+ end
60
+ end
61
+
62
+ betabrite = UsbBetabrite.new
63
+
64
+ sign = BetaBrite.new
65
+ sign.sleep_time = 0
66
+
67
+ 0.upto(9) { |i|
68
+ string = BetaBrite::Memory::String.new(i.to_s, 64)
69
+ sign.add string
70
+ }
71
+
72
+ text = BetaBrite::Memory::Text.new('A', 4096)
73
+
74
+ sign.add text
75
+
76
+ write_text = ''
77
+ sign.allocate { |text|
78
+ write_text << text
79
+ }
80
+ betabrite.write write_text
81
+
@@ -0,0 +1,40 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'betabrite'
4
+ require 'stockdata'
5
+ require 'serialport'
6
+
7
+ sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
8
+
9
+ sd = StockData.new(%w{ ^DJI CSCO DIS GM GPS LU MAT PER PFE UNTD })
10
+
11
+ sign = BetaBrite.new
12
+ tf = BetaBrite::TextFile.new { |a|
13
+ a.mode = BetaBrite::TextFile::Mode::COMPRESSED_ROTATE
14
+ }
15
+
16
+ string = ''
17
+ sd.each_with_index do |stock, i|
18
+ color = BetaBrite::String::Color::AMBER
19
+ if stock.change =~ /^[+]/
20
+ color = BetaBrite::String::Color::GREEN
21
+ elsif stock.change =~ /^[-]/
22
+ color = BetaBrite::String::Color::RED
23
+ end
24
+ s = BetaBrite::String.new(
25
+ "#{stock.symbol} #{stock.last_trade} #{stock.change}",
26
+ :color => color
27
+ )
28
+ t = BetaBrite::StringFile.new(i.to_s, s)
29
+ sign.add t
30
+ string << "#{t.id} "
31
+ end
32
+
33
+ tf.message = BetaBrite::String.new(string.strip)
34
+
35
+ #sign.add tf
36
+
37
+ sign.write { |text|
38
+ sp.write text
39
+ }
40
+
@@ -0,0 +1,65 @@
1
+ require 'open-uri'
2
+ require 'ostruct'
3
+ require 'csv'
4
+ require 'uri'
5
+
6
+ class StockData
7
+ include Enumerable
8
+
9
+ SOURCE_URL = "http://finance.yahoo.com/d/quotes.csv"
10
+
11
+ #These are the symbols I understand, which are limited
12
+ OPTIONS = {
13
+ :symbol=>"s",
14
+ :name=>"n",
15
+ :change=>"c1",
16
+ :last_trade=>"l1",
17
+ :last_trade_date=>"d1",
18
+ :last_trade_time=>"t1",
19
+ :open=>"o",
20
+ :high=>"h",
21
+ :low=>"g",
22
+ :high_52_week=>"k",
23
+ :low_52_week=>"j"
24
+ }
25
+
26
+ def initialize(symbols, options = [ :symbol,
27
+ :name,
28
+ :last_trade,
29
+ :last_trade_date,
30
+ :last_trade_time,
31
+ :change])
32
+ @symbols = symbols
33
+ @options = options
34
+ @data = nil
35
+ end
36
+
37
+ def each
38
+ data.each do |row|
39
+ struct = OpenStruct.new(Hash[*(@options.zip(row).flatten)])
40
+ yield struct
41
+ end
42
+ end
43
+
44
+ def each_hash
45
+ data.each do |row|
46
+ hash = Hash[*(@options.zip(row).flatten)]
47
+ yield hash
48
+ end
49
+ end
50
+
51
+ def refresh
52
+ symbol_fragment = @symbols.join "+"
53
+ option_fragment = @options.map{|s| OPTIONS[s] }.join ""
54
+ url = SOURCE_URL + "?s=#{URI.escape(symbol_fragment)}&f=#{option_fragment}"
55
+ @data = []
56
+ CSV.parse open(url).read do |row|
57
+ @data << row
58
+ end
59
+ end
60
+
61
+ def data
62
+ refresh unless @data
63
+ @data
64
+ end
65
+ end
@@ -0,0 +1,6 @@
1
+ %w{ ../lib }.each do |path|
2
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), path)))
3
+ end
4
+
5
+ require 'test/unit'
6
+ require 'betabrite'
@@ -3,11 +3,11 @@ $:.unshift File.join(File.dirname(__FILE__), "..", "test")
3
3
 
4
4
  require 'test/unit'
5
5
  require 'betabrite'
6
- require 'bb_override'
7
6
 
8
- class MemoryAllocTest < Test::Unit::TestCase
7
+ class ManyMemoryAllocTest < Test::Unit::TestCase
9
8
  def setup
10
- @sign = BetaBrite.new
9
+ @sign = BetaBrite::Base.new
10
+ @sign.sleep_time = 0
11
11
  end
12
12
 
13
13
  def test_text_and_string
@@ -24,18 +24,14 @@ class MemoryAllocTest < Test::Unit::TestCase
24
24
  0x30,0x30,0x30,0x30,0x30,0x04
25
25
  ]
26
26
  final_s = final.collect { |x| x.chr }.join
27
- @sign.add BetaBrite::Memory::Text.new('A', 4096)
27
+ @sign.memory << BetaBrite::Memory::Text.new('A', 4096)
28
28
 
29
29
  0.upto(9) { |i|
30
30
  string = BetaBrite::Memory::String.new(i.to_s, 64)
31
- @sign.add string
31
+ @sign.memory << string
32
32
  }
33
33
 
34
- test_string = ''
35
- @sign.allocate { |text|
36
- #obj.tty.write text
37
- test_string << text
38
- }
34
+ test_string = @sign.memory_message
39
35
  assert_equal(final_s, test_string)
40
36
  end
41
37
  end