madrona-rad 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/History.txt +14 -0
  2. data/Manifest.txt +41 -18
  3. data/Rakefile +5 -0
  4. data/bin/rad +16 -6
  5. data/lib/examples/blink_m_address_assignment.rb +104 -0
  6. data/lib/examples/blink_m_multi.rb +61 -0
  7. data/lib/examples/configure_pa_lcd_boot.rb +91 -0
  8. data/lib/examples/external_variables.rb +5 -1
  9. data/lib/examples/hello_array.rb +48 -0
  10. data/lib/examples/hello_array2.rb +79 -0
  11. data/lib/examples/hello_array_eeprom.rb +61 -0
  12. data/lib/examples/hello_eeprom.rb +4 -7
  13. data/lib/examples/hello_eeprom_lcdpa.rb +81 -0
  14. data/lib/examples/hello_lcd_charset.rb +75 -0
  15. data/lib/examples/hello_pa_lcd.rb +59 -0
  16. data/lib/examples/hysteresis_duel.rb +39 -0
  17. data/lib/examples/motor_knob.rb +30 -0
  18. data/lib/examples/orig_servo_throttle.rb +1 -1
  19. data/lib/examples/servo_calibrate_continuous.rb +92 -0
  20. data/lib/examples/servo_throttle.rb +1 -1
  21. data/lib/examples/sparkfun_lcd.rb +2 -2
  22. data/lib/examples/spectra_soft_pot.rb +34 -0
  23. data/lib/libraries/SWSerLCDpa/SWSerLCDpa.cpp +60 -25
  24. data/lib/libraries/SWSerLCDpa/SWSerLCDpa.h +8 -2
  25. data/lib/libraries/SWSerLCDsf/SWSerLCDsf.cpp +46 -27
  26. data/lib/libraries/SWSerLCDsf/SWSerLCDsf.h +5 -2
  27. data/lib/libraries/Stepper/Stepper.cpp +220 -0
  28. data/lib/libraries/Stepper/Stepper.h +86 -0
  29. data/lib/libraries/Stepper/keywords.txt +28 -0
  30. data/lib/libraries/Wire/utility/twi.c +449 -0
  31. data/lib/libraries/Wire/utility/twi.h +57 -0
  32. data/lib/plugins/blink_m.rb +79 -46
  33. data/lib/plugins/hysteresis.rb +52 -0
  34. data/lib/plugins/lcd_padding.rb +39 -0
  35. data/lib/plugins/spectra_symbol.rb +79 -0
  36. data/lib/rad/arduino_plugin.rb +21 -0
  37. data/lib/rad/arduino_sketch.rb +231 -53
  38. data/lib/rad/init.rb +2 -2
  39. data/lib/rad/rad_processor.rb +42 -1
  40. data/lib/rad/rad_type_checker.rb +26 -0
  41. data/lib/rad/sim/arduino_sketch.rb +57 -0
  42. data/lib/rad/tasks/build_and_make.rake +4 -4
  43. data/lib/rad/variable_processing.rb +49 -12
  44. data/lib/rad/version.rb +1 -1
  45. data/test/test_array_processing.rb +179 -0
  46. data/test/test_plugin_loading.rb +151 -0
  47. data/test/test_translation_post_processing.rb +185 -0
  48. data/{lib/test → test}/test_variable_processing.rb +63 -13
  49. data/website/index.html +8 -7
  50. metadata +66 -30
  51. data/lib/test/test_array_processing.rb +0 -78
