Class: ArduinoPlugin

Inherits:
Object
  • Object
show all
Defined in:
lib/rad/arduino_plugin.rb

Direct Known Subclasses

ParallaxPing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArduinoPlugin

:nodoc:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rad/arduino_plugin.rb', line 47

def initialize #:nodoc:
  
  $plugin_directives = $plugin_directives || []
  $plugin_external_variables = $plugin_external_variables || []
  # moved to check_for_plugin_use $plugin_structs = $plugin_structs || {}
  $plugin_signatures = $plugin_signatures || []
  $plugin_methods = $plugin_methods || []
 # $plugin_methods_hash = $plugin_methods_hash || {}  ### new
 # $plugins_to_load = $plugins_to_load || []  ### new
  $add_to_setup = $add_to_setup || []
  $load_libraries = $load_libraries || []


end

Class Method Details



113
114
115
116
117
118
119
120
# File 'lib/rad/arduino_plugin.rb', line 113

def self.add_blink_m_struct
  $plugin_structs[:blink_m] = <<-STR
  typedef struct _blinkm_script_line {
      uint8_t dur;
      uint8_t cmd[4];    // cmd,arg1,arg2,arg3
  } blinkm_script_line;
  STR
end

.add_debounce_structObject



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rad/arduino_plugin.rb', line 149

def self.add_debounce_struct
  $plugin_structs[:debounce] = <<-STR
  struct debounce {
    int state;
    int read;
    int prev;
    unsigned long time;
    unsigned long adjust;
  };
  STR
end

.add_sensor_structObject



176
177
178
179
180
181
182
183
# File 'lib/rad/arduino_plugin.rb', line 176

def self.add_sensor_struct
  $plugin_structs[:sensor] = <<-STR
  struct hysteresis {
    int pin;
    int state;
  };
  STR
end

.add_servo_structObject



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rad/arduino_plugin.rb', line 162

def self.add_servo_struct
  $plugin_structs[:servo] = <<-STR
  struct servo {
    int pin;
    long unsigned pulseWidth;
    long unsigned lastPulse;
    long unsigned startPulse;
    long unsigned refreshTime;
    int min;
    int max;
  };
  STR
end

.add_spectra_structObject



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rad/arduino_plugin.rb', line 185

def self.add_spectra_struct
  $plugin_structs[:spectra] = <<-STR
  struct spectra {
    int pin;
    int state;
    int r1;
    int r2;
    int r3;
  };
  STR
end

.check_for_plugin_use(sketch_string, plugin_string, file_name) ⇒ Object

rename klass to filename



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rad/arduino_plugin.rb', line 198

def self.check_for_plugin_use(sketch_string, plugin_string, file_name) # rename klass to filename
  $plugin_structs = $plugin_structs || {}
  $plugin_methods_hash = $plugin_methods_hash || {}  
  $plugins_to_load = $plugins_to_load || []  
  plugin_signatures = []
  plugin_methods = []
  ## need a test for this
  ## fails on string interpolation, but since ruby_to_c also currently fails ...
  sketch_string = sketch_string.gsub(/#(?!\{.*\}).*/, "")
  plugin_signatures << plugin_string.scan(/^\s*(((#{PLUGIN_C_VAR_TYPES})\s*)+\w*\(.*\))/)
  # gather just the method name and then add to #plugin_methods_hash
  plugin_signatures[0].map {|sig| "#{sig[0]}"}.each {|m| plugin_methods << m.gsub!(/^.*\s(\w*)\(.*\)/, '\1')}
  # we don't know the methods yet, so... 
  $plugin_methods_hash[file_name] = plugin_methods
  $plugin_methods_hash.each do |k,meths|
    meths.each do |meth|
      if sketch_string.include?(meth)
         # load this plugin... 
         $plugins_to_load << k unless $plugins_to_load.include?(k)
       end
    end
  end

end

.process(plugin_string) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rad/arduino_plugin.rb', line 224

def self.process(plugin_string)
  plugin_signatures = []
  first_process = plugin_string 
  # todo: need to add plugin names to the methods, so we can add them as comments in the c code
  # gather the c methods
  $plugin_methods << first_process.scan(/^\s*(((#{PLUGIN_C_VAR_TYPES}).*\)).*(\n.*)*^\s*\})/)
  plugin_signatures << first_process.scan(/^\s((#{PLUGIN_C_VAR_TYPES}).*\(.*\))/)
  $plugin_signatures << plugin_signatures[0].map {|sig| "#{sig[0]};"}
  ## strip out the methods and pass it back 
  result = plugin_string
  # strip out the c methods so we have only ruby before eval
  result.gsub(/^\s*(#{PLUGIN_C_VAR_TYPES}).*(\n.*)*^\s*\}/, ""  )
  
end

Instance Method Details



104
105
106
107
108
109
110
111
# File 'lib/rad/arduino_plugin.rb', line 104

def add_blink_m_struct
  $plugin_structs[:blink_m] = <<-STR
  typedef struct _blinkm_script_line {
      uint8_t dur;
      uint8_t cmd[4];    // cmd,arg1,arg2,arg3
  } blinkm_script_line;
  STR
end

#add_debounce_structObject



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rad/arduino_plugin.rb', line 122

def add_debounce_struct
  $plugin_structs[:debounce] = <<-STR
  struct debounce {
    int state;
    int read;
    int prev;
    unsigned long time;
    unsigned long adjust;
  };
  STR
end

#add_servo_structObject



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rad/arduino_plugin.rb', line 135

def add_servo_struct 
  $plugin_structs[:servo] = <<-STR
  struct servo {
    int pin;
    long unsigned pulseWidth;
    long unsigned lastPulse;
    long unsigned startPulse;
    long unsigned refreshTime;
    int min;
    int max;
  };
  STR
end

#add_to_setup(*args) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/rad/arduino_plugin.rb', line 74

def add_to_setup(*args)
  if args
     args.each do |arg|
       $add_to_setup << arg
     end
   end
end

#external_variables(*args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rad/arduino_plugin.rb', line 92

def external_variables(*args)
  if args
     args.each do |arg|
       puts self.class
       puts "\tadding plugin external variables: #{arg}"
       # colons aptional
       colon  = arg[arg.length - 1, 1] == ";"  ? '' : ';' 
       $plugin_external_variables << "#{arg}#{colon}"
     end
   end
end

#include_wireObject

c declarations are automatic you won’t need them in the plugins so, the first thing we can do is gather all the plugin methods, and scan the sketch available plugins… if the sketch has them, we include the plugins in the build… otherwise not..



69
70
71
# File 'lib/rad/arduino_plugin.rb', line 69

def include_wire
  $load_libraries << "Wire" unless $load_libraries.include?("Wire")
end

#plugin_directives(*args) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/rad/arduino_plugin.rb', line 83

def plugin_directives(*args)
  if args
     args.each do |arg|
       $plugin_directives << arg
     end
   end
end