atduskgreg-rad 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/History.txt +104 -0
  2. data/License.txt +282 -0
  3. data/Manifest.txt +142 -0
  4. data/README.rdoc +55 -0
  5. data/Rakefile +142 -0
  6. data/bin/hello_world_test/Makefile +436 -0
  7. data/bin/hello_world_test/hello_world.cpp +23 -0
  8. data/bin/rad +302 -0
  9. data/lib/examples/add_hysteresis.rb +13 -0
  10. data/lib/examples/basic_blink.rb +10 -0
  11. data/lib/examples/blink_m_address_assignment.rb +104 -0
  12. data/lib/examples/blink_m_hello.rb +14 -0
  13. data/lib/examples/blink_m_multi.rb +61 -0
  14. data/lib/examples/blink_with_serial.rb +16 -0
  15. data/lib/examples/configure_pa_lcd_boot.rb +91 -0
  16. data/lib/examples/debounce_methods.rb +49 -0
  17. data/lib/examples/external_variable_fu.rb +26 -0
  18. data/lib/examples/external_variables.rb +32 -0
  19. data/lib/examples/first_sound.rb +23 -0
  20. data/lib/examples/frequency_generator.rb +30 -0
  21. data/lib/examples/hello_array.rb +48 -0
  22. data/lib/examples/hello_array2.rb +79 -0
  23. data/lib/examples/hello_array_eeprom.rb +59 -0
  24. data/lib/examples/hello_clock.rb +84 -0
  25. data/lib/examples/hello_eeprom.rb +51 -0
  26. data/lib/examples/hello_eeprom_lcdpa.rb +81 -0
  27. data/lib/examples/hello_format_print.rb +94 -0
  28. data/lib/examples/hello_lcd_charset.rb +75 -0
  29. data/lib/examples/hello_pa_lcd.rb +59 -0
  30. data/lib/examples/hello_servos.rb +88 -0
  31. data/lib/examples/hello_spectra_sound.rb +38 -0
  32. data/lib/examples/hello_world.rb +11 -0
  33. data/lib/examples/hello_xbee.rb +12 -0
  34. data/lib/examples/hysteresis_duel.rb +39 -0
  35. data/lib/examples/i2c_with_clock_chip.rb +124 -0
  36. data/lib/examples/midi_beat_box.rb +86 -0
  37. data/lib/examples/midi_scales.rb +94 -0
  38. data/lib/examples/motor_knob.rb +30 -0
  39. data/lib/examples/servo_buttons.rb +23 -0
  40. data/lib/examples/servo_calibrate_continuous.rb +92 -0
  41. data/lib/examples/servo_throttle.rb +40 -0
  42. data/lib/examples/software_serial.rb +10 -0
  43. data/lib/examples/sparkfun_lcd.rb +48 -0
  44. data/lib/examples/spectra_soft_pot.rb +34 -0
  45. data/lib/examples/times_method.rb +8 -0
  46. data/lib/examples/toggle.rb +10 -0
  47. data/lib/examples/twitter.rb +57 -0
  48. data/lib/examples/two_wire.rb +14 -0
  49. data/lib/libraries/AFSoftSerial/AFSoftSerial.cpp +321 -0
  50. data/lib/libraries/AFSoftSerial/AFSoftSerial.h +61 -0
  51. data/lib/libraries/AFSoftSerial/keywords.txt +18 -0
  52. data/lib/libraries/AF_XPort/AF_XPort.cpp +166 -0
  53. data/lib/libraries/AF_XPort/AF_XPort.h +48 -0
  54. data/lib/libraries/DS1307/DS1307.cpp +162 -0
  55. data/lib/libraries/DS1307/DS1307.h +66 -0
  56. data/lib/libraries/DS1307/keywords.txt +18 -0
  57. data/lib/libraries/FrequencyTimer2/FrequencyTimer2.cpp +144 -0
  58. data/lib/libraries/FrequencyTimer2/FrequencyTimer2.h +42 -0
  59. data/lib/libraries/FrequencyTimer2/keywords.txt +22 -0
  60. data/lib/libraries/I2CEEPROM/I2CEEPROM.cpp +120 -0
  61. data/lib/libraries/I2CEEPROM/I2CEEPROM.h +70 -0
  62. data/lib/libraries/I2CEEPROM/keywords.txt +21 -0
  63. data/lib/libraries/LoopTimer/LoopTimer.cpp +35 -0
  64. data/lib/libraries/LoopTimer/LoopTimer.h +34 -0
  65. data/lib/libraries/LoopTimer/keywords.txt +27 -0
  66. data/lib/libraries/OneWire/OneWire.cpp +194 -0
  67. data/lib/libraries/OneWire/OneWire.h +63 -0
  68. data/lib/libraries/OneWire/keywords.txt +35 -0
  69. data/lib/libraries/OneWire/readme.txt +13 -0
  70. data/lib/libraries/SWSerLCDpa/SWSerLCDpa.cpp +296 -0
  71. data/lib/libraries/SWSerLCDpa/SWSerLCDpa.h +69 -0
  72. data/lib/libraries/SWSerLCDsf/SWSerLCDsf.cpp +311 -0
  73. data/lib/libraries/SWSerLCDsf/SWSerLCDsf.h +67 -0
  74. data/lib/libraries/Servo/Servo.cpp +192 -0
  75. data/lib/libraries/Servo/Servo.h +61 -0
  76. data/lib/libraries/Stepper/Stepper.cpp +220 -0
  77. data/lib/libraries/Stepper/Stepper.h +86 -0
  78. data/lib/libraries/Stepper/keywords.txt +28 -0
  79. data/lib/libraries/Wire/Wire.cpp +262 -0
  80. data/lib/libraries/Wire/Wire.h +67 -0
  81. data/lib/libraries/Wire/keywords.txt +31 -0
  82. data/lib/libraries/Wire/twi.h +57 -0
  83. data/lib/libraries/Wire/utility/twi.c +449 -0
  84. data/lib/libraries/Wire/utility/twi.h +57 -0
  85. data/lib/plugins/bitwise_ops.rb +54 -0
  86. data/lib/plugins/blink.rb +25 -0
  87. data/lib/plugins/blink_m.rb +356 -0
  88. data/lib/plugins/debounce.rb +138 -0
  89. data/lib/plugins/debug_output_to_lcd.rb +71 -0
  90. data/lib/plugins/hysteresis.rb +52 -0
  91. data/lib/plugins/input_output_state.rb +84 -0
  92. data/lib/plugins/lcd_padding.rb +58 -0
  93. data/lib/plugins/mem_test.rb +37 -0
  94. data/lib/plugins/midi.rb +60 -0
  95. data/lib/plugins/parallax_ping.rb +50 -0
  96. data/lib/plugins/servo_pulse.rb +31 -0
  97. data/lib/plugins/servo_setup.rb +86 -0
  98. data/lib/plugins/smoother.rb +54 -0
  99. data/lib/plugins/spark_fun_serial_lcd.rb +100 -0
  100. data/lib/plugins/spectra_symbol.rb +79 -0
  101. data/lib/plugins/twitter_connect.rb +145 -0
  102. data/lib/rad.rb +5 -0
  103. data/lib/rad/README.rdoc +5 -0
  104. data/lib/rad/arduino_plugin.rb +246 -0
  105. data/lib/rad/arduino_sketch.rb +627 -0
  106. data/lib/rad/generators/makefile/makefile.erb +243 -0
  107. data/lib/rad/generators/makefile/makefile.rb +38 -0
  108. data/lib/rad/hardware_library.rb +813 -0
  109. data/lib/rad/init.rb +14 -0
  110. data/lib/rad/progressbar.rb +236 -0
  111. data/lib/rad/rad_processor.rb +128 -0
  112. data/lib/rad/rad_rewriter.rb +94 -0
  113. data/lib/rad/rad_type_checker.rb +26 -0
  114. data/lib/rad/sim/arduino_sketch.rb +57 -0
  115. data/lib/rad/sketch_compiler.rb +47 -0
  116. data/lib/rad/tasks/build_and_make.rake +210 -0
  117. data/lib/rad/tasks/rad.rb +2 -0
  118. data/lib/rad/todo.txt +13 -0
  119. data/lib/rad/variable_processing.rb +153 -0
  120. data/lib/rad/version.rb +9 -0
  121. data/scripts/txt2html +67 -0
  122. data/setup.rb +1585 -0
  123. data/spec/examples/hello_world.rb +11 -0
  124. data/spec/examples/serial_motor.rb +12 -0
  125. data/spec/models/arduino_sketch_spec.rb +82 -0
  126. data/spec/models/sketch_compiler_spec.rb +96 -0
  127. data/spec/models/spec_helper.rb +2 -0
  128. data/spec/sim/hello_world_spec.rb +42 -0
  129. data/spec/spec.opts +1 -0
  130. data/test/test_array_processing.rb +179 -0
  131. data/test/test_plugin_loading.rb +151 -0
  132. data/test/test_translation_post_processing.rb +185 -0
  133. data/test/test_variable_processing.rb +238 -0
  134. data/website/examples/assembler_test.rb.html +73 -0
  135. data/website/examples/gps_reader.rb.html +39 -0
  136. data/website/examples/hello_world.rb.html +38 -0
  137. data/website/examples/serial_motor.rb.html +41 -0
  138. data/website/index.html +178 -0
  139. data/website/index.txt +64 -0
  140. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  141. data/website/stylesheets/screen.css +169 -0
  142. data/website/template.rhtml +48 -0
  143. metadata +217 -0
