A test of creating custom classes for control over RPC. Creates an LED class with member functions callable over serial.
main.cpp@4:840f6002b8c2, 2010-09-29 (annotated)
- Committer:
- JimmyTheHack
- Date:
- Wed Sep 29 01:48:43 2010 +0000
- Revision:
- 4:840f6002b8c2
- Parent:
- 3:d70703d85ff9
separate header file to try to get documentation.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimmyTheHack | 4:840f6002b8c2 | 1 | #include LED.h |
JimmyTheHack | 0:b0ab03e03fdc | 2 | |
JimmyTheHack | 0:b0ab03e03fdc | 3 | |
JimmyTheHack | 1:ddf1739fcbb8 | 4 | /**Initialization Function**/ |
JimmyTheHack | 1:ddf1739fcbb8 | 5 | LED::LED(PinName mypin, const char *name) : Base(name), LEDpin(mypin) { //initialize pin |
JimmyTheHack | 0:b0ab03e03fdc | 6 | LEDpin.write(0); |
JimmyTheHack | 0:b0ab03e03fdc | 7 | state=false; //set LED to off |
JimmyTheHack | 0:b0ab03e03fdc | 8 | } |
JimmyTheHack | 1:ddf1739fcbb8 | 9 | /**switch the state of the LED**/ |
JimmyTheHack | 1:ddf1739fcbb8 | 10 | int LED::toggle() { |
JimmyTheHack | 0:b0ab03e03fdc | 11 | if (state==0) { |
JimmyTheHack | 0:b0ab03e03fdc | 12 | state=1; |
JimmyTheHack | 0:b0ab03e03fdc | 13 | } else { |
JimmyTheHack | 0:b0ab03e03fdc | 14 | state=0; |
JimmyTheHack | 0:b0ab03e03fdc | 15 | } |
JimmyTheHack | 0:b0ab03e03fdc | 16 | LEDpin=state; |
JimmyTheHack | 0:b0ab03e03fdc | 17 | return state; //print the current state of the LED |
JimmyTheHack | 0:b0ab03e03fdc | 18 | } |
JimmyTheHack | 0:b0ab03e03fdc | 19 | |
JimmyTheHack | 1:ddf1739fcbb8 | 20 | /**blink the LED n times**/ |
JimmyTheHack | 1:ddf1739fcbb8 | 21 | void LED::blink(int n=1) { |
JimmyTheHack | 0:b0ab03e03fdc | 22 | do { //blink at least once |
JimmyTheHack | 0:b0ab03e03fdc | 23 | toggle(); //toggle LED state |
JimmyTheHack | 0:b0ab03e03fdc | 24 | wait(.2); |
JimmyTheHack | 0:b0ab03e03fdc | 25 | toggle(); //return LED to original state |
JimmyTheHack | 0:b0ab03e03fdc | 26 | wait(.2); |
JimmyTheHack | 0:b0ab03e03fdc | 27 | n--; |
JimmyTheHack | 0:b0ab03e03fdc | 28 | } while (n>=1); |
JimmyTheHack | 0:b0ab03e03fdc | 29 | } |
JimmyTheHack | 0:b0ab03e03fdc | 30 | |
JimmyTheHack | 0:b0ab03e03fdc | 31 | |
JimmyTheHack | 0:b0ab03e03fdc | 32 | #ifdef MBED_RPC |
JimmyTheHack | 1:ddf1739fcbb8 | 33 | /**Create a list of the available methods which can be called for this class**/ |
JimmyTheHack | 1:ddf1739fcbb8 | 34 | const rpc_method *LED::get_rpc_methods() { |
JimmyTheHack | 0:b0ab03e03fdc | 35 | static const rpc_method rpc_methods[] = { |
JimmyTheHack | 0:b0ab03e03fdc | 36 | { "toggle", rpc_method_caller<int, LED, &LED::toggle> }, //first specify the name string. The arguments to rpc_method_caller appear to be <outputs, RPC class, inputs, reference to function>. |
JimmyTheHack | 0:b0ab03e03fdc | 37 | //In this case, we have one output and no inputs so they are skipped. We must only specify the class of the object the method belongs to and the address of the method. |
JimmyTheHack | 0:b0ab03e03fdc | 38 | |
JimmyTheHack | 0:b0ab03e03fdc | 39 | { "blink", rpc_method_caller<LED, int, &LED::blink> }, //this method has no outputs so they are skipped, but we must specify the input to be of type int. |
JimmyTheHack | 0:b0ab03e03fdc | 40 | RPC_METHOD_SUPER(Base), |
JimmyTheHack | 0:b0ab03e03fdc | 41 | }; |
JimmyTheHack | 0:b0ab03e03fdc | 42 | return rpc_methods; |
JimmyTheHack | 0:b0ab03e03fdc | 43 | } |
JimmyTheHack | 1:ddf1739fcbb8 | 44 | /**Register the class itself as an RPC-callable class**/ |
JimmyTheHack | 1:ddf1739fcbb8 | 45 | rpc_class *LED::get_rpc_class() { |
JimmyTheHack | 0:b0ab03e03fdc | 46 | static const rpc_function funcs[] = { |
JimmyTheHack | 0:b0ab03e03fdc | 47 | { "new", rpc_function_caller<const char*, PinName, const char*, &Base::construct<LED,PinName,const char*> > }, //still don't fully understand the arguments in this line. I suppose the first argument may just be an output echo of the name. |
JimmyTheHack | 0:b0ab03e03fdc | 48 | RPC_METHOD_END |
JimmyTheHack | 0:b0ab03e03fdc | 49 | }; |
JimmyTheHack | 0:b0ab03e03fdc | 50 | static rpc_class c = { "LED", funcs, NULL }; |
JimmyTheHack | 0:b0ab03e03fdc | 51 | return &c; |
JimmyTheHack | 0:b0ab03e03fdc | 52 | } |
JimmyTheHack | 0:b0ab03e03fdc | 53 | #endif // MBED_RPC |
JimmyTheHack | 0:b0ab03e03fdc | 54 | |
JimmyTheHack | 0:b0ab03e03fdc | 55 | |
JimmyTheHack | 0:b0ab03e03fdc | 56 | /* end LED class definition */ |
JimmyTheHack | 0:b0ab03e03fdc | 57 | |
JimmyTheHack | 0:b0ab03e03fdc | 58 | |
JimmyTheHack | 0:b0ab03e03fdc | 59 | |
JimmyTheHack | 0:b0ab03e03fdc | 60 | |
JimmyTheHack | 0:b0ab03e03fdc | 61 | |
JimmyTheHack | 0:b0ab03e03fdc | 62 | Serial pc(USBTX, USBRX); //set up serial communication |
JimmyTheHack | 1:ddf1739fcbb8 | 63 | /**Wait for RPC commands and then call the interpreter**/ |
JimmyTheHack | 1:ddf1739fcbb8 | 64 | int main() { |
JimmyTheHack | 0:b0ab03e03fdc | 65 | // specify which classes we would like to be able to call over LED |
JimmyTheHack | 0:b0ab03e03fdc | 66 | Base::add_rpc_class<Timer>(); //a class included in the core mbed RPC library |
JimmyTheHack | 0:b0ab03e03fdc | 67 | Base::add_rpc_class<LED>(); //my own custom LED class |
JimmyTheHack | 0:b0ab03e03fdc | 68 | |
JimmyTheHack | 0:b0ab03e03fdc | 69 | // receive commands, and send back the responses |
JimmyTheHack | 0:b0ab03e03fdc | 70 | char buf[256], outbuf[256]; |
JimmyTheHack | 0:b0ab03e03fdc | 71 | while (1) { |
JimmyTheHack | 0:b0ab03e03fdc | 72 | pc.gets(buf, 256); //grab serial commands |
JimmyTheHack | 0:b0ab03e03fdc | 73 | rpc(buf, outbuf); //interpret the RPC commands |
JimmyTheHack | 0:b0ab03e03fdc | 74 | //this function only requires an input string and output string, so we can obtain RPC commands via a different interface than serial if desired. |
JimmyTheHack | 0:b0ab03e03fdc | 75 | pc.printf("%s\n", outbuf); //print back over serial |
JimmyTheHack | 0:b0ab03e03fdc | 76 | } |
JimmyTheHack | 0:b0ab03e03fdc | 77 | } |
JimmyTheHack | 0:b0ab03e03fdc | 78 | |
JimmyTheHack | 0:b0ab03e03fdc | 79 | |
JimmyTheHack | 0:b0ab03e03fdc | 80 |