Class: HardwareLibrary

Inherits:
ArduinoSketch show all
Defined in:
lib/rad/hardware_library.rb

Instance Attribute Summary

Attributes inherited from ArduinoSketch

#pins

Instance Method Summary collapse

Methods inherited from ArduinoSketch

#add, add_to_setup, #array, #assembler, #comment_box, #compose_setup, #define, #delay, #digitalWrite, #formatted_print, #input_pin, #input_pins, #loop, #output_pin, output_pin, post_process_ruby_to_c_methods, pre_process, #serial_begin

Methods included from ExternalVariableProcessing

#c_type, #check_variable_type, #post_process_arrays, #post_process_vars, #pre_process_vars, #process_external_vars, #translate_variables

Constructor Details

#initializeHardwareLibrary

Returns a new instance of HardwareLibrary.



3
4
5
# File 'lib/rad/hardware_library.rb', line 3

def initialize
  super
end

Instance Method Details

#ds1307(pin, opts = {}) ⇒ Object

DS1307 real time clock routines routines

Raises:

  • (ArgumentError)


718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/rad/hardware_library.rb', line 718

def ds1307(pin, opts={}) # DS1307 real time clock routines routines

  @@ds1307_inc ||= FALSE
  raise ArgumentError, "only one DS1307  may be used for i2c" unless @@ds1307_inc == FALSE
  @@ds1307_inc = TRUE
  raise ArgumentError, "can only define pin from Fixnum, got #{pin.class}" unless pin.is_a?(Fixnum)
  raise ArgumentError, "only pin 19 may be used for i2c, got #{pin}" unless pin == 19
  if opts[:as]
      @declarations << "DS1307 _#{opts[ :as ]} = DS1307();"
      $load_libraries << "DS1307"
      accessor = <<-STR
      DS1307& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
      void get( DS1307& s, byte *buf, boolean r ) {
        return s.get( buf, r );
      }
      byte get( DS1307& s, int b, boolean r ) {
        return s.get( b, r );
      }
      void set( DS1307& s, int b, int r ) {
        return s.set( b, r );
      }
      void start( DS1307& s ) {
        return s.start();
      }
      void stop( DS1307& s ) {
        return s.stop();
      }
      STR

      @accessors << accessor

      @signatures << "DS1307& #{opts[ :as ]}();"
      @other_setup << "\t_#{opts[ :as ]}.start();" if opts[:rtcstart]
  end
end

#ethernet(pin, opts = {}) ⇒ Object

work in progress

Raises:

  • (ArgumentError)


663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/rad/hardware_library.rb', line 663

def ethernet(pin, opts={})
  raise ArgumentError, "can only define pin from Fixnum, got #{pin.class}" unless pin.is_a?(Fixnum)
  if opts[:as]
    accessor = []
    $load_libraries << "AF_XPort"
    $load_libraries << "AFSoftSerial"
    # needs to be more granular:
    accessor << "AF_XPort xport = AF_XPort(XPORT_RXPIN, XPORT_TXPIN, XPORT_RESETPIN, XPORT_DTRPIN, XPORT_RTSPIN, XPORT_CTSPIN);"
    rate = opts[:rate] ? opts[:rate] : 57600
    @other_setup << "xport.begin(#{rate});"
    @accessors << accessor.join( "\n" )
  end
end

#fourwire_stepper(pin1, pin2, pin3, pin4, opts = {}) ⇒ Object

servo motor routines #

Raises:

  • (ArgumentError)


452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/rad/hardware_library.rb', line 452

