Simple single-color 8x8x8 LED Cube with AVRs
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

unixSerial.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * unixSerial.c
  3. *
  4. * POSIX compatible serial port library
  5. * Uses 8 databits, no parity, 1 stop bit, no handshaking
  6. *
  7. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <termios.h>
  29. #include <dirent.h>
  30. #include <errno.h>
  31. #define BAUD B38400
  32. int fd = -1;
  33. // Open the serial port
  34. int serialOpen(char *port) {
  35. struct termios options;
  36. if ((strcmp(ttyname(fileno(stdin)), port) == 0) || (strcmp(ttyname(fileno(stdout)), port) == 0)) {
  37. return -1;
  38. }
  39. if (fd != -1) {
  40. close(fd);
  41. }
  42. fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
  43. if (fd == -1) {
  44. return -1;
  45. }
  46. if (fcntl(fd, F_SETFL, FNDELAY) == -1) { // read() isn't blocking'
  47. close(fd);
  48. return -1;
  49. }
  50. if (tcgetattr(fd, &options) == -1) {
  51. close(fd);
  52. return -1;
  53. }
  54. if (cfsetispeed(&options, BAUD) == -1) { // Set speed
  55. close(fd);
  56. return -1;
  57. }
  58. if (cfsetospeed(&options, BAUD) == -1) {
  59. close(fd);
  60. return -1;
  61. }
  62. options.c_cflag |= (CLOCAL | CREAD);
  63. options.c_cflag &= ~PARENB; // 8N1
  64. options.c_cflag &= ~CSTOPB;
  65. options.c_cflag &= ~CSIZE;
  66. options.c_cflag |= CS8;
  67. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  68. options.c_oflag &= ~OPOST; // Raw output
  69. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  70. if (tcsetattr(fd, TCSANOW, &options) == -1) {
  71. close(fd);
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. // Write to port. Returns number of characters sent, -1 on error
  77. ssize_t serialWrite(char *data, size_t length) {
  78. return write(fd, data, length);
  79. }
  80. // Read from port. Return number of characters read, 0 if none available, -1 on error
  81. ssize_t serialRead(char *data, size_t length) {
  82. ssize_t temp = read(fd, data, length);
  83. if ((temp == -1) && (errno == EAGAIN)) {
  84. return 0;
  85. } else {
  86. return temp;
  87. }
  88. }
  89. // Close the serial Port
  90. void serialClose(void) {
  91. close(fd);
  92. }
  93. char** namesInDev(int *siz) {
  94. DIR *dir;
  95. struct dirent *ent;
  96. int i = 0, size = 0;
  97. char **files = NULL;
  98. dir = opendir("/dev/");
  99. while ((ent = readdir(dir)) != NULL) {
  100. size++;
  101. }
  102. files = (char **)malloc((size + 1) * sizeof(char *));
  103. files[size++] = NULL;
  104. closedir(dir);
  105. // printf("JNI: Counted files in /dev/: %d\n", size);
  106. dir = opendir("/dev/");
  107. while ((ent = readdir(dir)) != NULL) {
  108. if (i == (size - 1)) {
  109. break;
  110. }
  111. files[i] = (char *)malloc((strlen(ent->d_name) + 1) * sizeof(char));
  112. files[i] = strcpy(files[i], ent->d_name);
  113. i++;
  114. }
  115. closedir(dir);
  116. // printf("JNI: Got names...\n");
  117. char *tmp = NULL;
  118. // Fix every string, add /dev/ in front of it...
  119. for (i = 0; i < (size - 1); i++) {
  120. // printf("JNI: (%d)Allocating %d\n", i, strlen(files[i]) + 6);
  121. tmp = (char *)malloc((strlen(files[i]) + 6) * sizeof(char));
  122. if (tmp == NULL) {
  123. printf("JNI: Ran out of memory!\n");
  124. exit(1);
  125. }
  126. tmp[0] = '/';
  127. tmp[1] = 'd';
  128. tmp[2] = 'e';
  129. tmp[3] = 'v';
  130. tmp[4] = '/';
  131. tmp[5] = '\0';
  132. files[i] = strncat(tmp, files[i], strlen(files[i]));
  133. }
  134. // printf("JNI: Paths fixed!\n");
  135. *siz = size;
  136. return files;
  137. }
  138. char** getSerialPorts(const char *search) {
  139. int size;
  140. char** files = namesInDev(&size);
  141. char **fin = NULL, **finish = NULL;
  142. int i = 0, j = 0, f, g;
  143. // printf("JNI: Got files in /dev (%d)\n", size);
  144. fin = (char **)malloc(size * sizeof(char *));
  145. // Has space for all files in dev!
  146. while (files[i] != NULL) {
  147. // Filter for SEARCH and if it is a serial port
  148. if (strstr(files[i], search) != NULL) {
  149. // We have a match
  150. // printf("JNI: %s matched %s", files[i], search);
  151. // Don't actually check if it is a serial port
  152. // It causes long delays while trying to connect
  153. // to Bluetooth devices...
  154. // f = serialOpen(files[i]);
  155. // if (f != -1) {
  156. // printf(" and is a serial port\n");
  157. fin[j++] = files[i];
  158. // serialClose();
  159. // } else {
  160. // printf(" and is not a serial port\n");
  161. // free(files[i]);
  162. // }
  163. } else {
  164. free(files[i]);
  165. }
  166. i++;
  167. }
  168. free(files);
  169. // printf("JNI: Found %d serial ports\n", j);
  170. // Copy in memory with matching size, NULL at end
  171. finish = (char **)malloc((j + 1) * sizeof(char *));
  172. finish[j] = NULL;
  173. for (i = 0; i < j; i++) {
  174. finish[i] = fin[i];
  175. }
  176. free(fin);
  177. return finish;
  178. }