Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Bradbury committed Jan 7, 2009
0 parents commit 99d5c42
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 0 deletions.
28 changes: 28 additions & 0 deletions config/environment.rb
@@ -0,0 +1,28 @@
$: << File.expand_path(File.dirname(__FILE__) + "/../lib/")
$: << File.expand_path(File.dirname(__FILE__) + "/../")
require 'rubygems'
require 'sinatra'
CONFIG = {}

configure :production do
require 'leds_S3C2440A'
require 'rack/handler/webrick'

Sinatra::Application.default_options.merge!(
:run => false,
:env => :production,
:port => 80
)
end

configure :test do
require 'mock_led'
CONFIG[:data_dir] = Dir.tmpdir + "/weblink_test"
Dir.mkdir(CONFIG[:data_dir]) unless File.exists?(CONFIG[:data_dir])
end

configure :development do
require 'mock_led'
CONFIG[:data_dir] = Dir.tmpdir + "/weblink_devlopment"
Dir.mkdir(CONFIG[:data_dir]) unless File.exists?(CONFIG[:data_dir])
end
24 changes: 24 additions & 0 deletions lib/led.rb
@@ -0,0 +1,24 @@
class Led
def initialize(options)
@data_register = options[:data_register]
@mask = options[:mask]
@mmio = options[:mmio]
end

def on
@mmio.write(@data_register,"w", current & ~@mask)
end

def off
@mmio.write(@data_register,"w", current | @mask)
end

def on?
current & @mask == 0
end

def current
@mmio.read(@data_register,"w")
end

end
22 changes: 22 additions & 0 deletions lib/leds_S3C2440A.rb
@@ -0,0 +1,22 @@
require 'mmio'
require 'led'

class MemoryObject
include MemoryMappedIO
end

class Led
def self.find(id)
if id == "1"
return Led.new(:data_register => 0x56000014, :mask => 0x00000001, :mmio => MemoryObject.new)
elsif id == "2"
return Led.new(:data_register => 0x56000014, :mask => 0x00000002, :mmio => MemoryObject.new)
else
return nil
end
end
end

mmio = MemoryObject.new
control_reg = mmio.read(0x56000010, "w")
mmio.write(0x56000010, "w", ((control_reg & 0xFFFFFFF0) | 0x00000005))
38 changes: 38 additions & 0 deletions lib/mock_led.rb
@@ -0,0 +1,38 @@
require 'yaml'

class MockLed
def initialize(id=1)
@id = id
end

def on
@on = true
save
end

def off
@on = false
save
end

def on?
@on
end

def save
File.open(CONFIG[:data_dir] + "/led#{@id}.yaml", "w") do |file|
file.write YAML::dump(self)
end
end

end

class Led
def self.find(id)
if File.exists?(CONFIG[:data_dir] + "/led#{id}.yaml")
return YAML::load(File.open(CONFIG[:data_dir] + "/led#{id}.yaml", "r"))
else
return MockLed.new(id)
end
end
end
27 changes: 27 additions & 0 deletions lib/morse_blinker.rb
@@ -0,0 +1,27 @@
class MorseBlinker
def initialize(led, tick)
@led = led
@tick = tick
end

def say(code)
code.each_byte do |char|
off_time = 1
case char
when 46 # '.'
turn_on_for(1)
when 45 # '-'
turn_on_for(3)
when 32 # ' '
sleep 3*@tick
end
end
end

def turn_on_for(ticks)
@led.on
sleep ticks*@tick
@led.off
sleep @tick
end
end
Binary file added public/green-off-64.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/green-off-64.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/green-on-64.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/green-on-64.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions public/main.css
@@ -0,0 +1,13 @@
.led img {
border:0px;
text-align:center;
}

.led p{
text-align:center;
}
.led {
padding: 10px 10px 10px 10px;
float:left;
width:200px;
}
31 changes: 31 additions & 0 deletions spec/led_spec.rb
@@ -0,0 +1,31 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
require 'led'

describe Led do
before(:each) do
@mock_mmio = mock("mmio", :read => nil, :write => nil)
@led = Led.new(:data_register => 0x40014232, :mask => 0x00000001, :mmio => @mock_mmio)
end

it "should turn on" do
@mock_mmio.should_receive(:read).with(0x40014232, "w").and_return(0xFF45FFFF)
@mock_mmio.should_receive(:write).with(0x40014232, "w", 0xFF45FFFE)
@led.on
end