def fourwire_stepper( pin1, pin2, pin3, pin4, opts={}) # servo motor routines #
  raise ArgumentError, "can only define pin1 from Fixnum, got #{pin1.class}" unless pin1.is_a?(Fixnum)
  raise ArgumentError, "can only define pin2 from Fixnum, got #{pin2.class}" unless pin2.is_a?(Fixnum)
  raise ArgumentError, "can only define pin3 from Fixnum, got #{pin3.class}" unless pin3.is_a?(Fixnum)
  raise ArgumentError, "can only define pin4 from Fixnum, got #{pin4.class}" unless pin4.is_a?(Fixnum)

  st_speed = opts[:speed] ? opts[:speed] : 30
  st_steps = opts[:steps] ? opts[:steps] : 100

  if opts[:as]
    @declarations << "Stepper _#{opts[ :as ]} = Stepper(#{st_steps},#{pin1},#{pin2},#{pin3},#{pin4});"
    $load_libraries << "Stepper"
    accessor = <<-STR
      Stepper& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
    STR
    @@stepr_inc ||= FALSE
    if (@@stepr_inc == FALSE)	# on second instance this stuff can't be repeated - BBR
      @@stepr_inc = TRUE
      accessor += <<-STR
      void set_speed( Stepper& s, long sp ) {
        return s.set_speed( sp );
      }
      void set_steps( Stepper& s, int b ) {
        return s.set_steps( b );
      }
      int version( Stepper& s ) {
        return s.version();
      }
      STR
    end

    @accessors << accessor

    @signatures << "Stepper& #{opts[ :as ]}();"

    @other_setup << "\t_#{opts[ :as ]}.set_speed(#{st_speed});" if opts[:speed]

  end
end

#frequency_timer(pin, opts = {}) ⇒ Object

frequency timer routines

Raises:

  • (ArgumentError)


494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/rad/hardware_library.rb', line 494

def frequency_timer(pin, opts={}) # frequency timer routines

  @@frequency_inc ||= FALSE
  raise ArgumentError, "there can be only one instance of Frequency Timer2" if @@frequency_inc == TRUE
  @@frequency_inc = TRUE

  raise ArgumentError, "can only define pin from Fixnum, got #{pin.class}" unless pin.is_a?(Fixnum)
  raise ArgumentError, "only pin 11 may be used for freq_out, got #{pin}" unless pin == 11

  if opts[:enable]
    raise ArgumentError, "enable option must include the frequency or period option" unless opts[:frequency] || opts[:period]
  end
  if opts[:frequency]
    raise ArgumentError, "the frequency option must be an integer, got #{opts[:frequency].class}" unless opts[:frequency].is_a?(Fixnum)
  end
  if opts[:period]
    raise ArgumentError, "the frequency option must be an integer, got #{opts[:period].class}" unless opts[:period].is_a?(Fixnum) 
  end
  # refer to: http://www.arduino.cc/playground/Code/FrequencyTimer2

  if opts[:as]

    @declarations << "FrequencyTimer2 _#{opts[ :as ]} = FrequencyTimer2();"

    $load_libraries << "FrequencyTimer2"
      accessor = <<-STR
      FrequencyTimer2& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
      void set_frequency( FrequencyTimer2& s, int b ) {
        return s.setPeriod( 1000000L/b );
      }
      void set_period( FrequencyTimer2& s, int b ) {
        return s.setPeriod( b );
      }
      void enable( FrequencyTimer2& s ) {
        return s.enable();
      }
      void disable( FrequencyTimer2& s ) {
        return s.disable();
      }
    STR

    @accessors << accessor

    @signatures << "FrequencyTimer2& #{opts[ :as ]}();"

    @other_setup << "\tFrequencyTimer2::setPeriod(0L);" unless opts[:frequency] || opts[:period]
    @other_setup << "\tFrequencyTimer2::setPeriod(1000000L/#{opts[:frequency]});" if opts[:frequency]
    @other_setup << "\tFrequencyTimer2::setPeriod(#{opts[:period]});" if opts[:period]
    @other_setup << "\tFrequencyTimer2::enable();" if opts[:enable] == :true
  end
end

#i2c_eeprom(pin, opts = {}) ⇒ Object

i2c serial eeprom routines #



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/rad/hardware_library.rb', line 678

