[224] | 1 | /*
|
---|
| 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.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include <ArduinoUnit.h>
|
---|
| 9 | #include <Firmata.h>
|
---|
| 10 |
|
---|
| 11 | void setup()
|
---|
| 12 | {
|
---|
| 13 | Serial.begin(9600);
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | void loop()
|
---|
| 17 | {
|
---|
| 18 | Test::run();
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | test(beginPrintsVersion)
|
---|
| 22 | {
|
---|
| 23 | FakeStream stream;
|
---|
| 24 |
|
---|
| 25 | Firmata.begin(stream);
|
---|
| 26 |
|
---|
| 27 | char expected[] = {
|
---|
| 28 | REPORT_VERSION,
|
---|
| 29 | FIRMATA_PROTOCOL_MAJOR_VERSION,
|
---|
| 30 | FIRMATA_PROTOCOL_MINOR_VERSION,
|
---|
| 31 | 0
|
---|
| 32 | };
|
---|
| 33 | assertEqual(expected, stream.bytesWritten());
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | void processMessage(const byte *message, size_t length)
|
---|
| 37 | {
|
---|
| 38 | FakeStream stream;
|
---|
| 39 | Firmata.begin(stream);
|
---|
| 40 |
|
---|
| 41 | for (size_t i = 0; i < length; i++) {
|
---|
| 42 | stream.nextByte(message[i]);
|
---|
| 43 | Firmata.processInput();
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | byte _digitalPort;
|
---|
| 48 | int _digitalPortValue;
|
---|
| 49 | void writeToDigitalPort(byte port, int value)
|
---|
| 50 | {
|
---|
| 51 | _digitalPort = port;
|
---|
| 52 | _digitalPortValue = value;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void setupDigitalPort()
|
---|
| 56 | {
|
---|
| 57 | _digitalPort = 0;
|
---|
| 58 | _digitalPortValue = 0;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | test(processWriteDigital_0)
|
---|
| 62 | {
|
---|
| 63 | setupDigitalPort();
|
---|
| 64 | Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
|
---|
| 65 |
|
---|
| 66 | byte message[] = { DIGITAL_MESSAGE, 0, 0 };
|
---|
| 67 | processMessage(message, 3);
|
---|
| 68 |
|
---|
| 69 | assertEqual(0, _digitalPortValue);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | test(processWriteDigital_127)
|
---|
| 73 | {
|
---|
| 74 | setupDigitalPort();
|
---|
| 75 | Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
|
---|
| 76 |
|
---|
| 77 | byte message[] = { DIGITAL_MESSAGE, 127, 0 };
|
---|
| 78 | processMessage(message, 3);
|
---|
| 79 |
|
---|
| 80 | assertEqual(127, _digitalPortValue);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | test(processWriteDigital_128)
|
---|
| 84 | {
|
---|
| 85 | setupDigitalPort();
|
---|
| 86 | Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
|
---|
| 87 |
|
---|
| 88 | byte message[] = { DIGITAL_MESSAGE, 0, 1 };
|
---|
| 89 | processMessage(message, 3);
|
---|
| 90 |
|
---|
| 91 | assertEqual(128, _digitalPortValue);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | test(processWriteLargestDigitalValue)
|
---|
| 95 | {
|
---|
| 96 | setupDigitalPort();
|
---|
| 97 | Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
|
---|
| 98 |
|
---|
| 99 | byte message[] = { DIGITAL_MESSAGE, 0x7F, 0x7F };
|
---|
| 100 | processMessage(message, 3);
|
---|
| 101 |
|
---|
| 102 | // Maximum of 14 bits can be set (B0011111111111111)
|
---|
| 103 | assertEqual(0x3FFF, _digitalPortValue);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | test(defaultDigitalWritePortIsZero)
|
---|
| 107 | {
|
---|
| 108 | setupDigitalPort();
|
---|
| 109 | Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
|
---|
| 110 |
|
---|
| 111 | byte message[] = { DIGITAL_MESSAGE, 0, 0 };
|
---|
| 112 | processMessage(message, 3);
|
---|
| 113 |
|
---|
| 114 | assertEqual(0, _digitalPort);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | test(specifiedDigitalWritePort)
|
---|
| 118 | {
|
---|
| 119 | setupDigitalPort();
|
---|
| 120 | Firmata.attach(DIGITAL_MESSAGE, writeToDigitalPort);
|
---|
| 121 |
|
---|
| 122 | byte message[] = { DIGITAL_MESSAGE + 1, 0, 0 };
|
---|
| 123 | processMessage(message, 3);
|
---|
| 124 |
|
---|
| 125 | assertEqual(1, _digitalPort);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | test(setFirmwareVersionDoesNotLeakMemory)
|
---|
| 129 | {
|
---|
| 130 | Firmata.setFirmwareVersion(1, 0);
|
---|
| 131 | int initialMemory = freeMemory();
|
---|
| 132 |
|
---|
| 133 | Firmata.setFirmwareVersion(1, 0);
|
---|
| 134 |
|
---|
| 135 | assertEqual(0, initialMemory - freeMemory());
|
---|
| 136 | }
|
---|