|
| 1 | +/* KerbalSimpitStressTest |
| 2 | + A demonstration of subscribing to all channel and check what is the impact |
| 3 | + of taking too long to call mysimpit.update. |
| 4 | +*/ |
| 5 | +#include "KerbalSimpit.h" |
| 6 | + |
| 7 | +// Declare a KerbalSimpit object that will |
| 8 | +// communicate using the "Serial" device. |
| 9 | +KerbalSimpit mySimpit(Serial); |
| 10 | + |
| 11 | +// Store last time the log was sent to KSP |
| 12 | +unsigned long lastTimeLogging = 0; |
| 13 | +const long intervalLogging = 5000; |
| 14 | + |
| 15 | +// Store number of messages received |
| 16 | +int msgNbr = 0; |
| 17 | + |
| 18 | +void setup() { |
| 19 | + // Open the serial connection. |
| 20 | + Serial.begin(115200); |
| 21 | + |
| 22 | + // Set up the build in LED, and turn it on. |
| 23 | + pinMode(LED_BUILTIN, OUTPUT); |
| 24 | + digitalWrite(LED_BUILTIN, HIGH); |
| 25 | + // This loop continually attempts to handshake with the plugin. |
| 26 | + // It will keep retrying until it gets a successful handshake. |
| 27 | + while (!mySimpit.init()) { |
| 28 | + delay(100); |
| 29 | + } |
| 30 | + // Turn off the built-in LED to indicate handshaking is complete. |
| 31 | + digitalWrite(LED_BUILTIN, LOW); |
| 32 | + // Display a message in KSP to indicate handshaking is complete. |
| 33 | + mySimpit.printToKSP("Connected", PRINT_TO_SCREEN); |
| 34 | + // Sets our callback function. The KerbalSimpit library will |
| 35 | + // call this function every time a packet is received. |
| 36 | + mySimpit.inboundHandler(messageHandler); |
| 37 | + |
| 38 | + // Here you can register all the channels you want to use. |
| 39 | + // mySimpit.registerChannel(ALTITUDE_MESSAGE); |
| 40 | + // ... |
| 41 | + |
| 42 | + // By default, register all available channels (even some non existant ones) |
| 43 | + for(byte b = 0; b <= 128; b++){ |
| 44 | + mySimpit.registerChannel(b); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void loop() { |
| 49 | + // Check for new serial messages. |
| 50 | + mySimpit.update(); |
| 51 | + |
| 52 | + // Emulate a time consuming treatement. Increase/descrease this value to see the impact on the number of recevied/lost messages. |
| 53 | + delay(10); |
| 54 | + |
| 55 | + unsigned long currentMillis = millis(); |
| 56 | + if (currentMillis - lastTimeLogging >= intervalLogging) { |
| 57 | + lastTimeLogging = currentMillis; |
| 58 | + |
| 59 | + mySimpit.printToKSP("Number of msg " + String(msgNbr)); |
| 60 | + msgNbr = 0; |
| 61 | + |
| 62 | + mySimpit.printToKSP("Number of lost msg " + String(mySimpit.packetDroppedNbr)); |
| 63 | + mySimpit.packetDroppedNbr = 0; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void messageHandler(byte messageType, byte msg[], byte msgSize) { |
| 68 | + //Ignore all messages, just count them |
| 69 | + msgNbr ++; |
| 70 | +} |
0 commit comments