def i2c_eeprom(pin, opts={}) # i2c serial eeprom routines #

  dev_addr = opts[:address] ? opts[:address] : 0

  if opts[:as]
    @declarations << "I2CEEPROM _#{opts[ :as ]} = I2CEEPROM(#{dev_addr});"
    $load_libraries << "I2CEEPROM"
    accessor = <<-STR
      I2CEEPROM& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
    STR
    @@i2cepr_inc ||= FALSE
    if (@@i2cepr_inc == FALSE)	# on second instance this stuff can't be repeated - BBR
      @@i2cepr_inc = TRUE
      accessor += <<-STR
      void write_byte( I2CEEPROM& s, unsigned int addr, byte b ) {
        return s.write_byte( addr, b );
      }
      void write_page( I2CEEPROM& s, unsigned int addr, byte* d, int l ) {
        return s.write_page( addr, d, l );
      }
      byte read_byte( I2CEEPROM& s, unsigned int addr ) {
        return s.read_byte( addr );
      }
      void read_buffer( I2CEEPROM& s, unsigned int addr, byte *d, int l ) {
        return s.read_buffer( addr, d, l );
      }
      STR
    end

    @accessors << accessor

    @signatures << "I2CEEPROM& #{opts[ :as ]}();"

  end
end

#loop_timer(opts = {}) ⇒ Object

loop timer methods #



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/rad/hardware_library.rb', line 299

def loop_timer(opts={}) # loop timer methods #

  if opts[:as]
    @declarations << "LoopTimer _#{opts[ :as ]} = LoopTimer();"
    $load_libraries << "LoopTimer"
    accessor = <<-STR
      LoopTimer& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
    STR
    @@loptim_inc ||= FALSE
    if (@@loptim_inc == FALSE)	# on second instance this stuff can't be repeated - BBR
      @@limtim_inc = TRUE
      accessor += <<-STR
      void track( LoopTimer& s ) {
        return s.track();
      }
      unsigned long get_total( LoopTimer& s ) {
        return s.get_total();
      }
      STR
    end

    @accessors << accessor

    @signatures << "LoopTimer& #{opts[ :as ]}();"

  end
end

#one_wire(pin, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/rad/hardware_library.rb', line 548

def one_wire(pin, opts={})
  raise ArgumentError, "can only define pin from Fixnum, got #{pin.class}" unless pin.is_a?(Fixnum)

  if opts[:as]
    @declarations << "OneWire _#{opts[ :as ]} = OneWire(#{pin});"
    accessor = []
    $load_libraries << "OneWire"
    accessor = <<-STR 
      OneWire& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
  STR
    @@onewire_inc ||= FALSE
    if (@@onewire_inc == FALSE)     # on second instance this stuff can't be repeated - BBR
      @@onewire_inc = TRUE
      accessor += <<-STR
      uint8_t reset(OneWire& s) {
        return s.reset();
      }
      void skip(OneWire& s) {
        return s.skip();
      }
      void write(OneWire& s, uint8_t v, uint8_t p = 0) {
        return s.write( v, p );
      }
      uint8_t read(OneWire& s) {
        return s.read();
      }
      void write_bit( OneWire& s, uint8_t b ) {
        return s.write_bit( b );
      }
      uint8_t read_bit(OneWire& s) {
        return s.read_bit();
      }
      void depower(OneWire& s) {
        return s.depower();
      }
      STR
    end
    @accessors << accessor

    @signatures << "OneWire& #{opts[ :as ]}();"
  end
end

#pa_lcd_setup(num, opts) ⇒ Object

use the pa lcd library



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rad/hardware_library.rb', line 53

def pa_lcd_setup(num, opts)
  if opts[:geometry]
    raise ArgumentError, "can only define pin from Fixnum, got #{opts[:geometry]}" unless opts[:geometry].is_a?(Fixnum)
    raise ArgumentError, "pa_lcd geometry must be 216, 220, 224, 240, 416, 420, got #{opts[:geometry]}" unless opts[:geometry].to_s =~ /(216|220|224|240|416|420)/
  end
  # move to plugin and load plugin
  # what's the default?
   opts[:rate] ||= 9600
  rate = opts[:rate] ? opts[:rate] : 9600
  swser_LCDpa(num, opts)
end

#servo(pin, opts = {}) ⇒ Object

servo motor routines #

Raises:

  • (ArgumentError)


351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/rad/hardware_library.rb', line 351

