|
| 1 | +// ============================================================================= |
| 2 | +// |
| 3 | +// Copyright (c) 2012-2016 Christopher Baker <http://christopherbaker.net> |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | +// |
| 23 | +// ============================================================================= |
| 24 | + |
| 25 | + |
| 26 | +#include <PacketSerial.h> |
| 27 | + |
| 28 | + |
| 29 | +// The PacketSerial object. |
| 30 | +// It cleverly wraps one of the Serial objects. |
| 31 | +// While it is still possible to use the Serial object |
| 32 | +// directly, it is recommended that the user let the |
| 33 | +// PacketSerial object manage all serial communication. |
| 34 | +// Thus the user should not call Serial.write(), etc. |
| 35 | +// Additionally the user should not use the serialEvent() |
| 36 | +// callbacks. |
| 37 | +SLIPPacketSerial serial; |
| 38 | + |
| 39 | + |
| 40 | +void setup() |
| 41 | +{ |
| 42 | + // We must specify a packet handler method so that |
| 43 | + serial.setPacketHandler(&onPacket); |
| 44 | + serial.begin(115200); |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +void loop() |
| 49 | +{ |
| 50 | + // Do other things here. |
| 51 | + |
| 52 | + // The update() method attempts to read in |
| 53 | + // any incoming serial data and emits packets via |
| 54 | + // the user's onPacket(const uint8_t* buffer, size_t size) |
| 55 | + // method registered with the setPacketHandler() method. |
| 56 | + // |
| 57 | + // The update() method should be called at the end of the loop(). |
| 58 | + serial.update(); |
| 59 | +} |
| 60 | + |
| 61 | +// This is our packet callback. |
| 62 | +// The buffer is delivered already decoded. |
| 63 | +void onPacket(const uint8_t* buffer, size_t size) |
| 64 | +{ |
| 65 | + // Make a temporary buffer. |
| 66 | + uint8_t tmp[size]; |
| 67 | + |
| 68 | + // Copy the packet into our temporary buffer. |
| 69 | + memcpy(tmp, buffer, size); |
| 70 | + |
| 71 | + // Reverse our temporaray buffer. |
| 72 | + reverse(tmp, size); |
| 73 | + |
| 74 | + // Send the reversed buffer back. |
| 75 | + // The send() method will encode the buffer |
| 76 | + // as a packet, set packet markers, etc. |
| 77 | + serial.send(tmp, size); |
| 78 | +} |
| 79 | + |
| 80 | +/// \brief A simple array reversal method. |
| 81 | +void reverse(uint8_t* buffer, size_t size) |
| 82 | +{ |
| 83 | + uint8_t tmp; |
| 84 | + |
| 85 | + for (int i=0; i < size / 2; i++) |
| 86 | + { |
| 87 | + tmp = buffer[i]; |
| 88 | + buffer[i] = buffer[size-i-1]; |
| 89 | + buffer[size-i-1] = tmp; |
| 90 | + } |
| 91 | +} |
| 92 | + |
0 commit comments