ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers duer_app.cpp Source File

duer_app.cpp

00001 // Copyright (2016) Baidu Inc. All rights reserved.
00002 /**
00003  * File: duer_app.cpp
00004  * Desc: Demo for how to start Duer OS.
00005  */
00006 
00007 #include "duer_app.h"
00008 #include "baidu_ca_scheduler.h"
00009 #include "baidu_media_manager.h"
00010 #include "duer_log.h"
00011 #include "device_controller.h"
00012 #include "events.h"
00013 #if defined(TARGET_UNO_91H)
00014 #include "gpadckey.h"
00015 #endif
00016 // #include "baidu_json.h"
00017 
00018 namespace duer {
00019 
00020 #if defined(TARGET_UNO_91H)
00021 #define LED_RED LED1
00022 #define LED_GREEN LED2
00023 #define LED_BLUE LED3
00024 
00025 static GpadcKey s_button(KEY_A0);
00026 static GpadcKey s_pause_button(KEY_A1);
00027 #else
00028 #define DUER_APP_RECORDER_BUTTON  SW2
00029 static mbed::InterruptIn s_button = mbed::InterruptIn(DUER_APP_RECORDER_BUTTON);
00030 #endif
00031 
00032 static const char BAIDU_DEV_CONNECT_SUCCESS_PROMPT_FILE[] = "/sd/cloud_connected.mp3";
00033 static const char BAIDU_DEV_DISCONNECTED_PROMPT_FILE[] = "/sd/cloud_disconnected.mp3";
00034 
00035 static const unsigned int RECONN_DELAY_MIN = 2000;
00036 static const unsigned int RECONN_DELAY_MAX = 32001;
00037 
00038 class SchedulerEventListener : public Scheduler::IOnEvent {
00039 public:
00040     SchedulerEventListener(DuerApp* app) :
00041             _app(app) {
00042         Scheduler::instance().set_on_event_listener(this);
00043     }
00044 
00045     virtual ~SchedulerEventListener() {
00046     }
00047 
00048     virtual int on_start() {
00049         DUER_LOGI("SchedulerEventListener::on_start");
00050 
00051         MEMORY_STATISTICS("Scheduler::on_start");
00052 
00053         device_controller_init();
00054 
00055         event_set_handler(EVT_KEY_REC_PRESS, _app, &DuerApp::talk_start);
00056         event_set_handler(EVT_KEY_REC_RELEASE, _app, &DuerApp::talk_stop);
00057         event_set_handler(EVT_KEY_PAUSE, _app, &DuerApp::pause_play);
00058 
00059         _app->set_color(DuerApp::CYAN);
00060 
00061         MediaManager::instance().play_local(BAIDU_DEV_CONNECT_SUCCESS_PROMPT_FILE);
00062 
00063         return 0;
00064     }
00065 
00066     virtual int on_stop() {
00067         _app->set_color(DuerApp::PURPLE);
00068 
00069         MediaManager::instance().stop();
00070         _app->talk_stop();
00071 
00072         event_set_handler(EVT_KEY_REC_PRESS, NULL);
00073         event_set_handler(EVT_KEY_REC_RELEASE, NULL);
00074         event_set_handler(EVT_KEY_PAUSE, NULL);
00075 
00076         MEMORY_STATISTICS("Scheduler::on_stop");
00077 
00078         DUER_LOGI("SchedulerEventListener::on_stop");
00079 
00080         _app->restart();
00081 
00082         return 0;
00083     }
00084 
00085     virtual int on_action(const char* action) {
00086         DUER_LOGI("SchedulerEventListener::on_action: %s", action);
00087         _app->set_color(DuerApp::BLUE);
00088         MediaManager::instance().play_url((char*)action);
00089         return 0;
00090     }
00091 
00092     virtual int on_data(const char* data) {
00093         DUER_LOGV("SchedulerEventListener::on_data: %s", data);
00094         // baidu_json* value = baidu_json_Parse(data);
00095         // baidu_json* payload = baidu_json_GetObjectItem(value, "payload");
00096 
00097         // if (payload != NULL) {
00098         //     baidu_json* audio_item_id = baidu_json_GetObjectItem(payload, "audio_item_id");
00099 
00100         //     if (audio_item_id != NULL) {
00101         //         DUER_LOGI("track id: %s", audio_item_id->valuestring);
00102         //     }
00103         // }
00104         // baidu_json_Delete(value);
00105 
00106         return 0;
00107     }
00108 
00109 private:
00110     DuerApp* _app;
00111 };
00112 
00113 class RecorderListener : public Recorder::IListener {
00114 public:
00115     RecorderListener(DuerApp* app) :
00116         _app(app),
00117         _start_send_data(false) {
00118     }
00119 
00120     virtual ~RecorderListener() {
00121     }
00122 
00123     virtual int on_start() {
00124         _app->set_color(DuerApp::RED);
00125         DUER_LOGI("RecorderObserver::on_start");
00126         MEMORY_STATISTICS("Recorder::on_start");
00127         return 0;
00128     }
00129 
00130     virtual int on_resume() {
00131         return 0;
00132     }
00133 
00134     virtual int on_data(const void* data, size_t size) {
00135         if (!_start_send_data) {
00136             _start_send_data = true;
00137         }
00138 
00139         DUER_LOGV("RecorderObserver::on_data: data = %p, size = %d", data, size);
00140         Scheduler::instance().send_content(data, size, false);
00141         return 0;
00142     }
00143 
00144     virtual int on_pause() {
00145         return 0;
00146     }
00147 
00148     virtual int on_stop() {
00149         if (_start_send_data) {
00150             Scheduler::instance().send_content(NULL, 0, false);
00151             _start_send_data = false;
00152         }
00153 
00154         MEMORY_STATISTICS("Recorder::on_stop");
00155         DUER_LOGI("RecorderObserver::on_stop");
00156         _app->set_color(DuerApp::GREEN);
00157         return 0;
00158     }
00159 
00160 private:
00161     DuerApp* _app;
00162     bool _start_send_data;
00163 };
00164 
00165 DuerApp::DuerApp()
00166     : _recorder_listener(new RecorderListener(this))
00167     , _on_event(new SchedulerEventListener(this))
00168 #if !defined(TARGET_UNO_91H)
00169     , _indicate(LED_BLUE, LED_GREEN, LED_RED)
00170 #endif
00171     , _timer(this, &DuerApp::start, osTimerOnce)
00172     , _delay(RECONN_DELAY_MIN)
00173 #if defined(TEST_BOARD)
00174     , _send_ticker(this, &DuerApp::send_timestamp, osTimerPeriodic)
00175 #endif
00176 {
00177     _recorder.set_listener(_recorder_listener);
00178 
00179 #if !defined(TARGET_UNO_91H)
00180     _indicate = OFF;
00181 #endif
00182 }
00183 
00184 DuerApp::~DuerApp() {
00185     delete _recorder_listener;
00186     delete _on_event;
00187 }
00188 
00189 void DuerApp::start() {
00190     Scheduler::instance().start();
00191 
00192     s_button.fall(this, &DuerApp::button_fall_handle);
00193     s_button.rise(this, &DuerApp::button_rise_handle);
00194 #if defined(TARGET_UNO_91H)
00195     s_pause_button.fall(this, &DuerApp::pause_button_fall_handle);
00196 #endif
00197 }
00198 
00199 void DuerApp::stop() {
00200     s_button.fall(NULL);
00201     s_button.rise(NULL);
00202 #if defined(TARGET_UNO_91H)
00203     s_pause_button.fall(NULL);
00204 #endif
00205     Scheduler::instance().stop();
00206 }
00207 
00208 void DuerApp::restart() {
00209     if (_delay < RECONN_DELAY_MAX) {
00210         _timer.start(_delay);
00211         _delay <<= 1;
00212     } else {
00213         MediaManager::instance().play_local(BAIDU_DEV_DISCONNECTED_PROMPT_FILE);
00214     }
00215 }
00216 
00217 void DuerApp::talk_start() {
00218     MediaManager::instance().stop();
00219     Scheduler::instance().clear_content();
00220     _recorder.start();
00221 }
00222 
00223 void DuerApp::talk_stop() {
00224     _recorder.stop();
00225 }
00226 
00227 void DuerApp::set_color(Color c) {
00228 #if !defined(TARGET_UNO_91H)
00229     _indicate = c;
00230 #endif
00231 
00232     if (c == CYAN) {
00233         _delay = RECONN_DELAY_MIN;
00234 #if defined(TEST_BOARD)
00235         _send_ticker.start(60 * 1000);//update interval to 1min
00236 #endif
00237     } else if (c == PURPLE) {
00238 #if defined(TEST_BOARD)
00239         _send_ticker.stop();
00240 #endif
00241     }
00242 }
00243 
00244 void DuerApp::button_fall_handle() {
00245     event_trigger(EVT_KEY_REC_PRESS);
00246 }
00247 
00248 void DuerApp::button_rise_handle() {
00249     event_trigger(EVT_KEY_REC_RELEASE);
00250 }
00251 
00252 void DuerApp::pause_button_fall_handle() {
00253     event_trigger(EVT_KEY_PAUSE);
00254 }
00255 
00256 void DuerApp::pause_play() {
00257     MediaManager::instance().pause_or_resume();
00258 }
00259 
00260 #if defined(TEST_BOARD)
00261 void DuerApp::send_timestamp() {
00262     Object data;
00263     data.putInt("time", us_ticker_read());
00264     Scheduler::instance().report(data);
00265 }
00266 #endif
00267 
00268 } // namespace duer