def servo(pin, opts={}) # servo motor routines #
  raise ArgumentError, "can only define pin from Fixnum, got #{pin.class}" unless pin.is_a?(Fixnum)

  minp = opts[:min] ? opts[:min] : 544
  maxp = opts[:max] ? opts[:max] : 2400

  if opts[:as]
    @declarations << "Servo _#{opts[ :as ]} = Servo();"
    $load_libraries << "Servo"
    accessor = <<-STR
      Servo& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
    STR
    @@servo_inc ||= FALSE
    if (@@servo_inc == FALSE)	# on second instance this stuff can't be repeated - BBR
      @@servo_inc = TRUE
      accessor += <<-STR
      uint8_t attach( Servo& s, int p ) {
        return s.attach(p);
      }
      uint8_t attach( Servo& s, int p, int pos ) {
        return s.attach(p, pos );
      }
      uint8_t attach( Servo& s, int p, uint16_t mn, uint16_t mx ) {
        return s.attach(p, mn, mx);
      }
      uint8_t attach( Servo& s, int p, int pos, uint16_t mn, uint16_t mx ) {
        return s.attach(p, pos, mn, mx);
      }
      void detach( Servo& s ) {
        return s.detach();
      }
      void position( Servo& s, int b ) {
        return s.position( b );
      }
      void speed( Servo& s, int b ) {
        return s.speed( b );
      }
      uint8_t read( Servo& s ) {
        return s.read();
      }
      uint8_t attached( Servo& s ) {
        return s.attached();
      }
      static void refresh( Servo& s ) {
        return s.refresh();
      }
      STR
    end

    @accessors << accessor

    @signatures << "Servo& #{opts[ :as ]}();"

    @other_setup << "\t_#{opts[ :as ]}.attach(#{pin}, #{opts[:position]}, #{minp}, #{maxp});" if opts[:position]
    @other_setup << "\t_#{opts[ :as ]}.attach(#{pin}, #{minp}, #{maxp});" unless opts[:position]

  end
end

#servo_setup(num, opts) ⇒ Object

use the servo library



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/rad/hardware_library.rb', line 330

def servo_setup(num, opts)
  if opts[:position]
    raise ArgumentError, "position must be an integer from 0 to 360, got #{opts[:position].class}" unless opts[:position].is_a?(Fixnum)
    raise ArgumentError, "position must be an integer from 0 to 360---, got #{opts[:position]}" if opts[:position] < 0 || opts[:position] > 360
  end
  servo(num, opts)
  # move this to better place ... 
  # should probably go along with servo code into plugin
  @@servo_dh ||= FALSE
  if (@@servo_dh == FALSE)	# on second instance this stuff can't be repeated - BBR
    @@servo_dh = TRUE
    @declarations << "void servo_refresh(void);"
    helper_methods = []
    helper_methods << "void servo_refresh(void)"
    helper_methods << "{"
    helper_methods <<  "\tServo::refresh();"
    helper_methods << "}"
    @helper_methods += "\n#{helper_methods.join("\n")}"
  end
end

#sf_lcd_setup(num, opts) ⇒ Object

use the sf (sparkfun) library



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

def sf_lcd_setup(num, opts)
  if opts[:geometry]
    raise ArgumentError, "can only define pin from Fixnum, got #{opts[:geometry]}" unless opts[:geometry].is_a?(Fixnum)
    raise ArgumentError, "sf_lcd geometry must be 216, 220, 416, 420, got #{opts[:geometry]}" unless opts[:geometry].to_s =~ /(216|220|416|420)/
  end
  # move to plugin and load plugin
   opts[:rate] ||= 9600
  rate = opts[:rate] ? opts[:rate] : 9600
  swser_LCDsf(num, opts)
end

#software_serial(rx, tx, opts = {}) ⇒ Object

Treat a pair of digital I/O pins as a serial line. See: www.arduino.cc/en/Tutorial/SoftwareSerial

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rad/hardware_library.rb', line 8

