2009-06-01 10:32:11 +02:00
|
|
|
/* Supports as many digital inputs and outputs as possible.
|
|
|
|
*
|
|
|
|
* This example code is in the public domain.
|
|
|
|
*/
|
|
|
|
#include <Firmata.h>
|
|
|
|
|
2010-08-06 23:55:17 +02:00
|
|
|
byte previousPIN[TOTAL_PORTS]; // PIN means PORT for input
|
|
|
|
byte previousPORT[TOTAL_PORTS];
|
2009-06-01 10:32:11 +02:00
|
|
|
|
|
|
|
void outputPort(byte portNumber, byte portValue)
|
|
|
|
{
|
2010-08-06 23:55:17 +02:00
|
|
|
// only send the data when it changes, otherwise you get too many messages!
|
|
|
|
if (previousPIN[portNumber] != portValue) {
|
2009-06-01 10:32:11 +02:00
|
|
|
Firmata.sendDigitalPort(portNumber, portValue);
|
|
|
|
previousPIN[portNumber] = portValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPinModeCallback(byte pin, int mode) {
|
2010-08-06 23:55:17 +02:00
|
|
|
if (IS_PIN_DIGITAL(pin)) {
|
|
|
|
pinMode(PIN_TO_DIGITAL(pin), mode);
|
2009-06-01 10:32:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void digitalWriteCallback(byte port, int value)
|
|
|
|
{
|
|
|
|
byte i;
|
|
|
|
byte currentPinValue, previousPinValue;
|
|
|
|
|
2010-08-06 23:55:17 +02:00
|
|
|
if (port < TOTAL_PORTS && value != previousPORT[port]) {
|
2009-06-01 10:32:11 +02:00
|
|
|
for(i=0; i<8; i++) {
|
|
|
|
currentPinValue = (byte) value & (1 << i);
|
|
|
|
previousPinValue = previousPORT[port] & (1 << i);
|
|
|
|
if(currentPinValue != previousPinValue) {
|
|
|
|
digitalWrite(i + (port*8), currentPinValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
previousPORT[port] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Firmata.setFirmwareVersion(0, 1);
|
|
|
|
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
|
|
|
|
Firmata.attach(SET_PIN_MODE, setPinModeCallback);
|
2009-07-14 23:32:55 +02:00
|
|
|
Firmata.begin(57600);
|
2009-06-01 10:32:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2010-08-06 23:55:17 +02:00
|
|
|
byte i;
|
|
|
|
|
|
|
|
for (i=0; i<TOTAL_PORTS; i++) {
|
2011-09-02 20:43:44 +02:00
|
|
|
outputPort(i, readPort(i, 0xff));
|
2010-08-06 23:55:17 +02:00
|
|
|
}
|
|
|
|
|
2009-06-01 10:32:11 +02:00
|
|
|
while(Firmata.available()) {
|
|
|
|
Firmata.processInput();
|
|
|
|
}
|
|
|
|
}
|