dino 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+
3
+ require File.expand_path(File.join('../..', 'lib/dino'), __FILE__)
@@ -0,0 +1,251 @@
1
+ #include <Servo.h>
2
+
3
+ bool debug = false;
4
+
5
+ int index = 0;
6
+
7
+ char messageBuffer[12];
8
+ char cmd[3];
9
+ char pin[3];
10
+ char val[4];
11
+ char aux[4];
12
+
13
+ Servo servo;
14
+
15
+ void setup() {
16
+ Serial.begin(115200);
17
+ }
18
+
19
+ void loop() {
20
+ while(Serial.available() > 0) {
21
+ char x = Serial.read();
22
+ if (x == '!') index = 0; // start
23
+ else if (x == '.') process(); // end
24
+ else messageBuffer[index++] = x;
25
+ }
26
+ }
27
+
28
+ /*
29
+ * Deal with a full message and determine function to call
30
+ */
31
+ void process() {
32
+ index = 0;
33
+
34
+ strncpy(cmd, messageBuffer, 2);
35
+ cmd[2] = '\0';
36
+ strncpy(pin, messageBuffer + 2, 2);
37
+ pin[2] = '\0';
38
+
39
+ if (atoi(cmd) > 90) {
40
+ strncpy(val, messageBuffer + 4, 2);
41
+ val[2] = '\0';
42
+ strncpy(aux, messageBuffer + 6, 3);
43
+ aux[3] = '\0';
44
+ } else {
45
+ strncpy(val, messageBuffer + 4, 3);
46
+ val[4] = '\0';
47
+ strncpy(aux, messageBuffer + 7, 3);
48
+ aux[4] = '\0';
49
+ }
50
+
51
+ if (debug) {
52
+ Serial.println(messageBuffer);
53
+ }
54
+ int cmdid = atoi(cmd);
55
+
56
+ // Serial.println(cmd);
57
+ // Serial.println(pin);
58
+ // Serial.println(val);
59
+ // Serial.println(aux);
60
+
61
+ switch(cmdid) {
62
+ case 0: sm(pin,val); break;
63
+ case 1: dw(pin,val); break;
64
+ case 2: dr(pin,val); break;
65
+ case 3: aw(pin,val); break;
66
+ case 4: ar(pin,val); break;
67
+ case 97: handlePing(pin,val,aux); break;
68
+ case 98: handleServo(pin,val,aux); break;
69
+ case 99: toggleDebug(val); break;
70
+ default: break;
71
+ }
72
+ }
73
+
74
+ /*
75
+ * Toggle debug mode
76
+ */
77
+ void toggleDebug(char *val) {
78
+ if (atoi(val) == 0) {
79
+ debug = false;
80
+ Serial.println("goodbye");
81
+ } else {
82
+ debug = true;
83
+ Serial.println("hello");
84
+ }
85
+ }
86
+
87
+ /*
88
+ * Set pin mode
89
+ */
90
+ void sm(char *pin, char *val) {
91
+ if (debug) Serial.println("sm");
92
+ int p = getPin(pin);
93
+ if(p == -1) { if(debug) Serial.println("badpin"); return; }
94
+ if (atoi(val) == 0) {
95
+ pinMode(p, OUTPUT);
96
+ } else {
97
+ pinMode(p, INPUT);
98
+ }
99
+ }
100
+
101
+ /*
102
+ * Digital write
103
+ */
104
+ void dw(char *pin, char *val) {
105
+ if (debug) Serial.println("dw");
106
+ int p = getPin(pin);
107
+ if(p == -1) { if(debug) Serial.println("badpin"); return; }
108
+ pinMode(p, OUTPUT);
109
+ if (atoi(val) == 0) {
110
+ digitalWrite(p, LOW);
111
+ } else {
112
+ digitalWrite(p, HIGH);
113
+ }
114
+ }
115
+
116
+ /*
117
+ * Digital read
118
+ */
119
+ void dr(char *pin, char *val) {
120
+ if (debug) Serial.println("dr");
121
+ int p = getPin(pin);
122
+ if(p == -1) { if(debug) Serial.println("badpin"); return; }
123
+ pinMode(p, INPUT);
124
+ int oraw = digitalRead(p);
125
+ char m[7];
126
+ sprintf(m, "%02d::%02d", p,oraw);
127
+ Serial.println(m);
128
+ }
129
+
130
+ /*
131
+ * Analog read
132
+ */
133
+ void ar(char *pin, char *val) {
134
+ if(debug) Serial.println("ar");
135
+ int p = getPin(pin);
136
+ if(p == -1) { if(debug) Serial.println("badpin"); return; }
137
+ pinMode(p, INPUT); // don't want to sw
138
+ int rval = analogRead(p);
139
+ char m[8];
140
+ sprintf(m, "%s::%03d", pin, rval);
141
+ Serial.println(m);
142
+ }
143
+
144
+ void aw(char *pin, char *val) {
145
+ if(debug) Serial.println("aw");
146
+ int p = getPin(pin);
147
+ pinMode(p, OUTPUT);
148
+ if(p == -1) { if(debug) Serial.println("badpin"); return; }
149
+ analogWrite(p,atoi(val));
150
+ }
151
+
152
+ int getPin(char *pin) { //Converts to A0-A5, and returns -1 on error
153
+ int ret = -1;
154
+ if(pin[0] == 'A' || pin[0] == 'a') {
155
+ switch(pin[1]) {
156
+ case '0': ret = A0; break;
157
+ case '1': ret = A1; break;
158
+ case '2': ret = A2; break;
159
+ case '3': ret = A3; break;
160
+ case '4': ret = A4; break;
161
+ case '5': ret = A5; break;
162
+ default: break;
163
+ }
164
+ } else {
165
+ ret = atoi(pin);
166
+ if(ret == 0 && (pin[0] != '0' || pin[1] != '0')) {
167
+ ret = -1;
168
+ }
169
+ }
170
+ return ret;
171
+ }
172
+
173
+ /*
174
+ * Handle Ping commands
175
+ * fire, read
176
+ */
177
+ void handlePing(char *pin, char *val, char *aux) {
178
+ if (debug) Serial.println("ss");
179
+ int p = getPin(pin);
180
+
181
+ if(p == -1) { if(debug) Serial.println("badpin"); return; }
182
+ Serial.println("got signal");
183
+
184
+ // 01(1) Fire and Read
185
+ if (atoi(val) == 1) {
186
+ char m[16];
187
+
188
+ pinMode(p, OUTPUT);
189
+ digitalWrite(p, LOW);
190
+ delayMicroseconds(2);
191
+ digitalWrite(p, HIGH);
192
+ delayMicroseconds(5);
193
+ digitalWrite(p, LOW);
194
+
195
+ Serial.println("ping fired");
196
+
197
+ pinMode(p, INPUT);
198
+ sprintf(m, "%s::read::%08d", pin, pulseIn(p, HIGH));
199
+ Serial.println(m);
200
+
201
+ delay(50);
202
+ }
203
+ }
204
+
205
+ /*
206
+ * Handle Servo commands
207
+ * attach, detach, write, read, writeMicroseconds, attached
208
+ */
209
+ void handleServo(char *pin, char *val, char *aux) {
210
+ if (debug) Serial.println("ss");
211
+ int p = getPin(pin);
212
+ if(p == -1) { if(debug) Serial.println("badpin"); return; }
213
+ Serial.println("signal: servo");
214
+
215
+ // 00(0) Detach
216
+ if (atoi(val) == 0) {
217
+ servo.detach();
218
+ char m[12];
219
+ sprintf(m, "%s::detached", pin);
220
+ Serial.println(m);
221
+
222
+ // 01(1) Attach
223
+ } else if (atoi(val) == 1) {
224
+ // servo.attach(p, 750, 2250);
225
+ servo.attach(p);
226
+ char m[12];
227
+ sprintf(m, "%s::attached", pin);
228
+ Serial.println(m);
229
+
230
+ // 02(2) Write
231
+ } else if (atoi(val) == 2) {
232
+ Serial.println("writing to servo");
233
+ Serial.println(atoi(aux));
234
+ // Write to servo
235
+ servo.write(atoi(aux));
236
+ delay(15);
237
+
238
+ // TODO: Experiment with microsecond pulses
239
+ // digitalWrite(pin, HIGH); // start the pulse
240
+ // delayMicroseconds(pulseWidth); // pulse width
241
+ // digitalWrite(pin, LOW); // stop the pulse
242
+
243
+ // 03(3) Read
244
+ } else if (atoi(val) == 3) {
245
+ Serial.println("reading servo");
246
+ int sval = servo.read();
247
+ char m[13];
248
+ sprintf(m, "%s::read::%03d", pin, sval);
249
+ Serial.println(m);
250
+ }
251
+ }
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dino
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.8'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Austinbv
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: serialport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! "A utility library for interfacting with an Arduino.\n Designed for
31
+ control, expansion, and with love."
32
+ email:
33
+ - austinbv@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - .rvmrc
40
+ - Gemfile
41
+ - LICENSE
42
+ - README.md
43
+ - Rakefile
44
+ - dino.gemspec
45
+ - examples/button/button.png
46
+ - examples/button/button.rb
47
+ - examples/ir_receiver.rb
48
+ - examples/led/led.rb
49
+ - examples/potentiometer.rb
50
+ - examples/rgb_led.rb
51
+ - examples/sensor.rb
52
+ - examples/servo.rb
53
+ - examples/stepper.rb
54
+ - lib/dino.rb
55
+ - lib/dino/board.rb
56
+ - lib/dino/components/base_component.rb
57
+ - lib/dino/components/button.rb
58
+ - lib/dino/components/ir_receiver.rb
59
+ - lib/dino/components/led.rb
60
+ - lib/dino/components/rgb_led.rb
61
+ - lib/dino/components/sensor.rb
62
+ - lib/dino/components/servo.rb
63
+ - lib/dino/components/stepper.rb
64
+ - lib/dino/tx_rx.rb
65
+ - lib/dino/version.rb
66
+ - spec/lib/board_spec.rb
67
+ - spec/lib/components/base_component_spec.rb
68
+ - spec/lib/components/button_spec.rb
69
+ - spec/lib/components/led_spec.rb
70
+ - spec/lib/components/rgb_led_spec.rb
71
+ - spec/lib/components/sensor_spec.rb
72
+ - spec/lib/components/servo_spec.rb
73
+ - spec/lib/components/stepper_spec.rb
74
+ - spec/lib/tx_rx_spec.rb
75
+ - spec/spec_helper.rb
76
+ - src/du.ino
77
+ homepage: https://github.com/austinbv/dino
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.24
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Control your arduino through a serial port
101
+ test_files:
102
+ - spec/lib/board_spec.rb
103
+ - spec/lib/components/base_component_spec.rb
104
+ - spec/lib/components/button_spec.rb
105
+ - spec/lib/components/led_spec.rb
106
+ - spec/lib/components/rgb_led_spec.rb
107
+ - spec/lib/components/sensor_spec.rb
108
+ - spec/lib/components/servo_spec.rb
109
+ - spec/lib/components/stepper_spec.rb
110
+ - spec/lib/tx_rx_spec.rb
111
+ - spec/spec_helper.rb