def software_serial(rx, tx, opts={})
  raise ArgumentError, "can only define rx from Fixnum, got #{rx.class}" unless rx.is_a?(Fixnum)
  raise ArgumentError, "can only define tx from Fixnum, got #{tx.class}" unless tx.is_a?(Fixnum)

  output_pin(tx)
  input_pin(rx)

  rate = opts[:rate] ? opts[:rate] : 9600
  if opts[:as]
    @declarations << "SoftwareSerial _#{opts[ :as ]} = SoftwareSerial(#{rx}, #{tx});"
    accessor = <<-STR
      SoftwareSerial& #{opts[ :as ]}() {
      return _#{opts[ :as ]};
      }
    STR
    @@swser_inc ||= FALSE
    if (@@swser_inc == FALSE) # on second instance this stuff can't be repeated
      @@swser_inc = TRUE
      accessor += <<-STR
      int read(SoftwareSerial& s) {
        return s.read();
      }
      void println( SoftwareSerial& s, char* str ) {
        return s.println( str );
      }
      void print( SoftwareSerial& s, char* str ) {
        return s.print( str );
      }
      void println( SoftwareSerial& s, int i ) {
        return s.println( i );
      }
      void print( SoftwareSerial& s, int i ) {
        return s.print( i );
      }
      STR
    end
    @accessors << accessor

    @signatures << "SoftwareSerial& #{opts[ :as ]}();"

    @other_setup << "\t_#{opts[ :as ]}.begin(#{rate});"
  end
end

#swser_LCDpa(tx, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rad/hardware_library.rb', line 65

def swser_LCDpa(tx, opts={})
  raise ArgumentError, "can only define tx from Fixnum, got #{tx.class}" unless tx.is_a?(Fixnum)

  rate = opts[:rate] ? opts[:rate] : 9600
  geometry = opts[:geometry] ? opts[:geometry] : 0
  if opts[:as] 
    @declarations << "SWSerLCDpa _#{opts[ :as ]} = SWSerLCDpa(#{tx}, #{geometry});"
    $load_libraries << "SWSerLCDpa"
    accessor = <<-STR
      SWSerLCDpa& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
    STR
    @@slcdpa_inc ||= FALSE
    if (@@slcdpa_inc == FALSE)	# on second instance this stuff can't be repeated - BBR
      @@slcdpa_inc = TRUE
      # ------------------- print generics -------------------------------
      accessor += <<-STR			  
      void print( SWSerLCDpa& s, uint8_t b ) {
        return s.print( b );
      }
      void print( SWSerLCDpa& s, const char *str ) {
        return s.print( str );
      }
      void print( SWSerLCDpa& s, char c ) {
        return s.print( c );
      }
      void print( SWSerLCDpa& s, int i ) {
        return s.print( i );
      }
      void print( SWSerLCDpa& s, unsigned int i ) {
        return s.print( i );
      }
      void print( SWSerLCDpa& s, long i ) {
        return s.print( i );
      }
      void print( SWSerLCDpa& s, unsigned long i ) {
        return s.print( i );
      }
      void print( SWSerLCDpa& s, long i, int b ) {
        return s.print( i, b );
      }
      STR
      # ------------------ PA-LCD specific functions ---------------------------------
      accessor += <<-STR
      void clearscr(SWSerLCDpa& s) {
        return s.clearscr();
      }
      void clearscr(SWSerLCDpa& s, const char *str) {
        return s.clearscr(str);
      }
      void clearscr(SWSerLCDpa& s, int n) {
        return s.clearscr(n);
      }
      void clearscr(SWSerLCDpa& s, long n, int b) {
        return s.clearscr(n, b);
      }
      void clearline(SWSerLCDpa& s, int line) {
        return s.clearline( line );
      }
      void clearline(SWSerLCDpa& s, int line, const char *str) {
        return s.clearline( line, str );
      }
      void clearline(SWSerLCDpa& s, int line, int n) {
        return s.clearline( line, n );
      }
      void clearline(SWSerLCDpa& s, int line, long n,  int b) {
        return s.clearline( line, n, b );
      }
      void home( SWSerLCDpa& s) {
        return s.home();
      }
      void home( SWSerLCDpa& s, const char *str) {
        return s.home( str );
      }
      void home( SWSerLCDpa& s, int n) {
        return s.home( n );
      }
      void home( SWSerLCDpa& s, long n, int b) {
        return s.home( n, b );
      }
      void setxy( SWSerLCDpa& s, int x, int y) {
        return s.setxy( x, y );
      }
      void setxy( SWSerLCDpa& s, int x, int y, const char *str) {
        return s.setxy( x, y, str );
      }
      void setxy( SWSerLCDpa& s, int x, int y, long n, int b) {
        return s.setxy( x, y, n, b );
      }
      void setxy( SWSerLCDpa& s, int x, int y, int n) {
        return s.setxy( x, y, n );
      }
      void setgeo( SWSerLCDpa& s, int g) {
        return s.setgeo( g );
      }
      void setintensity( SWSerLCDpa& s, int i ) {
        return s.setintensity( i );
      }
      void intoBignum(SWSerLCDpa& s) {
        return s.intoBignum();
      }
      void outofBignum(SWSerLCDpa& s) {
        return s.outofBignum();
      }
      STR
    end

    @accessors << accessor

    @signatures << "SWSerLCDpa& #{opts[ :as ]}();"

    @other_setup << "\t_#{opts[ :as ]}.begin(#{rate});"
    @other_setup << "\t_#{opts[ :as ]}.clearscr();"     if :clear_screen == :true

  end
