Permalink
Browse files

move to mac

  • Loading branch information...
1 parent 13d2ae0 commit 5c14e86f7decc23d85d46dab37bc9950908e18d2 Sergey Pchelincev committed Jan 28, 2012
@@ -0,0 +1,28 @@
+ports:
+ a:
+ 0: 'GPIO'
+ 1: 'GPIO'
+ 2: 'GPIO'
+ 3: 'GPIO'
+ 4: 'GPIO'
+ 5: 'GPIO'
+ 6: 'GPIO'
+ 7: 'GPIO'
+ b:
+ 0: 'GPIO'
+ 1: 'GPIO'
+ 2: 'GPIO'
+ 3: 'GPIO'
+ 4: 'GPIO'
+ 5: 'GPIO'
+ 6: 'GPIO'
+ 7: 'GPIO'
+ d:
+ 0: 'GPIO'
+ 1: 'GPIO'
+ 2: 'GPIO'
+ 3: 'GPIO'
+ 4: 'GPIO'
+ 5: 'GPIO'
+ 6: 'GPIO'
+ 7: 'GPIO'
@@ -0,0 +1,6 @@
+modules:
+ avr: 'boot cpufunc eeprom fuse interrupt io lock pgmspace power sfr_defs signature sleep version wdt'
+ common: 'alloca assert ctype errno inttypes math setjmp stdint stdio stdlib string'
+ util: 'atomic crc16 delay delay_basic parity setbaud twi'
+types:
+ GPIO: 'input pullup output'
View
@@ -6,25 +6,33 @@ module AVR
class << self
- def common_modules
- %w{alloca assert ctype errno inttypes math setjmp stdint stdio stdlib string}
- end
+ attr_accessor :configuration
- def avr_modules
- %w{boot cpufunc eeprom fuse interrupt io lock pgmspace power
- sfr_defs signature sleep version wdt}.map{|module_name| "avr/#{module_name}"}
+ def configuration
+ @configuration ||= YAML.load_file("configuration/avr.yaml")
end
- def util_modules
- %w{atomic crc16 delay delay_basic parity setbaud twi}.map{|module_name| "util/#{module_name}"}
+ def method_missing(name, *args)
+ if result = configuration[name]
+ result
+ else
+ super(name, *args)
+ end
end
def modules
- common_modules + avr_modules + util_modules
+ @modules ||= begin
+ hash_list = configuration['modules']
+ collection = list.delete(:common).split
+ hash_list.each do |namespace_name, namespace_modules|
+ collection << namespace_modules.split.map{|module_name| "#{namespace_name}/#{module_name}"}
+ end
+ collection
+ end
end
end
end
-end
+end
@@ -1,24 +1,39 @@
+require 'yaml'
require 'raudi/avr/controller_proxy'
require 'raudi/avr/controller_source'
+require 'raudi/avr/port'
module Raudi
module AVR
class Controller
- attr_accessor :model_name, :modules, :source
+ attr_accessor :model_name, :modules, :source, :ports
def initialize(model_name)
self.model_name = model_name
+ load_configuration_file
self.modules = []
self.source = ControllerSource.new(self)
yield ControllerProxy.new(self)
Raudi.controller = self
end
+ private
+
+ def load_configuration_file
+ path = File.dirname(__FILE__)
+ path << '/configuration'
+ path << "/#{model_name}.yaml"
+ raise "Unknow controller model" unless File.exist?(path)
+ ports = YAML.load_file(path)['ports']
+ raise "Internal error, configuration file is invalid" unless ports.is_a?(Hash)
+ self.ports = ports.map {|port_name, pins| Port.new(port_name, pins)}
+ end
+
end
end
-end
+end
@@ -4,8 +4,10 @@ module AVR
class ControllerProxy
+ attr_accessor :controller
+
def initialize(controller)
- @controller = controller
+ self.controller = controller
end
def modules(*args)
@@ -18,13 +20,35 @@ def modules(*args)
else
next
end
- @controller.modules << param if allow_module?(param)
+ controller.modules << param if allow_module?(param)
+ end
+ end
+
+ def output(*args)
+ args.each do |arg|
+ case arg
+ when Symbol, String
+ port_name = arg[/[a-z]+/i]
+ raise "Port is not specified" unless port_name
+ port_name.upcase!
+ pin_number = arg[/\d+/]
+ raise "Pin is not specified" unless pin_number
+ controller.ports.each do |port|
+ if port.name == port_name
+ port.pins.each do |pin|
+ if pin.number == pin_number
+ pin.output!
+ end
+ end
+ end
+ end
+ end
end
end
def method_missing(method_name, *args)
- if @controller.respond_to("#{method_name}=")
- @controller.send("#{method_name}=", *args)
+ if controller.respond_to?("#{method_name}=")
+ controller.send("#{method_name}=", *args)
else
raise "Illegal property #{method_name}, check configuration file"
end
@@ -33,10 +57,10 @@ def method_missing(method_name, *args)
private
def allow_module?(module_name)
- AVR.modules.include?(module_name) and !@controller.modules.include?(module_name)
+ AVR.modules.include?(module_name) and !controller.modules.include?(module_name)
end
end
end
-end
+end
@@ -1,21 +1,23 @@
+require "raudi/avr/source"
+
module Raudi
module AVR
- class ControllerSource
+ class ControllerSource < Source
def initialize(controller)
@controller = controller
end
def modules
@controller.modules.inject("") do |source, module_name|
- source << "#include <#{module_name}.h>\n"
+ source << "#include <#{module_name}.h>#{new_line}"
end
end
end
end
-end
+end
View
@@ -0,0 +1,39 @@
+require 'yaml'
+
+module Raudi
+
+ module AVR
+
+ class Pin
+
+ attr_accessor :number, :states
+
+ class << self
+
+ def valid_types
+ {
+ "GPIO" => %w{}
+ }
+ end
+
+ def type_names
+ valid_types.map(&:first)
+ end
+
+ def state_names
+ valid_types.map(&:last).flatten
+ end
+
+ end
+
+
+ def initialize(number, states)
+ self.number = number
+ self.states = states.split(',')
+ end
+
+ end
+
+ end
+
+end
View
@@ -0,0 +1,22 @@
+require 'raudi/avr/pin'
+require 'raudi/avr/port_source'
+
+module Raudi
+
+ module AVR
+
+ class Port
+
+ attr_accessor :name, :pins, :source
+
+ def initialize(name, pins)
+ self.name = name.upcase
+ self.pins = pins.map{|pin_number, states| Pin.new(pin_number, states)}
+ self.source = PortSource.new(self)
+ end
+
+ end
+
+ end
+
+end
@@ -0,0 +1,34 @@
+require 'raudi/avr/source'
+
+module Raudi
+
+ module AVR
+
+ class PortSource < Source
+
+ attr_accessor :port
+
+ def initialize(port)
+ self.port = port
+ end
+
+ def startup
+ source = ""
+ source << output_config
+ source
+ end
+
+ def output_config
+ return "" if port.output_pins.empty?
+ source = "DDR#{port.name}"
+ source << " |= "
+ source << port.output_pins.map {|pin| "1 << #{pin.number}"}.join(' || ')
+ source << ";#{new_line}"
+ source
+ end
+
+ end
+
+ end
+
+end
@@ -0,0 +1,15 @@
+module Raudi
+
+ module AVR
+
+ class Source
+
+ def new_line
+ "\n"
+ end
+
+ end
+
+ end
+
+end
@@ -16,6 +16,18 @@
end
+ context 'pin section' do
+
+ it 'set output pins' do
+ controller = klass.new :atmega_328 do |config|
+ config.output :d0, :d3, :c3, :u34
+ config.output b: [0,2,3]
+ end
+ controller.source.startup_ports.should == "DDRB |= 1 << 0 || 1 << 2 || 1 << 3;\nDDRD |= 1 << 0 || 1 << 3;"
+ end
+
+ end
+
end
-end
+end

0 comments on commit 5c14e86

Please sign in to comment.