A library to send and receive packets over serial, uses MODSERIAL

Dependents:   SimpleSerialProtocolExample SerialFileReceiver

Committer:
p3p
Date:
Fri Sep 19 15:51:05 2014 +0000
Revision:
3:5caff50e14a7
Parent:
1:98ad30934a71
fixed packet detection when struggling to keep up with data stream

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p3p 0:1639507580d5 1 /*
p3p 0:1639507580d5 2 Copyright (c) 2011 Andy Kirkham
p3p 0:1639507580d5 3
p3p 0:1639507580d5 4 Permission is hereby granted, free of charge, to any person obtaining a copy
p3p 0:1639507580d5 5 of this software and associated documentation files (the "Software"), to deal
p3p 0:1639507580d5 6 in the Software without restriction, including without limitation the rights
p3p 0:1639507580d5 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
p3p 0:1639507580d5 8 copies of the Software, and to permit persons to whom the Software is
p3p 0:1639507580d5 9 furnished to do so, subject to the following conditions:
p3p 0:1639507580d5 10
p3p 0:1639507580d5 11 The above copyright notice and this permission notice shall be included in
p3p 0:1639507580d5 12 all copies or substantial portions of the Software.
p3p 0:1639507580d5 13
p3p 0:1639507580d5 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
p3p 0:1639507580d5 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
p3p 0:1639507580d5 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
p3p 0:1639507580d5 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
p3p 0:1639507580d5 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
p3p 0:1639507580d5 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
p3p 0:1639507580d5 20 THE SOFTWARE.
p3p 1:98ad30934a71 21
p3p 1:98ad30934a71 22 Modified for use with SimpleSerialProtocol by Christopher Pepper
p3p 0:1639507580d5 23 */
p3p 0:1639507580d5 24
p3p 0:1639507580d5 25 #ifndef AJK_FPOINTER_H
p3p 0:1639507580d5 26 #define AJK_FPOINTER_H
p3p 0:1639507580d5 27
p3p 0:1639507580d5 28 namespace SimpleSerialProtocol {
p3p 0:1639507580d5 29
p3p 0:1639507580d5 30 class FPointerDummy;
p3p 1:98ad30934a71 31 class Packet;
p3p 1:98ad30934a71 32 class Protocol;
p3p 0:1639507580d5 33
p3p 0:1639507580d5 34 typedef Packet* Packet_ptr;
p3p 1:98ad30934a71 35 typedef Protocol* Protocol_ptr;
p3p 0:1639507580d5 36
p3p 0:1639507580d5 37 class FPointer {
p3p 0:1639507580d5 38
p3p 0:1639507580d5 39 protected:
p3p 0:1639507580d5 40
p3p 0:1639507580d5 41 //! C callback function pointer.
p3p 1:98ad30934a71 42 void (*c_callback)(Protocol_ptr, Packet_ptr);
p3p 0:1639507580d5 43
p3p 0:1639507580d5 44 //! C++ callback object/method pointer (the object part).
p3p 0:1639507580d5 45 FPointerDummy *obj_callback;
p3p 0:1639507580d5 46
p3p 0:1639507580d5 47 //! C++ callback object/method pointer (the method part).
p3p 1:98ad30934a71 48 void (FPointerDummy::*method_callback)(Protocol_ptr, Packet_ptr);
p3p 0:1639507580d5 49
p3p 0:1639507580d5 50 public:
p3p 0:1639507580d5 51
p3p 0:1639507580d5 52 /** Constructor
p3p 0:1639507580d5 53 */
p3p 0:1639507580d5 54 FPointer() {
p3p 0:1639507580d5 55 c_callback = NULL;
p3p 0:1639507580d5 56 obj_callback = NULL;
p3p 0:1639507580d5 57 method_callback = NULL;
p3p 0:1639507580d5 58 }
p3p 0:1639507580d5 59
p3p 0:1639507580d5 60
p3p 1:98ad30934a71 61 void attach(void (*function)(Protocol_ptr, Packet_ptr) = 0) {
p3p 0:1639507580d5 62 c_callback = function;
p3p 0:1639507580d5 63 }
p3p 0:1639507580d5 64
p3p 0:1639507580d5 65 template<class T>
p3p 0:1639507580d5 66 void attach(T* item, void (T::*method)(Packet_ptr)) {
p3p 0:1639507580d5 67 obj_callback = (FPointerDummy *)item;
p3p 1:98ad30934a71 68 method_callback = (void (FPointerDummy::*)(Protocol_ptr, Packet_ptr))method;
p3p 0:1639507580d5 69 }
p3p 0:1639507580d5 70
p3p 1:98ad30934a71 71 void call(Protocol_ptr comms, Packet_ptr arg) {
p3p 0:1639507580d5 72 if (c_callback != NULL) {
p3p 1:98ad30934a71 73 return (*c_callback)(comms, arg);
p3p 0:1639507580d5 74 } else {
p3p 0:1639507580d5 75 if (obj_callback != NULL && method_callback != NULL) {
p3p 1:98ad30934a71 76 (obj_callback->*method_callback)(comms, arg);
p3p 0:1639507580d5 77 return;
p3p 0:1639507580d5 78 }
p3p 0:1639507580d5 79 }
p3p 0:1639507580d5 80 return;
p3p 0:1639507580d5 81 }
p3p 0:1639507580d5 82
p3p 0:1639507580d5 83 void call(void) {
p3p 0:1639507580d5 84 if (c_callback != NULL) {
p3p 1:98ad30934a71 85 (*c_callback)((Protocol_ptr)NULL, (Packet_ptr)NULL);
p3p 0:1639507580d5 86 return;
p3p 0:1639507580d5 87 } else {
p3p 0:1639507580d5 88 if (obj_callback != NULL && method_callback != NULL) {
p3p 1:98ad30934a71 89 (obj_callback->*method_callback)((Protocol_ptr)NULL, (Packet_ptr)NULL);
p3p 0:1639507580d5 90 return;
p3p 0:1639507580d5 91 }
p3p 0:1639507580d5 92 }
p3p 0:1639507580d5 93 return;
p3p 0:1639507580d5 94 }
p3p 0:1639507580d5 95 };
p3p 0:1639507580d5 96
p3p 0:1639507580d5 97 };
p3p 0:1639507580d5 98
p3p 0:1639507580d5 99
p3p 0:1639507580d5 100 #endif