end

#swser_LCDsf(tx, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rad/hardware_library.rb', line 196

def swser_LCDsf(tx, opts={})
  raise ArgumentError, "can only define tx from Fixnum, got #{tx.class}" unless tx.is_a?(Fixnum)    

  rate = opts[:rate] ? opts[:rate] : 9600
  geometry = opts[:geometry] ? opts[:geometry] : 0
  if opts[:as] 
    @declarations << "SWSerLCDsf _#{opts[ :as ]} = SWSerLCDsf(#{tx}, #{geometry});"

    $load_libraries << "SWSerLCDsf"
    accessor = <<-STR
      SWSerLCDsf& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
    STR
    @@slcdsf_inc ||= FALSE # assign only if nil
    if (@@slcdsf_inc == FALSE)	# on second instance this stuff can't be repeated - BBR
      @@slcdsf_inc = TRUE
      accessor += <<-STR
      void print( SWSerLCDsf& s, uint8_t b ) {
        return s.print( b );
      }
      void print( SWSerLCDsf& s, const char *str ) {
        return s.print( str );
      }
      void print( SWSerLCDsf& s, char c ) {
        return s.print( c );
      }
      void print( SWSerLCDsf& s, int i ) {
        return s.print( i );
      }
      void print( SWSerLCDsf& s, unsigned int i ) {
        return s.print( i );
      }
      void print( SWSerLCDsf& s, long i ) {
        return s.print( i );
      }
      void print( SWSerLCDsf& s, unsigned long i ) {
        return s.print( i );
      }
      void print( SWSerLCDsf& s, long i, int b ) {
        return s.print( i, b );
      }
      STR
      # ------------------ Spark Fun Specific  Functions ---------------------------------
      accessor += <<-STR
      void clearscr(SWSerLCDsf& s) {
        return s.clearscr();
      }
      void clearscr(SWSerLCDsf& s, const char *str) {
        return s.clearscr(str);
      }
      void clearscr(SWSerLCDsf& s, int n) {
        return s.clearscr(n);
      }
      void clearscr(SWSerLCDsf& s, long n, int b) {
        return s.clearscr(n, b);
      }
      void home( SWSerLCDsf& s) {
        return s.home();
      }
      void home( SWSerLCDsf& s, const char *str) {
        return s.home( str );
      }
      void home( SWSerLCDsf& s, int n) {
        return s.home( n );
      }
      void home( SWSerLCDsf& s, long n, int b) {
        return s.home( n, b );
      }
      void setxy( SWSerLCDsf& s, int x, int y) {
        return s.setxy( x, y );
      }
      void setxy( SWSerLCDsf& s, int x, int y, const char *str) {
        return s.setxy( x, y, str );
      }
      void setxy( SWSerLCDsf& s, int x, int y, int n) {
        return s.setxy( x, y, n );
      }
      void setxy( SWSerLCDsf& s, int x, int y, long n, int b) {
        return s.setxy( x, y, n, b );
      }
      void setgeo( SWSerLCDsf& s, int g) {
        return s.setgeo( g );
      }
      void setintensity( SWSerLCDsf& s, int i ) {
        return s.setintensity( i );
      }
      void setcmd( SWSerLCDsf& s, uint8_t a, uint8_t b) {
        return s.setcmd( a, b );
      }
      STR
    end
    @accessors << accessor

    @signatures << "SWSerLCDsf& #{opts[ :as ]}();"

    @other_setup << "\t_#{opts[ :as ]}.begin(#{rate});"
    @other_setup << "\t_#{opts[ :as ]}.clearscr();"     if :clear_screen == :true

  end