@@ -0,0 +1,61 @@
1
+ /*
2
+ Servo.h - Arduino Software Servo Library
3
+ Author: ????
4
+ Modified: Brian Riley <brianbr@wulfden.org> Jun/Jul 2008
5
+ Copyright (c) 2007 David A. Mellis. All right reserved.
6
+
7
+ This library is free software; you can redistribute it and/or
8
+ modify it under the terms of the GNU Lesser General Public
9
+ License as published by the Free Software Foundation; either
10
+ version 2.1 of the License, or (at your option) any later version.
11
+
12
+ This library is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ Lesser General Public License for more details.
16
+
17
+ You should have received a copy of the GNU Lesser General Public
18
+ License along with this library; if not, write to the Free Software
19
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+ #ifndef SERVO_IS_IN
22
+ #define SERVO_IS_IN
23
+
24
+ #include <inttypes.h>
25
+ #include <wiring.h>
26
+
27
+ #define DEFLT_MINP 34
28
+ #define DEFLT_MAXP 150
29
+
30
+ class Servo
31
+ {
32
+ private:
33
+ uint8_t pin;
34
+ uint8_t angle; // in degrees
35
+ uint16_t pulse0; // pulse width in TCNT0 counts
36
+ uint8_t min16; // minimum pulse, 16uS units (default is DEFLT_MINP)
37
+ uint8_t max16; // maximum pulse, 16uS units, 0-4ms range (default is DEFLT_MAXP)
38
+ class Servo *next;
39
+ static Servo* first;
40
+ void write(int); // specify the angle in degrees, 0 to 180
41
+ public:
42
+ Servo();
43
+ uint8_t attach(int); // attach to a pin, sets pinMode, returns 0 on failure, won't
44
+ // position the servo until a subsequent write() happens
45
+ uint8_t attach(int,int);
46
+ // same, except position is also is specified
47
+ uint8_t attach(int,uint16_t,uint16_t);
48
+ // same, except min/max pulse is specified
49
+ uint8_t attach(int,int,uint16_t,uint16_t);
50
+ // same, except position and min/max pulse is specified
51
+ void detach();
52
+ void position(int); // enter an angle from 0 to 180
53
+ void speed(int); // enter a speed from -90 to +90
54
+ uint8_t read();
55
+ uint8_t attached();
56
+ static void refresh(); // must be called at least every 50ms or so to keep servo alive
57
+ // you can call more often, it won't happen more than once
58
+ // every 20ms
59
+ };
60
+
61
+ #endif
@@ -0,0 +1,220 @@
1
+ /*
2
+ Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
3
+
4
+ Original library (0.1) by Tom Igoe.
5
+ Two-wire modifications (0.2) by Sebastian Gassner
6
+ Combination version (0.3) by Tom Igoe and David Mellis
7
+ Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
8
+
9
+ Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
10
+
11
+ When wiring multiple stepper motors to a microcontroller,
12
+ you quickly run out of output pins, with each motor requiring 4 connections.
13
+
14
+ By making use of the fact that at any time two of the four motor
15
+ coils are the inverse of the other two, the number of
16
+ control connections can be reduced from 4 to 2.
17
+
18
+ A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
19
+ connects to only 2 microcontroler pins, inverts the signals received,
20
+ and delivers the 4 (2 plus 2 inverted ones) output signals required
21
+ for driving a stepper motor.
22
+
23
+ The sequence of control signals for 4 control wires is as follows:
24
+
25
+ Step C0 C1 C2 C3
26
+ 1 1 0 1 0
27
+ 2 0 1 1 0
28
+ 3 0 1 0 1
29
+ 4 1 0 0 1
30
+
31
+ The sequence of controls signals for 2 control wires is as follows
32
+ (columns C1 and C2 from above):
33
+
34
+ Step C0 C1
35
+ 1 0 1
36
+ 2 1 1
37
+ 3 1 0
38
+ 4 0 0
39
+
40
+ The circuits can be found at
41
+
42
+ http://www.arduino.cc/en/Tutorial/Stepper
43
+
44
+
45
+ */
46
+
47
+
48
+ #include "WProgram.h"
49
+ #include "Stepper.h"
50
+
51
+ /*
52
+ * two-wire constructor.
53
+ * Sets which wires should control the motor.
54
+ */
55
+ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
56
+ {
57
+ this->step_number = 0; // which step the motor is on
58
+ this->speed = 0; // the motor speed, in revolutions per minute
59
+ this->direction = 0; // motor direction
60
+ this->last_step_time = 0; // time stamp in ms of the last step taken
61
+ this->number_of_steps = number_of_steps; // total number of steps for this motor
62
+
63
+ // Arduino pins for the motor control connection:
64
+ this->motor_pin_1 = motor_pin_1;
65
+ this->motor_pin_2 = motor_pin_2;
66
+
67
+ // setup the pins on the microcontroller:
68
+ pinMode(this->motor_pin_1, OUTPUT);
69
+ pinMode(this->motor_pin_2, OUTPUT);
70
+
71
+ // When there are only 2 pins, set the other two to 0:
72
+ this->motor_pin_3 = 0;
73
+ this->motor_pin_4 = 0;
74
+
75
+ // pin_count is used by the stepMotor() method:
76
+ this->pin_count = 2;
77
+ }
78
+
79
+
80
+ /*
81
+ * constructor for four-pin version
82
+ * Sets which wires should control the motor.
83
+ */
84
+
85
+ Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
86
+ {
87
+ this->step_number = 0; // which step the motor is on
88
+ this->speed = 0; // the motor speed, in revolutions per minute
89
+ this->direction = 0; // motor direction
90
+ this->last_step_time = 0; // time stamp in ms of the last step taken
91
+ this->number_of_steps = number_of_steps; // total number of steps for this motor
92
+
93
+ // Arduino pins for the motor control connection:
94
+ this->motor_pin_1 = motor_pin_1;
95
+ this->motor_pin_2 = motor_pin_2;
96
+ this->motor_pin_3 = motor_pin_3;
97
+ this->motor_pin_4 = motor_pin_4;
98
+
99
+ // setup the pins on the microcontroller:
100
+ pinMode(this->motor_pin_1, OUTPUT);
101
+ pinMode(this->motor_pin_2, OUTPUT);
102
+ pinMode(this->motor_pin_3, OUTPUT);
103
+ pinMode(this->motor_pin_4, OUTPUT);
104
+
105
+ // pin_count is used by the stepMotor() method:
106
+ this->pin_count = 4;
107
+ }
108
+
109
+ /*
110
+ Sets the speed in revs per minute
111
+
112
+ */
113
+ void Stepper::set_speed(long whatSpeed)
114
+ {
115
+ this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
116
+ }
117
+
118
+ /*
119
+ Moves the motor steps_to_move steps. If the number is negative,
120
+ the motor moves in the reverse direction.
121
+ */
122
+ void Stepper::set_steps(int steps_to_move)
123
+ {
124
+ int steps_left = abs(steps_to_move); // how many steps to take
125
+
126
+ // determine direction based on whether steps_to_mode is + or -:
127
+ if (steps_to_move > 0) {this->direction = 1;}
128
+ if (steps_to_move < 0) {this->direction = 0;}
129
+
130
+
131
+ // decrement the number of steps, moving one step each time:
132
+ while(steps_left > 0) {
133
+ // move only if the appropriate delay has passed:
134
+ if (millis() - this->last_step_time >= this->step_delay) {
135
+ // step the motor to step number 0, 1, 2, or 3:
136
+ stepMotor(this->step_number % 4);
137
+ // get the timeStamp of when you stepped:
138
+ this->last_step_time = millis();
139
+ // increment or decrement the step number,
140
+ // depending on direction:
141
+ if (this->direction == 1) {
142
+ this->step_number++;
143
+ if (this->step_number == this->number_of_steps) {
144
+ this->step_number = 0;
145
+ }
146
+ }
147
+ else {
148
+ if (this->step_number == 0) {
149
+ this->step_number = this->number_of_steps;
150
+ }
151
+ this->step_number--;
152
+ }
153
+ // decrement the steps left:
154
+ steps_left--;
155
+ }
156
+ }
157
+ }
158
+
159
+ /*
160
+ * Moves the motor forward or backwards.
161
+ */
162
+ void Stepper::stepMotor(int thisStep)
163
+ {
164
+ if (this->pin_count == 2) {
165
+ switch (thisStep) {
166
+ case 0: /* 01 */
167
+ digitalWrite(motor_pin_1, LOW);
168
+ digitalWrite(motor_pin_2, HIGH);
169
+ break;
170
+ case 1: /* 11 */
171
+ digitalWrite(motor_pin_1, HIGH);
172
+ digitalWrite(motor_pin_2, HIGH);
173
+ break;
174
+ case 2: /* 10 */
175
+ digitalWrite(motor_pin_1, HIGH);
176
+ digitalWrite(motor_pin_2, LOW);
177
+ break;
178
+ case 3: /* 00 */
179
+ digitalWrite(motor_pin_1, LOW);
180
+ digitalWrite(motor_pin_2, LOW);
181
+ break;
182
+ }
183
+ }
184
+ if (this->pin_count == 4) {
185
+ switch (thisStep) {
186
+ case 0: // 1010
187
+ digitalWrite(motor_pin_1, HIGH);
188
+ digitalWrite(motor_pin_2, LOW);
189
+ digitalWrite(motor_pin_3, HIGH);
190
+ digitalWrite(motor_pin_4, LOW);
191
+ break;
192
+ case 1: // 0110
193
+ digitalWrite(motor_pin_1, LOW);
194
+ digitalWrite(motor_pin_2, HIGH);
195
+ digitalWrite(motor_pin_3, HIGH);
196
+ digitalWrite(motor_pin_4, LOW);
197
+ break;
198
+ case 2: //0101
199
+ digitalWrite(motor_pin_1, LOW);
200
+ digitalWrite(motor_pin_2, HIGH);
201
+ digitalWrite(motor_pin_3, LOW);
202
+ digitalWrite(motor_pin_4, HIGH);
203
+ break;
204
+ case 3: //1001
205
+ digitalWrite(motor_pin_1, HIGH);
206
+ digitalWrite(motor_pin_2, LOW);
207
+ digitalWrite(motor_pin_3, LOW);
208
+ digitalWrite(motor_pin_4, HIGH);
209
+ break;
210
+ }
211
+ }
212
+ }
213
+
214
+ /*
215
+ version() returns the version of the library:
216
+ */
217
+ int Stepper::version(void)
218
+ {
219
+ return 4;
220
+ }
@@ -0,0 +1,86 @@
1
+ /*
2
+ Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4
3
+
4
+ Original library (0.1) by Tom Igoe.
5
+ Two-wire modifications (0.2) by Sebastian Gassner
6
+ Combination version (0.3) by Tom Igoe and David Mellis
7
+ Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
8
+
9
+ Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
10
+
11
+ When wiring multiple stepper motors to a microcontroller,
12
+ you quickly run out of output pins, with each motor requiring 4 connections.
13
+
14
+ By making use of the fact that at any time two of the four motor
15
+ coils are the inverse of the other two, the number of
16
+ control connections can be reduced from 4 to 2.
17
+
18
+ A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
19
+ connects to only 2 microcontroler pins, inverts the signals received,
20
+ and delivers the 4 (2 plus 2 inverted ones) output signals required
21
+ for driving a stepper motor.
22
+
23
+ The sequence of control signals for 4 control wires is as follows:
24
+
25
+ Step C0 C1 C2 C3
26
+ 1 1 0 1 0
27
+ 2 0 1 1 0
28
+ 3 0 1 0 1
29
+ 4 1 0 0 1
30
+
31
+ The sequence of controls signals for 2 control wires is as follows
32
+ (columns C1 and C2 from above):
33
+
34
+ Step C0 C1
35
+ 1 0 1
36
+ 2 1 1
37
+ 3 1 0
38
+ 4 0 0
39
+
40
+ The circuits can be found at
41
+ http://www.arduino.cc/en/Tutorial/Stepper
42
+ */
43
+
44
+ // ensure this library description is only included once
45
+ #ifndef Stepper_h
46
+ #define Stepper_h
47
+
48
+ // include types & constants of Wiring core API
49
+ #include "WConstants.h"
50
+
51
+ // library interface description
52
+ class Stepper {
53
+ public:
54
+ // constructors:
55
+ Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);
56
+ Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4);
57
+
58
+ // speed setter method:
59
+ void set_speed(long whatSpeed);
60
+
61
+ // mover method:
62
+ void set_steps(int number_of_steps);
63
+
64
+ int version(void);
65
+
66
+ private:
67
+ void stepMotor(int this_step);
68
+
69
+ int direction; // Direction of rotation
70
+ int speed; // Speed in RPMs
71
+ unsigned long step_delay; // delay between steps, in ms, based on speed
72
+ int number_of_steps; // total number of steps this motor can take
73
+ int pin_count; // whether you're driving the motor with 2 or 4 pins
74
+ int step_number; // which step the motor is on
75
+
76
+ // motor pin numbers:
77
+ int motor_pin_1;
78
+ int motor_pin_2;
79
+ int motor_pin_3;
80
+ int motor_pin_4;
81
+
82
+ long last_step_time; // time stamp in ms of when the last step was taken
83
+ };
84
+
85
+ #endif
86
+
@@ -0,0 +1,28 @@
1
+ #######################################
2
+ # Syntax Coloring Map For Test
3
+ #######################################
4
+
5
+ #######################################
6
+ # Datatypes (KEYWORD1)
7
+ #######################################
8
+
9
+ Stepper KEYWORD1
10
+
11
+ #######################################
12
+ # Methods and Functions (KEYWORD2)
13
+ #######################################
14
+
15
+ step KEYWORD2
16
+ setSpeed KEYWORD2
17
+ version KEYWORD2
18
+
19
+ ######################################
20
+ # Instances (KEYWORD2)
21
+ #######################################
22
+ direction KEYWORD2
23
+ speed KEYWORD2
24
+
25
+
26
+ #######################################
27
+ # Constants (LITERAL1)
28
+ #######################################
@@ -0,0 +1,262 @@
1
+ /*
2
+ TwoWire.cpp - 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
+ extern "C" {
21
+ #include <stdlib.h>
22
+ #include <string.h>
23
+ #include <inttypes.h>
24
+ #include "twi.h"
25
+ }
26
+
27
+ #include "Wire.h"
28
+
29
+ // Initialize Class Variables //////////////////////////////////////////////////
30
+
31
+ uint8_t* TwoWire::rxBuffer = 0;
32
+ uint8_t TwoWire::rxBufferIndex = 0;
33
+ uint8_t TwoWire::rxBufferLength = 0;
34
+
35
+ uint8_t TwoWire::txAddress = 0;
36
+ uint8_t* TwoWire::txBuffer = 0;
37
+ uint8_t TwoWire::txBufferIndex = 0;
38
+ uint8_t TwoWire::txBufferLength = 0;
39
+
40
+ uint8_t TwoWire::transmitting = 0;
41
+ void (*TwoWire::user_onRequest)(void);
42
+ void (*TwoWire::user_onReceive)(int);
43
+
44
+ // Constructors ////////////////////////////////////////////////////////////////
45
+
46
+ TwoWire::TwoWire()
47
+ {
48
+ }
49
+
50
+ // Public Methods //////////////////////////////////////////////////////////////
51
+
52
+ void TwoWire::begin(void)
53
+ {
54
+ // init buffer for reads
55
+ rxBuffer = (uint8_t*) calloc(BUFFER_LENGTH, sizeof(uint8_t));
56
+ rxBufferIndex = 0;
57
+ rxBufferLength = 0;
58
+
59
+ // init buffer for writes
60
+ txBuffer = (uint8_t*) calloc(BUFFER_LENGTH, sizeof(uint8_t));
61
+ txBufferIndex = 0;
62
+ txBufferLength = 0;
63
+
64
+ twi_init();
65
+ }
66
+
67
+ void TwoWire::begin(uint8_t address)
68
+ {
69
+ twi_setAddress(address);
70
+ twi_attachSlaveTxEvent(onRequestService);
71
+ twi_attachSlaveRxEvent(onReceiveService);
72
+ begin();
73
+ }
74
+
75
+ void TwoWire::begin(int address)
76
+ {
77
+ begin((uint8_t)address);
78
+ }
79
+
80
+ void TwoWire::requestFrom(uint8_t address, uint8_t quantity)
81
+ {
82
+ // clamp to buffer length
83
+ if(quantity > BUFFER_LENGTH){
84
+ quantity = BUFFER_LENGTH;
85
+ }
86
+ // perform blocking read into buffer
87
+ twi_readFrom(address, rxBuffer, quantity);
88
+ // set rx buffer iterator vars
89
+ rxBufferIndex = 0;
90
+ rxBufferLength = quantity;
91
+ }
92
+
93
+ void TwoWire::requestFrom(int address, int quantity)
94
+ {
95
+ requestFrom((uint8_t)address, (uint8_t)quantity);
96
+ }
97
+
98
+ void TwoWire::beginTransmission(uint8_t address)
99
+ {
100
+ // indicate that we are transmitting
101
+ transmitting = 1;
102
+ // set address of targeted slave
103
+ txAddress = address;
104
+ // reset tx buffer iterator vars
105
+ txBufferIndex = 0;
106
+ txBufferLength = 0;
107
+ }
108
+
109
+ void TwoWire::beginTransmission(int address)
110
+ {
111
+ beginTransmission((uint8_t)address);
112
+ }
113
+
114
+ void TwoWire::endTransmission(void)
115
+ {
116
+ // transmit buffer (blocking)
117
+ twi_writeTo(txAddress, txBuffer, txBufferLength, 1);
118
+ // reset tx buffer iterator vars
119
+ txBufferIndex = 0;
120
+ txBufferLength = 0;
121
+ // indicate that we are done transmitting
122
+ transmitting = 0;
123
+ }
124
+
125
+ // must be called in:
126
+ // slave tx event callback
127
+ // or after beginTransmission(address)
128
+ void TwoWire::send(uint8_t data)
129
+ {
130
+ if(transmitting){
131
+ // in master transmitter mode
132
+ // don't bother if buffer is full
133
+ if(txBufferLength >= BUFFER_LENGTH){
134
+ return;
135
+ }
136
+ // put byte in tx buffer
137
+ txBuffer[txBufferIndex] = data;
138
+ ++txBufferIndex;
139
+ // update amount in buffer
140
+ txBufferLength = txBufferIndex;
141
+ }else{
142
+ // in slave send mode
143
+ // reply to master
144
+ twi_transmit(&data, 1);
145
+ }
146
+ }
147
+
148
+ // must be called in:
149
+ // slave tx event callback
150
+ // or after beginTransmission(address)
151
+ void TwoWire::send(uint8_t* data, uint8_t quantity)
152
+ {
153
+ if(transmitting){
154
+ // in master transmitter mode
155
+ for(uint8_t i = 0; i < quantity; ++i){
156
+ send(data[i]);
157
+ }
158
+ }else{
159
+ // in slave send mode
160
+ // reply to master
161
+ twi_transmit(data, quantity);
162
+ }
163
+ }
164
+
165
+ // must be called in:
166
+ // slave tx event callback
167
+ // or after beginTransmission(address)
168
+ void TwoWire::send(char* data)
169
+ {
170
+ send((uint8_t*)data, strlen(data));
171
+ }
172
+
173
+ // must be called in:
174
+ // slave tx event callback
175
+ // or after beginTransmission(address)
176
+ void TwoWire::send(int data)
177
+ {
178
+ send((uint8_t)data);
179
+ }
180
+
181
+ // must be called in:
182
+ // slave rx event callback
183
+ // or after requestFrom(address, numBytes)
184
+ uint8_t TwoWire::available(void)
185
+ {
186
+ return rxBufferLength - rxBufferIndex;
187
+ }
188
+
189
+ // must be called in:
190
+ // slave rx event callback
191
+ // or after requestFrom(address, numBytes)
192
+ uint8_t TwoWire::receive(void)
193
+ {
194
+ // default to returning null char
195
+ // for people using with char strings
196
+ uint8_t value = '\0';
197
+
198
+ // get each successive byte on each call
199
+ if(rxBufferIndex < rxBufferLength){
200
+ value = rxBuffer[rxBufferIndex];
201
+ ++rxBufferIndex;
202
+ }
203
+
204
+ return value;
205
+ }
206
+
207
+ // behind the scenes function that is called when data is received
208
+ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
209
+ {
210
+ // don't bother if user hasn't registered a callback
211
+ if(!user_onReceive){
212
+ return;
213
+ }
214
+ // don't bother if rx buffer is in use by a master requestFrom() op
215
+ // i know this drops data, but it allows for slight stupidity
216
+ // meaning, they may not have read all the master requestFrom() data yet
217
+ if(rxBufferIndex < rxBufferLength){
218
+ return;
219
+ }
220
+ // copy twi rx buffer into local read buffer
221
+ // this enables new reads to happen in parallel
222
+ for(uint8_t i = 0; i < numBytes; ++i){
223
+ rxBuffer[i] = inBytes[i];
224
+ }
225
+ // set rx iterator vars
226
+ rxBufferIndex = 0;
227
+ rxBufferLength = numBytes;
228
+ // alert user program
229
+ user_onReceive(numBytes);
230
+ }
231
+
232
+ // behind the scenes function that is called when data is requested
233
+ void TwoWire::onRequestService(void)
234
+ {
235
+ // don't bother if user hasn't registered a callback
236
+ if(!user_onRequest){
237
+ return;
238
+ }
239
+ // reset tx buffer iterator vars
240
+ // !!! this will kill any pending pre-master sendTo() activity
241
+ txBufferIndex = 0;
242
+ txBufferLength = 0;
243
+ // alert user program
244
+ user_onRequest();
245
+ }
246
+
247
+ // sets function called on slave write
248
+ void TwoWire::onReceive( void (*function)(int) )
249
+ {
250
+ user_onReceive = function;
251
+ }
252
+
253
+ // sets function called on slave read
254
+ void TwoWire::onRequest( void (*function)(void) )
255
+ {
256
+ user_onRequest = function;
257
+ }
258
+
259
+ // Preinstantiate Objects //////////////////////////////////////////////////////
260
+
261
+ TwoWire Wire = TwoWire();
262
+