json test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include <json/config.h>
00004 #include <json/value.h>
00005 #include <json/writer.h>
00006 #include <json/config.h>
00007 #include <json/json.h>
00008 #include <cstring>
00009 #include <limits>
00010 #include <sstream>
00011 #include <string>
00012 #include <iomanip>
00013 #include <stdio.h>
00014 #include <deque>
00015 
00016 DigitalOut led1(LED1);
00017 Serial pc(USBTX, USBRX);
00018 
00019 bool running;
00020 Thread thread;
00021 
00022 // Blink function toggles the led in a long running loop
00023 void blink(DigitalOut *led) {
00024     while (running) {
00025         *led = !*led;
00026         wait(1);
00027     }
00028 }
00029 
00030 void callback_ex() {
00031     // Note: you need to actually read from the serial to clear the RX interrupt
00032     int tmp=pc.getc();
00033     //printf("%c\n", tmp);
00034 }
00035 
00036 // Spawns a thread to run blink for 5 seconds
00037 // main() runs in its own thread in the OS
00038 int main() {
00039     
00040     
00041     pc.baud(9600);
00042     pc.attach(&callback_ex);
00043     
00044     thread.start(callback(blink, &led1));
00045     
00046     running = true;
00047 
00048     
00049     //thread_gsensor.start(callback(gsensorThread));
00050     //wait(1);
00051     //thread_motor.start(callback(motorThread));
00052     //wait(1);
00053     //thread_sonar.start(callback(sonarThread));
00054     
00055     //Thread thread_sonar(sonarThread);
00056     
00057      Json::Reader reader;
00058 
00059   Json::Value json_object;
00060 
00061   const char* json_document = "{\"age\" : 26,\"name\" : \"huchao\"}";
00062 
00063   if (!reader.parse(json_document, json_object))return 0;
00064 
00065   printf("name : %s\n",json_object["name"]);
00066 
00067   printf("name : %s\n",json_object["age"]);
00068 }
00069 
00070 
00071 /*
00072 主程序指定中断函数
00073 flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
00074 
00075 主程序指定 类 的 方法
00076 t.attach(callback(&f, &Flipper::flip), 2.0); // the address of the object, member function, and interval
00077 
00078 
00079 1、线程
00080 需加载mbed-os
00081 
00082 #include "mbed.h"
00083 
00084 Thread thread;
00085 DigitalOut led1(LED1);
00086 volatile bool running = true;
00087 
00088 // Blink function toggles the led in a long running loop
00089 void blink(DigitalOut *led) {
00090     while (running) {
00091         *led = !*led;
00092         wait(1);
00093     }
00094 }
00095 
00096 // Spawns a thread to run blink for 5 seconds
00097 int main() {
00098     thread.start(callback(blink, &led1));
00099     wait(5);
00100     running = false;
00101     thread.join();
00102 }
00103 
00104 
00105 2、休眠+按键中断唤醒
00106 需更新mbed
00107 使用F401板时,IDD电流测试,运行时:18mA,灯亮时21 .2mA,睡眠时:6.26 mA,深度睡眠时:0.68 mA,
00108 
00109 RTC唤醒使用第三方类(未测试)
00110 
00111 #include "mbed.h"
00112 
00113 InterruptIn event(USER_BUTTON);
00114 DigitalOut myled(LED1);
00115 
00116 int go_to_sleep = 0;
00117 
00118 void pressed()
00119 {
00120     printf("Button pressed\n");
00121     go_to_sleep = go_to_sleep + 1;
00122     if (go_to_sleep > 3) go_to_sleep = 0;
00123 
00124 }
00125 
00126 int main()
00127 {
00128     int i = 0;
00129 
00130     printf("\nPress Button to enter/exit sleep & deepsleep\n");
00131 
00132     event.fall(&pressed);
00133 
00134     while (1) {
00135 
00136         if ((go_to_sleep == 0) || (go_to_sleep == 2)) {
00137             printf("%d: Running\n", i);
00138             myled = !myled;
00139             wait(1.0);
00140         }
00141 
00142         if (go_to_sleep == 1) {
00143             myled = 0;
00144             printf("%d: Entering sleep (press user button to resume)\n", i);
00145             sleep();
00146         }
00147 
00148         if (go_to_sleep == 3) {
00149             myled = 0;
00150             printf("%d: Entering deepsleep (press user button to resume)\n", i);
00151             deepsleep();
00152         }
00153 
00154         i++;
00155     }
00156 }
00157 
00158 
00159 3、系统定时器中断
00160 #include "mbed.h"
00161 
00162 Ticker toggle_led_ticker;
00163 
00164 DigitalOut led1(LED1);
00165 
00166 void toggle_led() {
00167     led1 = !led1;
00168 }
00169 
00170 int main() {
00171     // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
00172     toggle_led_ticker.attach(&toggle_led, 0.1);
00173     while (true) {
00174         // Do other things...
00175     }
00176 }
00177 
00178 4、超时定时器中断
00179 #include "mbed.h"
00180  
00181 // A class for flip()-ing a DigitalOut 
00182 class Flipper {
00183 public:
00184     Flipper(PinName pin) : _pin(pin) {
00185         _pin = 0;
00186     }
00187     void flip() {
00188         _pin = !_pin;
00189     }
00190 private:
00191     DigitalOut _pin;
00192 };
00193  
00194 DigitalOut led1(LED1);
00195 Flipper f(LED2);
00196 Timeout t;
00197  
00198 int main() {
00199     // the address of the object, member function, and interval
00200     t.attach(callback(&f, &Flipper::flip), 2.0); 
00201  
00202     // spin in a main loop. flipper will interrupt it to call flip
00203     while(1) {
00204         led1 = !led1;
00205         wait(0.2);
00206     }
00207 }
00208             
00209 
00210 5、通用定时器中断
00211 
00212  // Count the time to toggle a LED
00213  
00214  #include "mbed.h"
00215  
00216  Timer timer;
00217  DigitalOut led(LED1);
00218  int begin, end;
00219  
00220  int main() {
00221      timer.start();
00222      begin = timer.read_us();
00223      led = !led;
00224      end = timer.read_us();
00225      printf("Toggle the led takes %d us", end - begin);
00226  }
00227 
00228 6、外部中断附加事件
00229 需加载mbed-os
00230 
00231 #include "mbed.h"
00232 #include "mbed_events.h"
00233 
00234 DigitalOut led1(LED1);
00235 InterruptIn sw(SW2);
00236 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00237 Thread t;
00238 
00239 void rise_handler(void) {
00240     // Toggle LED
00241     led1 = !led1;
00242 }
00243 
00244 void fall_handler(void) {
00245     printf("fall_handler in context %p\r\n", Thread::gettid());
00246     // Toggle LED
00247     led1 = !led1;
00248 }
00249 
00250 int main() {
00251     // Start the event queue
00252     t.start(callback(&queue, &EventQueue::dispatch_forever));
00253     printf("Starting in context %p\r\n", Thread::gettid());
00254     // The 'rise' handler will execute in IRQ context
00255     sw.rise(rise_handler);
00256     // The 'fall' handler will execute in the context of thread 't'
00257     sw.fall(queue.event(fall_handler));
00258 }
00259 
00260 
00261 7、线程间信号量的传递
00262 需加载mbed-os
00263 
00264 #include "mbed.h"
00265  
00266 Thread thread;
00267 DigitalOut led(LED1);
00268  
00269 void led_thread() {
00270     while (true) {
00271         // Signal flags that are reported as event are automatically cleared.
00272         Thread::signal_wait(0x1);
00273         led = !led;
00274     }
00275 }
00276  
00277 int main (void) {
00278     thread.start(callback(led_thread));
00279  
00280     while (true) {
00281         wait(1);
00282         thread.signal_set(0x1);
00283     }
00284 }
00285 
00286 */