usart_sim.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL);
  75. if (res)
  76. {
  77. printf("pthread create faild, <%d>\n", res);
  78. exit(EXIT_FAILURE);
  79. }
  80. }
  81. #endif
  82. #ifdef _WIN32
  83. static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam)
  84. #else
  85. static struct termios oldt, newt;
  86. /*simulate windows' getch(), it works!!*/
  87. void set_stty(void)
  88. {
  89. /* get terminal input's attribute */
  90. tcgetattr(STDIN_FILENO, &oldt);
  91. newt = oldt;
  92. /* set termios' local mode */
  93. newt.c_lflag &= ~(ECHO|ICANON);
  94. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  95. }
  96. void restore_stty(void)
  97. {
  98. /* recover terminal's attribute */
  99. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  100. }
  101. #define getch getchar
  102. static void * ThreadforKeyGet(void * lpParam)
  103. #endif /* not _WIN32*/
  104. {
  105. /*
  106. * 方向键(←): 0xe04b
  107. * 方向键(↑): 0xe048
  108. * 方向键(→): 0xe04d
  109. * 方向键(↓): 0xe050
  110. */
  111. unsigned char key;
  112. #ifndef _WIN32
  113. sigset_t sigmask, oldmask;
  114. /* set the getchar without buffer */
  115. sigfillset(&sigmask);
  116. pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
  117. set_stty();
  118. #endif
  119. (void)lpParam; //prevent compiler warnings
  120. for (;;)
  121. {
  122. key = getch();
  123. #ifdef _WIN32
  124. if (key == 0xE0)
  125. {
  126. key = getch();
  127. if (key == 0x48) //up key , 0x1b 0x5b 0x41
  128. {
  129. SAVEKEY(0x1b);
  130. SAVEKEY(0x5b);
  131. SAVEKEY(0x41);
  132. }
  133. else if (key == 0x50)//0x1b 0x5b 0x42
  134. {
  135. SAVEKEY(0x1b);
  136. SAVEKEY(0x5b);
  137. SAVEKEY(0x42);
  138. }
  139. continue;
  140. }
  141. #endif
  142. SAVEKEY(key);
  143. }
  144. } /*** ThreadforKeyGet ***/