@@ -0,0 +1,26 @@
1
+ require 'ruby_to_ansi_c'
2
+
3
+ class RADTypeChecker < TypeChecker
4
+
5
+ def process_const(exp)
6
+ c = exp.shift
7
+ if c.to_s =~ /^[A-Z]/ then
8
+ # TODO: validate that it really is a const?
9
+ # uber hackery
10
+ # since constants are defined in the arduino_sketch define method and
11
+ # we can't inject them into the methods
12
+ # transport them here with a $define_types hash
13
+
14
+ $define_types.each do |k,v|
15
+ if k == c.to_s
16
+ @const_type = eval "Type.#{v}"
17
+ end
18
+ end
19
+ return t(:const, c, @const_type)
20
+ else
21
+ raise "I don't know what to do with const #{c.inspect}. It doesn't look like a class."
22
+ end
23
+ raise "need to finish process_const in #{self.class}"
24
+ end
25
+
26
+ end
@@ -0,0 +1,57 @@
1
+ ON = true
2
+ OFF = !ON
3
+ HIGH = ON
4
+ LOW = !HIGH
5
+
6
+ class ArduinoSketch
7
+ attr_accessor :pins
8
+
9
+ def initialize
10
+ @pins = self.class.instance_variable_get("@pins")
11
+ end
12
+
13
+ def self.output_pin(num, opts)
14
+ module_eval "@pins ||= []"
15
+ module_eval do
16
+ @pins << Pin.new( num, :type => :output )
17
+ end
18
+
19
+ if opts[:as]
20
+ module_eval <<-CODE
21
+ def #{opts[:as]}
22
+ pins.select{|p| p.num == #{num}}.first
23
+ end
24
+ CODE
25
+ end
26
+ end
27
+
28
+ def loop
29
+ end
30
+
31
+ def digitalWrite( pin, value )
32
+ to_change = pins.select{|p| p.num == pin.num}.first
33
+ to_change.value = value
34
+ end
35
+
36
+ def delay( millis )
37
+ end
38
+
39
+ # def serial_read
40
+ # end
41
+
42
+ # def serial_available
43
+ # end
44
+
45
+ # def blink
46
+ # end
47
+ end
48
+
49
+ class Pin
50
+ attr_accessor :num, :type, :value
51
+
52
+ def initialize num, opts
53
+ @num = num
54
+ @type = opts[:type]
55
+ @value = opts[:value] || false
56
+ end
57
+ end
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../init.rb")
2
2
  require 'ruby_to_ansi_c'
3
3
 
4
- C_VAR_TYPES = "unsigned|int|long|double|str|char|byte"
4
+ C_VAR_TYPES = "unsigned|int|long|double|str|char|byte|bool"
5
5
 
6
6
  # incredibly primitive tests
7
7
  # rake test:compile or rake test:upload
@@ -55,14 +55,14 @@ namespace :make do
55
55
  end
56
56
 
57
57
  desc "generate a makefile and use it to compile the .cpp using the current .cpp file"
58
- task :compile_cpp => ["build:sketch_dir", :clean_sketch_dir] do # should also depend on "build:sketch"
58
+ task :compile_cpp => ["build:sketch_dir", "build:gather_required_plugins", "build:plugin_setup", "build:setup", :clean_sketch_dir] do # should also depend on "build:sketch"
59
59
  Makefile.compose_for_sketch( @test_dir + @sketch_name )
60
60
  # not allowed? sh %{export PATH=#{Makefile.software_params[:arduino_root]}/tools/avr/bin:$PATH}
61
61
  sh %{cd #{RAD_ROOT}/#{@test_dir + @sketch_name}; make depend; make}
62
62
  end
63
63
 
64
64
  desc "generate a makefile and use it to compile the .cpp and upload it using current .cpp file"
65
- task :upload_cpp => ["build:sketch_dir", :clean_sketch_dir] do # should also depend on "build:sketch"
65
+ task :upload_cpp => ["build:sketch_dir", "build:gather_required_plugins", "build:plugin_setup", "build:setup", :clean_sketch_dir] do # should also depend on "build:sketch"
66
66
  Makefile.compose_for_sketch( @test_dir + @sketch_name )
67
67
  # not allowed? sh %{export PATH=#{Makefile.software_params[:arduino_root]}/tools/avr/bin:$PATH}
68
68
  sh %{cd #{RAD_ROOT}/#{@test_dir + @sketch_name}; make depend; make upload}
@@ -70,7 +70,7 @@ namespace :make do
70
70
 
71
71
  task :clean_sketch_dir => ["build:file_list", "build:sketch_dir"] do
72
72
  @sketch_name = @sketch_class.split(".").first
73
- FileList.new(Dir.entries("#{RAD_ROOT}/#{@test_dir + @sketch_name}")).exclude("#{@test_dir + @sketch_name}.cpp").exclude(/^\./).each do |f|
73
+ FileList.new(Dir.entries("#{RAD_ROOT}/#{@test_dir + @sketch_name}")).exclude("#{@sketch_name}.cpp").exclude(/^\./).each do |f|
74
74
  sh %{rm #{RAD_ROOT}/#{@test_dir + @sketch_name}/#{f}}
75
75
  end
76
76
  end
@@ -1,6 +1,6 @@
1
1
  module ExternalVariableProcessing
2
- # issues
3
- # testing
2
+ # issues
3
+ # testing
4
4
  # add checking for colon
5
5
 
6
6
 
@@ -42,11 +42,11 @@ module ExternalVariableProcessing
42
42
  type = var
43
43
  value = nil
44
44
  post_process_vars(name, type, value)
45
- return
45
+ else
46
+ value = var.split(",").first.lstrip
47
+ type = var.split(",")[1].nil? ? nil : var.split(",")[1].lstrip
48
+ translate_variables( name , type, value )
46
49
  end
47
- value = var.split(",").first.lstrip
48
- type = var.split(",")[1].nil? ? nil : var.split(",")[1].lstrip
49
- translate_variables( name , type, value )
50
50
  when TrueClass
51
51
  # puts "pre_process: #{name}, #{var}, #{var.inspect} got #{var.class} on 50"
52
52
  value = 1
@@ -57,13 +57,14 @@ module ExternalVariableProcessing
57
57
  value = 0
58
58
  type = "bool"
59
59
  post_process_vars(name, type, value)
60
+ when Array
61
+ post_process_arrays(name, var)
60
62
  else
61
- raise ArgumentError, "error message here.. got #{name}"
62
- end
63
-
64
-
63
+ raise ArgumentError, "not sure what to do here... got #{name} with value #{var} which is a #{var.class}"
64
+ end
65
65
  end
66
66
 
67
+
67
68
  def translate_variables(name, type = nil, value = nil)
68
69
 
69
70
  unless type.nil?
@@ -75,6 +76,16 @@ module ExternalVariableProcessing
75
76
  # puts "translate_variables: #{name}, #{value}, #{type} is a fixnum, got #{value.class} on 74"
76
77
  elsif value.class == Float
77
78
  # puts "translate_variables: #{name}, #{value}, #{type} is a float, got #{value.class} on 76"
79
+ elsif value =~ /^-(\d|x)*$/
80
+ value = value.to_i
81
+ type = "int" if type.nil?
82
+ elsif value =~ /^-(\d|\.|x)*$/
83
+ value = value.to_f
84
+ unless type.nil?
85
+ raise ArgumentError, "#{value} should be a float got #{type}" unless type == "float"
86
+ end
87
+ type = "float" if type.nil?
88
+
78
89
  elsif value[0,1] !~ /\d/
79
90
  # puts value[0,1]
80
91
  # puts "translate_variables: #{name}, #{value}, #{type} is a number of some type, got #{value.class} on 79"
@@ -101,16 +112,42 @@ module ExternalVariableProcessing
101
112
 
102
113
 
103
114
  def post_process_vars(name, type, value = nil)
104
- value = " = #{value}" if value
105
-
115
+ value = " = #{value}" if value
106
116
  $external_var_identifiers << "__#{name}" unless $external_var_identifiers.include?("__#{name}")
107
117
  $external_vars << "#{type} __#{name}#{value};"
108
118
  end
109
119
 
120
+ def post_process_arrays(name, var)
121
+ type = c_type(var[0])
122
+ $array_types[name] = type
123
+ assignment = var.inspect.gsub("[","{").gsub("]","}")
124
+ c_style_array = "#{type} __#{name}[] = #{assignment};"
125
+ $external_var_identifiers << "__#{name}" unless $external_var_identifiers.include?("__#{name}")
126
+ $external_array_vars << c_style_array unless $external_array_vars.include?(c_style_array)
127
+ end
128
+
110
129
  def check_variable_type(type)
111
130
  unless type =~ /#{C_VAR_TYPES}/
112
131
  raise ArgumentError, "the following variable types are supported \n #{C_VAR_TYPES.gsub("|",", ")} got #{type}"
113
132
  end
114
133
  end
134
+
135
+ def c_type(typ)
136
+ type =
137
+ case typ
138
+ when Integer
139
+ "int"
140
+ when String
141
+ "char*"
142
+ when TrueClass
143
+ "bool"
144
+ when FalseClass
145
+ "bool"
146
+ else
147
+ raise "Bug! Unknown type #{typ.inspect} in c_type"
148
+ end
149
+
150
+ type
151
+ end
115
152
 
116
153
  end
data/lib/rad/version.rb CHANGED
@@ -2,7 +2,7 @@ module Rad #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- TINY = 2
5
+ TINY = 6
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -0,0 +1,179 @@
1
+ $TESTING = true
2
+
3
+ # need to tell it where we are
4
+ # lets review these
5
+ # neee to remove this constant from tests and pull it from rad
6
+ C_VAR_TYPES = "unsigned|int|long|double|str|char|byte|float|bool"
7
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/variable_processing"
8
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/arduino_sketch"
9
+ require 'test/unit'
10
+
11
+
12
+ class TestArrayProcessing < Test::Unit::TestCase
13
+
14
+ def setup
15
+ @t = ArduinoSketch.new
16
+ # vars
17
+ end
18
+
19
+ # with July 25 2008 rework of arrays, this needs reworking/pruning
20
+
21
+ def test_int_array
22
+ name = "foo_a"
23
+ value_string = "int tomatoes[]"
24
+ expected = "int tomatoes[];"
25
+ result = @t.array(value_string)
26
+ assert_equal(expected, result[0])
27
+ end
28
+
29
+ def test_int_array_with_semi
30
+ name = "foo_b"
31
+ value_string = "int tomatoes[];"
32
+ expected = "int tomatoes[];"
33
+ result = @t.array(value_string)
34
+ assert_equal(expected, result[0])
35
+ end
36
+
37
+ def test_int_array_with_assignment
38
+ name = "foo_c"
39
+ value_string = "int tomatoes[] = {1,2,3,4}"
40
+ expected = "int tomatoes[] = {1,2,3,4};"
41
+ result = @t.array(value_string)
42
+ assert_equal(expected, result[0])
43
+ end
44
+
45
+ def test_int_array_with_assignment_and_semi
46
+ name = "foo_d"
47
+ value_string = "int tomatoes[] = {1,2,3};"
48
+ expected = "int tomatoes[] = {1,2,3};"
49
+ result = @t.array(value_string)
50
+ assert_equal(expected, result[0])
51
+ end
52
+
53
+ def test_unsigned_int_array
54
+ name = "foo_e"
55
+ value_string = "unsigned int tomatoes[]"
56
+ expected = "unsigned int tomatoes[];"
57
+ result = @t.array(value_string)
58
+ assert_equal(expected, result[0])
59
+ end
60
+
61
+ def test_unsigned_int_array_with_assignment
62
+ name = "foo_f"
63
+ value_string = "unsigned int tomatoes[] = {1,2,3};"
64
+ expected = "unsigned int tomatoes[] = {1,2,3};"
65
+ result = @t.array(value_string)
66
+ assert_equal(expected, result[0])
67
+ end
68
+
69
+ ### adding defines
70
+
71
+ def test_define_numbers
72
+ name = "foo_g"
73
+ value_string = "NUMBERS 10"
74
+ expected = "#define NUMBERS 10"
75
+ result = @t.define(value_string)
76
+ assert_equal(expected, result[0])
77
+ end
78
+
79
+ def test_define_numbers_type
80
+ name = "foo_gg"
81
+ value_string = "NUMBERS 10"
82
+ expected = "long"
83
+ result = @t.define(value_string)
84
+ assert_equal(expected, result[1])
85
+ end
86
+
87
+ def test_define_value_type_long_via_gvar
88
+ name = "foo_ggg"
89
+ value_string = "NUMBERS 10"
90
+ expected = "long"
91
+ result = @t.define(value_string)
92
+ assert_equal(expected, $define_types["NUMBERS"])
93
+ end
94
+
95
+ def test_define_string
96
+ name = "foo_h"
97
+ value_string = "TEXT word"
98
+ expected = "#define TEXT \"word\""
99
+ result = @t.define(value_string)
100
+ assert_equal(expected, result[0])
101
+ end
102
+
103
+ def test_define_string_type
104
+ name = "foo_hh"
105
+ value_string = "TEXT word"
106
+ expected = "str"
107
+ result = @t.define(value_string)
108
+ assert_equal(expected, result[1])
109
+ end
110
+
111
+ def test_define_string_type__via_gvar
112
+ name = "foo_hhh"
113
+ value_string = "TEXT word"
114
+ expected = "str"
115
+ result = @t.define(value_string)
116
+ assert_equal(expected, $define_types["TEXT"])
117
+ end
118
+
119
+ def test_define_stings_with_spaces
120
+ name = "foo_i"
121
+ value_string = "TEXT words with spaces"
122
+ expected = "#define TEXT \"words with spaces\""
123
+ result = @t.define(value_string)
124
+ assert_equal(expected, result[0])
125
+ end
126
+
127
+ def test_define_stings_with_spaces_type
128
+ name = "foo_ii"
129
+ value_string = "TEXT words with spaces"
130
+ expected = "str"
131
+ result = @t.define(value_string)
132
+ assert_equal(expected, result[1])
133
+ end
134
+
135
+ def test_define_stings_with_spaces_type_via_gvar
136
+ name = "foo_iii"
137
+ value_string = "TEXT words with spaces"
138
+ expected = "str"
139
+ result = @t.define(value_string)
140
+ assert_equal(expected, $define_types["TEXT"])
141
+ end
142
+
143
+ def test_define_float
144
+ name = "foo_j"
145
+ value_string = "FLOAT 10.0"
146
+ expected = "#define FLOAT 10.0"
147
+ result = @t.define(value_string)
148
+ assert_equal(expected, result[0])
149
+ end
150
+
151
+ def test_define_float_type
152
+ name = "foo_jj"
153
+ value_string = "FLOAT 10.0"
154
+ expected = "float"
155
+ result = @t.define(value_string)
156
+ assert_equal(expected, result[1])
157
+ end
158
+
159
+
160
+ def test_define_float_type_via_gvar
161
+ name = "foo_jjj"
162
+ value_string = "FLOAT 10.0"
163
+ expected = "float"
164
+ result = @t.define(value_string)
165
+ assert_equal(expected, $define_types["FLOAT"])
166
+ end
167
+
168
+ #
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+ # question for brian... do we need variable assignment with no value when
178
+ # we have "" and 0
179
+ end
@@ -0,0 +1,151 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ $TESTING = true
4
+
5
+ # this is a test stub for now
6
+ # lets review these
7
+ # neee to remove this constant from tests and pull it from rad
8
+ PLUGIN_C_VAR_TYPES = "int|void|unsigned|long|short|uint8_t|static|char\\*|byte"
9
+
10
+ require "rubygems"
11
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/rad_processor.rb"
12
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/rad_rewriter.rb"
13
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/rad_type_checker.rb"
14
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/variable_processing"
15
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/arduino_sketch"
16
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/rad/arduino_plugin"
17
+ require 'test/unit'
18
+
19
+
20
+
21
+
22
+ class TestPluginLoading < Test::Unit::TestCase
23
+
24
+
25
+
26
+
27
+ def setup
28
+ $external_var_identifiers = ["__foo", "__toggle", "wiggle"]
29
+ $define_types = { "KOOL" => "long", "ZAK" => "str"}
30
+ $array_types = { "my_array" => "int"}
31
+ $plugin_directives = []
32
+ $plugin_external_variables = []
33
+ $plugin_signatures =[]
34
+ $plugin_methods = []
35
+ $add_to_setup = []
36
+ $load_libraries = []
37
+ $plugin_structs = {}
38
+ $plugin_methods_hash = {}
39
+ $plugins_to_load = []
40
+ plugin_signatures = []
41
+ plugin_methods = []
42
+
43
+ @plugin_string =<<-STR
44
+ class PluginTesting < ArduinoPlugin
45
+
46
+
47
+
48
+ #
49
+ #
50
+ # BlinkM_funcs.h -- Arduino library to control BlinkM
51
+ # --------------
52
+ #
53
+ #
54
+ # Note: original version of this file lives with the BlinkMTester sketch
55
+ #
56
+ # 2007, Tod E. Kurt, ThingM, http://thingm.com/
57
+ #
58
+ # version: 20080203
59
+ #
60
+ # history:
61
+ # 20080101 - initial release
62
+ # 20080203 - added setStartupParam(), bugfix receiveBytes() from Dan Julio
63
+ # 20080727 - ported to rad jd barnhart
64
+ #
65
+ # first step, declare output pin 19 as i2c
66
+ ## output_pin 19, :as => :wire, :device => :i2c, :enable => :true # reminder, true issues wire.begin
67
+
68
+
69
+ include_wire
70
+
71
+ add_blink_m_struct
72
+
73
+
74
+
75
+ # Not needed when pin is declared with :enable => :true
76
+
77
+ static int BlinkM_sendBack(byte addr)
78
+ {
79
+ int num = 0x11;
80
+ char buf[5];
81
+ itoa(num, buf, 16);
82
+ return "cool"
83
+ }
84
+
85
+ static char* another_method(byte addr)
86
+ {
87
+ int num = 0x11;
88
+ char buf[5];
89
+ itoa(num, buf, 16);
90
+ return "cool"
91
+ }
92
+
93
+ end
94
+ STR
95
+
96
+ @sketch_string =<<-STR
97
+ class SanMiquel < ArduinoSketch
98
+
99
+ # looking for hints? check out the examples directory
100
+ # example sketches can be uploaded to your arduino with
101
+ # rake make:upload sketch=examples/hello_world
102
+ # just replace hello_world with other examples
103
+
104
+ def loop
105
+ delay 100
106
+ my_lcd.home "k"
107
+ my_lcd.setxy 0,1
108
+
109
+ BlinkM_sendBack 10
110
+ delay 1000
111
+ test_address
112
+ end
113
+
114
+
115
+ end
116
+
117
+ STR
118
+
119
+ end
120
+
121
+ # remove these external variables and parens on variables
122
+ # need to actually run code through ruby_to_c for some of these tests
123
+
124
+ def test_int
125
+ name = "foo_a"
126
+ # check_for_plugin_use(sketch_string, plugin_string, file_name)
127
+ ArduinoPlugin.check_for_plugin_use(@sketch_string, @plugin_string, "hello_plugins")
128
+ value_string = "int(__toggle = 0);"
129
+ expected = ["hello_plugins"]
130
+ result = $plugins_to_load
131
+ assert_equal(expected, result)
132
+ end
133
+
134
+ def test_two
135
+ name = "foo_a"
136
+ # check_for_plugin_use(sketch_string, plugin_string, file_name)
137
+ ArduinoPlugin.check_for_plugin_use(@sketch_string, @plugin_string, "hello_plugins")
138
+ value_string = "int(__toggle = 0);"
139
+ expected = {"hello_plugins"=>["BlinkM_sendBack", "another_method"]}
140
+ result = $plugin_methods_hash
141
+ assert_equal(expected, result)
142
+ end
143
+
144
+
145
+
146
+ ## need to look at unsigned long
147
+ ## need parens removal tests
148
+
149
+
150
+
151
+ end