raudi 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.DS_Store +0 -0
  2. data/.rvmrc +1 -1
  3. data/bin/raudi +17 -0
  4. data/configuration/atmega_328.yml +69 -0
  5. data/configuration/avr.yml +61 -0
  6. data/lib/.DS_Store +0 -0
  7. data/lib/raudi.rb +58 -9
  8. data/lib/raudi/.DS_Store +0 -0
  9. data/lib/raudi/action_processor.rb +34 -26
  10. data/lib/raudi/avr.rb +4 -0
  11. data/lib/raudi/avr/.DS_Store +0 -0
  12. data/lib/raudi/avr/controller.rb +73 -0
  13. data/lib/raudi/avr/pin.rb +30 -0
  14. data/lib/raudi/avr/pin_states.rb +73 -0
  15. data/lib/raudi/avr/port.rb +36 -0
  16. data/lib/raudi/avr/timer.rb +49 -0
  17. data/lib/raudi/core_ext.rb +1 -0
  18. data/lib/raudi/core_ext/fixnum.rb +16 -0
  19. data/lib/raudi/info.rb +52 -0
  20. data/lib/raudi/processing.rb +63 -0
  21. data/lib/raudi/processing/gpio.rb +25 -0
  22. data/lib/raudi/processing/int.rb +36 -0
  23. data/lib/raudi/processing/pcint.rb +35 -0
  24. data/lib/raudi/processing/timer.rb +56 -0
  25. data/lib/raudi/proxy/.DS_Store +0 -0
  26. data/lib/raudi/proxy/controller.rb +65 -0
  27. data/lib/raudi/proxy/external_interrupt.rb +30 -0
  28. data/lib/raudi/proxy/gpio.rb +19 -0
  29. data/lib/raudi/proxy/headers.rb +31 -0
  30. data/lib/raudi/proxy/timer.rb +61 -0
  31. data/lib/raudi/source.rb +48 -0
  32. data/lib/raudi/source/action.rb +16 -0
  33. data/lib/raudi/source/bit_operations.rb +35 -0
  34. data/lib/raudi/source/block.rb +44 -0
  35. data/lib/raudi/source/controller.rb +56 -0
  36. data/lib/raudi/source/function.rb +50 -0
  37. data/lib/raudi/source/interrupt.rb +35 -0
  38. data/lib/raudi/source/variable.rb +31 -0
  39. data/lib/raudi/state_list.rb +17 -0
  40. data/raudi.gemspec +3 -0
  41. data/spec/.DS_Store +0 -0
  42. data/spec/examples/sample.raudi +5 -0
  43. data/spec/lib/.DS_Store +0 -0
  44. data/spec/lib/raudi/.DS_Store +0 -0
  45. data/spec/lib/raudi/action_processor_spec.rb +35 -0
  46. data/spec/lib/raudi/avr/controller_spec.rb +13 -0
  47. data/spec/lib/raudi/avr/pin_spec.rb +70 -0
  48. data/spec/lib/raudi/avr/timer_spec.rb +22 -0
  49. data/spec/lib/raudi/info_spec.rb +9 -0
  50. data/spec/lib/raudi/proxy/external_interrupt_spec.rb +22 -0
  51. data/spec/lib/raudi/proxy/gpio_spec.rb +18 -0
  52. data/spec/lib/raudi/proxy/headers_spec.rb +34 -0
  53. data/spec/lib/raudi/proxy/timer_spec.rb +67 -0
  54. data/spec/lib/raudi/source/external_interrupt_spec.rb +55 -0
  55. data/spec/lib/raudi/source/gpio_spec.rb +18 -0
  56. data/spec/lib/raudi/source/main_structure_spec.rb +19 -0
  57. data/spec/lib/raudi/source/timer_spec.rb +44 -0
  58. data/spec/lib/raudi_spec.rb +41 -0
  59. data/spec/spec_helper.rb +10 -0
  60. data/spec/support/raudi_spec_helper.rb +27 -0
  61. metadata +106 -19
  62. data/examples/actions.raudi +0 -1
  63. data/examples/config.rb +0 -3
  64. data/examples/result.c +0 -10
  65. data/lib/raudi/avr_controller.rb +0 -23
  66. data/spec/raudi_spec.rb +0 -9
