atduskgreg-rad 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +104 -0
- data/License.txt +282 -0
- data/Manifest.txt +142 -0
- data/README.rdoc +55 -0
- data/Rakefile +142 -0
- data/bin/hello_world_test/Makefile +436 -0
- data/bin/hello_world_test/hello_world.cpp +23 -0
- data/bin/rad +302 -0
- data/lib/examples/add_hysteresis.rb +13 -0
- data/lib/examples/basic_blink.rb +10 -0
- data/lib/examples/blink_m_address_assignment.rb +104 -0
- data/lib/examples/blink_m_hello.rb +14 -0
- data/lib/examples/blink_m_multi.rb +61 -0
- data/lib/examples/blink_with_serial.rb +16 -0
- data/lib/examples/configure_pa_lcd_boot.rb +91 -0
- data/lib/examples/debounce_methods.rb +49 -0
- data/lib/examples/external_variable_fu.rb +26 -0
- data/lib/examples/external_variables.rb +32 -0
- data/lib/examples/first_sound.rb +23 -0
- data/lib/examples/frequency_generator.rb +30 -0
- data/lib/examples/hello_array.rb +48 -0
- data/lib/examples/hello_array2.rb +79 -0
- data/lib/examples/hello_array_eeprom.rb +59 -0
- data/lib/examples/hello_clock.rb +84 -0
- data/lib/examples/hello_eeprom.rb +51 -0
- data/lib/examples/hello_eeprom_lcdpa.rb +81 -0
- data/lib/examples/hello_format_print.rb +94 -0
- data/lib/examples/hello_lcd_charset.rb +75 -0
- data/lib/examples/hello_pa_lcd.rb +59 -0
- data/lib/examples/hello_servos.rb +88 -0
- data/lib/examples/hello_spectra_sound.rb +38 -0
- data/lib/examples/hello_world.rb +11 -0
- data/lib/examples/hello_xbee.rb +12 -0
- data/lib/examples/hysteresis_duel.rb +39 -0
- data/lib/examples/i2c_with_clock_chip.rb +124 -0
- data/lib/examples/midi_beat_box.rb +86 -0
- data/lib/examples/midi_scales.rb +94 -0
- data/lib/examples/motor_knob.rb +30 -0
- data/lib/examples/servo_buttons.rb +23 -0
- data/lib/examples/servo_calibrate_continuous.rb +92 -0
- data/lib/examples/servo_throttle.rb +40 -0
- data/lib/examples/software_serial.rb +10 -0
- data/lib/examples/sparkfun_lcd.rb +48 -0
- data/lib/examples/spectra_soft_pot.rb +34 -0
- data/lib/examples/times_method.rb +8 -0
- data/lib/examples/toggle.rb +10 -0
- data/lib/examples/twitter.rb +57 -0
- data/lib/examples/two_wire.rb +14 -0
- data/lib/libraries/AFSoftSerial/AFSoftSerial.cpp +321 -0
- data/lib/libraries/AFSoftSerial/AFSoftSerial.h +61 -0
- data/lib/libraries/AFSoftSerial/keywords.txt +18 -0
- data/lib/libraries/AF_XPort/AF_XPort.cpp +166 -0
- data/lib/libraries/AF_XPort/AF_XPort.h +48 -0
- data/lib/libraries/DS1307/DS1307.cpp +162 -0
- data/lib/libraries/DS1307/DS1307.h +66 -0
- data/lib/libraries/DS1307/keywords.txt +18 -0
- data/lib/libraries/FrequencyTimer2/FrequencyTimer2.cpp +144 -0
- data/lib/libraries/FrequencyTimer2/FrequencyTimer2.h +42 -0
- data/lib/libraries/FrequencyTimer2/keywords.txt +22 -0
- data/lib/libraries/I2CEEPROM/I2CEEPROM.cpp +120 -0
- data/lib/libraries/I2CEEPROM/I2CEEPROM.h +70 -0
- data/lib/libraries/I2CEEPROM/keywords.txt +21 -0
- data/lib/libraries/LoopTimer/LoopTimer.cpp +35 -0
- data/lib/libraries/LoopTimer/LoopTimer.h +34 -0
- data/lib/libraries/LoopTimer/keywords.txt +27 -0
- data/lib/libraries/OneWire/OneWire.cpp +194 -0
- data/lib/libraries/OneWire/OneWire.h +63 -0
- data/lib/libraries/OneWire/keywords.txt +35 -0
- data/lib/libraries/OneWire/readme.txt +13 -0
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.cpp +296 -0
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.h +69 -0
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.cpp +311 -0
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.h +67 -0
- data/lib/libraries/Servo/Servo.cpp +192 -0
- data/lib/libraries/Servo/Servo.h +61 -0
- data/lib/libraries/Stepper/Stepper.cpp +220 -0
- data/lib/libraries/Stepper/Stepper.h +86 -0
- data/lib/libraries/Stepper/keywords.txt +28 -0
- data/lib/libraries/Wire/Wire.cpp +262 -0
- data/lib/libraries/Wire/Wire.h +67 -0
- data/lib/libraries/Wire/keywords.txt +31 -0
- data/lib/libraries/Wire/twi.h +57 -0
- data/lib/libraries/Wire/utility/twi.c +449 -0
- data/lib/libraries/Wire/utility/twi.h +57 -0
- data/lib/plugins/bitwise_ops.rb +54 -0
- data/lib/plugins/blink.rb +25 -0
- data/lib/plugins/blink_m.rb +356 -0
- data/lib/plugins/debounce.rb +138 -0
- data/lib/plugins/debug_output_to_lcd.rb +71 -0
- data/lib/plugins/hysteresis.rb +52 -0
- data/lib/plugins/input_output_state.rb +84 -0
- data/lib/plugins/lcd_padding.rb +58 -0
- data/lib/plugins/mem_test.rb +37 -0
- data/lib/plugins/midi.rb +60 -0
- data/lib/plugins/parallax_ping.rb +50 -0
- data/lib/plugins/servo_pulse.rb +31 -0
- data/lib/plugins/servo_setup.rb +86 -0
- data/lib/plugins/smoother.rb +54 -0
- data/lib/plugins/spark_fun_serial_lcd.rb +100 -0
- data/lib/plugins/spectra_symbol.rb +79 -0
- data/lib/plugins/twitter_connect.rb +145 -0
- data/lib/rad.rb +5 -0
- data/lib/rad/README.rdoc +5 -0
- data/lib/rad/arduino_plugin.rb +246 -0
- data/lib/rad/arduino_sketch.rb +627 -0
- data/lib/rad/generators/makefile/makefile.erb +243 -0
- data/lib/rad/generators/makefile/makefile.rb +38 -0
- data/lib/rad/hardware_library.rb +813 -0
- data/lib/rad/init.rb +14 -0
- data/lib/rad/progressbar.rb +236 -0
- data/lib/rad/rad_processor.rb +128 -0
- data/lib/rad/rad_rewriter.rb +94 -0
- data/lib/rad/rad_type_checker.rb +26 -0
- data/lib/rad/sim/arduino_sketch.rb +57 -0
- data/lib/rad/sketch_compiler.rb +47 -0
- data/lib/rad/tasks/build_and_make.rake +210 -0
- data/lib/rad/tasks/rad.rb +2 -0
- data/lib/rad/todo.txt +13 -0
- data/lib/rad/variable_processing.rb +153 -0
- data/lib/rad/version.rb +9 -0
- data/scripts/txt2html +67 -0
- data/setup.rb +1585 -0
- data/spec/examples/hello_world.rb +11 -0
- data/spec/examples/serial_motor.rb +12 -0
- data/spec/models/arduino_sketch_spec.rb +82 -0
- data/spec/models/sketch_compiler_spec.rb +96 -0
- data/spec/models/spec_helper.rb +2 -0
- data/spec/sim/hello_world_spec.rb +42 -0
- data/spec/spec.opts +1 -0
- data/test/test_array_processing.rb +179 -0
- data/test/test_plugin_loading.rb +151 -0
- data/test/test_translation_post_processing.rb +185 -0
- data/test/test_variable_processing.rb +238 -0
- data/website/examples/assembler_test.rb.html +73 -0
- data/website/examples/gps_reader.rb.html +39 -0
- data/website/examples/hello_world.rb.html +38 -0
- data/website/examples/serial_motor.rb.html +41 -0
- data/website/index.html +178 -0
- data/website/index.txt +64 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +169 -0
- data/website/template.rhtml +48 -0
- metadata +217 -0
@@ -0,0 +1,311 @@
|
|
1
|
+
/*
|
2
|
+
SWSerLCDsf.cpp - Software serial to SparkFun controller chip based
|
3
|
+
LCD display library Adapted from SoftwareSerial.cpp (c) 2006 David A. Mellis
|
4
|
+
by Brian B. Riley, Underhill Center, Vermont, USA, July 2008
|
5
|
+
|
6
|
+
This library is free software; you can redistribute it and/or
|
7
|
+
modify it under the terms of the GNU Lesser General Public
|
8
|
+
License as published by the Free Software Foundation; either
|
9
|
+
version 2.1 of the License, or (at your option) any later version.
|
10
|
+
|
11
|
+
This library is distributed in the hope that it will be useful,
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
Lesser General Public License for more details.
|
15
|
+
|
16
|
+
You should have received a copy of the GNU Lesser General Public
|
17
|
+
License along with this library; if not, write to the Free Software
|
18
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
19
|
+
*/
|
20
|
+
|
21
|
+
/******************************************************************************
|
22
|
+
* Includes
|
23
|
+
******************************************************************************/
|
24
|
+
|
25
|
+
#include "WConstants.h"
|
26
|
+
#include "SWSerLCDsf.h"
|
27
|
+
|
28
|
+
/******************************************************************************
|
29
|
+
* Definitions
|
30
|
+
******************************************************************************/
|
31
|
+
|
32
|
+
/******************************************************************************
|
33
|
+
* Constructors
|
34
|
+
******************************************************************************/
|
35
|
+
|
36
|
+
SWSerLCDsf::SWSerLCDsf(uint8_t transmitPin)
|
37
|
+
{
|
38
|
+
_transmitPin = transmitPin;
|
39
|
+
_baudRate = 0;
|
40
|
+
_rows = 2;
|
41
|
+
_cols = 16;
|
42
|
+
_geometry = 0;
|
43
|
+
}
|
44
|
+
|
45
|
+
SWSerLCDsf::SWSerLCDsf(uint8_t transmitPin, int geometry)
|
46
|
+
{
|
47
|
+
_transmitPin = transmitPin;
|
48
|
+
_baudRate = 0;
|
49
|
+
pinMode(_transmitPin, OUTPUT);
|
50
|
+
_rows = 2;
|
51
|
+
_cols = 16;
|
52
|
+
_geometry = geometry;
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
/******************************************************************************
|
57
|
+
* User API
|
58
|
+
******************************************************************************/
|
59
|
+
|
60
|
+
void SWSerLCDsf::begin(long speed)
|
61
|
+
{
|
62
|
+
_baudRate = speed;
|
63
|
+
_bitPeriod = 1000000 / _baudRate;
|
64
|
+
|
65
|
+
digitalWrite(_transmitPin, HIGH);
|
66
|
+
delayMicroseconds( _bitPeriod); // if we were low this establishes the end
|
67
|
+
delay(50);
|
68
|
+
if (_geometry)
|
69
|
+
setgeo(_geometry);
|
70
|
+
}
|
71
|
+
|
72
|
+
void SWSerLCDsf::print(uint8_t b)
|
73
|
+
{
|
74
|
+
if (_baudRate == 0)
|
75
|
+
return;
|
76
|
+
|
77
|
+
int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
|
78
|
+
byte mask;
|
79
|
+
|
80
|
+
digitalWrite(_transmitPin, LOW);
|
81
|
+
delayMicroseconds(bitDelay);
|
82
|
+
|
83
|
+
for (mask = 0x01; mask; mask <<= 1) {
|
84
|
+
if (b & mask){ // choose bit
|
85
|
+
digitalWrite(_transmitPin,HIGH); // send 1
|
86
|
+
}
|
87
|
+
else{
|
88
|
+
digitalWrite(_transmitPin,LOW); // send 1
|
89
|
+
}
|
90
|
+
delayMicroseconds(bitDelay);
|
91
|
+
}
|
92
|
+
|
93
|
+
digitalWrite(_transmitPin, HIGH);
|
94
|
+
delayMicroseconds(bitDelay);
|
95
|
+
}
|
96
|
+
|
97
|
+
void SWSerLCDsf::print(const char *s)
|
98
|
+
{
|
99
|
+
while (*s) {
|
100
|
+
print(*s++);
|
101
|
+
delay(1);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
void SWSerLCDsf::print(char c)
|
106
|
+
{
|
107
|
+
print((uint8_t) c);
|
108
|
+
}
|
109
|
+
|
110
|
+
void SWSerLCDsf::print(int n)
|
111
|
+
{
|
112
|
+
print((long) n);
|
113
|
+
}
|
114
|
+
|
115
|
+
void SWSerLCDsf::print(unsigned int n)
|
116
|
+
{
|
117
|
+
print((unsigned long) n);
|
118
|
+
}
|
119
|
+
|
120
|
+
void SWSerLCDsf::print(long n)
|
121
|
+
{
|
122
|
+
if (n < 0) {
|
123
|
+
print('-');
|
124
|
+
n = -n;
|
125
|
+
}
|
126
|
+
printNumber(n, 10);
|
127
|
+
}
|
128
|
+
|
129
|
+
void SWSerLCDsf::print(unsigned long n)
|
130
|
+
{
|
131
|
+
printNumber(n, 10);
|
132
|
+
}
|
133
|
+
|
134
|
+
void SWSerLCDsf::print(long n, int base)
|
135
|
+
{
|
136
|
+
if (base == 0)
|
137
|
+
print((char) n);
|
138
|
+
else if (base == 10)
|
139
|
+
print(n);
|
140
|
+
else
|
141
|
+
printNumber(n, base);
|
142
|
+
}
|
143
|
+
|
144
|
+
// -------- Spark Fun unique codes -------------------------
|
145
|
+
|
146
|
+
void SWSerLCDsf::clearscr(void)
|
147
|
+
{
|
148
|
+
print((uint8_t) 0xFE);
|
149
|
+
print((uint8_t) 0x01);
|
150
|
+
delay(100);
|
151
|
+
}
|
152
|
+
|
153
|
+
void SWSerLCDsf::clearscr(const char *s)
|
154
|
+
{
|
155
|
+
clearscr();
|
156
|
+
print(s);
|
157
|
+
}
|
158
|
+
|
159
|
+
void SWSerLCDsf::clearscr(int n)
|
160
|
+
{
|
161
|
+
clearscr();
|
162
|
+
print(n);
|
163
|
+
}
|
164
|
+
|
165
|
+
void SWSerLCDsf::clearscr(long n, int b)
|
166
|
+
{
|
167
|
+
clearscr();
|
168
|
+
print(n,b);
|
169
|
+
}
|
170
|
+
|
171
|
+
void SWSerLCDsf::home(void)
|
172
|
+
{
|
173
|
+
setxy(0, 0);
|
174
|
+
}
|
175
|
+
|
176
|
+
void SWSerLCDsf::home(const char *s)
|
177
|
+
{
|
178
|
+
home();
|
179
|
+
print(s);
|
180
|
+
}
|
181
|
+
|
182
|
+
void SWSerLCDsf::home(int n)
|
183
|
+
{
|
184
|
+
home();
|
185
|
+
print(n);
|
186
|
+
}
|
187
|
+
|
188
|
+
void SWSerLCDsf::home(long n, int b)
|
189
|
+
{
|
190
|
+
home();
|
191
|
+
print(n,b);
|
192
|
+
}
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
void SWSerLCDsf::setxy(byte x, byte y)
|
197
|
+
{
|
198
|
+
byte posvar;
|
199
|
+
|
200
|
+
switch (y) {
|
201
|
+
case 0:
|
202
|
+
posvar = 128 + x;
|
203
|
+
break;
|
204
|
+
case 1:
|
205
|
+
posvar = 192+ x;
|
206
|
+
break;
|
207
|
+
case 2:
|
208
|
+
posvar = ((_cols == 16) ? 144 : 148) + x;
|
209
|
+
break;
|
210
|
+
case 3:
|
211
|
+
posvar = ((_cols == 16) ? 208 : 212) + x;
|
212
|
+
break;
|
213
|
+
}
|
214
|
+
print((uint8_t) 0xFE);
|
215
|
+
print((uint8_t) posvar);
|
216
|
+
}
|
217
|
+
|
218
|
+
void SWSerLCDsf::setxy(byte x, byte y, const char *s)
|
219
|
+
{
|
220
|
+
setxy(x,y);
|
221
|
+
print(s);
|
222
|
+
}
|
223
|
+
|
224
|
+
void SWSerLCDsf::setxy(byte x, byte y, int n)
|
225
|
+
{
|
226
|
+
setxy(x,y);
|
227
|
+
print(n);
|
228
|
+
}
|
229
|
+
|
230
|
+
void SWSerLCDsf::setxy(byte x, byte y, long n, int b)
|
231
|
+
{
|
232
|
+
setxy(x,y);
|
233
|
+
print(n,b);
|
234
|
+
}
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
void SWSerLCDsf::setcmd(byte code, byte cmd)
|
239
|
+
{
|
240
|
+
print((uint8_t) code);
|
241
|
+
print((uint8_t) cmd);
|
242
|
+
}
|
243
|
+
|
244
|
+
|
245
|
+
void SWSerLCDsf::setgeo(int geometry)
|
246
|
+
{
|
247
|
+
byte rows=6, cols=4;
|
248
|
+
switch (geometry) {
|
249
|
+
case 216:
|
250
|
+
break;
|
251
|
+
case 220:
|
252
|
+
_cols = 20;
|
253
|
+
cols = 3;
|
254
|
+
break;
|
255
|
+
case 416:
|
256
|
+
rows = 5;
|
257
|
+
_rows = 4;
|
258
|
+
break;
|
259
|
+
case 420:
|
260
|
+
_rows = 4;
|
261
|
+
_cols = 20;
|
262
|
+
cols = 3;
|
263
|
+
rows = 5;
|
264
|
+
break;
|
265
|
+
default:
|
266
|
+
return;
|
267
|
+
break;
|
268
|
+
}
|
269
|
+
print((uint8_t) 0x7C);
|
270
|
+
print((uint8_t) rows);
|
271
|
+
print((uint8_t) 0x7C);
|
272
|
+
print((uint8_t) cols);
|
273
|
+
delay(200);
|
274
|
+
}
|
275
|
+
|
276
|
+
void SWSerLCDsf::setintensity(int intensity)
|
277
|
+
{
|
278
|
+
if (intensity > 29)
|
279
|
+
intensity = 29;
|
280
|
+
print((uint8_t) 0x7C);
|
281
|
+
print((uint8_t) (0x80 + intensity));
|
282
|
+
delay(100);
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
// Private Methods /////////////////////////////////////////////////////////////
|
290
|
+
|
291
|
+
void SWSerLCDsf::printNumber(unsigned long n, uint8_t base)
|
292
|
+
{
|
293
|
+
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
|
294
|
+
unsigned long i = 0;
|
295
|
+
|
296
|
+
if (n == 0) {
|
297
|
+
print('0');
|
298
|
+
return;
|
299
|
+
}
|
300
|
+
|
301
|
+
while (n > 0) {
|
302
|
+
buf[i++] = n % base;
|
303
|
+
n /= base;
|
304
|
+
}
|
305
|
+
|
306
|
+
for (; i > 0; i--)
|
307
|
+
print((char) (buf[i - 1] < 10 ? '0' + buf[i - 1] : 'A' + buf[i - 1] - 10));
|
308
|
+
|
309
|
+
delay(8);
|
310
|
+
|
311
|
+
}
|
@@ -0,0 +1,67 @@
|
|
1
|
+
/*
|
2
|
+
SWSerLCDsf.h - Software serial to SparkFun controller chip based
|
3
|
+
LCD display library Adapted from SoftwareSerial.cpp (c) 2006 David A. Mellis
|
4
|
+
by Brian B. Riley, Underhill Center, Vermont, USA, July 2008
|
5
|
+
|
6
|
+
This library is free software; you can redistribute it and/or
|
7
|
+
modify it under the terms of the GNU Lesser General Public
|
8
|
+
License as published by the Free Software Foundation; either
|
9
|
+
version 2.1 of the License, or (at your option) any later version.
|
10
|
+
|
11
|
+
This library is distributed in the hope that it will be useful,
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
Lesser General Public License for more details.
|
15
|
+
|
16
|
+
You should have received a copy of the GNU Lesser General Public
|
17
|
+
License along with this library; if not, write to the Free Software
|
18
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
19
|
+
*/
|
20
|
+
|
21
|
+
#ifndef SWSerLCDsf_h
|
22
|
+
#define SWSerLCDsf_h
|
23
|
+
|
24
|
+
#include <inttypes.h>
|
25
|
+
|
26
|
+
class SWSerLCDsf
|
27
|
+
{
|
28
|
+
private:
|
29
|
+
uint8_t _transmitPin;
|
30
|
+
long _baudRate;
|
31
|
+
int _bitPeriod;
|
32
|
+
byte _rows;
|
33
|
+
byte _cols;
|
34
|
+
int _geometry;
|
35
|
+
void printNumber(unsigned long, uint8_t);
|
36
|
+
|
37
|
+
public:
|
38
|
+
SWSerLCDsf(uint8_t);
|
39
|
+
SWSerLCDsf(uint8_t, int);
|
40
|
+
void begin(long);
|
41
|
+
void print(char);
|
42
|
+
void print(const char[]);
|
43
|
+
void print(uint8_t);
|
44
|
+
void print(int);
|
45
|
+
void print(unsigned int);
|
46
|
+
void print(long);
|
47
|
+
void print(unsigned long);
|
48
|
+
void print(long, int);
|
49
|
+
void clearscr(void);
|
50
|
+
void clearscr(const char[]);
|
51
|
+
void clearscr(int);
|
52
|
+
void clearscr(long, int);
|
53
|
+
void home(void);
|
54
|
+
void home(const char[]);
|
55
|
+
void home(int);
|
56
|
+
void home(long, int);
|
57
|
+
void setxy(byte, byte);
|
58
|
+
void setxy(byte, byte, const char[]);
|
59
|
+
void setxy(byte, byte, int);
|
60
|
+
void setxy(byte, byte, long, int);
|
61
|
+
void setgeo(int);
|
62
|
+
void setintensity(int);
|
63
|
+
void setcmd(byte, byte);
|
64
|
+
};
|
65
|
+
|
66
|
+
#endif
|
67
|
+
|
@@ -0,0 +1,192 @@
|
|
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
|
+
|
22
|
+
#include "WConstants.h"
|
23
|
+
#include <Servo.h>
|
24
|
+
|
25
|
+
Servo *Servo::first;
|
26
|
+
|
27
|
+
#define NO_ANGLE (0xff)
|
28
|
+
|
29
|
+
Servo::Servo() : pin(0),angle(NO_ANGLE),pulse0(0),min16(DEFLT_MINP),max16(DEFLT_MAXP),next(0)
|
30
|
+
{}
|
31
|
+
|
32
|
+
|
33
|
+
uint8_t Servo::attach(int pinArg)
|
34
|
+
{
|
35
|
+
pin = pinArg;
|
36
|
+
angle = NO_ANGLE;
|
37
|
+
pulse0 = 0;
|
38
|
+
next = first;
|
39
|
+
first = this;
|
40
|
+
digitalWrite(pin,0);
|
41
|
+
pinMode(pin,OUTPUT);
|
42
|
+
return 1;
|
43
|
+
}
|
44
|
+
|
45
|
+
uint8_t Servo::attach(int pinArg, int angleArg)
|
46
|
+
{
|
47
|
+
attach(pinArg);
|
48
|
+
write(angleArg);
|
49
|
+
return 1;
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
uint8_t Servo::attach(int pinArg, uint16_t minp, uint16_t maxp)
|
54
|
+
{
|
55
|
+
attach(pinArg);
|
56
|
+
min16 = minp/16;
|
57
|
+
max16 = maxp/16;
|
58
|
+
return 1;
|
59
|
+
}
|
60
|
+
|
61
|
+
uint8_t Servo::attach(int pinArg, int angleArg, uint16_t minp, uint16_t maxp)
|
62
|
+
{
|
63
|
+
attach(pinArg);
|
64
|
+
min16 = minp/16;
|
65
|
+
max16 = maxp/16;
|
66
|
+
write(angleArg);
|
67
|
+
return 1;
|
68
|
+
}
|
69
|
+
|
70
|
+
void Servo::detach()
|
71
|
+
{
|
72
|
+
for ( Servo **p = &first; *p != 0; p = &((*p)->next) ) {
|
73
|
+
if ( *p == this) {
|
74
|
+
*p = this->next;
|
75
|
+
this->next = 0;
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
void Servo::write(int angleArg)
|
82
|
+
{
|
83
|
+
if ( angleArg < 0) angleArg = 0;
|
84
|
+
if ( angleArg > 180) angleArg = 180;
|
85
|
+
angle = angleArg;
|
86
|
+
// bleh, have to use longs to prevent overflow, could be tricky if always a 16MHz clock, but not true
|
87
|
+
// That 64L on the end is the TCNT0 prescaler, it will need to change if the clock's prescaler changes,
|
88
|
+
// but then there will likely be an overflow problem, so it will have to be handled by a human.
|
89
|
+
pulse0 = (min16*16L*clockCyclesPerMicrosecond() + (max16-min16)*(16L*clockCyclesPerMicrosecond())*angle/180L)/64L;
|
90
|
+
}
|
91
|
+
|
92
|
+
void Servo::position(int angleArg)
|
93
|
+
{
|
94
|
+
write(angleArg);
|
95
|
+
}
|
96
|
+
|
97
|
+
void Servo::speed(int speedVal)
|
98
|
+
{
|
99
|
+
speedVal += 90;
|
100
|
+
write(speedVal);
|
101
|
+
}
|
102
|
+
|
103
|
+
uint8_t Servo::read()
|
104
|
+
{
|
105
|
+
return angle;
|
106
|
+
}
|
107
|
+
|
108
|
+
uint8_t Servo::attached()
|
109
|
+
{
|
110
|
+
for ( Servo *p = first; p != 0; p = p->next ) {
|
111
|
+
if ( p == this) return 1;
|
112
|
+
}
|
113
|
+
return 0;
|
114
|
+
}
|
115
|
+
|
116
|
+
/*************************************************
|
117
|
+
* refresh() - the heart of the method
|
118
|
+
*************************************************/
|
119
|
+
|
120
|
+
void Servo::refresh()
|
121
|
+
{
|
122
|
+
uint8_t count = 0, i = 0;
|
123
|
+
uint16_t base = 0;
|
124
|
+
Servo *p;
|
125
|
+
static unsigned long lastRefresh = 0;
|
126
|
+
unsigned long m = millis();
|
127
|
+
|
128
|
+
// if we haven't wrapped millis, and 20ms have not passed,
|
129
|
+
// then don't do anything
|
130
|
+
|
131
|
+
if ( m >= lastRefresh && m < lastRefresh + 20) return;
|
132
|
+
lastRefresh = m;
|
133
|
+
|
134
|
+
for ( p = first; p != 0; p = p->next ) if ( p->pulse0) count++;
|
135
|
+
|
136
|
+
if ( count == 0) return;
|
137
|
+
|
138
|
+
// gather all the servos in an array
|
139
|
+
|
140
|
+
Servo *s[count];
|
141
|
+
|
142
|
+
for ( p = first; p != 0; p = p->next ) if ( p->pulse0) s[i++] = p;
|
143
|
+
|
144
|
+
// bubblesort the servos by pulse time, ascending order
|
145
|
+
|
146
|
+
for(;;) {
|
147
|
+
uint8_t moved = 0;
|
148
|
+
for ( i = 1; i < count; i++) {
|
149
|
+
if ( s[i]->pulse0 < s[i-1]->pulse0) {
|
150
|
+
Servo *t = s[i];
|
151
|
+
s[i] = s[i-1];
|
152
|
+
s[i-1] = t;
|
153
|
+
moved = 1;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
if ( !moved) break;
|
157
|
+
}
|
158
|
+
|
159
|
+
/*********************************************************************
|
160
|
+
* Turn on all the pins
|
161
|
+
* Note the timing error here... when you have many servos going, the
|
162
|
+
* ones at the front will get a pulse that is a few microseconds too long.
|
163
|
+
* Figure about 4uS/servo after them. This could be compensated, but I feel
|
164
|
+
* it is within the margin of error of software servos that could catch
|
165
|
+
* an extra interrupt handler at any time.
|
166
|
+
********************************************************************/
|
167
|
+
|
168
|
+
for ( i = 0; i < count; i++) digitalWrite( s[i]->pin, 1);
|
169
|
+
|
170
|
+
uint8_t start = TCNT0;
|
171
|
+
uint8_t now = start;
|
172
|
+
uint8_t last = now;
|
173
|
+
|
174
|
+
// Now wait for each pin's time in turn..
|
175
|
+
|
176
|
+
for ( i = 0; i < count; i++) {
|
177
|
+
uint16_t go = start + s[i]->pulse0;
|
178
|
+
|
179
|
+
// loop until we reach or pass 'go' time
|
180
|
+
|
181
|
+
for (;;) {
|
182
|
+
now = TCNT0;
|
183
|
+
if ( now < last) base += 256;
|
184
|
+
last = now;
|
185
|
+
|
186
|
+
if ( base+now > go) {
|
187
|
+
digitalWrite( s[i]->pin,0);
|
188
|
+
break;
|
189
|
+
}
|
190
|
+
}
|
191
|
+
}
|
192
|
+
}
|