Mac OS X gamepad emulator for serial RC transmitters
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

protocol.c 4.8KB

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