1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +03:00

Updating to the latest version of Firmata (rev. 41).

This commit is contained in:
David A. Mellis
2009-12-29 18:01:06 +00:00
parent 6672d51d56
commit c22d9942ee
3 changed files with 33 additions and 20 deletions

View File

@ -60,21 +60,18 @@ FirmataClass::FirmataClass(void)
/* begin method for overriding default serial bitrate */ /* begin method for overriding default serial bitrate */
void FirmataClass::begin(void) void FirmataClass::begin(void)
{ {
Serial.begin(57600); begin(57600);
blinkVersion();
delay(300);
printVersion();
} }
/* begin method for overriding default serial bitrate */ /* begin method for overriding default serial bitrate */
void FirmataClass::begin(long speed) void FirmataClass::begin(long speed)
{ {
blinkVersion();
#if defined(__AVR_ATmega128__) // Wiring #if defined(__AVR_ATmega128__) // Wiring
Serial.begin((uint32_t)speed); Serial.begin((uint32_t)speed);
#else #else
Serial.begin(speed); Serial.begin(speed);
#endif #endif
blinkVersion();
delay(300); delay(300);
printVersion(); printVersion();
printFirmwareVersion(); printFirmwareVersion();

View File

@ -158,6 +158,7 @@ extern FirmataClass Firmata;
#define ANALOG_PORT 2 // port# of analog used as digital #define ANALOG_PORT 2 // port# of analog used as digital
#define FIRST_ANALOG_PIN 14 // pin# corresponding to analog 0 #define FIRST_ANALOG_PIN 14 // pin# corresponding to analog 0
#define VERSION_BLINK_PIN 13 // digital pin to blink version on #define VERSION_BLINK_PIN 13 // digital pin to blink version on
#define FIRST_SERVO_PIN 2 // pin# of the first servo pin
#elif defined(__AVR_ATmega8__) // old Arduinos #elif defined(__AVR_ATmega8__) // old Arduinos
#define TOTAL_ANALOG_PINS 6 #define TOTAL_ANALOG_PINS 6
#define TOTAL_DIGITAL_PINS 20 // 14 digital + 6 analog #define TOTAL_DIGITAL_PINS 20 // 14 digital + 6 analog
@ -165,6 +166,7 @@ extern FirmataClass Firmata;
#define ANALOG_PORT 2 // port# of analog used as digital #define ANALOG_PORT 2 // port# of analog used as digital
#define FIRST_ANALOG_PIN 14 // pin# corresponding to analog 0 #define FIRST_ANALOG_PIN 14 // pin# corresponding to analog 0
#define VERSION_BLINK_PIN 13 // digital pin to blink version on #define VERSION_BLINK_PIN 13 // digital pin to blink version on
#define FIRST_SERVO_PIN 2 // pin# of the first servo pin
#elif defined(__AVR_ATmega1280__)// Arduino Mega #elif defined(__AVR_ATmega1280__)// Arduino Mega
#define TOTAL_ANALOG_PINS 16 #define TOTAL_ANALOG_PINS 16
#define TOTAL_DIGITAL_PINS 70 // 54 digital + 16 analog #define TOTAL_DIGITAL_PINS 70 // 54 digital + 16 analog
@ -172,13 +174,15 @@ extern FirmataClass Firmata;
#define ANALOG_PORT 8 // port# of analog used as digital #define ANALOG_PORT 8 // port# of analog used as digital
#define FIRST_ANALOG_PIN 54 // pin# corresponding to analog 0 #define FIRST_ANALOG_PIN 54 // pin# corresponding to analog 0
#define VERSION_BLINK_PIN 13 // digital pin to blink version on #define VERSION_BLINK_PIN 13 // digital pin to blink version on
#define FIRST_SERVO_PIN 2 // pin# of the first servo pin
#elif defined(__AVR_ATmega128__)// Wiring #elif defined(__AVR_ATmega128__)// Wiring
#define TOTAL_ANALOG_PINS 8 #define TOTAL_ANALOG_PINS 8
#define TOTAL_DIGITAL_PINS 51 #define TOTAL_DIGITAL_PINS 51
#define TOTAL_PORTS 7 // total number of ports for the board #define TOTAL_PORTS 7 // total number of ports for the board
#define ANALOG_PORT 5 // port# of analog used as digital #define ANALOG_PORT 5 // port# of analog used as digital
#define FIRST_ANALOG_PIN 40 // pin# corresponding to analog 0 #define FIRST_ANALOG_PIN 40 // pin# corresponding to analog 0
#define VERSION_BLINK_PIN 13 // digital pin to blink version on #define VERSION_BLINK_PIN 48 // digital pin to blink version on
#define FIRST_SERVO_PIN 8 // pin# of the first servo pin
#elif defined(__AVR_AT90USB162__) // Teensy #elif defined(__AVR_AT90USB162__) // Teensy
#define TOTAL_ANALOG_PINS 0 #define TOTAL_ANALOG_PINS 0
#define TOTAL_DIGITAL_PINS 21 // 21 digital + no analog #define TOTAL_DIGITAL_PINS 21 // 21 digital + no analog

View File

@ -11,9 +11,7 @@
formatted using the GNU C formatting and indenting formatted using the GNU C formatting and indenting
*/ */
/* /*
* TODO: add Servo support using setPinModeCallback(pin, SERVO);
* TODO: use Program Control to load stored profiles from EEPROM * TODO: use Program Control to load stored profiles from EEPROM
*/ */
@ -39,7 +37,7 @@ unsigned long currentMillis; // store the current value from millis()
unsigned long nextExecuteMillis; // for comparison with currentMillis unsigned long nextExecuteMillis; // for comparison with currentMillis
int samplingInterval = 19; // how often to run the main loop (in ms) int samplingInterval = 19; // how often to run the main loop (in ms)
Servo servos[2]; // the servo library can control servos on pins 9 and 10 only Servo servos[MAX_SERVOS];
/*============================================================================== /*==============================================================================
* FUNCTIONS * FUNCTIONS
@ -99,6 +97,9 @@ void setPinModeCallback(byte pin, int mode) {
} }
if(pin > 1) { // ignore RxTx (pins 0 and 1) if(pin > 1) { // ignore RxTx (pins 0 and 1)
if (isServoSupportedPin(pin) && mode != SERVO)
if (servos[pin - FIRST_SERVO_PIN].attached())
servos[pin - FIRST_SERVO_PIN].detach();
if(pin > 13) if(pin > 13)
reportAnalogCallback(pin - 14, mode == ANALOG ? 1 : 0); // turn on/off reporting reportAnalogCallback(pin - 14, mode == ANALOG ? 1 : 0); // turn on/off reporting
switch(mode) { switch(mode) {
@ -117,10 +118,13 @@ void setPinModeCallback(byte pin, int mode) {
portStatus[port] = portStatus[port] | (1 << (pin - offset)); portStatus[port] = portStatus[port] | (1 << (pin - offset));
break; break;
case SERVO: case SERVO:
if((pin == 9 || pin == 10)) // TODO: Support Arduino Mega
if (isServoSupportedPin(pin)) {
pinStatus[pin] = mode; pinStatus[pin] = mode;
else if (!servos[pin - FIRST_SERVO_PIN].attached())
Firmata.sendString("Servo only on pins 9 and 10"); servos[pin - FIRST_SERVO_PIN].attach(pin);
} else
Firmata.sendString("Servo only on pins from 2 to 13");
break; break;
case I2C: case I2C:
pinStatus[pin] = mode; pinStatus[pin] = mode;
@ -137,8 +141,8 @@ void analogWriteCallback(byte pin, int value)
{ {
switch(pinStatus[pin]) { switch(pinStatus[pin]) {
case SERVO: case SERVO:
if(pin == 9) servos[0].write(value); if (isServoSupportedPin(pin))
if(pin == 10) servos[1].write(value); servos[pin - FIRST_SERVO_PIN].write(value);
break; break;
case PWM: case PWM:
analogWrite(pin, value); analogWrite(pin, value);
@ -179,7 +183,6 @@ void reportAnalogCallback(byte pin, int value)
} }
else { // everything but 0 enables reporting of that pin else { // everything but 0 enables reporting of that pin
analogInputsToReport = analogInputsToReport | (1 << pin); analogInputsToReport = analogInputsToReport | (1 << pin);
setPinModeCallback(pin, ANALOG);
} }
// TODO: save status to EEPROM here, if changed // TODO: save status to EEPROM here, if changed
} }
@ -201,13 +204,18 @@ void sysexCallback(byte command, byte argc, byte *argv)
case SERVO_CONFIG: case SERVO_CONFIG:
if(argc > 4) { if(argc > 4) {
// these vars are here for clarity, they'll optimized away by the compiler // these vars are here for clarity, they'll optimized away by the compiler
byte pin = argv[0] - 9; // servos are pins 9 and 10, so offset for array byte pin = argv[0];
int minPulse = argv[1] + (argv[2] << 7); int minPulse = argv[1] + (argv[2] << 7);
int maxPulse = argv[3] + (argv[4] << 7); int maxPulse = argv[3] + (argv[4] << 7);
servos[pin].attach(argv[0], minPulse, maxPulse);
// TODO does the Servo have to be detach()ed before reconfiguring? if (isServoSupportedPin(pin)) {
// servos are pins from 2 to 13, so offset for array
if (servos[pin - FIRST_SERVO_PIN].attached())
servos[pin - FIRST_SERVO_PIN].detach();
servos[pin - FIRST_SERVO_PIN].attach(pin, minPulse, maxPulse);
setPinModeCallback(pin, SERVO); setPinModeCallback(pin, SERVO);
} }
}
break; break;
case SAMPLING_INTERVAL: case SAMPLING_INTERVAL:
if (argc > 1) if (argc > 1)
@ -218,6 +226,10 @@ void sysexCallback(byte command, byte argc, byte *argv)
} }
} }
boolean isServoSupportedPin(byte pin)
{
return ((FIRST_SERVO_PIN <= pin) && (pin <= (FIRST_SERVO_PIN + MAX_SERVOS)));
}
/*============================================================================== /*==============================================================================
* SETUP() * SETUP()
@ -239,7 +251,7 @@ void setup()
portStatus[1] = B11000000; // ignore 14/15 pins portStatus[1] = B11000000; // ignore 14/15 pins
portStatus[2] = B00000000; portStatus[2] = B00000000;
for(i=0; i<TOTAL_DIGITAL_PINS; ++i) { // TODO make this work with analogs for(i=0; i < FIRST_ANALOG_PIN; ++i) {
setPinModeCallback(i,OUTPUT); setPinModeCallback(i,OUTPUT);
} }
// set all outputs to 0 to make sure internal pull-up resistors are off // set all outputs to 0 to make sure internal pull-up resistors are off