/*************************************************************************** * Copyright (c) 2012, Broadcom Corporation * All Rights Reserved * Confidential Property of Broadcom Corporation * * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. * * $brcm_Workfile: $ * $brcm_Revision: $ * $brcm_Date: $ * * Module Description: * * Revision History: * * $brcm_Log: $ * ***************************************************************************/ #include "nexus_platform.h" #include "nexus_ir_input.h" #include "bapp_remote.h" #include "bos.h" #include "bir_codes.h" BDBG_MODULE(app_remote); /* Register software module with debug interface */ #define BUSER_IO_NUM_EVENTS 16 #define BUSER_IO_MSG BDBG_ERR /* Convert timeout from milliseconds to ticks */ #define MAX_TIMEOUT ((300 * g_ticks_per_second)/ 1000) #define DEFAULT_PEND_TIMEOUT 10 /* in milliseconds */ static bapp_remote_config_t s_config = { 8, false/*rf4ce*/, true/*ir*/ }; struct bapp_remote { /* User IO */ NEXUS_IrInputHandle irHandle; unsigned int timeout; unsigned int pend_timeout; b_queue_t queue; b_queue_t userio_queue; b_event_t events[BUSER_IO_NUM_EVENTS]; b_event_t userio_events[BUSER_IO_NUM_EVENTS]; unsigned int last_key; }; static struct bapp_remote *s_p_remote = NULL; static unsigned int bapp_remote_irmap(unsigned int code); /* User Input API */ void bapp_remote_ircallback(void *pParam, int iParam) { size_t numEvents = 1; NEXUS_Error rc = 0; bool overflow; int mapped_code; unsigned int cur_ticks = bos_getticks(); NEXUS_IrInputHandle irHandle = *(NEXUS_IrInputHandle *)pParam; BSTD_UNUSED(iParam); while (numEvents && !rc) { NEXUS_IrInputEvent irEvent; rc = NEXUS_IrInput_GetEvents(irHandle, &irEvent, 1, &numEvents, &overflow); if (numEvents) { mapped_code = bapp_remote_irmap(irEvent.code & 0xFF); s_p_remote->last_key = mapped_code; if (irEvent.repeat == false) { bos_post_event(s_p_remote->userio_queue, (b_event_t*)(eKEY_DOWN|mapped_code)); } s_p_remote->timeout = cur_ticks + MS_TO_TICKS(100); } } } /* Summary: Open a user input object for receiving IR remote and keypad input. Description: For now, the following id's are used: 0 - remote a 1 - remote b 2 - 56 MHz Sejin IR Keyboard 3 - keypad (TODO implement) 8 - Moto remote, device_type = 0x14 */ bapp_result_t bapp_remote_open(bapp_remote_t *p_remote) { NEXUS_IrInputSettings irSettings; *p_remote = (struct bapp_remote*)BKNI_Malloc(sizeof(struct bapp_remote)); if (!*p_remote) return eBAPP_RESULT_ALLOC_FAILURE; BKNI_Memset(*p_remote,0,sizeof(struct bapp_remote)); bos_create_queue(&(*p_remote)->userio_queue, (*p_remote)->userio_events, BUSER_IO_NUM_EVENTS); if (!(*p_remote)->userio_queue) { bapp_remote_close(*p_remote); return eBAPP_RESULT_ALLOC_FAILURE; } bos_create_queue(&(*p_remote)->queue,(*p_remote)->events,BUSER_IO_NUM_EVENTS); if (!(*p_remote)->queue) { bapp_remote_close(*p_remote); return eBAPP_RESULT_ALLOC_FAILURE; } NEXUS_IrInput_GetDefaultSettings(&irSettings); irSettings.mode = NEXUS_IrInputMode_eRemoteA; irSettings.dataReady.callback = bapp_remote_ircallback; irSettings.dataReady.context = &(*p_remote)->irHandle; (*p_remote)->irHandle = NEXUS_IrInput_Open(0, &irSettings); s_p_remote = *p_remote; s_p_remote->pend_timeout = DEFAULT_PEND_TIMEOUT; return eBAPP_RESULT_OK; } /* Summary: Close a user input handle. Description: Releases all resources associated with the user input object */ void bapp_remote_close( bapp_remote_t handle /* user input object */ ) { struct bapp_remote *p_remote = (struct bapp_remote*)handle; if (p_remote->queue) { bos_delete_queue(&p_remote->queue); } if (p_remote->userio_queue) { bos_delete_queue(&p_remote->userio_queue); } NEXUS_IrInput_Close(handle->irHandle); BKNI_Free(p_remote); } static unsigned int bapp_remote_irmap(unsigned int code) { unsigned int mapped_code = 0xFF; if ((code == 0x71) || (code == 0x72)) { return code; } switch(s_config.user_io_id) { default: return code; case 8: switch (code) { default: mapped_code = code; break; case 0x01: mapped_code = eIR_1; break; case 0x02: mapped_code = eIR_2; break; case 0x03: mapped_code = eIR_3; break; case 0x04: mapped_code = eIR_4; break; case 0x05: mapped_code = eIR_5; break; case 0x06: mapped_code = eIR_6; break; case 0x07: mapped_code = eIR_7; break; case 0x08: mapped_code = eIR_8; break; case 0x09: mapped_code = eIR_9; break; case 0x00: mapped_code = eIR_0; break; case 0x0b: mapped_code = eIR_CH_UP; break; case 0x0c: mapped_code = eIR_CH_DOWN; break; /* mute */ case 0x0f: mapped_code = eIR_MUTE; break; /* + vol */ case 0x0d: mapped_code = eIR_VOL_UP; break; /* - vol */ case 0x0e: mapped_code = eIR_VOL_DOWN; break; /* '-' */ case 0x39: mapped_code = eIR_DOT; break; /* C */ case 0x28: mapped_code = eIR_DEBUG1; break; /* A */ case 0x17: mapped_code = eIR_LANG; break; /* B */ case 0x27: mapped_code = eIR_POWER_SAVING; break; /* exit */ case 0x12: mapped_code = eIR_EXIT; break; /* menu */ case 0x19: mapped_code = eIR_MENU; break; /* arrow buttons */ case 0x34: mapped_code = eIR_UP; break; case 0x35: mapped_code = eIR_DOWN; break; case 0x36: mapped_code = eIR_LEFT; break; case 0x37: mapped_code = eIR_RIGHT; break; /* select or OK */ case 0x11: mapped_code = eIR_SELECT; break; case 0x33: mapped_code = eIR_INFO; break; case 0x30: mapped_code = eIR_GUIDE; break; case 0x13: mapped_code = eIR_PRECH; break; /* power */ case 0x0a: mapped_code = eIR_POWER; break; /* help */ case 0x32: mapped_code = eIR_HELP; break; /* FAV */ case 0x15: mapped_code = eIR_FAV; break; } break; } return mapped_code; } /* Summary: Map key codes to match type 0 */ static unsigned int bapp_remote_map( unsigned int code ) { unsigned int mapped_code = 0xFF; BDBG_ERR(("############### %s, code = 0x%08x\n",__FUNCTION__,code)); code &= 0x000000FF; switch (code) { default: mapped_code = code; break; case 0x09: mapped_code = eIR_MENU; break; case 0x37: mapped_code = eIR_PGUP; break; case 0x38: mapped_code = eIR_PGDOWN; break; case 0x0d: mapped_code = eIR_EXIT; break; case 0x4c: mapped_code = eIR_PRECH; break; /* TODO: */ #if 0 case 0x74: mapped_code = eIR_A; break; case 0x71: mapped_code = eIR_B; break; case 0x72: mapped_code = eIR_C; break; case 0x73: mapped_code = eIR_D; break; #endif case 0x00: mapped_code = eIR_SELECT; break; case 0x01: mapped_code = eIR_UP; break; case 0x02: mapped_code = eIR_DOWN; break; case 0x03: mapped_code = eIR_LEFT; break; case 0x04: mapped_code = eIR_RIGHT; break; case 0x0b: mapped_code = eIR_GUIDE; break; case 0x6b: mapped_code = eIR_POWER; break; case 0x21: mapped_code = eIR_1; break; case 0x22: mapped_code = eIR_2; break; case 0x23: mapped_code = eIR_3; break; case 0x24: mapped_code = eIR_4; break; case 0x25: mapped_code = eIR_5; break; case 0x26: mapped_code = eIR_6; break; case 0x27: mapped_code = eIR_7; break; case 0x28: mapped_code = eIR_8; break; case 0x29: mapped_code = eIR_9; break; case 0x20: mapped_code = eIR_0; break; case 0x30: mapped_code = eIR_CH_UP; break; case 0x31: mapped_code = eIR_CH_DOWN; break; /* mute */ case 0x43: mapped_code = eIR_MUTE; break; /* + vol */ case 0x41: mapped_code = eIR_VOL_UP; break; /* - vol */ case 0x42: mapped_code = eIR_VOL_DOWN; break; /* exit */ case 0x2a: mapped_code = eIR_EXIT; break; case 0x2b: mapped_code = eIR_SELECT; break; case 0x35: mapped_code = eIR_INFO; break; case 0x32: mapped_code = eIR_PRECH; break; /* power off/on */ case 0x6c: case 0x6d: mapped_code = eIR_POWER; break; /* help */ case 0x56: mapped_code = eIR_HELP; break; /* FAV */ case 0x52: mapped_code = eIR_FAV; break; case 0xfd: mapped_code = eIR_LANG; break; } return mapped_code; } /* Summary: map gpio based buttons to key code based on current IR remote protocol used */ static unsigned int bapp_remote_map_code( bapp_remote_t handle, /* user input object */ unsigned int button_id) { BSTD_UNUSED(handle); return bapp_remote_map(button_id); } /* Summary: Read events from a user input device. Description: Because this function does not return a void* to raw data, but an array of structures, it is not called buser_input_read. */ bapp_result_t bapp_remote_get_event( bapp_remote_t handle, /* user input object */ bapp_event_t *p_event /* [out] event from the user input device */ ) { uint32_t pend_event; bapp_result_t result = eBAPP_RESULT_BUSY; struct bapp_remote *p_remote = (struct bapp_remote*)handle; unsigned int cur_ticks = bos_getticks(); /* check first IR remote (10ms timeout is consumed) */ pend_event = (unsigned int)bos_pend_event(p_remote->userio_queue, p_remote->pend_timeout); if (pend_event) { p_event->code = (pend_event & 0xFF); if (pend_event & eKEY_UP) { p_event->code |= eKEY_UP; } return eBAPP_RESULT_OK; } else if (s_p_remote->timeout && (cur_ticks > s_p_remote->timeout)) { bos_post_event(s_p_remote->userio_queue, (b_event_t *)(eKEY_UP|s_p_remote->last_key)); s_p_remote->timeout = 0; return eBAPP_RESULT_BUSY; } /* for RF4CE remote */ pend_event = (unsigned int)bos_pend_event(p_remote->queue,p_remote->pend_timeout); if (pend_event) { if (!s_config.use_rf4ce) return eBAPP_RESULT_TIMEOUT; BDBG_ERR(("%s, pend_event = 0x%08x\n",__FUNCTION__,pend_event)); p_event->code = pend_event & 0xFF; if (!(pend_event & eKEY_CMD)) { p_event->code = bapp_remote_map_code(handle,p_event->code); } if (pend_event & eKEY_UP) p_event->code |= eKEY_UP; result = eBAPP_RESULT_OK; BUSER_IO_MSG(("%s, event = 0x%08x\n",__FUNCTION__,p_event->code)); } return result; } void bapp_remote_set_config( bapp_remote_config_t *p_config ) { BKNI_Memcpy(&s_config,p_config,sizeof(s_config)); } void bapp_remote_get_config( bapp_remote_config_t *p_config ) { BKNI_Memcpy(p_config,&s_config,sizeof(s_config)); } void bapp_remote_post_event(uint32_t code,uint8_t modifier) { if (s_p_remote && s_p_remote->queue) { if (modifier == 0x01) bos_post_event(s_p_remote->queue,(b_event_t*)(eKEY_DOWN | code)); else bos_post_event(s_p_remote->queue,(b_event_t*)(eKEY_UP | code)); } } void buser_input_sim_key(uint32_t code) { bapp_remote_post_event(code, 0); }