bowen liu
/
mbed-os-example-blinky
ex
Fork of mbed-os-example-mbed5-blinky by
DuerOS-Light-SDK-v1.1.0/demo/duer_app.cpp@50:9ecaa144d1f3, 2017-07-18 (annotated)
- Committer:
- tmboy
- Date:
- Tue Jul 18 09:08:52 2017 +0000
- Revision:
- 50:9ecaa144d1f3
- Parent:
- 47:9e361da97763
add .json
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TMBOY | 47:9e361da97763 | 1 | // Copyright (2016) Baidu Inc. All rights reserved. |
TMBOY | 47:9e361da97763 | 2 | /** |
TMBOY | 47:9e361da97763 | 3 | * File: duer_app.cpp |
TMBOY | 47:9e361da97763 | 4 | * Desc: Demo for how to start Duer OS. |
TMBOY | 47:9e361da97763 | 5 | */ |
TMBOY | 47:9e361da97763 | 6 | |
TMBOY | 47:9e361da97763 | 7 | #include "duer_app.h" |
TMBOY | 47:9e361da97763 | 8 | #include "baidu_ca_scheduler.h" |
TMBOY | 47:9e361da97763 | 9 | #include "baidu_media_manager.h" |
TMBOY | 47:9e361da97763 | 10 | #include "duer_log.h" |
TMBOY | 47:9e361da97763 | 11 | #include "device_controller.h" |
TMBOY | 47:9e361da97763 | 12 | #include "events.h" |
TMBOY | 47:9e361da97763 | 13 | #if defined(TARGET_UNO_91H) |
TMBOY | 47:9e361da97763 | 14 | #include "gpadckey.h" |
TMBOY | 47:9e361da97763 | 15 | #endif |
TMBOY | 47:9e361da97763 | 16 | // #include "baidu_json.h" |
TMBOY | 47:9e361da97763 | 17 | |
TMBOY | 47:9e361da97763 | 18 | namespace duer { |
TMBOY | 47:9e361da97763 | 19 | |
TMBOY | 47:9e361da97763 | 20 | #if defined(TARGET_UNO_91H) |
TMBOY | 47:9e361da97763 | 21 | #define LED_RED LED1 |
TMBOY | 47:9e361da97763 | 22 | #define LED_GREEN LED2 |
TMBOY | 47:9e361da97763 | 23 | #define LED_BLUE LED3 |
TMBOY | 47:9e361da97763 | 24 | |
TMBOY | 47:9e361da97763 | 25 | static GpadcKey s_button(KEY_A0); |
TMBOY | 47:9e361da97763 | 26 | static GpadcKey s_pause_button(KEY_A1); |
TMBOY | 47:9e361da97763 | 27 | #else |
TMBOY | 47:9e361da97763 | 28 | #define DUER_APP_RECORDER_BUTTON SW2 |
TMBOY | 47:9e361da97763 | 29 | static mbed::InterruptIn s_button = mbed::InterruptIn(DUER_APP_RECORDER_BUTTON); |
TMBOY | 47:9e361da97763 | 30 | #endif |
TMBOY | 47:9e361da97763 | 31 | |
TMBOY | 47:9e361da97763 | 32 | static const char BAIDU_DEV_CONNECT_SUCCESS_PROMPT_FILE[] = "/sd/cloud_connected.mp3"; |
TMBOY | 47:9e361da97763 | 33 | static const char BAIDU_DEV_DISCONNECTED_PROMPT_FILE[] = "/sd/cloud_disconnected.mp3"; |
TMBOY | 47:9e361da97763 | 34 | |
TMBOY | 47:9e361da97763 | 35 | static const unsigned int RECONN_DELAY_MIN = 2000; |
TMBOY | 47:9e361da97763 | 36 | static const unsigned int RECONN_DELAY_MAX = 32001; |
TMBOY | 47:9e361da97763 | 37 | |
TMBOY | 47:9e361da97763 | 38 | class SchedulerEventListener : public Scheduler::IOnEvent { |
TMBOY | 47:9e361da97763 | 39 | public: |
TMBOY | 47:9e361da97763 | 40 | SchedulerEventListener(DuerApp* app) : |
TMBOY | 47:9e361da97763 | 41 | _app(app) { |
TMBOY | 47:9e361da97763 | 42 | Scheduler::instance().set_on_event_listener(this); |
TMBOY | 47:9e361da97763 | 43 | } |
TMBOY | 47:9e361da97763 | 44 | |
TMBOY | 47:9e361da97763 | 45 | virtual ~SchedulerEventListener() { |
TMBOY | 47:9e361da97763 | 46 | } |
TMBOY | 47:9e361da97763 | 47 | |
TMBOY | 47:9e361da97763 | 48 | virtual int on_start() { |
TMBOY | 47:9e361da97763 | 49 | DUER_LOGI("SchedulerEventListener::on_start"); |
TMBOY | 47:9e361da97763 | 50 | |
TMBOY | 47:9e361da97763 | 51 | MEMORY_STATISTICS("Scheduler::on_start"); |
TMBOY | 47:9e361da97763 | 52 | |
TMBOY | 47:9e361da97763 | 53 | device_controller_init(); |
TMBOY | 47:9e361da97763 | 54 | |
TMBOY | 47:9e361da97763 | 55 | event_set_handler(EVT_KEY_REC_PRESS, _app, &DuerApp::talk_start); |
TMBOY | 47:9e361da97763 | 56 | event_set_handler(EVT_KEY_REC_RELEASE, _app, &DuerApp::talk_stop); |
TMBOY | 47:9e361da97763 | 57 | event_set_handler(EVT_KEY_PAUSE, _app, &DuerApp::pause_play); |
TMBOY | 47:9e361da97763 | 58 | |
TMBOY | 47:9e361da97763 | 59 | _app->set_color(DuerApp::CYAN); |
TMBOY | 47:9e361da97763 | 60 | |
TMBOY | 47:9e361da97763 | 61 | MediaManager::instance().play_local(BAIDU_DEV_CONNECT_SUCCESS_PROMPT_FILE); |
TMBOY | 47:9e361da97763 | 62 | |
TMBOY | 47:9e361da97763 | 63 | return 0; |
TMBOY | 47:9e361da97763 | 64 | } |
TMBOY | 47:9e361da97763 | 65 | |
TMBOY | 47:9e361da97763 | 66 | virtual int on_stop() { |
TMBOY | 47:9e361da97763 | 67 | _app->set_color(DuerApp::PURPLE); |
TMBOY | 47:9e361da97763 | 68 | |
TMBOY | 47:9e361da97763 | 69 | MediaManager::instance().stop(); |
TMBOY | 47:9e361da97763 | 70 | _app->talk_stop(); |
TMBOY | 47:9e361da97763 | 71 | |
TMBOY | 47:9e361da97763 | 72 | event_set_handler(EVT_KEY_REC_PRESS, NULL); |
TMBOY | 47:9e361da97763 | 73 | event_set_handler(EVT_KEY_REC_RELEASE, NULL); |
TMBOY | 47:9e361da97763 | 74 | event_set_handler(EVT_KEY_PAUSE, NULL); |
TMBOY | 47:9e361da97763 | 75 | |
TMBOY | 47:9e361da97763 | 76 | MEMORY_STATISTICS("Scheduler::on_stop"); |
TMBOY | 47:9e361da97763 | 77 | |
TMBOY | 47:9e361da97763 | 78 | DUER_LOGI("SchedulerEventListener::on_stop"); |
TMBOY | 47:9e361da97763 | 79 | |
TMBOY | 47:9e361da97763 | 80 | _app->restart(); |
TMBOY | 47:9e361da97763 | 81 | |
TMBOY | 47:9e361da97763 | 82 | return 0; |
TMBOY | 47:9e361da97763 | 83 | } |
TMBOY | 47:9e361da97763 | 84 | |
TMBOY | 47:9e361da97763 | 85 | virtual int on_action(const char* action) { |
TMBOY | 47:9e361da97763 | 86 | DUER_LOGI("SchedulerEventListener::on_action: %s", action); |
TMBOY | 47:9e361da97763 | 87 | _app->set_color(DuerApp::BLUE); |
TMBOY | 47:9e361da97763 | 88 | MediaManager::instance().play_url((char*)action); |
TMBOY | 47:9e361da97763 | 89 | return 0; |
TMBOY | 47:9e361da97763 | 90 | } |
TMBOY | 47:9e361da97763 | 91 | |
TMBOY | 47:9e361da97763 | 92 | virtual int on_data(const char* data) { |
TMBOY | 47:9e361da97763 | 93 | DUER_LOGV("SchedulerEventListener::on_data: %s", data); |
TMBOY | 47:9e361da97763 | 94 | // baidu_json* value = baidu_json_Parse(data); |
TMBOY | 47:9e361da97763 | 95 | // baidu_json* payload = baidu_json_GetObjectItem(value, "payload"); |
TMBOY | 47:9e361da97763 | 96 | |
TMBOY | 47:9e361da97763 | 97 | // if (payload != NULL) { |
TMBOY | 47:9e361da97763 | 98 | // baidu_json* audio_item_id = baidu_json_GetObjectItem(payload, "audio_item_id"); |
TMBOY | 47:9e361da97763 | 99 | |
TMBOY | 47:9e361da97763 | 100 | // if (audio_item_id != NULL) { |
TMBOY | 47:9e361da97763 | 101 | // DUER_LOGI("track id: %s", audio_item_id->valuestring); |
TMBOY | 47:9e361da97763 | 102 | // } |
TMBOY | 47:9e361da97763 | 103 | // } |
TMBOY | 47:9e361da97763 | 104 | // baidu_json_Delete(value); |
TMBOY | 47:9e361da97763 | 105 | |
TMBOY | 47:9e361da97763 | 106 | return 0; |
TMBOY | 47:9e361da97763 | 107 | } |
TMBOY | 47:9e361da97763 | 108 | |
TMBOY | 47:9e361da97763 | 109 | private: |
TMBOY | 47:9e361da97763 | 110 | DuerApp* _app; |
TMBOY | 47:9e361da97763 | 111 | }; |
TMBOY | 47:9e361da97763 | 112 | |
TMBOY | 47:9e361da97763 | 113 | class RecorderListener : public Recorder::IListener { |
TMBOY | 47:9e361da97763 | 114 | public: |
TMBOY | 47:9e361da97763 | 115 | RecorderListener(DuerApp* app) : |
TMBOY | 47:9e361da97763 | 116 | _app(app), |
TMBOY | 47:9e361da97763 | 117 | _start_send_data(false) { |
TMBOY | 47:9e361da97763 | 118 | } |
TMBOY | 47:9e361da97763 | 119 | |
TMBOY | 47:9e361da97763 | 120 | virtual ~RecorderListener() { |
TMBOY | 47:9e361da97763 | 121 | } |
TMBOY | 47:9e361da97763 | 122 | |
TMBOY | 47:9e361da97763 | 123 | virtual int on_start() { |
TMBOY | 47:9e361da97763 | 124 | _app->set_color(DuerApp::RED); |
TMBOY | 47:9e361da97763 | 125 | DUER_LOGI("RecorderObserver::on_start"); |
TMBOY | 47:9e361da97763 | 126 | MEMORY_STATISTICS("Recorder::on_start"); |
TMBOY | 47:9e361da97763 | 127 | return 0; |
TMBOY | 47:9e361da97763 | 128 | } |
TMBOY | 47:9e361da97763 | 129 | |
TMBOY | 47:9e361da97763 | 130 | virtual int on_resume() { |
TMBOY | 47:9e361da97763 | 131 | return 0; |
TMBOY | 47:9e361da97763 | 132 | } |
TMBOY | 47:9e361da97763 | 133 | |
TMBOY | 47:9e361da97763 | 134 | virtual int on_data(const void* data, size_t size) { |
TMBOY | 47:9e361da97763 | 135 | if (!_start_send_data) { |
TMBOY | 47:9e361da97763 | 136 | _start_send_data = true; |
TMBOY | 47:9e361da97763 | 137 | } |
TMBOY | 47:9e361da97763 | 138 | |
TMBOY | 47:9e361da97763 | 139 | DUER_LOGV("RecorderObserver::on_data: data = %p, size = %d", data, size); |
TMBOY | 47:9e361da97763 | 140 | Scheduler::instance().send_content(data, size, false); |
TMBOY | 47:9e361da97763 | 141 | return 0; |
TMBOY | 47:9e361da97763 | 142 | } |
TMBOY | 47:9e361da97763 | 143 | |
TMBOY | 47:9e361da97763 | 144 | virtual int on_pause() { |
TMBOY | 47:9e361da97763 | 145 | return 0; |
TMBOY | 47:9e361da97763 | 146 | } |
TMBOY | 47:9e361da97763 | 147 | |
TMBOY | 47:9e361da97763 | 148 | virtual int on_stop() { |
TMBOY | 47:9e361da97763 | 149 | if (_start_send_data) { |
TMBOY | 47:9e361da97763 | 150 | Scheduler::instance().send_content(NULL, 0, false); |
TMBOY | 47:9e361da97763 | 151 | _start_send_data = false; |
TMBOY | 47:9e361da97763 | 152 | } |
TMBOY | 47:9e361da97763 | 153 | |
TMBOY | 47:9e361da97763 | 154 | MEMORY_STATISTICS("Recorder::on_stop"); |
TMBOY | 47:9e361da97763 | 155 | DUER_LOGI("RecorderObserver::on_stop"); |
TMBOY | 47:9e361da97763 | 156 | _app->set_color(DuerApp::GREEN); |
TMBOY | 47:9e361da97763 | 157 | return 0; |
TMBOY | 47:9e361da97763 | 158 | } |
TMBOY | 47:9e361da97763 | 159 | |
TMBOY | 47:9e361da97763 | 160 | private: |
TMBOY | 47:9e361da97763 | 161 | DuerApp* _app; |
TMBOY | 47:9e361da97763 | 162 | bool _start_send_data; |
TMBOY | 47:9e361da97763 | 163 | }; |
TMBOY | 47:9e361da97763 | 164 | |
TMBOY | 47:9e361da97763 | 165 | DuerApp::DuerApp() |
TMBOY | 47:9e361da97763 | 166 | : _recorder_listener(new RecorderListener(this)) |
TMBOY | 47:9e361da97763 | 167 | , _on_event(new SchedulerEventListener(this)) |
TMBOY | 47:9e361da97763 | 168 | #if !defined(TARGET_UNO_91H) |
TMBOY | 47:9e361da97763 | 169 | , _indicate(LED_BLUE, LED_GREEN, LED_RED) |
TMBOY | 47:9e361da97763 | 170 | #endif |
TMBOY | 47:9e361da97763 | 171 | , _timer(this, &DuerApp::start, osTimerOnce) |
TMBOY | 47:9e361da97763 | 172 | , _delay(RECONN_DELAY_MIN) |
TMBOY | 47:9e361da97763 | 173 | #if defined(TEST_BOARD) |
TMBOY | 47:9e361da97763 | 174 | , _send_ticker(this, &DuerApp::send_timestamp, osTimerPeriodic) |
TMBOY | 47:9e361da97763 | 175 | #endif |
TMBOY | 47:9e361da97763 | 176 | { |
TMBOY | 47:9e361da97763 | 177 | _recorder.set_listener(_recorder_listener); |
TMBOY | 47:9e361da97763 | 178 | |
TMBOY | 47:9e361da97763 | 179 | #if !defined(TARGET_UNO_91H) |
TMBOY | 47:9e361da97763 | 180 | _indicate = OFF; |
TMBOY | 47:9e361da97763 | 181 | #endif |
TMBOY | 47:9e361da97763 | 182 | } |
TMBOY | 47:9e361da97763 | 183 | |
TMBOY | 47:9e361da97763 | 184 | DuerApp::~DuerApp() { |
TMBOY | 47:9e361da97763 | 185 | delete _recorder_listener; |
TMBOY | 47:9e361da97763 | 186 | delete _on_event; |
TMBOY | 47:9e361da97763 | 187 | } |
TMBOY | 47:9e361da97763 | 188 | |
TMBOY | 47:9e361da97763 | 189 | void DuerApp::start() { |
TMBOY | 47:9e361da97763 | 190 | Scheduler::instance().start(); |
TMBOY | 47:9e361da97763 | 191 | |
TMBOY | 47:9e361da97763 | 192 | s_button.fall(this, &DuerApp::button_fall_handle); |
TMBOY | 47:9e361da97763 | 193 | s_button.rise(this, &DuerApp::button_rise_handle); |
TMBOY | 47:9e361da97763 | 194 | #if defined(TARGET_UNO_91H) |
TMBOY | 47:9e361da97763 | 195 | s_pause_button.fall(this, &DuerApp::pause_button_fall_handle); |
TMBOY | 47:9e361da97763 | 196 | #endif |
TMBOY | 47:9e361da97763 | 197 | } |
TMBOY | 47:9e361da97763 | 198 | |
TMBOY | 47:9e361da97763 | 199 | void DuerApp::stop() { |
TMBOY | 47:9e361da97763 | 200 | s_button.fall(NULL); |
TMBOY | 47:9e361da97763 | 201 | s_button.rise(NULL); |
TMBOY | 47:9e361da97763 | 202 | #if defined(TARGET_UNO_91H) |
TMBOY | 47:9e361da97763 | 203 | s_pause_button.fall(NULL); |
TMBOY | 47:9e361da97763 | 204 | #endif |
TMBOY | 47:9e361da97763 | 205 | Scheduler::instance().stop(); |
TMBOY | 47:9e361da97763 | 206 | } |
TMBOY | 47:9e361da97763 | 207 | |
TMBOY | 47:9e361da97763 | 208 | void DuerApp::restart() { |
TMBOY | 47:9e361da97763 | 209 | if (_delay < RECONN_DELAY_MAX) { |
TMBOY | 47:9e361da97763 | 210 | _timer.start(_delay); |
TMBOY | 47:9e361da97763 | 211 | _delay <<= 1; |
TMBOY | 47:9e361da97763 | 212 | } else { |
TMBOY | 47:9e361da97763 | 213 | MediaManager::instance().play_local(BAIDU_DEV_DISCONNECTED_PROMPT_FILE); |
TMBOY | 47:9e361da97763 | 214 | } |
TMBOY | 47:9e361da97763 | 215 | } |
TMBOY | 47:9e361da97763 | 216 | |
TMBOY | 47:9e361da97763 | 217 | void DuerApp::talk_start() { |
TMBOY | 47:9e361da97763 | 218 | MediaManager::instance().stop(); |
TMBOY | 47:9e361da97763 | 219 | Scheduler::instance().clear_content(); |
TMBOY | 47:9e361da97763 | 220 | _recorder.start(); |
TMBOY | 47:9e361da97763 | 221 | } |
TMBOY | 47:9e361da97763 | 222 | |
TMBOY | 47:9e361da97763 | 223 | void DuerApp::talk_stop() { |
TMBOY | 47:9e361da97763 | 224 | _recorder.stop(); |
TMBOY | 47:9e361da97763 | 225 | } |
TMBOY | 47:9e361da97763 | 226 | |
TMBOY | 47:9e361da97763 | 227 | void DuerApp::set_color(Color c) { |
TMBOY | 47:9e361da97763 | 228 | #if !defined(TARGET_UNO_91H) |
TMBOY | 47:9e361da97763 | 229 | _indicate = c; |
TMBOY | 47:9e361da97763 | 230 | #endif |
TMBOY | 47:9e361da97763 | 231 | |
TMBOY | 47:9e361da97763 | 232 | if (c == CYAN) { |
TMBOY | 47:9e361da97763 | 233 | _delay = RECONN_DELAY_MIN; |
TMBOY | 47:9e361da97763 | 234 | #if defined(TEST_BOARD) |
TMBOY | 47:9e361da97763 | 235 | _send_ticker.start(60 * 1000);//update interval to 1min |
TMBOY | 47:9e361da97763 | 236 | #endif |
TMBOY | 47:9e361da97763 | 237 | } else if (c == PURPLE) { |
TMBOY | 47:9e361da97763 | 238 | #if defined(TEST_BOARD) |
TMBOY | 47:9e361da97763 | 239 | _send_ticker.stop(); |
TMBOY | 47:9e361da97763 | 240 | #endif |
TMBOY | 47:9e361da97763 | 241 | } |
TMBOY | 47:9e361da97763 | 242 | } |
TMBOY | 47:9e361da97763 | 243 | |
TMBOY | 47:9e361da97763 | 244 | void DuerApp::button_fall_handle() { |
TMBOY | 47:9e361da97763 | 245 | event_trigger(EVT_KEY_REC_PRESS); |
TMBOY | 47:9e361da97763 | 246 | } |
TMBOY | 47:9e361da97763 | 247 | |
TMBOY | 47:9e361da97763 | 248 | void DuerApp::button_rise_handle() { |
TMBOY | 47:9e361da97763 | 249 | event_trigger(EVT_KEY_REC_RELEASE); |
TMBOY | 47:9e361da97763 | 250 | } |
TMBOY | 47:9e361da97763 | 251 | |
TMBOY | 47:9e361da97763 | 252 | void DuerApp::pause_button_fall_handle() { |
TMBOY | 47:9e361da97763 | 253 | event_trigger(EVT_KEY_PAUSE); |
TMBOY | 47:9e361da97763 | 254 | } |
TMBOY | 47:9e361da97763 | 255 | |
TMBOY | 47:9e361da97763 | 256 | void DuerApp::pause_play() { |
TMBOY | 47:9e361da97763 | 257 | MediaManager::instance().pause_or_resume(); |
TMBOY | 47:9e361da97763 | 258 | } |
TMBOY | 47:9e361da97763 | 259 | |
TMBOY | 47:9e361da97763 | 260 | #if defined(TEST_BOARD) |
TMBOY | 47:9e361da97763 | 261 | void DuerApp::send_timestamp() { |
TMBOY | 47:9e361da97763 | 262 | Object data; |
TMBOY | 47:9e361da97763 | 263 | data.putInt("time", us_ticker_read()); |
TMBOY | 47:9e361da97763 | 264 | Scheduler::instance().report(data); |
TMBOY | 47:9e361da97763 | 265 | } |
TMBOY | 47:9e361da97763 | 266 | #endif |
TMBOY | 47:9e361da97763 | 267 | |
TMBOY | 47:9e361da97763 | 268 | } // namespace duer |