Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
myrpc.cpp
00001 // myrpc.cpp 00002 // 00003 // Based on: 00004 // http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.h 00005 // http://mbed.org/projects/cookbook/svn/Servo/trunk/Servo.cpp 00006 // http://mbed.org/forum/topic/234/ 00007 00008 #include "myrpc.h" 00009 #ifdef MBED_RPC 00010 #include "rpc.h" 00011 #endif 00012 00013 #include "SNTPClient.h" 00014 00015 extern int gDebug; 00016 00017 namespace mbed { 00018 00019 myrpc::myrpc(PinName pin, const char *name) : Base(name), _pin(pin) {} 00020 00021 void myrpc::debug(int val) { 00022 gDebug=val; 00023 printf("Debug is %s\r\n", gDebug ? "ON" : "OFF"); 00024 } 00025 00026 void myrpc::blink(int n) { 00027 for (int i=0; i<n; i++) { 00028 _pin = 1; 00029 wait(0.2); 00030 _pin = 0; 00031 wait(0.2); 00032 } 00033 } 00034 00035 char *myrpc::echo(const char * in) { 00036 printf("DEBUG myrpc::echo() IN=%s\r\n",in); 00037 sprintf(_buffer, "%s", in); 00038 return _buffer; 00039 } 00040 00041 void myrpc::settime(const char *t) { 00042 time_t seconds = time(NULL); 00043 if (seconds == (unsigned)-1 || seconds == 0) { 00044 seconds = 1256729737; // Set RTC time to Wed, 28 Oct 2009 11:35:37 00045 } 00046 // set_time(seconds); 00047 SNTPSetTime(seconds); 00048 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&seconds)); 00049 printf("settime(%s) seconds=%d datetime=%s\r\n", t, seconds, _buffer); 00050 } 00051 char *myrpc::gettime(const char *fmt="%A %m/%d/%Y %H:%M:%S") { 00052 // time_t seconds = time(NULL); 00053 time_t seconds = SNTPTime(); 00054 //printf("Time as seconds since January 1, 1970 = %d\r\n", seconds); 00055 //printf("Time as a basic string = %s\r\n", ctime(&seconds)); 00056 if (!fmt || !fmt[0]) 00057 fmt = "%A %m/%d/%Y %H:%M:%S"; 00058 strftime(_buffer, sizeof(_buffer), fmt, localtime(&seconds)); 00059 return _buffer; 00060 } 00061 00062 #ifdef MBED_RPC 00063 const rpc_method *myrpc::get_rpc_methods() { 00064 static const rpc_method rpc_methods[] = { 00065 { "debug", rpc_method_caller<myrpc, int, &myrpc::debug> }, 00066 { "blink", rpc_method_caller<myrpc, int, &myrpc::blink> }, 00067 { "echo", rpc_method_caller<char *, myrpc, const char *, &myrpc::echo> }, 00068 { "settime", rpc_method_caller<myrpc, const char *, &myrpc::settime> }, 00069 { "gettime", rpc_method_caller<char *, myrpc, const char *, &myrpc::gettime> }, 00070 RPC_METHOD_SUPER(Base) 00071 }; 00072 return rpc_methods; 00073 } 00074 rpc_class *myrpc::get_rpc_class() { 00075 static const rpc_function funcs[] = { 00076 "new", rpc_function_caller<const char*, PinName, const char*, &Base::construct<myrpc,PinName,const char*> >, 00077 RPC_METHOD_END 00078 }; 00079 static rpc_class c = { "myrpc", funcs, NULL }; 00080 return &c; 00081 } 00082 #endif 00083 00084 } // namespace mbed
Generated on Tue Jul 12 2022 17:12:14 by
1.7.2