| 1 | /****************************************************************************** |
|---|
| 2 | *_Copyright (c) 2009 Digital Stream Technology Inc. All Rights Reserved. |
|---|
| 3 | * |
|---|
| 4 | * Module: dstdduirt.c |
|---|
| 5 | * |
|---|
| 6 | * Description |
|---|
| 7 | * |
|---|
| 8 | * @author |
|---|
| 9 | * @version $Revision: 1.1 $ |
|---|
| 10 | * |
|---|
| 11 | ******************************************************************************/ |
|---|
| 12 | |
|---|
| 13 | #include <stdio.h> |
|---|
| 14 | #include <fcntl.h> |
|---|
| 15 | #include <sys/time.h> |
|---|
| 16 | #include <sys/types.h> |
|---|
| 17 | #include <sys/ioctl.h> |
|---|
| 18 | #include <unistd.h> |
|---|
| 19 | #include <sys/poll.h> |
|---|
| 20 | |
|---|
| 21 | #include "dsthalcommon.h" |
|---|
| 22 | #include "dsthallocal.h" |
|---|
| 23 | |
|---|
| 24 | #include "dstdduirt.h" |
|---|
| 25 | |
|---|
| 26 | #ifdef DMALLOC |
|---|
| 27 | #include <dmalloc.h> |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | /****************************************************************************** |
|---|
| 31 | * Global variable declaration |
|---|
| 32 | ******************************************************************************/ |
|---|
| 33 | |
|---|
| 34 | /****************************************************************************** |
|---|
| 35 | * Imported variable declaration |
|---|
| 36 | ******************************************************************************/ |
|---|
| 37 | |
|---|
| 38 | /****************************************************************************** |
|---|
| 39 | * Imported function declaration |
|---|
| 40 | ******************************************************************************/ |
|---|
| 41 | |
|---|
| 42 | /****************************************************************************** |
|---|
| 43 | * Local definitions |
|---|
| 44 | ******************************************************************************/ |
|---|
| 45 | |
|---|
| 46 | /****************************************************************************** |
|---|
| 47 | * Local typedefs |
|---|
| 48 | ******************************************************************************/ |
|---|
| 49 | |
|---|
| 50 | /****************************************************************************** |
|---|
| 51 | * Local variables declaration |
|---|
| 52 | ******************************************************************************/ |
|---|
| 53 | static int g_IrFD = -1; |
|---|
| 54 | static int g_KeyFD = -1; |
|---|
| 55 | |
|---|
| 56 | /****************************************************************************** |
|---|
| 57 | * Local function prototypes |
|---|
| 58 | ******************************************************************************/ |
|---|
| 59 | static int Init_IRThread(void); |
|---|
| 60 | static int Init_KeyPadThread(void); |
|---|
| 61 | static void Terminate_IRThread(void); |
|---|
| 62 | static void Terminate_KeyThread(void); |
|---|
| 63 | static int ir_debug = 0; |
|---|
| 64 | static int key_debug = 0; |
|---|
| 65 | |
|---|
| 66 | #if 0 |
|---|
| 67 | ___Common_Functions___() |
|---|
| 68 | #endif |
|---|
| 69 | DHL_RESULT DD_IR_Init(DHL_IR_FORMAT IrFormat) |
|---|
| 70 | { |
|---|
| 71 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 72 | |
|---|
| 73 | if ( (IrFormat < DHL_IR_START) || (IrFormat > DHL_IR_END) ) |
|---|
| 74 | { |
|---|
| 75 | printf("|%s| This IR format is not supported yet.\n", __FUNCTION__); |
|---|
| 76 | dhlResult = DHL_FAIL_NO_HARDWARE_SUPPORT; |
|---|
| 77 | return dhlResult; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // |
|---|
| 81 | // Open IR device. |
|---|
| 82 | // |
|---|
| 83 | |
|---|
| 84 | // |
|---|
| 85 | // Init and starts IR thread. |
|---|
| 86 | // |
|---|
| 87 | if (Init_IRThread() < 0 ) |
|---|
| 88 | { |
|---|
| 89 | printf("%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__); |
|---|
| 90 | dhlResult = DHL_FAIL_CORE_DRIVER; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | return dhlResult; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | DHL_RESULT DD_IR_Close(void) |
|---|
| 97 | { |
|---|
| 98 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 99 | |
|---|
| 100 | Terminate_IRThread(); |
|---|
| 101 | |
|---|
| 102 | if ( g_IrFD != -1 ) |
|---|
| 103 | close(g_IrFD); |
|---|
| 104 | |
|---|
| 105 | g_IrFD = -1; |
|---|
| 106 | |
|---|
| 107 | return dhlResult; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | DHL_RESULT DD_IR_SetFormat(DHL_IR_FORMAT IrFormat) |
|---|
| 111 | { |
|---|
| 112 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 113 | |
|---|
| 114 | if ( (IrFormat < DHL_IR_START) || (IrFormat > DHL_IR_END) ) |
|---|
| 115 | { |
|---|
| 116 | printf("|%s| This IR format is not supported yet.\n", __FUNCTION__); |
|---|
| 117 | dhlResult = DHL_FAIL_NO_HARDWARE_SUPPORT; |
|---|
| 118 | return dhlResult; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | return dhlResult; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | DHL_RESULT DD_IR_SetCustomCode(DS_U16 CustomCode) |
|---|
| 125 | { |
|---|
| 126 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 127 | |
|---|
| 128 | return dhlResult; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | static int g_IR_ScanTime = IR_REPEAT_TIMEOUT * 10; |
|---|
| 132 | void DD_IR_SetScanTime(DS_U16 ScanTime) |
|---|
| 133 | { |
|---|
| 134 | g_IR_ScanTime = ScanTime; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | #if 0 |
|---|
| 138 | ___IR_Thread___() |
|---|
| 139 | #endif |
|---|
| 140 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 141 | // |
|---|
| 142 | // NEC - HDiT ¸®¸ðÄÁÀ¸·Î Å×½ºÆ® °á°ú |
|---|
| 143 | // ÀÀ´ä¼Óµµ: (Button 1ȸ ÀÔ·Â ÈÄ, DN --> UP Event ¶ß´Â ¼Óµµ) 120 msec |
|---|
| 144 | // |
|---|
| 145 | static int g_IrThreadRun = 0; |
|---|
| 146 | static OS_TASK_ID irId = (OS_TASK_ID)0; |
|---|
| 147 | //static DS_U32 IRTimeout = 0; |
|---|
| 148 | |
|---|
| 149 | void cbIrFunction(int bKeypad, DS_U32 Code, int bPressed) |
|---|
| 150 | { |
|---|
| 151 | extern IRCbFunc_t cbIRFunc; |
|---|
| 152 | DS_U32 curTick = OS_GetTickCount(); |
|---|
| 153 | |
|---|
| 154 | if ( ir_debug ) |
|---|
| 155 | printf("%d.%02d [%s-0x%x-%s]\n", (int)(curTick / 100), (int)(curTick % 100), bKeypad!=DHL_IR_REMOCON ? "KEYPAD" : "REMOCON", (unsigned int)Code, bPressed ? "DN" : "UP"); |
|---|
| 156 | |
|---|
| 157 | if ( cbIRFunc ) |
|---|
| 158 | cbIRFunc( bKeypad, Code, bPressed ); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | static void tIR_Thread(DS_U32 arg) |
|---|
| 162 | { |
|---|
| 163 | unsigned long ch[4] = {0,0,0,0}; |
|---|
| 164 | struct pollfd pfd; |
|---|
| 165 | while (1) |
|---|
| 166 | { |
|---|
| 167 | OS_Delay(1); |
|---|
| 168 | if (g_IrThreadRun == 0) continue; |
|---|
| 169 | pfd.fd = g_IrFD; |
|---|
| 170 | pfd.events = POLLIN; |
|---|
| 171 | pfd.revents = 0; |
|---|
| 172 | if (poll(&pfd, 1, 0) <= 0) continue; |
|---|
| 173 | if (read(g_IrFD, ch, 4) <= 0) continue; |
|---|
| 174 | cbIrFunction( DHL_IR_REMOCON, ch[0], 1 ); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | static int Init_IRThread(void) |
|---|
| 179 | { |
|---|
| 180 | if ( g_IrThreadRun == 1 ) { |
|---|
| 181 | printf("|%s| WARNING: IR thread is already initialized. (IR Thread ID: 0x%08lX)\n", __FUNCTION__, (DS_U32)irId); |
|---|
| 182 | return 0; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if ( irId == (OS_TASK_ID)0 ) |
|---|
| 186 | { |
|---|
| 187 | irId = OS_SpawnTask( tIR_Thread, "tIR_Thread", IR_THREAD_PRIORITY, IR_THREAD_STACKSIZE, 0 ); |
|---|
| 188 | if ( irId == (OS_TASK_ID)0 ) { |
|---|
| 189 | printf("|%s| ERROR: Cannot create thread for IR.\n", __FUNCTION__); |
|---|
| 190 | return -1; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | g_IrThreadRun = 1; |
|---|
| 195 | return 0; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | static void Terminate_IRThread(void) |
|---|
| 199 | { |
|---|
| 200 | g_IrThreadRun = 0; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | void set_ir_debug(int d) |
|---|
| 204 | { |
|---|
| 205 | ir_debug = d; |
|---|
| 206 | printf("IR Debug: %s (%d)\n", d ? "On" : "Off", d); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | #if 0 |
|---|
| 210 | ___Common_Functions_For_KeyPad___() |
|---|
| 211 | #endif |
|---|
| 212 | |
|---|
| 213 | ///////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 214 | // |
|---|
| 215 | // |
|---|
| 216 | // |
|---|
| 217 | // |
|---|
| 218 | static int g_KeyThreadRun = 0; |
|---|
| 219 | static OS_TASK_ID KeyId = (OS_TASK_ID)0; |
|---|
| 220 | //static DS_U32 KeyPadTimeout = 0; |
|---|
| 221 | static int g_KeyPad_ScanTime = 100; |
|---|
| 222 | |
|---|
| 223 | DHL_RESULT DD_KeyPad_Init(void) |
|---|
| 224 | { |
|---|
| 225 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 226 | // |
|---|
| 227 | // Init and starts IR thread. |
|---|
| 228 | // |
|---|
| 229 | if (Init_KeyPadThread() < 0 ) { |
|---|
| 230 | printf("%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__); |
|---|
| 231 | dhlResult = DHL_FAIL_CORE_DRIVER; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | return dhlResult; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | DHL_RESULT DD_KeyPad_Close(void) |
|---|
| 238 | { |
|---|
| 239 | DHL_RESULT dhlResult = DHL_OK; |
|---|
| 240 | |
|---|
| 241 | Terminate_KeyThread(); |
|---|
| 242 | |
|---|
| 243 | if ( g_KeyFD != -1 ) |
|---|
| 244 | close(g_IrFD); |
|---|
| 245 | |
|---|
| 246 | g_KeyFD = -1; |
|---|
| 247 | |
|---|
| 248 | return dhlResult; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | void DD_KeyPad_SetScanTime(DS_U16 ScanTime) |
|---|
| 252 | { |
|---|
| 253 | g_KeyPad_ScanTime = ScanTime; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | static void tKeyPad_Thread(DS_U32 arg) |
|---|
| 257 | { |
|---|
| 258 | while ( 1 ) |
|---|
| 259 | { |
|---|
| 260 | OS_mDelay(g_KeyPad_ScanTime); |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | static int Init_KeyPadThread(void) |
|---|
| 265 | { |
|---|
| 266 | if ( g_KeyThreadRun == 1 ) |
|---|
| 267 | { |
|---|
| 268 | printf("|%s| WARNING: IR thread is already initialized. (IR Thread ID: 0x%08lX)\n", __FUNCTION__, (DS_U32)irId); |
|---|
| 269 | return 0; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | if ( KeyId == (OS_TASK_ID)0 ) |
|---|
| 273 | { |
|---|
| 274 | irId = OS_SpawnTask( tKeyPad_Thread, "tKeyPad_Thread", IR_THREAD_PRIORITY, IR_THREAD_STACKSIZE, 0 ); |
|---|
| 275 | if ( irId == (OS_TASK_ID)0 ) |
|---|
| 276 | { |
|---|
| 277 | printf("|%s| ERROR: Cannot create thread for IR.\n", __FUNCTION__); |
|---|
| 278 | return -1; |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | g_IrThreadRun = 1; |
|---|
| 283 | return 0; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | static void Terminate_KeyThread(void) |
|---|
| 287 | { |
|---|
| 288 | g_IrThreadRun = 0; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | void set_key_debug(int d) |
|---|
| 292 | { |
|---|
| 293 | key_debug = d; |
|---|
| 294 | printf("KEY Debug: %s (%d)\n", d ? "On" : "Off", d); |
|---|
| 295 | } |
|---|