Mac OS X gamepad emulator for serial RC transmitters
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  5. * you can do whatever you want with this stuff. If we meet some day, and you
  6. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  7. * ----------------------------------------------------------------------------
  8. */
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <signal.h>
  12. #include "serial.h"
  13. #define BAUDRATE 115200
  14. #define PACKETSIZE 18
  15. #define HEADERBYTES 2
  16. #define HEADERBYTE_A 85
  17. #define HEADERBYTE_B 252
  18. #define CHECKSUMBYTES 2
  19. #define PAYLOADBYTES (PACKETSIZE - HEADERBYTES - CHECKSUMBYTES)
  20. #define CHANNELS 6
  21. /*
  22. #define TESTCHANNEL 2
  23. #define TESTCHANNELVALUE 2044
  24. */
  25. static int running = 1;
  26. static int firstPrint = 0;
  27. static int extraLine = 0;
  28. static void signalHandler(int signo) {
  29. running = 0;
  30. }
  31. int main(int argc, char* argv[]) {
  32. if (argc != 2) {
  33. printf("Usage:\n\t%s /dev/serial_port\n", argv[0]);
  34. return 1;
  35. }
  36. if (signal(SIGINT, signalHandler) == SIG_ERR) {
  37. perror("Couldn't register signal handler");
  38. return 1;
  39. }
  40. int fd = serialOpen(argv[1], BAUDRATE);
  41. if (fd == -1) {
  42. return 1;
  43. }
  44. while (running != 0) {
  45. if (serialHasChar(fd)) {
  46. unsigned char c1;
  47. serialReadChar(fd, (char*)&c1);
  48. if (c1 == HEADERBYTE_A) {
  49. // Found first byte of protocol start
  50. while (!serialHasChar(fd)) {
  51. if (running == 0) {
  52. serialClose(fd);
  53. return 0;
  54. }
  55. }
  56. unsigned char c2;
  57. serialReadChar(fd, (char*)&c2);
  58. if (c2 == HEADERBYTE_B) {
  59. // Protocol start has been found, read payload
  60. unsigned char data[PAYLOADBYTES];
  61. int read = 0;
  62. while ((read < PAYLOADBYTES) && (running != 0)) {
  63. read += serialReadRaw(fd, (char*)data + read, PAYLOADBYTES - read);
  64. }
  65. // Read 16bit checksum
  66. unsigned char checksumData[CHECKSUMBYTES];
  67. read = 0;
  68. while ((read < CHECKSUMBYTES) && (running != 0)) {
  69. read += serialReadRaw(fd, (char*)checksumData + read,
  70. CHECKSUMBYTES - read);
  71. }
  72. // Check if checksum matches
  73. uint16_t checksum = 0;
  74. for (int i = 0; i < PAYLOADBYTES; i++) {
  75. checksum += data[i];
  76. }
  77. if (checksum != ((checksumData[0] << 8) | checksumData[1])) {
  78. printf("Wrong checksum: %d != %d \n",
  79. checksum, ((checksumData[0] << 8) | checksumData[1]));
  80. extraLine++;
  81. if (extraLine > 1) {
  82. extraLine--;
  83. printf("\r\033[1A");
  84. }
  85. } else {
  86. // Decode channel values
  87. uint16_t buff[CHANNELS + 1];
  88. for (int i = 0; i < (CHANNELS + 1); i++) {
  89. buff[i] = data[2 * i] << 8;
  90. buff[i] |= data[(2 * i) + 1];
  91. }
  92. /*
  93. // Check Test Channel Value (?)
  94. if (buff[CHANNELS] != (TESTCHANNELVALUE - buff[TESTCHANNEL])) {
  95. printf("Wrong test channel value: %d != %d (%d - %d)\n",
  96. buff[CHANNELS], TESTCHANNELVALUE - buff[TESTCHANNEL],
  97. TESTCHANNELVALUE, buff[TESTCHANNEL]);
  98. printf("Correct would be: %d\n",
  99. buff[TESTCHANNEL] + buff[CHANNELS]);
  100. }
  101. */
  102. if (firstPrint == 0) {
  103. firstPrint = 1;
  104. } else {
  105. int num = CHANNELS + extraLine;
  106. extraLine = 0;
  107. for (int i = 0; i < num; i++) {
  108. printf("\r\033[1A");
  109. }
  110. }
  111. for (int i = 0; i < CHANNELS; i++) {
  112. printf("CH%d: %d \n", i + 1, buff[i] - 1000);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. serialClose(fd);
  120. return 0;
  121. }