usart_sim.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * File : serial.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-09-25 prife first implementation
  13. * 2013-01-15 prife support linux
  14. * 2013-02-6 prife rewrite to fit the new serial.c
  15. */
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #ifdef _WIN32
  19. #include <windows.h>
  20. #include <mmsystem.h>
  21. #include <conio.h>
  22. #endif
  23. #include <stdio.h>
  24. #include "serial.h"
  25. struct serial_device serial1;
  26. #define SAVEKEY(key) seial_save_byte(key, &serial1)
  27. #ifdef _WIN32
  28. /*
  29. * Handler for OSKey Thread
  30. */
  31. static HANDLE OSKey_Thread;
  32. static DWORD OSKey_ThreadID;
  33. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
  34. void rt_hw_usart_init(void)
  35. {
  36. rt_hw_serial_init(&serial1, RT_CONSOLE_DEVICE_NAME);
  37. /*
  38. * create serial thread that receive key input from keyboard
  39. */
  40. OSKey_Thread = CreateThread(NULL,
  41. 0,
  42. (LPTHREAD_START_ROUTINE)ThreadforKeyGet,
  43. 0,
  44. CREATE_SUSPENDED,
  45. &OSKey_ThreadID);
  46. if (OSKey_Thread == NULL)
  47. {
  48. //Display Error Message
  49. return;
  50. }
  51. SetThreadPriority(OSKey_Thread,
  52. THREAD_PRIORITY_NORMAL);
  53. SetThreadPriorityBoost(OSKey_Thread,
  54. TRUE);
  55. SetThreadAffinityMask(OSKey_Thread,
  56. 0x01);
  57. /*
  58. * Start OS get key Thread
  59. */
  60. ResumeThread(OSKey_Thread);
  61. }
  62. #else /* POSIX version */
  63. #include <pthread.h>
  64. #include <semaphore.h>
  65. #include <stdlib.h>
  66. #include <signal.h>
  67. #include <termios.h> /* for tcxxxattr, ECHO, etc */
  68. #include <unistd.h> /* for STDIN_FILENO */
  69. static void * ThreadforKeyGet(void * lpParam);
  70. static pthread_t OSKey_Thread;
  71. void rt_hw_usart_init(void)
  72. {
  73. int res;
  74. rt_hw_serial_init(&serial1, RT_CONSOLE_DEVICE_NAME);
  75. res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL);
  76. if (res)
  77. {
  78. printf("pthread create faild, <%d>\n", res);
  79. exit(EXIT_FAILURE);
  80. }
  81. }
  82. #endif
  83. #ifdef _WIN32
  84. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
  85. #else
  86. static struct termios oldt, newt;
  87. /*simulate windows' getch(), it works!!*/
  88. void set_stty(void)
  89. {
  90. /* get terminal input's attribute */
  91. tcgetattr(STDIN_FILENO, &oldt);
  92. newt = oldt;
  93. /* set termios' local mode */
  94. newt.c_lflag &= ~(ECHO|ICANON);
  95. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  96. }
  97. void restore_stty(void)
  98. {
  99. /* recover terminal's attribute */
  100. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  101. }
  102. #define getch getchar
  103. static void * ThreadforKeyGet(void * lpParam)
  104. #endif /* not _WIN32*/
  105. {
  106. /*
  107. * 方向键(←): 0xe04b
  108. * 方向键(↑): 0xe048
  109. * 方向键(→): 0xe04d
  110. * 方向键(↓): 0xe050
  111. */
  112. unsigned char key;
  113. #ifndef _WIN32
  114. sigset_t sigmask, oldmask;
  115. /* set the getchar without buffer */
  116. sigfillset(&sigmask);
  117. pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
  118. set_stty();
  119. #endif
  120. (void)lpParam; //prevent compiler warnings
  121. for (;;)
  122. {
  123. key = getch();
  124. #ifdef _WIN32
  125. if (key == 0xE0)
  126. {
  127. key = getch();
  128. if (key == 0x48) //up key , 0x1b 0x5b 0x41
  129. {
  130. SAVEKEY(0x1b);
  131. SAVEKEY(0x5b);
  132. SAVEKEY(0x41);
  133. }
  134. else if (key == 0x50)//0x1b 0x5b 0x42
  135. {
  136. SAVEKEY(0x1b);
  137. SAVEKEY(0x5b);
  138. SAVEKEY(0x42);
  139. }
  140. continue;
  141. }
  142. #endif
  143. SAVEKEY(key);
  144. }
  145. } /*** ThreadforKeyGet ***/