Lab 1 Program C

Dependents:   Lab1C

Fork of mbed by -deleted-

Committer:
simon.ford@mbed.co.uk
Date:
Thu Jan 22 18:32:40 2009 +0000
Revision:
5:62573be585e9
Parent:
4:5d1359a283bc
Child:
7:15d74db76485
* Added initial RPC release
* Added RTC and helper functions
* Added read_u16()/write_u16() to AnalogIn/Out
* Ticker/Timeout timing fixed!
* mbedinfo() helper added
* error() and printf() added to replace DEBUG() and ERROR()
* DigitalIn supports methods on rise/fall
* SPI and Serial support NC
* LED1-4 also map to 1-4
* Timer object reset fixed
* SPI uses single mode
* SPI3 added

Who changed what in which revision?

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