涂 桂旺
/
mbed-os-example-cj
json test
main.cpp@0:2ee762ea11b3, 2018-01-26 (annotated)
- Committer:
- tgw
- Date:
- Fri Jan 26 06:05:31 2018 +0000
- Revision:
- 0:2ee762ea11b3
json
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tgw | 0:2ee762ea11b3 | 1 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 2 | |
tgw | 0:2ee762ea11b3 | 3 | #include <json/config.h> |
tgw | 0:2ee762ea11b3 | 4 | #include <json/value.h> |
tgw | 0:2ee762ea11b3 | 5 | #include <json/writer.h> |
tgw | 0:2ee762ea11b3 | 6 | #include <json/config.h> |
tgw | 0:2ee762ea11b3 | 7 | #include <json/json.h> |
tgw | 0:2ee762ea11b3 | 8 | #include <cstring> |
tgw | 0:2ee762ea11b3 | 9 | #include <limits> |
tgw | 0:2ee762ea11b3 | 10 | #include <sstream> |
tgw | 0:2ee762ea11b3 | 11 | #include <string> |
tgw | 0:2ee762ea11b3 | 12 | #include <iomanip> |
tgw | 0:2ee762ea11b3 | 13 | #include <stdio.h> |
tgw | 0:2ee762ea11b3 | 14 | #include <deque> |
tgw | 0:2ee762ea11b3 | 15 | |
tgw | 0:2ee762ea11b3 | 16 | DigitalOut led1(LED1); |
tgw | 0:2ee762ea11b3 | 17 | Serial pc(USBTX, USBRX); |
tgw | 0:2ee762ea11b3 | 18 | |
tgw | 0:2ee762ea11b3 | 19 | bool running; |
tgw | 0:2ee762ea11b3 | 20 | Thread thread; |
tgw | 0:2ee762ea11b3 | 21 | |
tgw | 0:2ee762ea11b3 | 22 | // Blink function toggles the led in a long running loop |
tgw | 0:2ee762ea11b3 | 23 | void blink(DigitalOut *led) { |
tgw | 0:2ee762ea11b3 | 24 | while (running) { |
tgw | 0:2ee762ea11b3 | 25 | *led = !*led; |
tgw | 0:2ee762ea11b3 | 26 | wait(1); |
tgw | 0:2ee762ea11b3 | 27 | } |
tgw | 0:2ee762ea11b3 | 28 | } |
tgw | 0:2ee762ea11b3 | 29 | |
tgw | 0:2ee762ea11b3 | 30 | void callback_ex() { |
tgw | 0:2ee762ea11b3 | 31 | // Note: you need to actually read from the serial to clear the RX interrupt |
tgw | 0:2ee762ea11b3 | 32 | int tmp=pc.getc(); |
tgw | 0:2ee762ea11b3 | 33 | //printf("%c\n", tmp); |
tgw | 0:2ee762ea11b3 | 34 | } |
tgw | 0:2ee762ea11b3 | 35 | |
tgw | 0:2ee762ea11b3 | 36 | // Spawns a thread to run blink for 5 seconds |
tgw | 0:2ee762ea11b3 | 37 | // main() runs in its own thread in the OS |
tgw | 0:2ee762ea11b3 | 38 | int main() { |
tgw | 0:2ee762ea11b3 | 39 | |
tgw | 0:2ee762ea11b3 | 40 | |
tgw | 0:2ee762ea11b3 | 41 | pc.baud(9600); |
tgw | 0:2ee762ea11b3 | 42 | pc.attach(&callback_ex); |
tgw | 0:2ee762ea11b3 | 43 | |
tgw | 0:2ee762ea11b3 | 44 | thread.start(callback(blink, &led1)); |
tgw | 0:2ee762ea11b3 | 45 | |
tgw | 0:2ee762ea11b3 | 46 | running = true; |
tgw | 0:2ee762ea11b3 | 47 | |
tgw | 0:2ee762ea11b3 | 48 | |
tgw | 0:2ee762ea11b3 | 49 | //thread_gsensor.start(callback(gsensorThread)); |
tgw | 0:2ee762ea11b3 | 50 | //wait(1); |
tgw | 0:2ee762ea11b3 | 51 | //thread_motor.start(callback(motorThread)); |
tgw | 0:2ee762ea11b3 | 52 | //wait(1); |
tgw | 0:2ee762ea11b3 | 53 | //thread_sonar.start(callback(sonarThread)); |
tgw | 0:2ee762ea11b3 | 54 | |
tgw | 0:2ee762ea11b3 | 55 | //Thread thread_sonar(sonarThread); |
tgw | 0:2ee762ea11b3 | 56 | |
tgw | 0:2ee762ea11b3 | 57 | Json::Reader reader; |
tgw | 0:2ee762ea11b3 | 58 | |
tgw | 0:2ee762ea11b3 | 59 | Json::Value json_object; |
tgw | 0:2ee762ea11b3 | 60 | |
tgw | 0:2ee762ea11b3 | 61 | const char* json_document = "{\"age\" : 26,\"name\" : \"huchao\"}"; |
tgw | 0:2ee762ea11b3 | 62 | |
tgw | 0:2ee762ea11b3 | 63 | if (!reader.parse(json_document, json_object))return 0; |
tgw | 0:2ee762ea11b3 | 64 | |
tgw | 0:2ee762ea11b3 | 65 | printf("name : %s\n",json_object["name"]); |
tgw | 0:2ee762ea11b3 | 66 | |
tgw | 0:2ee762ea11b3 | 67 | printf("name : %s\n",json_object["age"]); |
tgw | 0:2ee762ea11b3 | 68 | } |
tgw | 0:2ee762ea11b3 | 69 | |
tgw | 0:2ee762ea11b3 | 70 | |
tgw | 0:2ee762ea11b3 | 71 | /* |
tgw | 0:2ee762ea11b3 | 72 | 主程序指定中断函数 |
tgw | 0:2ee762ea11b3 | 73 | flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds) |
tgw | 0:2ee762ea11b3 | 74 | |
tgw | 0:2ee762ea11b3 | 75 | 主程序指定 类 的 方法 |
tgw | 0:2ee762ea11b3 | 76 | t.attach(callback(&f, &Flipper::flip), 2.0); // the address of the object, member function, and interval |
tgw | 0:2ee762ea11b3 | 77 | |
tgw | 0:2ee762ea11b3 | 78 | |
tgw | 0:2ee762ea11b3 | 79 | 1、线程 |
tgw | 0:2ee762ea11b3 | 80 | 需加载mbed-os |
tgw | 0:2ee762ea11b3 | 81 | |
tgw | 0:2ee762ea11b3 | 82 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 83 | |
tgw | 0:2ee762ea11b3 | 84 | Thread thread; |
tgw | 0:2ee762ea11b3 | 85 | DigitalOut led1(LED1); |
tgw | 0:2ee762ea11b3 | 86 | volatile bool running = true; |
tgw | 0:2ee762ea11b3 | 87 | |
tgw | 0:2ee762ea11b3 | 88 | // Blink function toggles the led in a long running loop |
tgw | 0:2ee762ea11b3 | 89 | void blink(DigitalOut *led) { |
tgw | 0:2ee762ea11b3 | 90 | while (running) { |
tgw | 0:2ee762ea11b3 | 91 | *led = !*led; |
tgw | 0:2ee762ea11b3 | 92 | wait(1); |
tgw | 0:2ee762ea11b3 | 93 | } |
tgw | 0:2ee762ea11b3 | 94 | } |
tgw | 0:2ee762ea11b3 | 95 | |
tgw | 0:2ee762ea11b3 | 96 | // Spawns a thread to run blink for 5 seconds |
tgw | 0:2ee762ea11b3 | 97 | int main() { |
tgw | 0:2ee762ea11b3 | 98 | thread.start(callback(blink, &led1)); |
tgw | 0:2ee762ea11b3 | 99 | wait(5); |
tgw | 0:2ee762ea11b3 | 100 | running = false; |
tgw | 0:2ee762ea11b3 | 101 | thread.join(); |
tgw | 0:2ee762ea11b3 | 102 | } |
tgw | 0:2ee762ea11b3 | 103 | |
tgw | 0:2ee762ea11b3 | 104 | |
tgw | 0:2ee762ea11b3 | 105 | 2、休眠+按键中断唤醒 |
tgw | 0:2ee762ea11b3 | 106 | 需更新mbed |
tgw | 0:2ee762ea11b3 | 107 | 使用F401板时,IDD电流测试,运行时:18mA,灯亮时21 .2mA,睡眠时:6.26 mA,深度睡眠时:0.68 mA, |
tgw | 0:2ee762ea11b3 | 108 | |
tgw | 0:2ee762ea11b3 | 109 | RTC唤醒使用第三方类(未测试) |
tgw | 0:2ee762ea11b3 | 110 | |
tgw | 0:2ee762ea11b3 | 111 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 112 | |
tgw | 0:2ee762ea11b3 | 113 | InterruptIn event(USER_BUTTON); |
tgw | 0:2ee762ea11b3 | 114 | DigitalOut myled(LED1); |
tgw | 0:2ee762ea11b3 | 115 | |
tgw | 0:2ee762ea11b3 | 116 | int go_to_sleep = 0; |
tgw | 0:2ee762ea11b3 | 117 | |
tgw | 0:2ee762ea11b3 | 118 | void pressed() |
tgw | 0:2ee762ea11b3 | 119 | { |
tgw | 0:2ee762ea11b3 | 120 | printf("Button pressed\n"); |
tgw | 0:2ee762ea11b3 | 121 | go_to_sleep = go_to_sleep + 1; |
tgw | 0:2ee762ea11b3 | 122 | if (go_to_sleep > 3) go_to_sleep = 0; |
tgw | 0:2ee762ea11b3 | 123 | |
tgw | 0:2ee762ea11b3 | 124 | } |
tgw | 0:2ee762ea11b3 | 125 | |
tgw | 0:2ee762ea11b3 | 126 | int main() |
tgw | 0:2ee762ea11b3 | 127 | { |
tgw | 0:2ee762ea11b3 | 128 | int i = 0; |
tgw | 0:2ee762ea11b3 | 129 | |
tgw | 0:2ee762ea11b3 | 130 | printf("\nPress Button to enter/exit sleep & deepsleep\n"); |
tgw | 0:2ee762ea11b3 | 131 | |
tgw | 0:2ee762ea11b3 | 132 | event.fall(&pressed); |
tgw | 0:2ee762ea11b3 | 133 | |
tgw | 0:2ee762ea11b3 | 134 | while (1) { |
tgw | 0:2ee762ea11b3 | 135 | |
tgw | 0:2ee762ea11b3 | 136 | if ((go_to_sleep == 0) || (go_to_sleep == 2)) { |
tgw | 0:2ee762ea11b3 | 137 | printf("%d: Running\n", i); |
tgw | 0:2ee762ea11b3 | 138 | myled = !myled; |
tgw | 0:2ee762ea11b3 | 139 | wait(1.0); |
tgw | 0:2ee762ea11b3 | 140 | } |
tgw | 0:2ee762ea11b3 | 141 | |
tgw | 0:2ee762ea11b3 | 142 | if (go_to_sleep == 1) { |
tgw | 0:2ee762ea11b3 | 143 | myled = 0; |
tgw | 0:2ee762ea11b3 | 144 | printf("%d: Entering sleep (press user button to resume)\n", i); |
tgw | 0:2ee762ea11b3 | 145 | sleep(); |
tgw | 0:2ee762ea11b3 | 146 | } |
tgw | 0:2ee762ea11b3 | 147 | |
tgw | 0:2ee762ea11b3 | 148 | if (go_to_sleep == 3) { |
tgw | 0:2ee762ea11b3 | 149 | myled = 0; |
tgw | 0:2ee762ea11b3 | 150 | printf("%d: Entering deepsleep (press user button to resume)\n", i); |
tgw | 0:2ee762ea11b3 | 151 | deepsleep(); |
tgw | 0:2ee762ea11b3 | 152 | } |
tgw | 0:2ee762ea11b3 | 153 | |
tgw | 0:2ee762ea11b3 | 154 | i++; |
tgw | 0:2ee762ea11b3 | 155 | } |
tgw | 0:2ee762ea11b3 | 156 | } |
tgw | 0:2ee762ea11b3 | 157 | |
tgw | 0:2ee762ea11b3 | 158 | |
tgw | 0:2ee762ea11b3 | 159 | 3、系统定时器中断 |
tgw | 0:2ee762ea11b3 | 160 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 161 | |
tgw | 0:2ee762ea11b3 | 162 | Ticker toggle_led_ticker; |
tgw | 0:2ee762ea11b3 | 163 | |
tgw | 0:2ee762ea11b3 | 164 | DigitalOut led1(LED1); |
tgw | 0:2ee762ea11b3 | 165 | |
tgw | 0:2ee762ea11b3 | 166 | void toggle_led() { |
tgw | 0:2ee762ea11b3 | 167 | led1 = !led1; |
tgw | 0:2ee762ea11b3 | 168 | } |
tgw | 0:2ee762ea11b3 | 169 | |
tgw | 0:2ee762ea11b3 | 170 | int main() { |
tgw | 0:2ee762ea11b3 | 171 | // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms) |
tgw | 0:2ee762ea11b3 | 172 | toggle_led_ticker.attach(&toggle_led, 0.1); |
tgw | 0:2ee762ea11b3 | 173 | while (true) { |
tgw | 0:2ee762ea11b3 | 174 | // Do other things... |
tgw | 0:2ee762ea11b3 | 175 | } |
tgw | 0:2ee762ea11b3 | 176 | } |
tgw | 0:2ee762ea11b3 | 177 | |
tgw | 0:2ee762ea11b3 | 178 | 4、超时定时器中断 |
tgw | 0:2ee762ea11b3 | 179 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 180 | |
tgw | 0:2ee762ea11b3 | 181 | // A class for flip()-ing a DigitalOut |
tgw | 0:2ee762ea11b3 | 182 | class Flipper { |
tgw | 0:2ee762ea11b3 | 183 | public: |
tgw | 0:2ee762ea11b3 | 184 | Flipper(PinName pin) : _pin(pin) { |
tgw | 0:2ee762ea11b3 | 185 | _pin = 0; |
tgw | 0:2ee762ea11b3 | 186 | } |
tgw | 0:2ee762ea11b3 | 187 | void flip() { |
tgw | 0:2ee762ea11b3 | 188 | _pin = !_pin; |
tgw | 0:2ee762ea11b3 | 189 | } |
tgw | 0:2ee762ea11b3 | 190 | private: |
tgw | 0:2ee762ea11b3 | 191 | DigitalOut _pin; |
tgw | 0:2ee762ea11b3 | 192 | }; |
tgw | 0:2ee762ea11b3 | 193 | |
tgw | 0:2ee762ea11b3 | 194 | DigitalOut led1(LED1); |
tgw | 0:2ee762ea11b3 | 195 | Flipper f(LED2); |
tgw | 0:2ee762ea11b3 | 196 | Timeout t; |
tgw | 0:2ee762ea11b3 | 197 | |
tgw | 0:2ee762ea11b3 | 198 | int main() { |
tgw | 0:2ee762ea11b3 | 199 | // the address of the object, member function, and interval |
tgw | 0:2ee762ea11b3 | 200 | t.attach(callback(&f, &Flipper::flip), 2.0); |
tgw | 0:2ee762ea11b3 | 201 | |
tgw | 0:2ee762ea11b3 | 202 | // spin in a main loop. flipper will interrupt it to call flip |
tgw | 0:2ee762ea11b3 | 203 | while(1) { |
tgw | 0:2ee762ea11b3 | 204 | led1 = !led1; |
tgw | 0:2ee762ea11b3 | 205 | wait(0.2); |
tgw | 0:2ee762ea11b3 | 206 | } |
tgw | 0:2ee762ea11b3 | 207 | } |
tgw | 0:2ee762ea11b3 | 208 | |
tgw | 0:2ee762ea11b3 | 209 | |
tgw | 0:2ee762ea11b3 | 210 | 5、通用定时器中断 |
tgw | 0:2ee762ea11b3 | 211 | |
tgw | 0:2ee762ea11b3 | 212 | // Count the time to toggle a LED |
tgw | 0:2ee762ea11b3 | 213 | |
tgw | 0:2ee762ea11b3 | 214 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 215 | |
tgw | 0:2ee762ea11b3 | 216 | Timer timer; |
tgw | 0:2ee762ea11b3 | 217 | DigitalOut led(LED1); |
tgw | 0:2ee762ea11b3 | 218 | int begin, end; |
tgw | 0:2ee762ea11b3 | 219 | |
tgw | 0:2ee762ea11b3 | 220 | int main() { |
tgw | 0:2ee762ea11b3 | 221 | timer.start(); |
tgw | 0:2ee762ea11b3 | 222 | begin = timer.read_us(); |
tgw | 0:2ee762ea11b3 | 223 | led = !led; |
tgw | 0:2ee762ea11b3 | 224 | end = timer.read_us(); |
tgw | 0:2ee762ea11b3 | 225 | printf("Toggle the led takes %d us", end - begin); |
tgw | 0:2ee762ea11b3 | 226 | } |
tgw | 0:2ee762ea11b3 | 227 | |
tgw | 0:2ee762ea11b3 | 228 | 6、外部中断附加事件 |
tgw | 0:2ee762ea11b3 | 229 | 需加载mbed-os |
tgw | 0:2ee762ea11b3 | 230 | |
tgw | 0:2ee762ea11b3 | 231 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 232 | #include "mbed_events.h" |
tgw | 0:2ee762ea11b3 | 233 | |
tgw | 0:2ee762ea11b3 | 234 | DigitalOut led1(LED1); |
tgw | 0:2ee762ea11b3 | 235 | InterruptIn sw(SW2); |
tgw | 0:2ee762ea11b3 | 236 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
tgw | 0:2ee762ea11b3 | 237 | Thread t; |
tgw | 0:2ee762ea11b3 | 238 | |
tgw | 0:2ee762ea11b3 | 239 | void rise_handler(void) { |
tgw | 0:2ee762ea11b3 | 240 | // Toggle LED |
tgw | 0:2ee762ea11b3 | 241 | led1 = !led1; |
tgw | 0:2ee762ea11b3 | 242 | } |
tgw | 0:2ee762ea11b3 | 243 | |
tgw | 0:2ee762ea11b3 | 244 | void fall_handler(void) { |
tgw | 0:2ee762ea11b3 | 245 | printf("fall_handler in context %p\r\n", Thread::gettid()); |
tgw | 0:2ee762ea11b3 | 246 | // Toggle LED |
tgw | 0:2ee762ea11b3 | 247 | led1 = !led1; |
tgw | 0:2ee762ea11b3 | 248 | } |
tgw | 0:2ee762ea11b3 | 249 | |
tgw | 0:2ee762ea11b3 | 250 | int main() { |
tgw | 0:2ee762ea11b3 | 251 | // Start the event queue |
tgw | 0:2ee762ea11b3 | 252 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
tgw | 0:2ee762ea11b3 | 253 | printf("Starting in context %p\r\n", Thread::gettid()); |
tgw | 0:2ee762ea11b3 | 254 | // The 'rise' handler will execute in IRQ context |
tgw | 0:2ee762ea11b3 | 255 | sw.rise(rise_handler); |
tgw | 0:2ee762ea11b3 | 256 | // The 'fall' handler will execute in the context of thread 't' |
tgw | 0:2ee762ea11b3 | 257 | sw.fall(queue.event(fall_handler)); |
tgw | 0:2ee762ea11b3 | 258 | } |
tgw | 0:2ee762ea11b3 | 259 | |
tgw | 0:2ee762ea11b3 | 260 | |
tgw | 0:2ee762ea11b3 | 261 | 7、线程间信号量的传递 |
tgw | 0:2ee762ea11b3 | 262 | 需加载mbed-os |
tgw | 0:2ee762ea11b3 | 263 | |
tgw | 0:2ee762ea11b3 | 264 | #include "mbed.h" |
tgw | 0:2ee762ea11b3 | 265 | |
tgw | 0:2ee762ea11b3 | 266 | Thread thread; |
tgw | 0:2ee762ea11b3 | 267 | DigitalOut led(LED1); |
tgw | 0:2ee762ea11b3 | 268 | |
tgw | 0:2ee762ea11b3 | 269 | void led_thread() { |
tgw | 0:2ee762ea11b3 | 270 | while (true) { |
tgw | 0:2ee762ea11b3 | 271 | // Signal flags that are reported as event are automatically cleared. |
tgw | 0:2ee762ea11b3 | 272 | Thread::signal_wait(0x1); |
tgw | 0:2ee762ea11b3 | 273 | led = !led; |
tgw | 0:2ee762ea11b3 | 274 | } |
tgw | 0:2ee762ea11b3 | 275 | } |
tgw | 0:2ee762ea11b3 | 276 | |
tgw | 0:2ee762ea11b3 | 277 | int main (void) { |
tgw | 0:2ee762ea11b3 | 278 | thread.start(callback(led_thread)); |
tgw | 0:2ee762ea11b3 | 279 | |
tgw | 0:2ee762ea11b3 | 280 | while (true) { |
tgw | 0:2ee762ea11b3 | 281 | wait(1); |
tgw | 0:2ee762ea11b3 | 282 | thread.signal_set(0x1); |
tgw | 0:2ee762ea11b3 | 283 | } |
tgw | 0:2ee762ea11b3 | 284 | } |
tgw | 0:2ee762ea11b3 | 285 | |
tgw | 0:2ee762ea11b3 | 286 | */ |