bowen liu
/
mbed-os-example-blinky
ex
Fork of mbed-os-example-mbed5-blinky by
DuerOS-Light-SDK-v1.1.0/demo/device_controller.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: device_controller.cpp |
TMBOY | 47:9e361da97763 | 4 | * Desc: Demo for control device. |
TMBOY | 47:9e361da97763 | 5 | */ |
TMBOY | 47:9e361da97763 | 6 | #include "device_controller.h" |
TMBOY | 47:9e361da97763 | 7 | #include "baidu_ca_scheduler.h" |
TMBOY | 47:9e361da97763 | 8 | #include "baidu_media_manager.h" |
TMBOY | 47:9e361da97763 | 9 | #include "duer_log.h" |
TMBOY | 47:9e361da97763 | 10 | |
TMBOY | 47:9e361da97763 | 11 | namespace duer { |
TMBOY | 47:9e361da97763 | 12 | |
TMBOY | 47:9e361da97763 | 13 | #if defined(TEST_BOARD) |
TMBOY | 47:9e361da97763 | 14 | |
TMBOY | 47:9e361da97763 | 15 | static bca_status_t media_stop(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) { |
TMBOY | 47:9e361da97763 | 16 | bca_handler handler = (bca_handler)ctx; |
TMBOY | 47:9e361da97763 | 17 | DUER_LOGV("media_stop"); |
TMBOY | 47:9e361da97763 | 18 | |
TMBOY | 47:9e361da97763 | 19 | if (handler && msg) { |
TMBOY | 47:9e361da97763 | 20 | duer::MediaManager::instance().stop(); |
TMBOY | 47:9e361da97763 | 21 | duer::Scheduler::instance().response(msg, BCA_MSG_RSP_CHANGED, NULL); |
TMBOY | 47:9e361da97763 | 22 | } |
TMBOY | 47:9e361da97763 | 23 | |
TMBOY | 47:9e361da97763 | 24 | return BCA_NO_ERR; |
TMBOY | 47:9e361da97763 | 25 | } |
TMBOY | 47:9e361da97763 | 26 | |
TMBOY | 47:9e361da97763 | 27 | static bca_status_t set_volume(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) { |
TMBOY | 47:9e361da97763 | 28 | bca_handler handler = (bca_handler)ctx; |
TMBOY | 47:9e361da97763 | 29 | static int volume = 10; |
TMBOY | 47:9e361da97763 | 30 | int msg_code = BCA_MSG_RSP_CHANGED; |
TMBOY | 47:9e361da97763 | 31 | const int LEN = 3; |
TMBOY | 47:9e361da97763 | 32 | char str_volume[LEN] = {0}; |
TMBOY | 47:9e361da97763 | 33 | DUER_LOGV("set_volume"); |
TMBOY | 47:9e361da97763 | 34 | |
TMBOY | 47:9e361da97763 | 35 | if (handler && msg) { |
TMBOY | 47:9e361da97763 | 36 | if (msg->msg_code == BCA_MSG_REQ_GET) { |
TMBOY | 47:9e361da97763 | 37 | DUER_LOGI("volume get: %d", volume); |
TMBOY | 47:9e361da97763 | 38 | snprintf(str_volume, LEN, "%d", volume); |
TMBOY | 47:9e361da97763 | 39 | } else if (msg->msg_code == BCA_MSG_REQ_PUT) { |
TMBOY | 47:9e361da97763 | 40 | if (msg->payload && msg->payload_len > 0 && msg->payload_len < LEN) { |
TMBOY | 47:9e361da97763 | 41 | snprintf(str_volume, LEN, "%s", (char*)msg->payload); |
TMBOY | 47:9e361da97763 | 42 | DUER_LOGI("volume set: %s", str_volume); |
TMBOY | 47:9e361da97763 | 43 | int vol = atoi(str_volume); |
TMBOY | 47:9e361da97763 | 44 | |
TMBOY | 47:9e361da97763 | 45 | if (vol > 16 || vol < 0) { |
TMBOY | 47:9e361da97763 | 46 | msg_code = BCA_MSG_RSP_FORBIDDEN; |
TMBOY | 47:9e361da97763 | 47 | } else { |
TMBOY | 47:9e361da97763 | 48 | if (volume == vol) { |
TMBOY | 47:9e361da97763 | 49 | msg_code = BCA_MSG_RSP_VALID; |
TMBOY | 47:9e361da97763 | 50 | } else { |
TMBOY | 47:9e361da97763 | 51 | duer::MediaManager::instance().set_volume(vol); |
TMBOY | 47:9e361da97763 | 52 | volume = vol; |
TMBOY | 47:9e361da97763 | 53 | } |
TMBOY | 47:9e361da97763 | 54 | } |
TMBOY | 47:9e361da97763 | 55 | } else { |
TMBOY | 47:9e361da97763 | 56 | msg_code = BCA_MSG_RSP_FORBIDDEN; |
TMBOY | 47:9e361da97763 | 57 | str_volume[0] = 0; |
TMBOY | 47:9e361da97763 | 58 | DUER_LOGI("volume set invalid"); |
TMBOY | 47:9e361da97763 | 59 | } |
TMBOY | 47:9e361da97763 | 60 | } |
TMBOY | 47:9e361da97763 | 61 | |
TMBOY | 47:9e361da97763 | 62 | Scheduler::instance().response(msg, msg_code, str_volume); |
TMBOY | 47:9e361da97763 | 63 | } |
TMBOY | 47:9e361da97763 | 64 | |
TMBOY | 47:9e361da97763 | 65 | return BCA_NO_ERR; |
TMBOY | 47:9e361da97763 | 66 | } |
TMBOY | 47:9e361da97763 | 67 | |
TMBOY | 47:9e361da97763 | 68 | static bca_status_t shutdown(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) { |
TMBOY | 47:9e361da97763 | 69 | bca_handler handler = (bca_handler)ctx; |
TMBOY | 47:9e361da97763 | 70 | DUER_LOGV("shutdown"); |
TMBOY | 47:9e361da97763 | 71 | |
TMBOY | 47:9e361da97763 | 72 | if (handler && msg) { |
TMBOY | 47:9e361da97763 | 73 | duer::Scheduler::instance().response(msg, BCA_MSG_RSP_CHANGED, NULL); |
TMBOY | 47:9e361da97763 | 74 | duer::Scheduler::instance().stop(); |
TMBOY | 47:9e361da97763 | 75 | } |
TMBOY | 47:9e361da97763 | 76 | |
TMBOY | 47:9e361da97763 | 77 | return BCA_NO_ERR; |
TMBOY | 47:9e361da97763 | 78 | } |
TMBOY | 47:9e361da97763 | 79 | |
TMBOY | 47:9e361da97763 | 80 | static bca_status_t set_mode(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) { |
TMBOY | 47:9e361da97763 | 81 | bca_handler handler = (bca_handler)ctx; |
TMBOY | 47:9e361da97763 | 82 | static const int LEN = 10; |
TMBOY | 47:9e361da97763 | 83 | static char mode[LEN] = {0}; |
TMBOY | 47:9e361da97763 | 84 | int msg_code = BCA_MSG_RSP_CHANGED; |
TMBOY | 47:9e361da97763 | 85 | DUER_LOGV("set_mode"); |
TMBOY | 47:9e361da97763 | 86 | |
TMBOY | 47:9e361da97763 | 87 | if (handler && msg) { |
TMBOY | 47:9e361da97763 | 88 | if (msg->msg_code == BCA_MSG_REQ_GET) { |
TMBOY | 47:9e361da97763 | 89 | DUER_LOGI("mode get: %s", mode); |
TMBOY | 47:9e361da97763 | 90 | } else if (msg->msg_code == BCA_MSG_REQ_PUT) { |
TMBOY | 47:9e361da97763 | 91 | if (msg->payload && msg->payload_len > 0) { |
TMBOY | 47:9e361da97763 | 92 | snprintf(mode, LEN, "%s", (char*)msg->payload); |
TMBOY | 47:9e361da97763 | 93 | DUER_LOGI("mode set: %s", mode); |
TMBOY | 47:9e361da97763 | 94 | } else { |
TMBOY | 47:9e361da97763 | 95 | msg_code = BCA_MSG_RSP_FORBIDDEN; |
TMBOY | 47:9e361da97763 | 96 | mode[0] = 0; |
TMBOY | 47:9e361da97763 | 97 | DUER_LOGI("mode set invalid"); |
TMBOY | 47:9e361da97763 | 98 | } |
TMBOY | 47:9e361da97763 | 99 | } |
TMBOY | 47:9e361da97763 | 100 | |
TMBOY | 47:9e361da97763 | 101 | Scheduler::instance().response(msg, msg_code, mode); |
TMBOY | 47:9e361da97763 | 102 | } |
TMBOY | 47:9e361da97763 | 103 | |
TMBOY | 47:9e361da97763 | 104 | return BCA_NO_ERR; |
TMBOY | 47:9e361da97763 | 105 | } |
TMBOY | 47:9e361da97763 | 106 | |
TMBOY | 47:9e361da97763 | 107 | static bca_status_t get_power(bca_context ctx, bca_msg_t* msg, bca_addr_t* addr) { |
TMBOY | 47:9e361da97763 | 108 | bca_handler handler = (bca_handler)ctx; |
TMBOY | 47:9e361da97763 | 109 | static double power = 1.0; |
TMBOY | 47:9e361da97763 | 110 | DUER_LOGV("get_power"); |
TMBOY | 47:9e361da97763 | 111 | |
TMBOY | 47:9e361da97763 | 112 | if (handler && msg) { |
TMBOY | 47:9e361da97763 | 113 | char str_power[4]; |
TMBOY | 47:9e361da97763 | 114 | snprintf(str_power, 4, "%lf", power); |
TMBOY | 47:9e361da97763 | 115 | DUER_LOGI("power: %s", str_power); |
TMBOY | 47:9e361da97763 | 116 | Scheduler::instance().response(msg, BCA_MSG_RSP_CHANGED, str_power); |
TMBOY | 47:9e361da97763 | 117 | |
TMBOY | 47:9e361da97763 | 118 | if (power > 0.5) { |
TMBOY | 47:9e361da97763 | 119 | power -= 0.01; |
TMBOY | 47:9e361da97763 | 120 | } |
TMBOY | 47:9e361da97763 | 121 | } |
TMBOY | 47:9e361da97763 | 122 | |
TMBOY | 47:9e361da97763 | 123 | return BCA_NO_ERR; |
TMBOY | 47:9e361da97763 | 124 | } |
TMBOY | 47:9e361da97763 | 125 | |
TMBOY | 47:9e361da97763 | 126 | #endif |
TMBOY | 47:9e361da97763 | 127 | |
TMBOY | 47:9e361da97763 | 128 | void device_controller_init(void) { |
TMBOY | 47:9e361da97763 | 129 | #if defined(TEST_BOARD) |
TMBOY | 47:9e361da97763 | 130 | bca_res_t res[] = { |
TMBOY | 47:9e361da97763 | 131 | {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT, "stop", media_stop}, |
TMBOY | 47:9e361da97763 | 132 | {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT | BCA_RES_OP_GET, "volume", set_volume}, |
TMBOY | 47:9e361da97763 | 133 | {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT, "shutdown", shutdown}, |
TMBOY | 47:9e361da97763 | 134 | {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_PUT | BCA_RES_OP_GET, "mode", set_mode}, |
TMBOY | 47:9e361da97763 | 135 | {BCA_RES_MODE_DYNAMIC, BCA_RES_OP_GET, "power", get_power}, |
TMBOY | 47:9e361da97763 | 136 | }; |
TMBOY | 47:9e361da97763 | 137 | Scheduler::instance().add_controll_points(res, sizeof(res) / sizeof(res[0])); |
TMBOY | 47:9e361da97763 | 138 | #endif |
TMBOY | 47:9e361da97763 | 139 | } |
TMBOY | 47:9e361da97763 | 140 | |
TMBOY | 47:9e361da97763 | 141 | } // namespace duer |