This is a data logger program to be implemented with an instrument amplifier.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Base.h Source File

Base.h

00001 /* mbed Microcontroller Library - Base
00002  * Copyright (c) 2006-2008 ARM Limited. All rights reserved.
00003  * sford, jbrawn
00004  */
00005  
00006 #ifndef MBED_BASE_H
00007 #define MBED_BASE_H
00008 
00009 #include "platform.h"
00010 #include "PinNames.h"
00011 #include "PeripheralNames.h"
00012 #include <cstdlib>
00013 #include "DirHandle.h"
00014 
00015 namespace mbed {
00016 
00017 #ifdef MBED_RPC
00018 struct rpc_function {
00019     const char *name;
00020     void (*caller)(const char*, char*);
00021 };
00022 
00023 struct rpc_class {
00024     const char *name;
00025     const rpc_function *static_functions;
00026     struct rpc_class *next;
00027 };
00028 #endif
00029 
00030 /* Class Base
00031  *  The base class for most things
00032  */
00033 class Base {
00034 
00035 public: 
00036     
00037     Base(const char *name = NULL);
00038 
00039     virtual ~Base();
00040 
00041     /* Function register_object
00042      *  Registers this object with the given name, so that it can be
00043      *  looked up with lookup. If this object has already been
00044      *  registered, then this just changes the name.
00045      *
00046      * Variables
00047      *   name - The name to give the object. If NULL we do nothing.
00048      */
00049     void register_object(const char *name);
00050 
00051     /* Function name
00052      *  Returns the name of the object, or NULL if it has no name.
00053      */
00054     const char *name();
00055 
00056 #ifdef MBED_RPC
00057 
00058     /* Function rpc
00059      *  Call the given method with the given arguments, and write the
00060      *  result into the string pointed to by result. The default
00061      *  implementation calls rpc_methods to determine the supported
00062      *  methods.
00063      *
00064      * Variables
00065      *  method - The name of the method to call.
00066      *  arguments - A list of arguments separated by spaces.
00067      *  result - A pointer to a string to write the result into. May
00068      *    be NULL, in which case nothing is written.
00069      *
00070      *  Returns
00071      *    true if method corresponds to a valid rpc method, or
00072      *    false otherwise.
00073      */
00074     virtual bool rpc(const char *method, const char *arguments, char *result);  
00075 
00076     /* Function get_rpc_methods
00077      *  Returns a pointer to an array describing the rpc methods
00078      *  supported by this object, terminated by either
00079      *  RPC_METHOD_END or RPC_METHOD_SUPER(Superclass).
00080      *
00081      * Example
00082      * > class Example : public Base {
00083      * >   int foo(int a, int b) { return a + b; }
00084      * >   virtual const struct rpc_method *get_rpc_methods() {
00085      * >     static const rpc_method rpc_methods[] = {
00086      * >       { "foo", generic_caller<int, Example, int, int, &Example::foo> },
00087      * >       RPC_METHOD_SUPER(Base)
00088      * >     };
00089      * >     return rpc_methods;
00090      * >   }
00091      * > };
00092      */
00093     virtual const struct rpc_method *get_rpc_methods();
00094 
00095     /* Function rpc
00096      *  Use the lookup function to lookup an object and, if
00097      *  successful, call its rpc method
00098      *
00099      * Variables
00100      *  returns - false if name does not correspond to an object,
00101      *    otherwise the return value of the call to the object's rpc
00102      *    method.
00103      */
00104     static bool rpc(const char *name, const char *method, const char *arguments, char *result);
00105 
00106 #endif
00107 
00108     /* Function lookup
00109      *  Lookup and return the object that has the given name.
00110      *
00111      * Variables
00112      *  name - the name to lookup.
00113      *  len - the length of name.
00114      */
00115     static Base *lookup(const char *name, unsigned int len);
00116 
00117     static DirHandle *opendir();
00118     friend class BaseDirHandle;
00119 
00120 protected: 
00121 
00122     static Base *_head;
00123     Base *_next;
00124     const char *_name;
00125     bool _from_construct;
00126 
00127 private:
00128 
00129 #ifdef MBED_RPC
00130     static rpc_class *_classes;
00131 
00132     static const rpc_function _base_funcs[];
00133     static rpc_class _base_class;
00134 #endif
00135 
00136     void delete_self();
00137     static void list_objs(const char *arguments, char *result);
00138     static void clear(const char*,char*);
00139 
00140     static char *new_name(Base *p);
00141 
00142 public:
00143 
00144 #ifdef MBED_RPC
00145     /* Function add_rpc_class
00146      *  Add the class to the list of classes which can have static
00147      *  methods called via rpc (the static methods which can be called
00148      *  are defined by that class' get_rpc_class() static method).
00149      */
00150     template<class C>
00151     static void add_rpc_class() {
00152         rpc_class *c = C::get_rpc_class();
00153         c->next = _classes;
00154         _classes = c;
00155     }
00156 
00157     template<class C> 
00158     static const char *construct() {
00159         Base *p = new C();
00160         p->_from_construct = true;
00161         if(p->_name==NULL) {
00162             p->register_object(new_name(p));
00163         }
00164         return p->_name;
00165     }
00166 
00167     template<class C, typename A1> 
00168     static const char *construct(A1 arg1) {
00169         Base *p = new C(arg1);
00170         p->_from_construct = true;
00171         if(p->_name==NULL) {
00172             p->register_object(new_name(p));
00173         }
00174         return p->_name;
00175     }
00176 
00177     template<class C, typename A1, typename A2> 
00178     static const char *construct(A1 arg1, A2 arg2) {
00179         Base *p = new C(arg1,arg2);
00180         p->_from_construct = true;
00181         if(p->_name==NULL) {
00182             p->register_object(new_name(p));
00183         }
00184         return p->_name;
00185     }
00186 
00187     template<class C, typename A1, typename A2, typename A3> 
00188     static const char *construct(A1 arg1, A2 arg2, A3 arg3) {
00189         Base *p = new C(arg1,arg2,arg3);
00190         p->_from_construct = true;
00191         if(p->_name==NULL) {
00192             p->register_object(new_name(p));
00193         }
00194         return p->_name;
00195     }
00196 
00197     template<class C, typename A1, typename A2, typename A3, typename A4> 
00198     static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) {
00199         Base *p = new C(arg1,arg2,arg3,arg4);
00200         p->_from_construct = true;
00201         if(p->_name==NULL) {
00202             p->register_object(new_name(p));
00203         }
00204         return p->_name;
00205     }
00206 #endif
00207 
00208 };
00209 
00210 /* Macro MBED_OBJECT_NAME_MAX
00211  *  The maximum size of object name (including terminating null byte)
00212  *  that will be recognised when using fopen to open a FileLike
00213  *  object, or when using the rpc function.
00214  */ 
00215 #define MBED_OBJECT_NAME_MAX 32
00216 
00217 /* Macro MBED_METHOD_NAME_MAX
00218  *  The maximum size of rpc method name (including terminating null
00219  *  byte) that will be recognised by the rpc function (in rpc.h).
00220  */ 
00221 #define MBED_METHOD_NAME_MAX 32
00222 
00223 } // namespace mbed
00224 
00225 #endif
00226