it "should turn off" do
@mock_mmio.should_receive(:read).with(0x40014232, "w").and_return(0xFFFFFFFE)
@mock_mmio.should_receive(:write).with(0x40014232, "w", 0xFFFFFFFF)
@led.off
end

it "should be on" do
@mock_mmio.should_receive(:read).with(0x40014232, "w").and_return(0xFFFFFFFE)
@led.should be_on
end

it "should not be on" do
@mock_mmio.should_receive(:read).with(0x40014232, "w").and_return(0xFFFFFFFF)
@led.should_not be_on
end
end
38 changes: 38 additions & 0 deletions spec/mock_led_spec.rb
@@ -0,0 +1,38 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'mock_led'

describe MockLed do
before(:each) do
@led = MockLed.new
end
it "should turn on" do
@led.on
@led.should be_on
end

it "should turn off" do
@led.off
@led.should_not be_on
end

it "should create an Led" do
Led.find(1).should be_is_a(MockLed)
end

it "should save the Led" do
Led.find(1).on
Led.find(1).should be_on
end

it "should save the led off" do
Led.find(1).off
Led.find(1).should_not be_on
end

it "should save two leds" do
Led.find(1).on
Led.find(2).off
Led.find(1).should be_on
Led.find(2).should_not be_on
end
end
105 changes: 105 additions & 0 deletions spec/morse_blinker_spec.rb
@@ -0,0 +1,105 @@
require File.dirname(__FILE__) + '/spec_helper'

require 'morse_blinker'
require 'stopwatch'

class TimedLed
def initialize
@stopwatch = Stopwatch.new
end

def on
@stopwatch.start_stop
end

def off
@stopwatch.start_stop
end

def on_time
return @stopwatch.elapsed
end

end


describe MorseBlinker do
before(:each) do
@led = mock("led", :on => nil, :off => nil)
@blinker = MorseBlinker.new(@led, 0.001)
end

it "should blink a dot" do
@led.should_receive(:on).once
@led.should_receive(:off).once
@blinker.say(".")
end

it "should blink two dots" do
@led.should_receive(:on).twice
@led.should_receive(:off).twice
@blinker.say("..")
end

it "should not blink for a space" do
@led.should_receive(:on).twice
@led.should_receive(:off).twice
@blinker.say(" . . ")
end
end
describe MorseBlinker do
before(:each) do
@led = TimedLed.new
@blinker = MorseBlinker.new(@led, 0.01)
@stopwatch = Stopwatch.new
end

it "should stay on for one tick on a ." do
@blinker.say(".")
@led.on_time.should be > 0.01
@led.on_time.should be < 0.02
end

it "should stay on for three ticks for a -" do
@blinker.say("-")
@led.on_time.should be > 0.03
@led.on_time.should be < 0.04
end

it "should stay off for one tick between after a ." do
@stopwatch.time do
@blinker.say(".")
end

(@stopwatch.elapsed - @led.on_time).should be > 0.01
(@stopwatch.elapsed - @led.on_time).should be < 0.02
end

it "should stay off for one tick between after a -" do
@stopwatch.time do
@blinker.say("-")
end

(@stopwatch.elapsed - @led.on_time).should be > 0.01
(@stopwatch.elapsed - @led.on_time).should be < 0.02
end

it "should stay off for three thicks between different characters" do
@stopwatch.time do
@blinker.say(" ")
end
@led.on_time.should == 0
@stopwatch.elapsed.should be > 0.03
@stopwatch.elapsed.should be < 0.04
end

it "should stay off for 6 ticks for two spaces " do
@stopwatch.time do
@blinker.say(" ")
end
@led.on_time.should == 0
@stopwatch.elapsed.should be > 0.06
@stopwatch.elapsed.should be < 0.07

end
end
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,12 @@
$: << File.expand_path(File.dirname(__FILE__) + "/../lib/")
$: << File.expand_path(File.dirname(__FILE__) + "/../")
$: << File.expand_path(File.dirname(__FILE__))
require 'rubygems'
require 'spec'
require 'sinatra'
require 'sinatra/test/rspec'

require 'tmpdir'
CONFIG = {}
CONFIG[:data_dir] = Dir.tmpdir + "/weblink_test_data"
Dir.mkdir(CONFIG[:data_dir]) unless File.exists?(CONFIG[:data_dir])

0 comments on commit 99d5c42

Please sign in to comment.