2 * To run this test suite, you must first install the ArduinoUnit library
3 * to your Arduino/libraries/ directory.
4 * You can get ArduinoUnit here: https://github.com/mmurdoch/arduinounit
5 * Download version 2.0 or greater or install it via the Arduino library manager.
8 #include <ArduinoUnit.h>
21 test(beginPrintsVersion)
25 Firmata.begin(stream);
29 FIRMATA_PROTOCOL_MAJOR_VERSION,
30 FIRMATA_PROTOCOL_MINOR_VERSION,
33 assertEqual(expected, stream.bytesWritten());
36 void processMessage(const byte *message, size_t length)
39 Firmata.begin(stream);
41 for (size_t i = 0; i < length; i++) {
42 stream.nextByte(message[i]);
43 Firmata.processInput();
48 int _digitalPortValue;
49 void writeToDigitalPort(byte port, int value)
52 _digitalPortValue = value;
55 void setupDigitalPort()
58 _digitalPortValue = 0;
61 char * _receivedString;
62 void handleStringCallback(char *str)
64 _receivedString = str;
67 test(processWriteDigital_0)
70 Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
72 byte message[] = { DIGITAL_MESSAGE, 0, 0 };
73 processMessage(message, 3);
75 assertEqual(0, _digitalPortValue);
78 test(processWriteDigital_127)
81 Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
83 byte message[] = { DIGITAL_MESSAGE, 127, 0 };
84 processMessage(message, 3);
86 assertEqual(127, _digitalPortValue);
89 test(processWriteDigital_128)
92 Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
94 byte message[] = { DIGITAL_MESSAGE, 0, 1 };
95 processMessage(message, 3);
97 assertEqual(128, _digitalPortValue);
100 test(processWriteLargestDigitalValue)
103 Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
105 byte message[] = { DIGITAL_MESSAGE, 0x7F, 0x7F };
106 processMessage(message, 3);
108 // Maximum of 14 bits can be set (B0011111111111111)
109 assertEqual(0x3FFF, _digitalPortValue);
112 test(defaultDigitalWritePortIsZero)
115 Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
117 byte message[] = { DIGITAL_MESSAGE, 0, 0 };
118 processMessage(message, 3);
120 assertEqual(0, _digitalPort);
123 test(specifiedDigitalWritePort)
126 Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
128 byte message[] = { DIGITAL_MESSAGE + 1, 0, 0 };
129 processMessage(message, 3);
131 assertEqual(1, _digitalPort);
134 test(setFirmwareVersionDoesNotLeakMemory)
136 Firmata.setFirmwareVersion(1, 0);
137 int initialMemory = freeMemory();
139 Firmata.setFirmwareVersion(1, 0);
141 assertEqual(0, initialMemory - freeMemory());
144 test(sendStringShouldEncode2BytesPerChar)
147 Firmata.begin(stream);
148 // reset the buffer because the firmware name string will be sent on Firmata.begin
151 char testString[] = "hi!";
152 Firmata.sendString(testString);
154 byte expected[] = { START_SYSEX, STRING_DATA, 'h', 0, 'i', 0, '!', 0, END_SYSEX };
156 int len = stream.bytesWritten().length();
157 assertEqual(sizeof(expected), len);
158 for (byte i = 0; i < len; i++) {
159 assertEqual(expected[i], (byte)stream.bytesWritten().charAt(i));
163 test(receivedStringShouldDecodeFrom2BytesPerChar)
165 Firmata.attach(STRING_DATA, handleStringCallback);
167 byte message[] = { START_SYSEX, STRING_DATA, 'b', 0, 'y', 0, 'e', 0, '!', 0, END_SYSEX };
168 processMessage(message, 11);
170 assertEqual("bye!", _receivedString);