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.5KB

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