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 00036 char *myrpc::echo(const char * in) { 00037 printf("DEBUG myrpc::echo() IN=%s\r\n",in); 00038 sprintf(_buffer, "%s", in); 00039 return _buffer; 00040 } 00041 00042 void myrpc::settime(const char *t) { 00043 time_t seconds = time(NULL); 00044 if (seconds == (unsigned)-1 || seconds == 0) { 00045 seconds = 1256729737; // Set RTC time to Wed, 28 Oct 2009 11:35:37 00046 } 00047 // set_time(seconds); 00048 SNTPSetTime(seconds); 00049 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&seconds)); 00050 printf("settime(%s) seconds=%d datetime=%s\r\n", t, seconds, _buffer); 00051 } 00052 char *myrpc::gettime(const char *fmt="%A %m/%d/%Y %H:%M:%S") { 00053 // time_t seconds = time(NULL); 00054 time_t seconds = SNTPTime(); 00055 //printf("Time as seconds since January 1, 1970 = %d\r\n", seconds); 00056 //printf("Time as a basic string = %s\r\n", ctime(&seconds)); 00057 if (!fmt || !fmt[0]) 00058 fmt = "%A %m/%d/%Y %H:%M:%S"; 00059 strftime(_buffer, sizeof(_buffer), fmt, localtime(&seconds)); 00060 return _buffer; 00061 } 00062 00063 #ifdef MBED_RPC 00064 const rpc_method *myrpc::get_rpc_methods() { 00065 static const rpc_method rpc_methods[] = { 00066 { "debug", rpc_method_caller<myrpc, int, &myrpc::debug> }, 00067 { "blink", rpc_method_caller<myrpc, int, &myrpc::blink> }, 00068 { "echo", rpc_method_caller<char *, myrpc, const char *, &myrpc::echo> }, 00069 { "settime", rpc_method_caller<myrpc, const char *, &myrpc::settime> }, 00070 { "gettime", rpc_method_caller<char *, myrpc, const char *, &myrpc::gettime> }, 00071 RPC_METHOD_SUPER(Base) 00072 }; 00073 return rpc_methods; 00074 } 00075 rpc_class *myrpc::get_rpc_class() { 00076 static const rpc_function funcs[] = { 00077 "new", rpc_function_caller<const char*, PinName, const char*, &Base::construct<myrpc,PinName,const char*> >, 00078 RPC_METHOD_END 00079 }; 00080 static rpc_class c = { "myrpc", funcs, NULL }; 00081 return &c; 00082 } 00083 #endif 00084 00085 } // namespace mbed
Generated on Tue Jul 12 2022 20:39:37 by
1.7.2