Simple single-color 8x8x8 LED Cube with AVRs
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.

unixSerial.c 4.6KB

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