@@ -0,0 +1,30 @@
1
+ module Raudi
2
+
3
+ module Proxy
4
+
5
+ module ExternalInterrupt
6
+
7
+ def external_interrupt(*args)
8
+ set_interrupt(*args) do |arg|
9
+ next unless arg.is_a?(Hash)
10
+ arg.each do |pin_name, interrupt_event|
11
+ pin = get_pin(pin_name)
12
+ pin.int!(interrupt_event)
13
+ pin.port.add_interrupt :int
14
+ end
15
+ end
16
+ end
17
+
18
+ def pc_interrupt(*args)
19
+ set_interrupt(*args) do |arg|
20
+ pin = get_pin(arg)
21
+ pin.pcint!
22
+ pin.port.add_interrupt :pcint
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,19 @@
1
+ module Raudi
2
+
3
+ module Proxy
4
+
5
+ module GPIO
6
+
7
+ def output(*args)
8
+ set_pin_mode(*args){|pin| pin.output!}
9
+ end
10
+
11
+ def pullup(*args)
12
+ set_pin_mode(*args){|pin| pin.pullup!}
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,31 @@
1
+ module Raudi
2
+
3
+ module Proxy
4
+
5
+ module Headers
6
+
7
+ def headers(*args)
8
+ args.each do |param|
9
+ case param
10
+ when String
11
+ next if param == ""
12
+ when Symbol
13
+ param = "avr/#{param}"
14
+ else
15
+ next
16
+ end
17
+ controller.headers << param if allow_header?(param)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def allow_header?(name)
24
+ Info.headers.include?(name) and !controller.headers.include?(name)
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,61 @@
1
+ module Raudi
2
+
3
+ module Proxy
4
+
5
+ module Timer
6
+
7
+ def activate_timer(number, params = {})
8
+ timer = get_tc_devise(number)
9
+ timer.prescale = prepare_prescale(params[:prescale]) || 1
10
+ base_tc_setting(timer, params)
11
+ end
12
+
13
+ def activate_counter(number, params = {})
14
+ counter = get_tc_devise(number)
15
+ counter.counter = get_counter_pin(number)
16
+ counter.prescale = prepare_prescale(params[:mode].to_s) || 6
17
+ base_tc_setting(counter, params)
18
+ end
19
+
20
+ private
21
+
22
+ def base_tc_setting(devise, params)
23
+ [:a, :b].each do |number|
24
+ if value = params[number] and devise.range.include?(value)
25
+ devise.ctc!
26
+ devise.mode_params[number] = value
27
+ end
28
+ end
29
+ if params[:interrupt]
30
+ devise.interrupt = true
31
+ set_interrupt
32
+ end
33
+ devise.active = true
34
+ end
35
+
36
+ def get_counter_pin(number)
37
+ pin = controller.pins.detect { |pin| pin.can_be_timer?(number) }
38
+ raise_timer_error(number, "can be timer pin") unless pin
39
+ pin.timer!
40
+ pin
41
+ end
42
+
43
+ def get_tc_devise(number)
44
+ devise = controller.timers.detect{|devise| devise.number == number}
45
+ raise_timer_error(number, "doesn't present") unless devise
46
+ devise
47
+ end
48
+
49
+ def prepare_prescale(value)
50
+ Info.timers['prescale'][value]
51
+ end
52
+
53
+ def raise_timer_error(number, message)
54
+ raise "Timer/Counter #{number} #{message} in controller #{controller.model_name}"
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,48 @@
1
+ require 'raudi/source/action'
2
+ require 'raudi/source/bit_operations'
3
+ require 'raudi/source/block'
4
+ require 'raudi/source/variable'
5
+ require 'raudi/source/function'
6
+
7
+ module Raudi
8
+
9
+ module Source
10
+
11
+ class Base
12
+
13
+ include Action
14
+ include BitOperations
15
+ include Block
16
+ include Function
17
+ include Variable
18
+
19
+ attr_accessor :source_text
20
+
21
+ def code_line(line, params = {})
22
+ source_text << indent_line
23
+ source_text << line
24
+ source_text << ';' unless params[:skip_semicolon]
25
+ source_text << new_line unless params[:same_line]
26
+ end
27
+
28
+ def code_lines(lines)
29
+ lines.each_line{ |line| code_line(line, skip_semicolon: true, same_line: true) }
30
+ end
31
+
32
+ def new_line
33
+ "\r\n"
34
+ end
35
+
36
+ def reset_source_text
37
+ self.source_text = ""
38
+ end
39
+
40
+ def source_text
41
+ @source_text || reset_source_text
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,16 @@
1
+ module Raudi
2
+
3
+ module Source
4
+
5
+ module Action
6
+
7
+ def insert_action(*names)
8
+ code = Raudi.action[*names]
9
+ code_lines(code) if code
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,35 @@
1
+ module Raudi
2
+
3
+ module Source
4
+
5
+ module BitOperations
6
+
7
+ def write_register(register_name, bits)
8
+ unless bits.empty?
9
+ source = register_name.to_s.upcase
10
+ source << " |= "
11
+ source << join_bits(bits)
12
+ code_line(source)
13
+ end
14
+ end
15
+
16
+ def clear_register(register_name, bits)
17
+ unless bits.empty?
18
+ source = register_name.to_s.upcase
19
+ source << " &= "
20
+ source << "~("
21
+ source << join_bits(bits)
22
+ source << ")"
23
+ code_line(source)
24
+ end
25
+ end
26
+
27
+ def join_bits(bits)
28
+ bits.map{|bit| "1 << #{bit}"}.join(" | ")
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,44 @@
1
+ module Raudi
2
+
3
+ module Source
4
+
5
+ module Block
6
+
7
+ def code_block(header)
8
+ code_line header, skip_semicolon: true
9
+ block_in
10
+ yield if block_given?
11
+ block_out
12
+ end
13
+
14
+ def indent_count
15
+ @indent_count || reset_indent_count
16
+ end
17
+
18
+ def indent_count=(value)
19
+ @indent_count = value unless value < 0
20
+ end
21
+
22
+ def reset_indent_count
23
+ @indent_count = 0
24
+ end
25
+
26
+ def indent_line
27
+ " " * indent_count
28
+ end
29
+
30
+ def block_in
31
+ code_line '{', skip_semicolon: true
32
+ self.indent_count += 1
33
+ end
34
+
35
+ def block_out
36
+ self.indent_count -= 1
37
+ code_line '}', skip_semicolon: true
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,56 @@
1
+ require 'raudi/processing'
2
+ require 'raudi/source'
3
+ require 'raudi/source/interrupt'
4
+
5
+ module Raudi
6
+
7
+ module Source
8
+
9
+ class Controller < Base
10
+
11
+ include Interrupt
12
+
13
+ attr_accessor :controller
14
+
15
+ def initialize(controller)
16
+ self.controller = controller
17
+ end
18
+
19
+ def generate_headers
20
+ controller.headers.each do |name|
21
+ code_line "#include <#{name}.h>", skip_semicolon: true
22
+ end
23
+ new_line
24
+ end
25
+
26
+ def to_c
27
+ generate_headers
28
+ generate_interrupts
29
+
30
+ function_block(:main) do
31
+ generate_config
32
+ insert_action :setup
33
+ allow_interrupt
34
+ code_block('while(1)') do
35
+ insert_action :main
36
+ end
37
+ end
38
+
39
+ source_text
40
+ end
41
+
42
+ private
43
+
44
+ def generate_config
45
+ processings.each(&:generate_config)
46
+ end
47
+
48
+ def processings
49
+ Processing.list.map{|klass| klass.new(controller, self)}
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,50 @@
1
+ module Raudi
2
+
3
+ module Source
4
+
5
+ module Function
6
+
7
+ def function_block(name, params = {}, &block_source)
8
+ function_header = generate_function_header(name, params)
9
+ code_block(function_header, &block_source)
10
+ end
11
+
12
+ def generate_function_header(name, params = {})
13
+ source = ''
14
+ source << generate_result(name, params[:result])
15
+ end
16
+
17
+ def generate_result(name, result = nil)
18
+ if result.is_a?(Hash)
19
+ result[:type] ||= :void
20
+ result[:name] = name
21
+ generate_variable result
22
+ else
23
+ result ||= :void
24
+ generate_variable name: name, type: result
25
+ end
26
+ end
27
+
28
+ def function_arguments(params)
29
+ arguments_line = "("
30
+ arguments_line << if params
31
+ params = [params] unless params.is_a? Array
32
+ params.map do |param|
33
+ argument = result_to_c(param)
34
+ if param[:array]
35
+ param[:array] = 1 unless param[:array].is_a?(Integer)
36
+ argument << ('*' * param[:array])
37
+ end
38
+ argument << param[:name].to_s
39
+ end.join(", ")
40
+ else
41
+ 'void'
42
+ end
43
+ arguments_line << ")"
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,35 @@
1
+ module Raudi
2
+
3
+ module Source
4
+
5
+ module Interrupt
6
+
7
+ def allow_interrupt
8
+ code_line("sei()") if controller.interrupt
9
+ end
10
+
11
+ def interrupt_block(*names, &block)
12
+ interrupt_header = "ISR(#{vector_name(*names)})"
13
+ new_block = lambda do
14
+ insert_action(*names)
15
+ block.call if block_given?
16
+ end
17
+ code_block(interrupt_header, &new_block)
18
+ new_line
19
+ end
20
+
21
+ def generate_interrupts
22
+ return unless controller.interrupt
23
+ processings.each(&:generate_interrupts)
24
+ end
25
+
26
+ def vector_name(*args)
27
+ vector_name = args.map(&:upcase).join('_')
28
+ vector_name << "_vect"
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,31 @@
1
+ module Raudi
2
+
3
+ module Source
4
+
5
+ module Variable
6
+
7
+ def generate_variable(variable)
8
+ source = ""
9
+
10
+ [:static, :const, :unsigned].each do |prefix|
11
+ source << "#{prefix} " if variable[prefix] or variable[prefix.to_s]
12
+ end
13
+
14
+ source << variable[:type].to_s
15
+ source << ' '
16
+
17
+ variable[:pointer] ||= variable[:array]
18
+ if variable[:pointer]
19
+ variable[:pointer] = 1 unless variable[:pointer].is_a?(Integer)
20
+ source << ('*' * variable[:pointer])
21
+ end
22
+
23
+ source << variable[:name].to_s
24
+ source
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end