usart_sim.c 3.2 KB

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