Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moving around diles

git-svn-id: svn+ssh://rubyforge.org/var/svn/betabrite/trunk@21 32089d7b-f314-0410-b504-a527b3b390a0
  • Loading branch information...
commit 46247fe668dba213a7bb8f54515d8ebad4a8e6bf 1 parent 24cc040
aaronp authored
View
90 lib/betabrite.rb
@@ -1,88 +1,6 @@
-require 'string'
-require 'files/textfile'
-require 'files/stringfile'
-require 'files/dotsfile'
-require 'memory/memory'
+require 'betabrite/string'
+require 'betabrite/files'
+require 'betabrite/base'
require 'betabrite/usb'
+require 'betabrite/memory'
-# = Synopsis
-# This class assembles all packets (different files) and yields the data
-# that needs to be written back to the caller of the BetaBrite#write method.
-class BetaBrite
- HEADER = [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ]
- STX = 0x02.chr
- ETX = 0x03.chr
- EOT = 0x04.chr
- ESC = 0x1b.chr
- DLE = 0x10.chr
- STRING = 0x14.chr
- CR = 0x0d.chr
- MEMORY_CODE = "E$"
-
- # Beta Brite sign
- SIGN_TYPE = 0x5a.chr
-
- VERSION = '0.1.0'
-
- attr_accessor :sleep_time
- attr_reader :string_files, :text_files, :dots_files, :memory
-
- def initialize
- # Default address is "00" which means broadcast to all signs
- @sign_address = [ 0x30, 0x30 ]
- @string_files = []
- @text_files = []
- @dots_files = []
- @memory = []
-
- # This shouldn't change except for unit testing
- @sleep_time = 0
- yield self if block_given?
- end
-
- def textfile(label = 'A', &block)
- @text_files << BetaBrite::TextFile.new(label, &block)
- end
-
- def stringfile(label, message = nil, &block)
- @string_files << BetaBrite::StringFile.new(label, message, &block)
- end
-
- def dotsfile(label, rows = nil, columns = nil, picture = nil, &block)
- @dots_files << BetaBrite::DotsFile.new(label, rows, columns, picture,&block)
- end
-
- def allocate(&block)
- @memory.push(*(BetaBrite::Memory::Factory.find(&block)))
- end
-
- def clear_memory!
- @memory << BetaBrite::Memory::Clear.new
- end
-
- # Get the message to be sent to the sign
- def message
- header +
- @text_files.each { |tf| tf.to_s }.join('') +
- @string_files.each { |tf| tf.to_s }.join('') +
- @dots_files.each { |tf| tf.to_s }.join('') +
- tail
- end
-
- def memory_message
- "#{header}#{STX}#{MEMORY_CODE}#{@memory.map { |packet| packet.to_s }.join('')}#{tail}"
- end
-
- private
-
- def header
- header_str = HEADER.collect { |a| a.chr }.join
- header_str << SIGN_TYPE << @sign_address.collect { |a| a.chr }.join
- end
- alias :inspect :header
- public :inspect
-
- def tail
- EOT
- end
-end
View
85 lib/betabrite/base.rb
@@ -0,0 +1,85 @@
+module BetaBrite
+ VERSION = '1.0.0'
+
+ class Base
+ include BetaBrite::Files
+
+ # = Synopsis
+ # This class assembles all packets (different files) and yields the data
+ # that needs to be written back to the caller of the BetaBrite#write method.
+ HEADER = [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ]
+ STX = 0x02.chr
+ ETX = 0x03.chr
+ EOT = 0x04.chr
+ ESC = 0x1b.chr
+ DLE = 0x10.chr
+ STRING = 0x14.chr
+ CR = 0x0d.chr
+ MEMORY_CODE = "E$"
+
+ # Beta Brite sign
+ SIGN_TYPE = 0x5a.chr
+
+ attr_accessor :sleep_time
+ attr_reader :string_files, :text_files, :dots_files, :memory
+
+ def initialize
+ # Default address is "00" which means broadcast to all signs
+ @sign_address = [ 0x30, 0x30 ]
+ @string_files = []
+ @text_files = []
+ @dots_files = []
+ @memory = []
+
+ # This shouldn't change except for unit testing
+ @sleep_time = 0
+ yield self if block_given?
+ end
+
+ def textfile(label = 'A', &block)
+ @text_files << Text.new(label, &block)
+ end
+
+ def stringfile(label, message = nil, &block)
+ @string_files << String.new(label, message, &block)
+ end
+
+ def dotsfile(label, rows = nil, columns = nil, picture = nil, &block)
+ @dots_files << Dots.new(label, rows, columns, picture,&block)
+ end
+
+ def allocate(&block)
+ @memory.push(*(BetaBrite::Memory::Factory.find(&block)))
+ end
+
+ def clear_memory!
+ @memory << BetaBrite::Memory::Clear.new
+ end
+
+ # Get the message to be sent to the sign
+ def message
+ header +
+ @text_files.each { |tf| tf.to_s }.join('') +
+ @string_files.each { |tf| tf.to_s }.join('') +
+ @dots_files.each { |tf| tf.to_s }.join('') +
+ tail
+ end
+
+ def memory_message
+ "#{header}#{STX}#{MEMORY_CODE}#{@memory.map { |packet| packet.to_s }.join('')}#{tail}"
+ end
+
+ private
+
+ def header
+ header_str = HEADER.collect { |a| a.chr }.join
+ header_str << SIGN_TYPE << @sign_address.collect { |a| a.chr }.join
+ end
+ alias :inspect :header
+ public :inspect
+
+ def tail
+ EOT
+ end
+ end
+end
View
3  lib/betabrite/files.rb
@@ -0,0 +1,3 @@
+require 'betabrite/files/stringfile'
+require 'betabrite/files/textfile'
+require 'betabrite/files/dotsfile'
View
24 lib/betabrite/files/dotsfile.rb
@@ -0,0 +1,24 @@
+module BetaBrite
+ module Files
+ class Dots
+ COMMAND_CODE = 0x49.chr
+
+ attr_accessor :label, :rows, :columns, :picture
+
+ def initialize(label, rows, columns, picture, &block)
+ @label = label
+ @rows = rows
+ @columns = columns
+ @picture = picture
+ instance_eval(&block) if block
+ end
+
+ def to_s
+ string = "#{BetaBrite::Base::STX}#{COMMAND_CODE}#{@label.to_s}" +
+ "#{sprintf('%02x', @rows)}#{sprintf('%02x', @columns)}" +
+ "#{picture.join(BetaBrite::Base::CR)}#{BetaBrite::Base::CR}"
+ end
+ alias :to_str :to_s
+ end
+ end
+end
View
36 lib/betabrite/files/stringfile.rb
@@ -0,0 +1,36 @@
+module BetaBrite
+ module Files
+ class String
+ COMMAND_CODE = 'G'
+
+ attr_accessor :label, :message
+
+ def initialize(label = nil, message = nil, &block)
+ @label = label
+ @message = message
+ instance_eval(&block) if block
+ end
+
+ def puts(some_string)
+ @message = @message ? @message + some_string : some_string
+ end
+
+ def string(some_string)
+ BetaBrite::String.new(some_string)
+ end
+
+ ::BetaBrite::String.constants.each do |constant|
+ next unless constant =~ /^[A-Z_]*$/
+ define_method(:"#{constant.downcase}") do
+ ::BetaBrite::String.const_get(constant)
+ end
+ end
+
+ def to_s
+ "#{BetaBrite::Base::STX}#{COMMAND_CODE}#{@label.to_s}" +
+ "#{@message.to_s}#{BetaBrite::Base::ETX}"
+ end
+ alias :to_str :to_s
+ end
+ end
+end
View
138 lib/betabrite/files/textfile.rb
@@ -0,0 +1,138 @@
+module BetaBrite
+ module Files
+ class Text
+ WRITE = 0x41.chr
+ READ = 0x42.chr
+
+ class Position
+ MIDDLE = 0x20.chr
+ TOP = 0x22.chr
+ BOTTOM = 0x26.chr
+ FILL = 0x30.chr
+ LEFT = 0x31.chr
+ RIGHT = 0x32.chr
+ end
+
+ class Mode
+ # Standard Modes
+ ROTATE = 0x61.chr # Message travels right to left
+ HOLD = 0x62.chr # Message remains stationary
+ FLASH = 0x63.chr # Message remains stationary and flashes
+ ROLL_UP = 0x65.chr # Message is pushed up by new message
+ ROLL_DOWN = 0x66.chr # Message is pushed down by new message
+ ROLL_LEFT = 0x67.chr # Message is pushed left by a new message
+ ROLL_RIGHT = 0x68.chr # Message is pushed right by a new message
+ WIPE_UP = 0x69.chr # Message is wiped over bottom to top
+ WIPE_DOWN = 0x6A.chr # Message is wiped over top to bottom
+ WIPE_LEFT = 0x6B.chr # Message is wiped over right to left
+ WIPE_RIGHT = 0x6C.chr # Message is wiped over left to right
+ SCROLL = 0x6D.chr # New message pushes up if 2 line sign
+ AUTOMODE = 0x6F.chr # Various modes are called automatically
+ ROLL_IN = 0x70.chr # Message pushed toward center of display
+ ROLL_OUT = 0x71.chr # Message pushed away from center of display
+ WIPE_IN = 0x72.chr # Message wiped over in inward motion
+ WIPE_OUT = 0x73.chr # Message wiped over in outward motion
+ COMPRESSED_ROTATE = 0x74.chr # Left to right, chars half normal width
+ EXPLODE = 0x75.chr # Message flies apart (Alpha 3.0)
+ CLOCK = 0x76.chr # Wipe in clockwise direction (Alpha 3.0)
+
+ # Special Modes
+ TWINKLE = 0x6E.chr << 0x30.chr # Message will twinkle
+ SPARKLE = 0x6E.chr << 0x31.chr # Message will sparkle
+ SNOW = 0x6E.chr << 0x32.chr # Message will snow
+ INTERLOCK = 0x6E.chr << 0x33.chr # Message will interlock
+ SWITCH = 0x6E.chr << 0x34.chr # Message will switch
+ SLIDE = 0x6E.chr << 0x35.chr # Message will slide
+ SPRAY = 0x6E.chr << 0x36.chr # Message will spray across
+ STARBURST = 0x6E.chr << 0x37.chr # Explodes message to screen
+ WELCOME = 0x6E.chr << 0x38.chr # The world "Welcome" is written
+ SLOT_MACHINE= 0x6E.chr << 0x39.chr # Slot Machine shows up
+ NEWS_FLASH = 0x6E.chr << 0x3A.chr # News flash animation
+ TRUMPET = 0x6E.chr << 0x3B.chr # Trumpet animation
+ CYCLE_COLORS= 0x6E.chr << 0x43.chr # Color changes from one to another
+
+ # Special Graphics
+ THANK_YOU = [0x6E, 0x53].pack('C*') # "Thank You" in script
+ FLAG = [0x6E, 0x54].pack('C*') # American Flag
+ NO_SMOKING = [0x6E, 0x55].pack('C*') # No smoking
+ DRINK_DRIVE = [0x6E, 0x56].pack('C*') # Don't drink and drive
+ ANIMAL_ANIM = [0x6E, 0x57].pack('C*') # Animal animation
+ FIREWORKS = [0x6E, 0x58].pack('C*') # Fireworks animation
+ BALLOONS = [0x6E, 0x59].pack('C*') # Balloon animation
+ CHERRY_BOMB = [0x6E, 0x5A].pack('C*') # A bomb animation or a smile
+ SMILE = [0x6E, 0x5A].pack('C*') # A bomb animation or a smile
+ end
+
+ attr_accessor :label, :position, :mode, :message
+
+ def initialize(label = 'A', &block)
+ @position = Position::MIDDLE
+ @label = label
+ @message = nil
+ @mode = Mode::ROTATE
+ instance_eval(&block) if block
+ end
+
+ def puts(some_string)
+ @message = @message ? @message + some_string : some_string
+ end
+
+ def string(some_string)
+ BetaBrite::String.new(some_string)
+ end
+
+ def stringfile(label)
+ "#{BetaBrite::Base::DLE}#{label}"
+ end
+
+ def dotsfile(label)
+ "#{BetaBrite::Base::STRING}#{label}"
+ end
+
+ def to_s
+ "#{combine}#{checksum(combine)}"
+ end
+
+ def checksum(string)
+ total = 0
+ 0.upto(string.length - 1) do |i|
+ total += string[i]
+ end
+
+ sprintf("%04x", total).upcase
+ end
+
+ alias :inspect :to_s
+
+ Mode.constants.each do |constant|
+ next unless constant =~ /^[A-Z_]*$/
+ define_method(:"#{constant.downcase}") do
+ puts constant
+ @mode = Mode.const_get(constant)
+ self
+ end
+ end
+
+ Position.constants.each do |constant|
+ next unless constant =~ /^[A-Z_]*$/
+ define_method(:"#{constant.downcase}") do
+ @position = Position.const_get(constant)
+ self
+ end
+ end
+
+ ::BetaBrite::String.constants.each do |constant|
+ next unless constant =~ /^[A-Z_]*$/
+ define_method(:"#{constant.downcase}") do
+ ::BetaBrite::String.const_get(constant)
+ end
+ end
+
+ private
+ def combine
+ "#{BetaBrite::Base::STX}#{WRITE}#{@label}#{BetaBrite::Base::ESC}" <<
+ "#{@position}#{@mode}#{@message}#{BetaBrite::Base::ETX}"
+ end
+ end
+ end
+end
View
6 lib/memory/memory.rb → lib/betabrite/memory.rb
@@ -1,4 +1,4 @@
-class BetaBrite
+module BetaBrite
class Memory
COMMAND_CODE = "E$"
TEXT = 'A'
@@ -7,7 +7,7 @@ class Memory
LOCKED = 'L'
UNLOCKED = 'U'
def self.clear
- BetaBrite::STX << COMMAND_CODE
+ BetaBrite::Base::STX << COMMAND_CODE
end
attr_accessor :label, :type, :locked, :size, :time
@@ -91,7 +91,7 @@ def initialize(label, rows, columns)
class Clear < Memory
def to_s
- "#{BetaBrite::STX}#{COMMAND_CODE}"
+ "#{BetaBrite::Base::STX}#{COMMAND_CODE}"
end
end
end
View
2  lib/string.rb → lib/betabrite/string.rb
@@ -1,4 +1,4 @@
-class BetaBrite
+module BetaBrite
# This class encapsulates a string and attributes about the string such as
# color, character set, and also contains special characters.
class String
View
4 lib/betabrite/usb.rb
@@ -1,5 +1,5 @@
-class BetaBrite
- class USB < BetaBrite
+module BetaBrite
+ class USB < Base
# USB Device Codes
PRODUCT_ID = 0x1234
VENDOR_ID = 0x8765
View
22 lib/files/dotsfile.rb
@@ -1,22 +0,0 @@
-class BetaBrite
- class DotsFile
- COMMAND_CODE = 0x49.chr
-
- attr_accessor :label, :rows, :columns, :picture
-
- def initialize(label, rows, columns, picture, &block)
- @label = label
- @rows = rows
- @columns = columns
- @picture = picture
- instance_eval(&block) if block
- end
-
- def to_s
- string = "#{BetaBrite::STX}#{COMMAND_CODE}#{@label.to_s}" +
- "#{sprintf('%02x', @rows)}#{sprintf('%02x', @columns)}" +
- "#{picture.join(BetaBrite::CR)}#{BetaBrite::CR}"
- end
- alias :to_str :to_s
- end
-end
View
34 lib/files/stringfile.rb
@@ -1,34 +0,0 @@
-class BetaBrite
- class StringFile
- COMMAND_CODE = 'G'
-
- attr_accessor :label, :message
-
- def initialize(label = nil, message = nil, &block)
- @label = label
- @message = message
- instance_eval(&block) if block
- end
-
- def puts(some_string)
- @message = @message ? @message + some_string : some_string
- end
-
- def string(some_string)
- BetaBrite::String.new(some_string)
- end
-
- ::BetaBrite::String.constants.each do |constant|
- next unless constant =~ /^[A-Z_]*$/
- define_method(:"#{constant.downcase}") do
- ::BetaBrite::String.const_get(constant)
- end
- end
-
- def to_s
- "#{BetaBrite::STX}#{COMMAND_CODE}#{@label.to_s}" +
- "#{@message.to_s}#{BetaBrite::ETX}"
- end
- alias :to_str :to_s
- end
-end
View
136 lib/files/textfile.rb
@@ -1,136 +0,0 @@
-class BetaBrite
- class TextFile
- WRITE = 0x41.chr
- READ = 0x42.chr
-
- class Position
- MIDDLE = 0x20.chr
- TOP = 0x22.chr
- BOTTOM = 0x26.chr
- FILL = 0x30.chr
- LEFT = 0x31.chr
- RIGHT = 0x32.chr
- end
-
- class Mode
- # Standard Modes
- ROTATE = 0x61.chr # Message travels right to left
- HOLD = 0x62.chr # Message remains stationary
- FLASH = 0x63.chr # Message remains stationary and flashes
- ROLL_UP = 0x65.chr # Message is pushed up by new message
- ROLL_DOWN = 0x66.chr # Message is pushed down by new message
- ROLL_LEFT = 0x67.chr # Message is pushed left by a new message
- ROLL_RIGHT = 0x68.chr # Message is pushed right by a new message
- WIPE_UP = 0x69.chr # Message is wiped over bottom to top
- WIPE_DOWN = 0x6A.chr # Message is wiped over top to bottom
- WIPE_LEFT = 0x6B.chr # Message is wiped over right to left
- WIPE_RIGHT = 0x6C.chr # Message is wiped over left to right
- SCROLL = 0x6D.chr # New message pushes up if 2 line sign
- AUTOMODE = 0x6F.chr # Various modes are called automatically
- ROLL_IN = 0x70.chr # Message pushed toward center of display
- ROLL_OUT = 0x71.chr # Message pushed away from center of display
- WIPE_IN = 0x72.chr # Message wiped over in inward motion
- WIPE_OUT = 0x73.chr # Message wiped over in outward motion
- COMPRESSED_ROTATE = 0x74.chr # Left to right, chars half normal width
- EXPLODE = 0x75.chr # Message flies apart (Alpha 3.0)
- CLOCK = 0x76.chr # Wipe in clockwise direction (Alpha 3.0)
-
- # Special Modes
- TWINKLE = 0x6E.chr << 0x30.chr # Message will twinkle
- SPARKLE = 0x6E.chr << 0x31.chr # Message will sparkle
- SNOW = 0x6E.chr << 0x32.chr # Message will snow
- INTERLOCK = 0x6E.chr << 0x33.chr # Message will interlock
- SWITCH = 0x6E.chr << 0x34.chr # Message will switch
- SLIDE = 0x6E.chr << 0x35.chr # Message will slide
- SPRAY = 0x6E.chr << 0x36.chr # Message will spray across
- STARBURST = 0x6E.chr << 0x37.chr # Explodes message to screen
- WELCOME = 0x6E.chr << 0x38.chr # The world "Welcome" is written
- SLOT_MACHINE= 0x6E.chr << 0x39.chr # Slot Machine shows up
- NEWS_FLASH = 0x6E.chr << 0x3A.chr # News flash animation
- TRUMPET = 0x6E.chr << 0x3B.chr # Trumpet animation
- CYCLE_COLORS= 0x6E.chr << 0x43.chr # Color changes from one to another
-
- # Special Graphics
- THANK_YOU = [0x6E, 0x53].pack('C*') # "Thank You" in script
- FLAG = [0x6E, 0x54].pack('C*') # American Flag
- NO_SMOKING = [0x6E, 0x55].pack('C*') # No smoking
- DRINK_DRIVE = [0x6E, 0x56].pack('C*') # Don't drink and drive
- ANIMAL_ANIM = [0x6E, 0x57].pack('C*') # Animal animation
- FIREWORKS = [0x6E, 0x58].pack('C*') # Fireworks animation
- BALLOONS = [0x6E, 0x59].pack('C*') # Balloon animation
- CHERRY_BOMB = [0x6E, 0x5A].pack('C*') # A bomb animation or a smile
- SMILE = [0x6E, 0x5A].pack('C*') # A bomb animation or a smile
- end
-
- attr_accessor :label, :position, :mode, :message
-
- def initialize(label = 'A', &block)
- @position = Position::MIDDLE
- @label = label
- @message = nil
- @mode = Mode::ROTATE
- instance_eval(&block) if block
- end
-
- def puts(some_string)
- @message = @message ? @message + some_string : some_string
- end
-
- def string(some_string)
- BetaBrite::String.new(some_string)
- end
-
- def stringfile(label)
- "#{BetaBrite::DLE}#{label}"
- end
-
- def dotsfile(label)
- "#{BetaBrite::STRING}#{label}"
- end
-
- def to_s
- "#{combine}#{checksum(combine)}"
- end
-
- def checksum(string)
- total = 0
- 0.upto(string.length - 1) do |i|
- total += string[i]
- end
-
- sprintf("%04x", total).upcase
- end
-
- alias :inspect :to_s
-
- Mode.constants.each do |constant|
- next unless constant =~ /^[A-Z_]*$/
- define_method(:"#{constant.downcase}") do
- puts constant
- @mode = Mode.const_get(constant)
- self
- end
- end
-
- Position.constants.each do |constant|
- next unless constant =~ /^[A-Z_]*$/
- define_method(:"#{constant.downcase}") do
- @position = Position.const_get(constant)
- self
- end
- end
-
- ::BetaBrite::String.constants.each do |constant|
- next unless constant =~ /^[A-Z_]*$/
- define_method(:"#{constant.downcase}") do
- ::BetaBrite::String.const_get(constant)
- end
- end
-
- private
- def combine
- "#{BetaBrite::STX}#{WRITE}#{@label}#{BetaBrite::ESC}" <<
- "#{@position}#{@mode}#{@message}#{BetaBrite::ETX}"
- end
- end
-end
View
2  test/test_many_mem.rb
@@ -6,7 +6,7 @@
class ManyMemoryAllocTest < Test::Unit::TestCase
def setup
- @sign = BetaBrite.new
+ @sign = BetaBrite::Base.new
@sign.sleep_time = 0
end
View
2  test/test_memory.rb
@@ -6,7 +6,7 @@
class MemoryAllocTest < Test::Unit::TestCase
def setup
- @sign = BetaBrite.new
+ @sign = BetaBrite::Base.new
@sign.sleep_time = 0
end
View
4 test/test_set_string.rb
@@ -6,7 +6,7 @@
class SetStringTest < Test::Unit::TestCase
def setup
- @sign = BetaBrite.new
+ @sign = BetaBrite::Base.new
@sign.sleep_time = 0
end
@@ -28,7 +28,7 @@ def test_many_mem
'3.37 ',
'16.47 '
].each_with_index do |price,idx|
- @sign.string_files << BetaBrite::StringFile.new(idx, price)
+ @sign.string_files << BetaBrite::Files::String.new(idx, price)
end
assert_equal('11045.28 ', @sign.string_files[0].message)
View
8 test/test_text_file.rb
@@ -5,13 +5,15 @@
require 'betabrite'
class TextFileTest < Test::Unit::TestCase
+ include BetaBrite::Files
+
def test_const_dsl
- tf = BetaBrite::TextFile.new {
+ tf = Text.new {
roll_down()
middle()
}
- assert_equal(BetaBrite::TextFile::Position::MIDDLE, tf.position)
- assert_equal(BetaBrite::TextFile::Mode::ROLL_DOWN, tf.mode)
+ assert_equal(Text::Position::MIDDLE, tf.position)
+ assert_equal(Text::Mode::ROLL_DOWN, tf.mode)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.