diff options
author | Tobias Jahnke <tobias.jahnke@microchip.com> | 2019-10-15 15:14:30 +0200 |
---|---|---|
committer | Tobias Jahnke <tobias.jahnke@microchip.com> | 2019-10-16 06:11:37 +0000 |
commit | e91eaa18befd14446d280311c9bd5c05f29801f0 (patch) | |
tree | c8921df433cacd724a7ade71f74be2aa962adff0 /binding/microphone/microphone.c | |
parent | 6854f72cb81b10e1add164e754aba1952df5b269 (diff) |
unicens-controller: add DOA feature
Introduce a new API to retrieve the current direction
of audio (DOA) recognized by the microphone.
Bug-AGL: SPEC-2899
Signed-off-by: Tobias Jahnke <tobias.jahnke@microchip.com>
Change-Id: I387efaeaf0cffd06b3d88efbeeaf2ef7eb23c780
Diffstat (limited to 'binding/microphone/microphone.c')
-rw-r--r-- | binding/microphone/microphone.c | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/binding/microphone/microphone.c b/binding/microphone/microphone.c index dc43cd3..344fbd7 100644 --- a/binding/microphone/microphone.c +++ b/binding/microphone/microphone.c @@ -49,6 +49,8 @@ enum microphone_mode */ static int microphone_mode_set(enum microphone_mode mode); +static int microphone_doa_get(void); +static void microphone_doa_status(uint16_t data_sz, uint8_t *data_ptr); /***************************************************************************** * local variables and definitions @@ -56,11 +58,15 @@ static int microphone_mode_set(enum microphone_mode mode); #define NODE_ID ((uint16_t)0x520U) #define MSG_ID_MODE 0x1001U +#define MSG_ID_DOA 0x1003U #define MSG_OP_SET 0x00U +#define MSG_OP_GET 0x01U #define MSG_MAX_PAYLOAD_SZ 2U static uint8_t _tx_payload[MSG_MAX_PAYLOAD_SZ]; static bool _available = false; +static bool _doa_running = false; +static afb_req_t _req_doa_get = NULL; /***************************************************************************** * functions @@ -70,6 +76,23 @@ extern void microphone_availablility_changed(uint16_t node_id, bool available) { if (node_id == NODE_ID) { AFB_API_DEBUG(afbBindingRoot, "%s: amplifier new availability=%d", __func__, available); _available = available; + _doa_running = false; + } +} + +extern void microphone_message_received(uint16_t node, uint16_t msg_id, uint16_t data_sz, uint8_t *data_ptr) { + + if (node != NODE_ID) { + return; + } + + switch (msg_id) { + case MSG_ID_DOA: + microphone_doa_status(data_sz, data_ptr); + break; + default: + AFB_API_NOTICE(afbBindingRoot, "microphone_message_received node=%d, msg_id=%d, data_sz=%d", node, msg_id, data_sz); + break; } } @@ -93,6 +116,40 @@ static int microphone_mode_set(enum microphone_mode mode) { return 0; } +static int microphone_doa_get(void) { + AFB_API_NOTICE(afbBindingRoot, "microphone_doa_get started"); + if (_available == false) { + AFB_API_NOTICE(afbBindingRoot, "%s: node is not available", __func__); + return -1; + } + + if (_doa_running) { + AFB_API_NOTICE(afbBindingRoot, "%s: request is still running", __func__); + return -2; + } + + _tx_payload[0] = MSG_OP_GET; + wrap_ucs_sendmessage_sync(NODE_ID, MSG_ID_DOA, _tx_payload, 1U); + + return 0; +} + +static void microphone_doa_status(uint16_t data_sz, uint8_t *data_ptr) { + uint16_t angle = 0U; + + if ((data_sz == 3U) && (data_ptr[0] == 0x0CU)) { + angle = (uint16_t)((uint16_t)data_ptr[1] << 8 | (uint16_t)data_ptr[2]); + AFB_API_NOTICE(afbBindingRoot, "microphone_doa_status: angle=%d", angle); + if (_req_doa_get != NULL) { + struct json_object* j_resp; + j_resp = json_object_new_object(); + wrap_json_pack(&j_resp, "{s:i}", "value", angle); + afb_req_reply(_req_doa_get, j_resp, NULL, "response successful"); + afb_req_unref(_req_doa_get); + _req_doa_get = NULL; + } + } +} /***************************************************************************** * JSON API @@ -150,4 +207,16 @@ extern void microphone_mode_set_api(afb_req_t request) { else { afb_req_fail(request, "missing argument 'value'", NULL); } -}
\ No newline at end of file +} + +extern void microphone_doa_get_api(afb_req_t request) { + struct json_object* j_obj = afb_req_json(request); + + if (microphone_doa_get() != 0) { + AFB_API_NOTICE(afbBindingRoot, "function call failed: %s:%s", __func__, json_object_get_string(j_obj)); + afb_req_fail(request, "function call failed", NULL); + } + else { + _req_doa_get = afb_req_addref(request); + } +} |