This is my quadcopter prototype software, still in development!
quadv3/mbed/Base.h@0:978110f7f027, 2013-01-30 (annotated)
- Committer:
- Anaesthetix
- Date:
- Wed Jan 30 13:14:44 2013 +0000
- Revision:
- 0:978110f7f027
My quadcopter prototype software, still in development.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anaesthetix | 0:978110f7f027 | 1 | /* mbed Microcontroller Library - Base |
Anaesthetix | 0:978110f7f027 | 2 | * Copyright (c) 2006-2008 ARM Limited. All rights reserved. |
Anaesthetix | 0:978110f7f027 | 3 | */ |
Anaesthetix | 0:978110f7f027 | 4 | |
Anaesthetix | 0:978110f7f027 | 5 | #ifndef MBED_BASE_H |
Anaesthetix | 0:978110f7f027 | 6 | #define MBED_BASE_H |
Anaesthetix | 0:978110f7f027 | 7 | |
Anaesthetix | 0:978110f7f027 | 8 | #include "platform.h" |
Anaesthetix | 0:978110f7f027 | 9 | #include "PinNames.h" |
Anaesthetix | 0:978110f7f027 | 10 | #include "PeripheralNames.h" |
Anaesthetix | 0:978110f7f027 | 11 | #include <cstdlib> |
Anaesthetix | 0:978110f7f027 | 12 | #include "DirHandle.h" |
Anaesthetix | 0:978110f7f027 | 13 | |
Anaesthetix | 0:978110f7f027 | 14 | namespace mbed { |
Anaesthetix | 0:978110f7f027 | 15 | |
Anaesthetix | 0:978110f7f027 | 16 | #ifdef MBED_RPC |
Anaesthetix | 0:978110f7f027 | 17 | struct rpc_function { |
Anaesthetix | 0:978110f7f027 | 18 | const char *name; |
Anaesthetix | 0:978110f7f027 | 19 | void (*caller)(const char*, char*); |
Anaesthetix | 0:978110f7f027 | 20 | }; |
Anaesthetix | 0:978110f7f027 | 21 | |
Anaesthetix | 0:978110f7f027 | 22 | struct rpc_class { |
Anaesthetix | 0:978110f7f027 | 23 | const char *name; |
Anaesthetix | 0:978110f7f027 | 24 | const rpc_function *static_functions; |
Anaesthetix | 0:978110f7f027 | 25 | struct rpc_class *next; |
Anaesthetix | 0:978110f7f027 | 26 | }; |
Anaesthetix | 0:978110f7f027 | 27 | #endif |
Anaesthetix | 0:978110f7f027 | 28 | |
Anaesthetix | 0:978110f7f027 | 29 | /* Class Base |
Anaesthetix | 0:978110f7f027 | 30 | * The base class for most things |
Anaesthetix | 0:978110f7f027 | 31 | */ |
Anaesthetix | 0:978110f7f027 | 32 | class Base { |
Anaesthetix | 0:978110f7f027 | 33 | |
Anaesthetix | 0:978110f7f027 | 34 | public: |
Anaesthetix | 0:978110f7f027 | 35 | |
Anaesthetix | 0:978110f7f027 | 36 | Base(const char *name = NULL); |
Anaesthetix | 0:978110f7f027 | 37 | |
Anaesthetix | 0:978110f7f027 | 38 | virtual ~Base(); |
Anaesthetix | 0:978110f7f027 | 39 | |
Anaesthetix | 0:978110f7f027 | 40 | /* Function register_object |
Anaesthetix | 0:978110f7f027 | 41 | * Registers this object with the given name, so that it can be |
Anaesthetix | 0:978110f7f027 | 42 | * looked up with lookup. If this object has already been |
Anaesthetix | 0:978110f7f027 | 43 | * registered, then this just changes the name. |
Anaesthetix | 0:978110f7f027 | 44 | * |
Anaesthetix | 0:978110f7f027 | 45 | * Variables |
Anaesthetix | 0:978110f7f027 | 46 | * name - The name to give the object. If NULL we do nothing. |
Anaesthetix | 0:978110f7f027 | 47 | */ |
Anaesthetix | 0:978110f7f027 | 48 | void register_object(const char *name); |
Anaesthetix | 0:978110f7f027 | 49 | |
Anaesthetix | 0:978110f7f027 | 50 | /* Function name |
Anaesthetix | 0:978110f7f027 | 51 | * Returns the name of the object, or NULL if it has no name. |
Anaesthetix | 0:978110f7f027 | 52 | */ |
Anaesthetix | 0:978110f7f027 | 53 | const char *name(); |
Anaesthetix | 0:978110f7f027 | 54 | |
Anaesthetix | 0:978110f7f027 | 55 | #ifdef MBED_RPC |
Anaesthetix | 0:978110f7f027 | 56 | |
Anaesthetix | 0:978110f7f027 | 57 | /* Function rpc |
Anaesthetix | 0:978110f7f027 | 58 | * Call the given method with the given arguments, and write the |
Anaesthetix | 0:978110f7f027 | 59 | * result into the string pointed to by result. The default |
Anaesthetix | 0:978110f7f027 | 60 | * implementation calls rpc_methods to determine the supported |
Anaesthetix | 0:978110f7f027 | 61 | * methods. |
Anaesthetix | 0:978110f7f027 | 62 | * |
Anaesthetix | 0:978110f7f027 | 63 | * Variables |
Anaesthetix | 0:978110f7f027 | 64 | * method - The name of the method to call. |
Anaesthetix | 0:978110f7f027 | 65 | * arguments - A list of arguments separated by spaces. |
Anaesthetix | 0:978110f7f027 | 66 | * result - A pointer to a string to write the result into. May |
Anaesthetix | 0:978110f7f027 | 67 | * be NULL, in which case nothing is written. |
Anaesthetix | 0:978110f7f027 | 68 | * |
Anaesthetix | 0:978110f7f027 | 69 | * Returns |
Anaesthetix | 0:978110f7f027 | 70 | * true if method corresponds to a valid rpc method, or |
Anaesthetix | 0:978110f7f027 | 71 | * false otherwise. |
Anaesthetix | 0:978110f7f027 | 72 | */ |
Anaesthetix | 0:978110f7f027 | 73 | virtual bool rpc(const char *method, const char *arguments, char *result); |
Anaesthetix | 0:978110f7f027 | 74 | |
Anaesthetix | 0:978110f7f027 | 75 | /* Function get_rpc_methods |
Anaesthetix | 0:978110f7f027 | 76 | * Returns a pointer to an array describing the rpc methods |
Anaesthetix | 0:978110f7f027 | 77 | * supported by this object, terminated by either |
Anaesthetix | 0:978110f7f027 | 78 | * RPC_METHOD_END or RPC_METHOD_SUPER(Superclass). |
Anaesthetix | 0:978110f7f027 | 79 | * |
Anaesthetix | 0:978110f7f027 | 80 | * Example |
Anaesthetix | 0:978110f7f027 | 81 | * > class Example : public Base { |
Anaesthetix | 0:978110f7f027 | 82 | * > int foo(int a, int b) { return a + b; } |
Anaesthetix | 0:978110f7f027 | 83 | * > virtual const struct rpc_method *get_rpc_methods() { |
Anaesthetix | 0:978110f7f027 | 84 | * > static const rpc_method rpc_methods[] = { |
Anaesthetix | 0:978110f7f027 | 85 | * > { "foo", generic_caller<int, Example, int, int, &Example::foo> }, |
Anaesthetix | 0:978110f7f027 | 86 | * > RPC_METHOD_SUPER(Base) |
Anaesthetix | 0:978110f7f027 | 87 | * > }; |
Anaesthetix | 0:978110f7f027 | 88 | * > return rpc_methods; |
Anaesthetix | 0:978110f7f027 | 89 | * > } |
Anaesthetix | 0:978110f7f027 | 90 | * > }; |
Anaesthetix | 0:978110f7f027 | 91 | */ |
Anaesthetix | 0:978110f7f027 | 92 | virtual const struct rpc_method *get_rpc_methods(); |
Anaesthetix | 0:978110f7f027 | 93 | |
Anaesthetix | 0:978110f7f027 | 94 | /* Function rpc |
Anaesthetix | 0:978110f7f027 | 95 | * Use the lookup function to lookup an object and, if |
Anaesthetix | 0:978110f7f027 | 96 | * successful, call its rpc method |
Anaesthetix | 0:978110f7f027 | 97 | * |
Anaesthetix | 0:978110f7f027 | 98 | * Variables |
Anaesthetix | 0:978110f7f027 | 99 | * returns - false if name does not correspond to an object, |
Anaesthetix | 0:978110f7f027 | 100 | * otherwise the return value of the call to the object's rpc |
Anaesthetix | 0:978110f7f027 | 101 | * method. |
Anaesthetix | 0:978110f7f027 | 102 | */ |
Anaesthetix | 0:978110f7f027 | 103 | static bool rpc(const char *name, const char *method, const char *arguments, char *result); |
Anaesthetix | 0:978110f7f027 | 104 | |
Anaesthetix | 0:978110f7f027 | 105 | #endif |
Anaesthetix | 0:978110f7f027 | 106 | |
Anaesthetix | 0:978110f7f027 | 107 | /* Function lookup |
Anaesthetix | 0:978110f7f027 | 108 | * Lookup and return the object that has the given name. |
Anaesthetix | 0:978110f7f027 | 109 | * |
Anaesthetix | 0:978110f7f027 | 110 | * Variables |
Anaesthetix | 0:978110f7f027 | 111 | * name - the name to lookup. |
Anaesthetix | 0:978110f7f027 | 112 | * len - the length of name. |
Anaesthetix | 0:978110f7f027 | 113 | */ |
Anaesthetix | 0:978110f7f027 | 114 | static Base *lookup(const char *name, unsigned int len); |
Anaesthetix | 0:978110f7f027 | 115 | |
Anaesthetix | 0:978110f7f027 | 116 | static DirHandle *opendir(); |
Anaesthetix | 0:978110f7f027 | 117 | friend class BaseDirHandle; |
Anaesthetix | 0:978110f7f027 | 118 | |
Anaesthetix | 0:978110f7f027 | 119 | protected: |
Anaesthetix | 0:978110f7f027 | 120 | |
Anaesthetix | 0:978110f7f027 | 121 | static Base *_head; |
Anaesthetix | 0:978110f7f027 | 122 | Base *_next; |
Anaesthetix | 0:978110f7f027 | 123 | const char *_name; |
Anaesthetix | 0:978110f7f027 | 124 | bool _from_construct; |
Anaesthetix | 0:978110f7f027 | 125 | |
Anaesthetix | 0:978110f7f027 | 126 | private: |
Anaesthetix | 0:978110f7f027 | 127 | |
Anaesthetix | 0:978110f7f027 | 128 | #ifdef MBED_RPC |
Anaesthetix | 0:978110f7f027 | 129 | static rpc_class *_classes; |
Anaesthetix | 0:978110f7f027 | 130 | |
Anaesthetix | 0:978110f7f027 | 131 | static const rpc_function _base_funcs[]; |
Anaesthetix | 0:978110f7f027 | 132 | static rpc_class _base_class; |
Anaesthetix | 0:978110f7f027 | 133 | #endif |
Anaesthetix | 0:978110f7f027 | 134 | |
Anaesthetix | 0:978110f7f027 | 135 | void delete_self(); |
Anaesthetix | 0:978110f7f027 | 136 | static void list_objs(const char *arguments, char *result); |
Anaesthetix | 0:978110f7f027 | 137 | static void clear(const char*,char*); |
Anaesthetix | 0:978110f7f027 | 138 | |
Anaesthetix | 0:978110f7f027 | 139 | static char *new_name(Base *p); |
Anaesthetix | 0:978110f7f027 | 140 | |
Anaesthetix | 0:978110f7f027 | 141 | public: |
Anaesthetix | 0:978110f7f027 | 142 | |
Anaesthetix | 0:978110f7f027 | 143 | #ifdef MBED_RPC |
Anaesthetix | 0:978110f7f027 | 144 | /* Function add_rpc_class |
Anaesthetix | 0:978110f7f027 | 145 | * Add the class to the list of classes which can have static |
Anaesthetix | 0:978110f7f027 | 146 | * methods called via rpc (the static methods which can be called |
Anaesthetix | 0:978110f7f027 | 147 | * are defined by that class' get_rpc_class() static method). |
Anaesthetix | 0:978110f7f027 | 148 | */ |
Anaesthetix | 0:978110f7f027 | 149 | template<class C> |
Anaesthetix | 0:978110f7f027 | 150 | static void add_rpc_class() { |
Anaesthetix | 0:978110f7f027 | 151 | rpc_class *c = C::get_rpc_class(); |
Anaesthetix | 0:978110f7f027 | 152 | c->next = _classes; |
Anaesthetix | 0:978110f7f027 | 153 | _classes = c; |
Anaesthetix | 0:978110f7f027 | 154 | } |
Anaesthetix | 0:978110f7f027 | 155 | |
Anaesthetix | 0:978110f7f027 | 156 | template<class C> |
Anaesthetix | 0:978110f7f027 | 157 | static const char *construct() { |
Anaesthetix | 0:978110f7f027 | 158 | Base *p = new C(); |
Anaesthetix | 0:978110f7f027 | 159 | p->_from_construct = true; |
Anaesthetix | 0:978110f7f027 | 160 | if(p->_name==NULL) { |
Anaesthetix | 0:978110f7f027 | 161 | p->register_object(new_name(p)); |
Anaesthetix | 0:978110f7f027 | 162 | } |
Anaesthetix | 0:978110f7f027 | 163 | return p->_name; |
Anaesthetix | 0:978110f7f027 | 164 | } |
Anaesthetix | 0:978110f7f027 | 165 | |
Anaesthetix | 0:978110f7f027 | 166 | template<class C, typename A1> |
Anaesthetix | 0:978110f7f027 | 167 | static const char *construct(A1 arg1) { |
Anaesthetix | 0:978110f7f027 | 168 | Base *p = new C(arg1); |
Anaesthetix | 0:978110f7f027 | 169 | p->_from_construct = true; |
Anaesthetix | 0:978110f7f027 | 170 | if(p->_name==NULL) { |
Anaesthetix | 0:978110f7f027 | 171 | p->register_object(new_name(p)); |
Anaesthetix | 0:978110f7f027 | 172 | } |
Anaesthetix | 0:978110f7f027 | 173 | return p->_name; |
Anaesthetix | 0:978110f7f027 | 174 | } |
Anaesthetix | 0:978110f7f027 | 175 | |
Anaesthetix | 0:978110f7f027 | 176 | template<class C, typename A1, typename A2> |
Anaesthetix | 0:978110f7f027 | 177 | static const char *construct(A1 arg1, A2 arg2) { |
Anaesthetix | 0:978110f7f027 | 178 | Base *p = new C(arg1,arg2); |
Anaesthetix | 0:978110f7f027 | 179 | p->_from_construct = true; |
Anaesthetix | 0:978110f7f027 | 180 | if(p->_name==NULL) { |
Anaesthetix | 0:978110f7f027 | 181 | p->register_object(new_name(p)); |
Anaesthetix | 0:978110f7f027 | 182 | } |
Anaesthetix | 0:978110f7f027 | 183 | return p->_name; |
Anaesthetix | 0:978110f7f027 | 184 | } |
Anaesthetix | 0:978110f7f027 | 185 | |
Anaesthetix | 0:978110f7f027 | 186 | template<class C, typename A1, typename A2, typename A3> |
Anaesthetix | 0:978110f7f027 | 187 | static const char *construct(A1 arg1, A2 arg2, A3 arg3) { |
Anaesthetix | 0:978110f7f027 | 188 | Base *p = new C(arg1,arg2,arg3); |
Anaesthetix | 0:978110f7f027 | 189 | p->_from_construct = true; |
Anaesthetix | 0:978110f7f027 | 190 | if(p->_name==NULL) { |
Anaesthetix | 0:978110f7f027 | 191 | p->register_object(new_name(p)); |
Anaesthetix | 0:978110f7f027 | 192 | } |
Anaesthetix | 0:978110f7f027 | 193 | return p->_name; |
Anaesthetix | 0:978110f7f027 | 194 | } |
Anaesthetix | 0:978110f7f027 | 195 | |
Anaesthetix | 0:978110f7f027 | 196 | template<class C, typename A1, typename A2, typename A3, typename A4> |
Anaesthetix | 0:978110f7f027 | 197 | static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) { |
Anaesthetix | 0:978110f7f027 | 198 | Base *p = new C(arg1,arg2,arg3,arg4); |
Anaesthetix | 0:978110f7f027 | 199 | p->_from_construct = true; |
Anaesthetix | 0:978110f7f027 | 200 | if(p->_name==NULL) { |
Anaesthetix | 0:978110f7f027 | 201 | p->register_object(new_name(p)); |
Anaesthetix | 0:978110f7f027 | 202 | } |
Anaesthetix | 0:978110f7f027 | 203 | return p->_name; |
Anaesthetix | 0:978110f7f027 | 204 | } |
Anaesthetix | 0:978110f7f027 | 205 | #endif |
Anaesthetix | 0:978110f7f027 | 206 | |
Anaesthetix | 0:978110f7f027 | 207 | }; |
Anaesthetix | 0:978110f7f027 | 208 | |
Anaesthetix | 0:978110f7f027 | 209 | /* Macro MBED_OBJECT_NAME_MAX |
Anaesthetix | 0:978110f7f027 | 210 | * The maximum size of object name (including terminating null byte) |
Anaesthetix | 0:978110f7f027 | 211 | * that will be recognised when using fopen to open a FileLike |
Anaesthetix | 0:978110f7f027 | 212 | * object, or when using the rpc function. |
Anaesthetix | 0:978110f7f027 | 213 | */ |
Anaesthetix | 0:978110f7f027 | 214 | #define MBED_OBJECT_NAME_MAX 32 |
Anaesthetix | 0:978110f7f027 | 215 | |
Anaesthetix | 0:978110f7f027 | 216 | /* Macro MBED_METHOD_NAME_MAX |
Anaesthetix | 0:978110f7f027 | 217 | * The maximum size of rpc method name (including terminating null |
Anaesthetix | 0:978110f7f027 | 218 | * byte) that will be recognised by the rpc function (in rpc.h). |
Anaesthetix | 0:978110f7f027 | 219 | */ |
Anaesthetix | 0:978110f7f027 | 220 | #define MBED_METHOD_NAME_MAX 32 |
Anaesthetix | 0:978110f7f027 | 221 | |
Anaesthetix | 0:978110f7f027 | 222 | } // namespace mbed |
Anaesthetix | 0:978110f7f027 | 223 | |
Anaesthetix | 0:978110f7f027 | 224 | #endif |
Anaesthetix | 0:978110f7f027 | 225 |