Skip to content

Commit

Permalink
add analog pin support
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrySievert committed May 29, 2012
1 parent 7abfe5f commit d6289f0
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 39 deletions.
17 changes: 7 additions & 10 deletions README.md
Expand Up @@ -69,7 +69,7 @@ The event system is pretty straightforward. On initialization an `init` event i

## Getting and Setting

Getting and setting of `pins` is very simple. The pin mode is changed automatically.
Getting and setting of `pins` is very simple. The pin mode is changed automatically. It is possible to `get` and `set` both digital and analog pins.

var eventduino = require('eventduino');

Expand All @@ -82,16 +82,16 @@ Getting and setting of `pins` is very simple. The pin mode is changed automatic
ardy.on('init', function (args, comment) {
console.log('Eventduino init version ' + comment);

// get the value of pin 1
ardy.get(1);
// get the value of analog pin 1
ardy.get(eventduino.A1);

// set the LED pin to HIGH (1)
ardy.set(13, 1);
});

## Watching for Changes

Eventduino can be set into watch mode, which will check for any changes to the `pin` and send a command if one occurs. A `watch` can be setup on as many `pins` as required.
Eventduino can be set into watch mode, which will check for any changes to the `pin` and send a command if one occurs. A `watch` can be setup on as many `pins` as required. As analog pins can have a very active variance, a second parameter `variance` can be passed for analog pins: an `event` will only be emitted if the value read on `watch` changes past the `variance`.

var eventduino = require('eventduino');

Expand All @@ -102,7 +102,7 @@ Eventduino can be set into watch mode, which will check for any changes to the `
// stop watching pin 5 on first change
if (args[0] === 5) {
ardy.unwatch(5);
ardy.unwatch(eventduino.A5);
ardy.set(13, 1);
}
});
Expand All @@ -111,9 +111,6 @@ Eventduino can be set into watch mode, which will check for any changes to the `
console.log('Eventduino init version ' + comment);

ardy.watch(1);
ardy.watch(5);
// only emit the event if +- 5 on the read
ardy.watch(eventduino.A5, 5);
});

## TODO

Implement analog pins.
14 changes: 12 additions & 2 deletions lib/index.js
Expand Up @@ -148,8 +148,12 @@ function EventDuino (options) {
self.send(self.GET, [ pin ]);
};

self.watch = function (pin) {
self.send(self.WATCH, [ pin ]);
self.watch = function (pin, variance) {
if (variance) {
self.send(self.WATCH, [ pin, variance ]);
} else {
self.send(self.WATCH, [ pin ]);
}
};

self.unwatch = function (pin) {
Expand All @@ -161,6 +165,12 @@ function EventDuino (options) {
};
}

EventDuino.A1 = 'A0';
EventDuino.A1 = 'A1';
EventDuino.A2 = 'A2';
EventDuino.A3 = 'A3';
EventDuino.A4 = 'A4';
EventDuino.A5 = 'A5';

util.inherits(EventDuino, events.EventEmitter);

Expand Down
128 changes: 101 additions & 27 deletions src/eventdu/eventdu.ino
@@ -1,4 +1,4 @@
#define VERSION "v0.1.0"
#define VERSION "v0.1.2"

#define INIT "00"
#define ERROR "01"
Expand All @@ -13,10 +13,14 @@
#define MAX_BUFFER 1024
#define MAX_ARGS 10

// digital watch
int d[14];
int a[6];
int wd[14];
// analog watch
int a[6];
int wa[6];
// analog variance
int va[6];

#include <stdarg.h>

Expand Down Expand Up @@ -47,6 +51,7 @@ void setup () {
if (i < 6) {
a[i] = 0;
wa[i] = 0;
va[i] = 0;
}
}

Expand Down Expand Up @@ -77,12 +82,58 @@ void loop () {
free(data[0]);
free(data[1]);
}
}
// if (i < 6) {
// a[i] = 0;
// wa[i] = 0;
// }
}

if (i < 6) {
if (wa[i]) {
int ar = analogRead(i);

if (ar != a[i] && abs(ar - a[i]) > va[i]) {
a[i] = ar;

char *data[2];

data[0] = p("A%d", i);
data[1] = p("%d", ar);

write(WATCH, data, 2, NULL);

free(data[0]);
free(data[1]);
}
}
}
}
}

// find the real pin
int realPin (char *pin) {
if (pin[0] == 'A') {
switch (pin[1]) {
case '0':
return A0;

case '1':
return A1;

case '2':
return A2;

case '3':
return A3;

case '4':
return A4;

case '5':
return A5;

default:
return -1;
}
}

return atoi(pin);
}

// build the packet and send via serial
Expand Down Expand Up @@ -283,44 +334,67 @@ void cmd_ping () {
}

void cmd_get (char **args, int count) {
if (count && args[0][0] != 'A') {
char *data[2];
pinMode(atoi(args[0]), INPUT);
data[0] = args[0];
int pin = realPin(args[0]);
pinMode(pin, INPUT);

int current = digitalRead(atoi(args[1]));
char *data[2];
data[0] = args[0];

if (args[0][0] != 'A') {
int current = digitalRead(pin);
data[1] = p("%d", current);

write(GET, data, 2, NULL);
free(data[1]);
} else {
int current = analogRead(pin);

data[1] = p("%d", current);
}

write(GET, data, 2, NULL);
free(data[1]);
}

void cmd_set (char **args, int count) {
pinMode(atoi(args[0]), OUTPUT);

digitalWrite(atoi(args[0]), atoi(args[1]));
int pin = realPin(args[0]);
pinMode(pin, OUTPUT);

d[atoi(args[0])] = 0;
if (args[0][0] != 'A') {
digitalWrite(pin, atoi(args[1]));
wd[atoi(args[0])] = 0;
} else {
analogWrite(pin, atoi(args[1]));
wa[atoi(args[0])] = 0;
}

write(OK, NULL, 0, NULL);
}

void cmd_watch (char **args, int count) {
if (count && args[0][0] != 'A') {
pinMode(atoi(args[0]), INPUT);

wd[atoi(args[0])] = 1;
d[atoi(args[0])] = digitalRead(atoi(args[0]));
int pin = realPin(args[0]);
pinMode(pin, INPUT);

write(OK, NULL, 0, NULL);
if (args[0][0] != 'A') {
wd[pin] = 1;
d[pin] = digitalRead(pin);
} else {
wa[atoi(&args[0][1])] = 1;
a[atoi(&args[0][1])] = analogRead(pin);
if (count == 2) {
va[atoi(&args[0][1])] = atoi(args[1]);
} else {
va[atoi(&args[0][1])] = 0;
}
}

write(OK, NULL, 0, NULL);
}

void cmd_unwatch (char **args, int count) {
if (count && args[0][0] != 'A') {
if (args[0][0] != 'A') {
wd[atoi(args[0])] = 0;
write(OK, NULL, 0, NULL);
} else {
wa[atoi(&args[0][1])] = 0;
}

write(OK, NULL, 0, NULL);
}

0 comments on commit d6289f0

Please sign in to comment.