Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Doug Bradbury
committed
Jan 7, 2009
0 parents
commit 99d5c42
Showing
19 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) |
Oops, something went wrong.