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,57 @@
1
+ /*
2
+ twi.h - TWI/I2C library for Wiring & Arduino
3
+ Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4
+
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+ #ifndef twi_h
21
+ #define twi_h
22
+
23
+ #include <inttypes.h>
24
+
25
+ //#define ATMEGA8
26
+
27
+ #ifndef CPU_FREQ
28
+ #define CPU_FREQ 16000000L
29
+ #endif
30
+
31
+ #ifndef TWI_FREQ
32
+ #define TWI_FREQ 100000L
33
+ #endif
34
+
35
+ #ifndef TWI_BUFFER_LENGTH
36
+ #define TWI_BUFFER_LENGTH 32
37
+ #endif
38
+
39
+ #define TWI_READY 0
40
+ #define TWI_MRX 1
41
+ #define TWI_MTX 2
42
+ #define TWI_SRX 3
43
+ #define TWI_STX 4
44
+
45
+ void twi_init(void);
46
+ void twi_setAddress(uint8_t);
47
+ uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t);
48
+ uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t);
49
+ uint8_t twi_transmit(uint8_t*, uint8_t);
50
+ void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) );
51
+ void twi_attachSlaveTxEvent( void (*)(void) );
52
+ void twi_reply(uint8_t);
53
+ void twi_stop(void);
54
+ void twi_releaseBus(void);
55
+
56
+ #endif
57
+
@@ -1,43 +1,46 @@
1
1
  class BlinkM < ArduinoPlugin
2
2
 
3
- # scaffolding for blink_m
4
- include_wire
3
+ #
4
+ #
5
+ # BlinkM_funcs.h -- Arduino library to control BlinkM
6
+ # --------------
7
+ #
8
+ #
9
+ # Note: original version of this file lives with the BlinkMTester sketch
10
+ #
11
+ # 2007, Tod E. Kurt, ThingM, http://thingm.com/
12
+ #
13
+ # version: 20080203
14
+ #
15
+ # history:
16
+ # 20080101 - initial release
17
+ # 20080203 - added setStartupParam(), bugfix receiveBytes() from Dan Julio
18
+ # 20080727 - ported to rad jd barnhart
19
+ #
20
+ # first step, declare output pin 19 as i2c
21
+ ## output_pin 19, :as => :wire, :device => :i2c, :enable => :true # reminder, true issues wire.begin
5
22
 
6
- add_blink_m_struct
7
23
 
8
- void simply_blink(void)
9
- {
10
-
11
- }
24
+ include_wire
12
25
 
26
+ add_blink_m_struct
13
27
 
14
- static void wire_begin(void)
15
- {
16
- delay(200);
17
- Wire.begin();
18
- }
19
28
 
20
- static void stop_script( void )
21
- {
22
- Wire.begin();
23
- Wire.beginTransmission(0x09);
24
- Wire.send('o');
25
- Wire.endTransmission();
26
- }
27
29
 
28
- // Call this first (when powering BlinkM from a power supply)
30
+ # Not needed when pin is declared with :enable => :true
31
+ # In fact, declaring it twice causes nothing but problems
29
32
 
30
33
  static void BlinkM_begin()
31
34
  {
32
35
  Wire.begin(); // join i2c bus (address optional for master)
33
36
  }
34
37
 
38
+
35
39
  // General version of BlinkM_beginWithPower().
36
- // Call this first when BlinkM is plugged directly into Arduino
37
40
 
38
41
  static void BlinkM_beginWithPowerPins(byte pwrpin, byte gndpin)
39
42
  {
40
- DDRC |= _BV(pwrpin) | _BV(gndpin);
43
+ DDRC |= _BV(pwrpin) | _BV(gndpin); // make outputs
41
44
  PORTC &=~ _BV(gndpin);
42
45
  PORTC |= _BV(pwrpin);
43
46
  delay(100); // wait for things to stabilize
@@ -46,6 +49,8 @@ static void BlinkM_beginWithPowerPins(byte pwrpin, byte gndpin)
46
49
  }
47
50
 
48
51
  // Call this first when BlinkM is plugged directly into Arduino
52
+ // The BlinkM's PWR (power) pins should line up with pins 2 and 3 of the connector,
53
+ // while the I2C (communications) pins should line up with pins 4 and 5.
49
54
 
50
55
  static void BlinkM_beginWithPower()
51
56
  {
@@ -68,6 +73,7 @@ static void BlinkM_sendCmd(byte addr, byte* cmd, int cmdlen)
68
73
 
69
74
  static int BlinkM_receiveBytes(byte addr, byte* resp, byte len)
70
75
  {
76
+ Wire.requestFrom(addr, len);
71
77
  if( Wire.available() ) {
72
78
  for( int i=0; i<len; i++)
73
79
  resp[i] = Wire.receive();
@@ -76,7 +82,10 @@ static int BlinkM_receiveBytes(byte addr, byte* resp, byte len)
76
82
  return -1;
77
83
  }
78
84
 
79
- // Sets the I2C address of the Arduino.
85
+ // Sets the I2C address of the BlinkM(s)
86
+ // Typically used to setup BlinkM addresses
87
+ // Connect one and call this with an address like 10,
88
+ // then 11 for the next and so on and so forth
80
89
  // Uses "general call" broadcast address
81
90
 
82
91
  static void BlinkM_setAddress(byte newaddress)
@@ -88,11 +97,11 @@ static void BlinkM_setAddress(byte newaddress)
88
97
  Wire.send(0x0D); // dood!
89
98
  Wire.send(newaddress);
90
99
  Wire.endTransmission();
91
- delay(30);
100
+ delay(50); // just in case
92
101
  }
93
102
 
94
103
 
95
- // Gets the I2C addrss of the Arduino
104
+ // Gets the I2C addrss of the BlinkM
96
105
  // Kind of redundant when sent to a specific address
97
106
  // but uses to verify BlinkM communication
98
107
 
@@ -126,7 +135,31 @@ static int BlinkM_getVersion(byte addr)
126
135
  }
127
136
 
128
137
  // Demonstrates how to verify you're talking to a BlinkM
129
- // and that you know its address
138
+ // and that you know its address -- message version
139
+
140
+ static char* blink_m_check_address_message(byte addr) // :as => :optional
141
+ {
142
+ char message[50];
143
+ char status[5];
144
+ strcpy(message, "received 0x");
145
+ //Serial.print("Checking BlinkM address...");
146
+ int b = BlinkM_getAddress(addr);
147
+ if( b==-1 ) {
148
+ //Serial.println("No response, that's not good");
149
+ return "No response, that's not good"; // no response
150
+ }
151
+ itoa(b, status ,16);
152
+
153
+ //Serial.print("received addr: 0x");
154
+ //Serial.print(b,HEX);
155
+ if( b != addr )
156
+ return "error, mismatch"; // error, addr mismatch
157
+ else
158
+ return strcat(message, status); // match, everything okay
159
+ }
160
+
161
+ // Demonstrates how to verify you're talking to a BlinkM
162
+ // and that you know its address -- digital version
130
163
 
131
164
  static int BlinkM_checkAddress(byte addr)
132
165
  {
@@ -136,6 +169,7 @@ static int BlinkM_checkAddress(byte addr)
136
169
  //Serial.println("No response, that's not good");
137
170
  return -1; // no response
138
171
  }
172
+
139
173
  //Serial.print("received addr: 0x");
140
174
  //Serial.print(b,HEX);
141
175
  if( b != addr )
@@ -214,7 +248,6 @@ static void BlinkM_fadeToRandomRGB(byte addr, byte rrnd, byte grnd, byte brnd)
214
248
  Wire.send(brnd);
215
249
  Wire.endTransmission();
216
250
  }
217
-
218
251
  // Fades to a random HSB color
219
252
 
220
253
  static void BlinkM_fadeToRandomHSB(byte addr, byte hrnd, byte srnd, byte brnd)
@@ -250,8 +283,6 @@ static void BlinkM_playScript(byte addr, byte script_id, byte reps, byte pos)
250
283
  Wire.endTransmission();
251
284
  }
252
285
 
253
-
254
-
255
286
  static void BlinkM_stopScript(byte addr)
256
287
  {
257
288
  Wire.beginTransmission(addr);
@@ -259,15 +290,6 @@ static void BlinkM_stopScript(byte addr)
259
290
  Wire.endTransmission();
260
291
  }
261
292
 
262
- static void BlinkM_startScript(byte addr)
263
- {
264
- Wire.beginTransmission(addr);
265
- Wire.send('p');
266
- Wire.endTransmission();
267
- }
268
-
269
-
270
-
271
293
  static void BlinkM_setScriptLengthReps(byte addr, byte script_id,
272
294
  byte len, byte reps)
273
295
  {
@@ -283,10 +305,11 @@ static void BlinkM_writeScriptLine(byte addr, byte script_id,
283
305
  byte pos, byte dur,
284
306
  byte cmd, byte arg1, byte arg2, byte arg3)
285
307
  {
286
- Serial.print("\nwriting line:"); Serial.print(pos,DEC);
308
+ #ifdef BLINKM_FUNCS_DEBUG
309
+ Serial.print("writing line:"); Serial.print(pos,DEC);
287
310
  Serial.print(" with cmd:"); Serial.print(cmd);
288
311
  Serial.print(" arg1:"); Serial.println(arg1,HEX);
289
-
312
+ #endif
290
313
  Wire.beginTransmission(addr);
291
314
  Wire.send('W');
292
315
  Wire.send(script_id);
@@ -303,6 +326,10 @@ static void BlinkM_writeScript(byte addr, byte script_id,
303
326
  byte len, byte reps,
304
327
  blinkm_script_line* lines)
305
328
  {
329
+ #ifdef BLINKM_FUNCS_DEBUG
330
+ Serial.print("writing script to addr:"); Serial.print(addr,DEC);
331
+ Serial.print(", script_id:"); Serial.println(script_id,DEC);
332
+ #endif
306
333
  for(byte i=0; i < len; i++) {
307
334
  blinkm_script_line l = lines[i];
308
335
  BlinkM_writeScriptLine( addr, script_id, i, l.dur,
@@ -312,12 +339,18 @@ static void BlinkM_writeScript(byte addr, byte script_id,
312
339
  }
313
340
 
314
341
 
315
-
316
-
317
-
318
-
319
-
320
-
342
+ static void BlinkM_setStartupParams(byte addr, byte mode, byte script_id,
343
+ byte reps, byte fadespeed, byte timeadj)
344
+ {
345
+ Wire.beginTransmission(addr);
346
+ Wire.send('B');
347
+ Wire.send(mode);
348
+ Wire.send(script_id);
349
+ Wire.send(reps);
350
+ Wire.send(fadespeed);
351
+ Wire.send(timeadj);
352
+ Wire.endTransmission();
353
+ }
321
354
 
322
355
 
323
356
  end
@@ -0,0 +1,52 @@
1
+ class Hysteresis < ArduinoPlugin
2
+
3
+ # jdbarnhart
4
+ # 20080728
5
+ #
6
+ #
7
+ # purpose
8
+ #
9
+ # add hysteresis to analog readings, typically sensors or potentiometers
10
+ #
11
+ # use
12
+ # two steps
13
+ #
14
+ # one
15
+ # declare :device => :sensor
16
+ # example:
17
+ # input_pin 1, :as => :sensor_one, :device => :sensor
18
+ #
19
+ # two
20
+ # instead of:
21
+ # my_lcd.print analogRead sensor_two
22
+ # use add_hyst
23
+ # my_lcd.print sensor_one.with_hyst 4
24
+ #
25
+ # # note, 4 is the amount of hysteresis
26
+ #
27
+ #
28
+ void with_hysteresis(int pin, int amt)
29
+ {
30
+ with_hyst(pin, amt);
31
+ }
32
+
33
+ int with_hyst(int pin, int amt)
34
+ {
35
+ int read;
36
+ unsigned int i;
37
+ read = analogRead(pin);
38
+ for (i = 0; i < (int) (sizeof(hyst) / sizeof(hyst[0])); i++) {
39
+ if (pin == hyst[i].pin) {
40
+ if (((read - hyst[i].state) > amt ) || ((hyst[i].state - read) > amt )) {
41
+ hyst[i].state = read;
42
+ return hyst[i].state;
43
+ }
44
+ else
45
+ return hyst[i].state;
46
+ }
47
+ }
48
+ }
49
+
50
+
51
+
52
+ end
@@ -0,0 +1,39 @@
1
+ class LCDPadding < ArduinoPlugin
2
+
3
+ # jdbarnhart
4
+ # 20080729
5
+ #
6
+ #
7
+ # purpose
8
+
9
+ # simple integer padding for lcd display pad
10
+ #
11
+ # example
12
+ # my_lcd.print pad_int_to_str 29, 5
13
+ #
14
+ # result
15
+ # " 29"
16
+
17
+
18
+
19
+ static char* pad_int_to_str(int num, int length)
20
+ {
21
+ int i = 0;
22
+ int start;
23
+ char plain[20];
24
+ char space[5] = " ";
25
+ char* pretty = " ";
26
+ itoa(num, plain ,10);
27
+ start = length - strlen(plain);
28
+ while (i <= length) {
29
+ if (i >= start)
30
+ pretty[i] = plain[i - start];
31
+ else
32
+ pretty[i] = space[0];
33
+ i++;
34
+ }
35
+ return pretty;
36
+ }
37
+
38
+
39
+ end
@@ -0,0 +1,79 @@
1
+ class SpectraSymbol < ArduinoPlugin
2
+
3
+ # jdbarnhart
4
+ # 20080729
5
+ #
6
+ # crazy experiment in progress
7
+ # purpose
8
+ #
9
+ # retain last reading after finger is removed from spectrasymbol
10
+ # soft pot
11
+ #
12
+ # use
13
+ # two steps
14
+ #
15
+ # one
16
+ # declare :device => :sensor
17
+ # example:
18
+ # input_pin 1, :as => :sensor_one, :device => :spectra
19
+ #
20
+ # two
21
+ # instead of:
22
+ # my_lcd.print analogRead sensor_two
23
+ # use soft_lock
24
+ # my_lcd.print sensor_one.spectra_lock
25
+ #
26
+ #
27
+ # notes:
28
+ # experimental settings for 100mm spectrasymbol
29
+ #
30
+ # hysteresis is set at 5
31
+ # amount of sensor drop is set to 3
32
+ # delay time (dtime) is 4
33
+ # cutoff set to 10
34
+ #
35
+ int soft_lock(int pin)
36
+
37
+ {
38
+ int hyst = 5;
39
+ int drop = 3;
40
+ int dtime = 4;
41
+ int cutoff = 10;
42
+ int read;
43
+ int r1;
44
+ int r2;
45
+ int r3;
46
+ unsigned int i;
47
+ read = analogRead(pin)/4;
48
+ delay(dtime);
49
+ r1 = analogRead(pin)/4;
50
+ delay(dtime);
51
+ r2 = analogRead(pin)/4;
52
+ delay(dtime);
53
+ r3 = analogRead(pin)/4;
54
+ delay(dtime);
55
+ for (i = 0; i < (int) (sizeof(spec) / sizeof(spec[0])); i++) {
56
+ if (pin == spec[i].pin) {
57
+ if (((r3 - r2) > drop) && ((r2 - r1) > drop) && ((r1 - read) > drop))
58
+ return spec[i].state - 10;
59
+ else
60
+ {
61
+ if (read < cutoff)
62
+ return spec[i].state - 10;
63
+ else
64
+ {
65
+ if (((read - spec[i].state) > hyst ) || ((spec[i].state - read) > hyst )) {
66
+ spec[i].state = read;
67
+ return spec[i].state - 10;
68
+ }
69
+ else
70
+ return spec[i].state - 10;
71
+ }
72
+ }
73
+ }
74
+ }
75
+ }
76
+
77
+
78
+
79
+ end