end

#two_wire(pin, opts = {}) ⇒ Object

i2c Two-Wire

Raises:

  • (ArgumentError)


593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/rad/hardware_library.rb', line 593

def two_wire (pin, opts={}) # i2c Two-Wire

  raise ArgumentError, "can only define pin from Fixnum, got #{pin.class}" unless pin.is_a?(Fixnum)
  raise ArgumentError, "only pin 19 may be used for i2c, got #{pin}" unless pin == 19

  if opts[:as]

    @@twowire_inc = TRUE
     @declarations << "TwoWire _wire = TwoWire();"
    $load_libraries << "Wire"	
    accessor = <<-STR
      TwoWire& wire() {
        return _wire;
      }
      void begin( TwoWire& s) {
        return s.begin();
      }
      void begin( TwoWire& s, uint8_t a) {
        return s.begin(a);
      }
      void begin( TwoWire& s, int a) {
        return s.begin(a);
      }
      void beginTransmission( TwoWire& s, uint8_t a ) {
        return s.beginTransmission( a );
      }
      void beginTransmission( TwoWire& s, int a ) {
        return s.beginTransmission( a );
      }
      void endTransmission( TwoWire& s ) {
        return s.endTransmission();
      }
      void requestFrom( TwoWire& s, uint8_t a, uint8_t q) {
        return s.requestFrom( a, q );
      }
      void requestFrom( TwoWire& s, int a, int q) {
        return s.requestFrom( a, q );
      }
      void send( TwoWire& s, uint8_t d) {
        return s.send(d);
      }
      void send( TwoWire& s, int d) {
        return s.send(d);
      }
      void send( TwoWire& s, char* d) {
        return s.send(d);
      }
      void send( TwoWire& s, uint8_t* d, uint8_t q) {
        return s.send( d, q );
      }
      uint8_t available( TwoWire& s) {
        return s.available();
      }
      uint8_t receive( TwoWire& s) {
        return s.receive();
      }
    STR

    @accessors << accessor

    @signatures << "TwoWire& wire();"

    @other_setup << "\t_wire.begin();"    # We never get here a second time. If we go to the trouble 
                                          # of setting up i2c, we gotta start it and it never gets 
                                          # stopped. This is not 'optional!'
  end

end

#twowire_stepper(pin1, pin2, opts = {}) ⇒ Object

servo motor routines #

Raises:

  • (ArgumentError)


412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/rad/hardware_library.rb', line 412

def twowire_stepper(pin1, pin2, opts={}) # servo motor routines #
  raise ArgumentError, "can only define pin1 from Fixnum, got #{pin1.class}" unless pin1.is_a?(Fixnum)
  raise ArgumentError, "can only define pin2 from Fixnum, got #{pin2.class}" unless pin2.is_a?(Fixnum)

  st_speed = opts[:speed] ? opts[:speed] : 30
  st_steps = opts[:steps] ? opts[:steps] : 100

  if opts[:as]
    @declarations << "Stepper _#{opts[ :as ]} = Stepper(#{st_steps},#{pin1},#{pin2});"
    $load_libraries << "Stepper"
    accessor = <<-STR
      Stepper& #{opts[ :as ]}() {
        return _#{opts[ :as ]};
      }
    STR
    @@stepr_inc ||= FALSE
    if (@@stepr_inc == FALSE)	# on second instance this stuff can't be repeated - BBR
    @@stepr_inc = TRUE
    accessor = <<-STR
      void set_speed( Stepper& s, long sp ) {
        return s.set_speed( sp );
      }
      void set_steps( Stepper& s, int b ) {
        return s.set_steps( b );
      }
      int version( Stepper& s ) {
        return s.version();
      }
      STR
  end

  @accessors << accessor

  @signatures << "Stepper& #{opts[ :as ]}();"

  @other_setup << "\t_#{opts[ :as ]}.set_speed(#{st_speed});" if opts[:speed]

  end
end