2020
2121 See file LICENSE.txt for further informations on licensing terms.
2222
23- Last updated by Jeff Hoefs: October 3rd , 2015
23+ Last updated by Jeff Hoefs: August 9th , 2015
2424*/
2525
2626#include < Servo.h>
2727#include < Wire.h>
2828#include < Firmata.h>
2929
30- // SoftwareSerial is only supported for AVR-based boards
31- // The second condition checks if the IDE is in the 1.0.x series, if so, include SoftwareSerial
32- // since it should be available to all boards in that IDE.
33- #if defined(ARDUINO_ARCH_AVR) || (ARDUINO >= 100 && ARDUINO < 10500)
34- #include < SoftwareSerial.h>
35- #endif
36- #include " utility/serialUtils.h"
37-
3830#define I2C_WRITE B00000000
3931#define I2C_READ B00001000
4032#define I2C_READ_CONTINUOUSLY B00010000
@@ -69,16 +61,6 @@ unsigned long currentMillis; // store the current value from millis()
6961unsigned long previousMillis; // for comparison with currentMillis
7062unsigned int samplingInterval = 19 ; // how often to run the main loop (in ms)
7163
72- /* serial message */
73- Stream *swSerial0 = NULL ;
74- Stream *swSerial1 = NULL ;
75- Stream *swSerial2 = NULL ;
76- Stream *swSerial3 = NULL ;
77-
78- byte reportSerial[MAX_SERIAL_PORTS];
79- int serialBytesToRead[SERIAL_READ_ARR_LEN];
80- signed char serialIndex;
81-
8264/* i2c data */
8365struct i2c_device_info {
8466 byte addr;
@@ -103,7 +85,6 @@ byte servoCount = 0;
10385
10486boolean isResetting = false ;
10587
106-
10788/* utility functions */
10889void wireWrite (byte data)
10990{
@@ -127,103 +108,6 @@ byte wireRead(void)
127108 * FUNCTIONS
128109 *============================================================================*/
129110
130- // get a pointer to the serial port associated with the specified port id
131- Stream* getPortFromId (byte portId)
132- {
133- switch (portId) {
134- case HW_SERIAL0:
135- // block use of Serial (typically pins 0 and 1) until ability to reclaim Serial is implemented
136- // return &Serial;
137- return NULL ;
138- #if defined(PIN_SERIAL1_RX)
139- case HW_SERIAL1:
140- return &Serial1;
141- #endif
142- #if defined(PIN_SERIAL2_RX)
143- case HW_SERIAL2:
144- return &Serial2;
145- #endif
146- #if defined(PIN_SERIAL3_RX)
147- case HW_SERIAL3:
148- return &Serial3;
149- #endif
150- #if defined(SoftwareSerial_h)
151- case SW_SERIAL0:
152- if (swSerial0 != NULL ) {
153- // instances of SoftwareSerial are already pointers so simply return the instance
154- return swSerial0;
155- }
156- break ;
157- case SW_SERIAL1:
158- if (swSerial1 != NULL ) {
159- return swSerial1;
160- }
161- break ;
162- case SW_SERIAL2:
163- if (swSerial2 != NULL ) {
164- return swSerial2;
165- }
166- break ;
167- case SW_SERIAL3:
168- if (swSerial3 != NULL ) {
169- return swSerial3;
170- }
171- break ;
172- #endif
173- }
174- return NULL ;
175- }
176-
177- // Check serial ports that have READ_CONTINUOUS mode set and relay any data
178- // for each port to the device attached to that port.
179- void checkSerial ()
180- {
181- byte portId, serialData;
182- int bytesToRead = 0 ;
183- int numBytesToRead = 0 ;
184- Stream* serialPort;
185-
186- if (serialIndex > -1 ) {
187-
188- // loop through all reporting (READ_CONTINUOUS) serial ports
189- for (byte i = 0 ; i < serialIndex + 1 ; i++) {
190- portId = reportSerial[i];
191- bytesToRead = serialBytesToRead[portId];
192- serialPort = getPortFromId (portId);
193- if (serialPort == NULL ) {
194- continue ;
195- }
196- #if defined(SoftwareSerial_h)
197- // only the SoftwareSerial port that is "listening" can read data
198- if (portId > 7 && !((SoftwareSerial*)serialPort)->isListening ()) {
199- continue ;
200- }
201- #endif
202- if (serialPort->available () > 0 ) {
203- Firmata.write (START_SYSEX);
204- Firmata.write (SERIAL_MESSAGE);
205- Firmata.write (SERIAL_REPLY | portId);
206-
207- if (bytesToRead == 0 || (serialPort->available () <= bytesToRead)) {
208- numBytesToRead = serialPort->available ();
209- } else {
210- numBytesToRead = bytesToRead;
211- }
212-
213- // relay serial data to the serial device
214- while (numBytesToRead > 0 ) {
215- serialData = serialPort->read ();
216- Firmata.write (serialData & 0x7F );
217- Firmata.write ((serialData >> 7 ) & 0x7F );
218- numBytesToRead--;
219- }
220- Firmata.write (END_SYSEX);
221- }
222-
223- }
224- }
225- }
226-
227111void attachServo (byte pin, int minPulse, int maxPulse)
228112{
229113 if (servoCount < MAX_SERVOS) {
@@ -414,10 +298,6 @@ void setPinModeCallback(byte pin, int mode)
414298 pinConfig[pin] = I2C;
415299 }
416300 break ;
417- case MODE_SERIAL:
418- // used for both HW and SW serial
419- pinConfig[pin] = MODE_SERIAL;
420- break ;
421301 default :
422302 Firmata.sendString (" Unknown pin mode" ); // TODO: put error msgs in EEPROM
423303 }
@@ -679,10 +559,6 @@ void sysexCallback(byte command, byte argc, byte *argv)
679559 Firmata.write (I2C);
680560 Firmata.write (1 ); // TODO: could assign a number to map to SCL or SDA
681561 }
682- if (IS_PIN_SERIAL (pin)) {
683- Firmata.write (MODE_SERIAL);
684- Firmata.write (getSerialPinType (pin));
685- }
686562 Firmata.write (127 );
687563 }
688564 Firmata.write (END_SYSEX);
@@ -710,155 +586,6 @@ void sysexCallback(byte command, byte argc, byte *argv)
710586 }
711587 Firmata.write (END_SYSEX);
712588 break ;
713-
714- case SERIAL_MESSAGE:
715- Stream * serialPort;
716- mode = argv[0 ] & SERIAL_MODE_MASK;
717- byte portId = argv[0 ] & SERIAL_PORT_ID_MASK;
718-
719- switch (mode) {
720- case SERIAL_CONFIG:
721- {
722- long baud = (long )argv[1 ] | ((long )argv[2 ] << 7 ) | ((long )argv[3 ] << 14 );
723- byte txPin, rxPin;
724- serial_pins pins;
725-
726- if (portId > 7 && argc > 4 ) {
727- rxPin = argv[4 ];
728- txPin = argv[5 ];
729- }
730-
731- if (portId < 8 ) {
732- serialPort = getPortFromId (portId);
733- if (serialPort != NULL ) {
734- pins = getSerialPinNumbers (portId);
735- if (pins.rx != 0 && pins.tx != 0 ) {
736- setPinModeCallback (pins.rx , MODE_SERIAL);
737- setPinModeCallback (pins.tx , MODE_SERIAL);
738- // Fixes an issue where some serial devices would not work properly with Arduino Due
739- // because all Arduino pins are set to OUTPUT by default in StandardFirmata.
740- pinMode (pins.rx , INPUT);
741- }
742- ((HardwareSerial*)serialPort)->begin (baud);
743- }
744- } else {
745- #if defined(SoftwareSerial_h)
746- switch (portId) {
747- case SW_SERIAL0:
748- if (swSerial0 == NULL ) {
749- swSerial0 = new SoftwareSerial (rxPin, txPin);
750- }
751- break ;
752- case SW_SERIAL1:
753- if (swSerial1 == NULL ) {
754- swSerial1 = new SoftwareSerial (rxPin, txPin);
755- }
756- break ;
757- case SW_SERIAL2:
758- if (swSerial2 == NULL ) {
759- swSerial2 = new SoftwareSerial (rxPin, txPin);
760- }
761- break ;
762- case SW_SERIAL3:
763- if (swSerial3 == NULL ) {
764- swSerial3 = new SoftwareSerial (rxPin, txPin);
765- }
766- break ;
767- }
768- serialPort = getPortFromId (portId);
769- if (serialPort != NULL ) {
770- setPinModeCallback (rxPin, MODE_SERIAL);
771- setPinModeCallback (txPin, MODE_SERIAL);
772- ((SoftwareSerial*)serialPort)->begin (baud);
773- }
774- #endif
775- }
776- break ; // SERIAL_CONFIG
777- }
778- case SERIAL_WRITE:
779- {
780- byte data;
781- serialPort = getPortFromId (portId);
782- if (serialPort == NULL ) {
783- break ;
784- }
785- for (byte i = 1 ; i < argc; i += 2 ) {
786- data = argv[i] + (argv[i + 1 ] << 7 );
787- serialPort->write (data);
788- }
789- break ; // SERIAL_WRITE
790- }
791- case SERIAL_READ:
792- if (argv[1 ] == SERIAL_READ_CONTINUOUSLY) {
793- if (serialIndex + 1 >= MAX_SERIAL_PORTS) {
794- break ;
795- }
796-
797- if (argc > 2 ) {
798- // maximum number of bytes to read from buffer per iteration of loop()
799- serialBytesToRead[portId] = (int )argv[2 ] | ((int )argv[3 ] << 7 );
800- } else {
801- // read all available bytes per iteration of loop()
802- serialBytesToRead[portId] = 0 ;
803- }
804- serialIndex++;
805- reportSerial[serialIndex] = portId;
806- } else if (argv[1 ] == SERIAL_STOP_READING) {
807- byte serialIndexToSkip;
808- if (serialIndex <= 0 ) {
809- serialIndex = -1 ;
810- } else {
811- for (byte i = 0 ; i < serialIndex + 1 ; i++) {
812- if (reportSerial[i] == portId) {
813- serialIndexToSkip = i;
814- break ;
815- }
816- }
817- // shift elements over to fill space left by removed element
818- for (byte i = serialIndexToSkip; i < serialIndex + 1 ; i++) {
819- if (i < MAX_SERIAL_PORTS) {
820- reportSerial[i] = reportSerial[i + 1 ];
821- }
822- }
823- serialIndex--;
824- }
825- }
826- break ; // SERIAL_READ
827- case SERIAL_CLOSE:
828- serialPort = getPortFromId (portId);
829- if (serialPort != NULL ) {
830- if (portId < 8 ) {
831- ((HardwareSerial*)serialPort)->end ();
832- } else {
833- #if defined(SoftwareSerial_h)
834- ((SoftwareSerial*)serialPort)->end ();
835- if (serialPort != NULL ) {
836- free (serialPort);
837- serialPort = NULL ;
838- }
839- #endif
840- }
841- }
842- break ; // SERIAL_CLOSE
843- case SERIAL_FLUSH:
844- serialPort = getPortFromId (portId);
845- if (serialPort != NULL ) {
846- getPortFromId (portId)->flush ();
847- }
848- break ; // SERIAL_FLUSH
849- #if defined(SoftwareSerial_h)
850- case SERIAL_LISTEN:
851- // can only call listen() on software serial ports
852- if (portId > 7 ) {
853- serialPort = getPortFromId (portId);
854- if (serialPort != NULL ) {
855- ((SoftwareSerial*)serialPort)->listen ();
856- }
857- }
858- break ; // SERIAL_LISTEN
859- #endif
860- }
861- break ;
862589 }
863590}
864591
@@ -892,7 +619,6 @@ void disableI2CPins() {
892619
893620void systemResetCallback ()
894621{
895- Stream *serialPort;
896622 isResetting = true ;
897623
898624 // initialize a defalt state
@@ -902,22 +628,6 @@ void systemResetCallback()
902628 disableI2CPins ();
903629 }
904630
905- #if defined(SoftwareSerial_h)
906- // free memory allocated for SoftwareSerial ports
907- for (byte i = SW_SERIAL0; i < SW_SERIAL3 + 1 ; i++) {
908- serialPort = getPortFromId (i);
909- if (serialPort != NULL ) {
910- free (serialPort);
911- serialPort = NULL ;
912- }
913- }
914- #endif
915-
916- serialIndex = -1 ;
917- for (byte i = 0 ; i < SERIAL_READ_ARR_LEN; i++) {
918- serialBytesToRead[i] = 0 ;
919- }
920-
921631 for (byte i = 0 ; i < TOTAL_PORTS; i++) {
922632 reportPINs[i] = false ; // by default, reporting off
923633 portConfigInputs[i] = 0 ; // until activated
@@ -971,7 +681,7 @@ void setup()
971681 // Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this:
972682 // Serial1.begin(57600);
973683 // Firmata.begin(Serial1);
974- // However do not do this if you are using SERIAL_MESSAGE
684+ // then comment out or remove lines 701 - 704 below
975685
976686 Firmata.begin (57600 );
977687 while (!Serial) {
@@ -1017,6 +727,4 @@ void loop()
1017727 }
1018728 }
1019729 }
1020-
1021- checkSerial ();
1022730}
0 commit comments