Simple single-color 8x8x8 LED Cube with AVRs
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

unixSerial.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #define BAUD B19200
  34. int fd = -1;
  35. // Open the serial port
  36. int serialOpen(char *port) {
  37. struct termios options;
  38. if ((strcmp(ttyname(fileno(stdin)), port) == 0) || (strcmp(ttyname(fileno(stdout)), port) == 0)) {
  39. return -1;
  40. }
  41. if (fd != -1) {
  42. close(fd);
  43. }
  44. fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
  45. if (fd == -1) {
  46. return -1;
  47. }
  48. if (fcntl(fd, F_SETFL, FNDELAY) == -1) { // read() isn't blocking'
  49. close(fd);
  50. return -1;
  51. }
  52. if (tcgetattr(fd, &options) == -1) {
  53. close(fd);
  54. return -1;
  55. }
  56. if (cfsetispeed(&options, BAUD) == -1) { // Set speed
  57. close(fd);
  58. return -1;
  59. }
  60. if (cfsetospeed(&options, BAUD) == -1) {
  61. close(fd);
  62. return -1;
  63. }
  64. options.c_cflag |= (CLOCAL | CREAD);
  65. options.c_cflag &= ~PARENB; // 8N1
  66. options.c_cflag &= ~CSTOPB;
  67. options.c_cflag &= ~CSIZE;
  68. options.c_cflag |= CS8;
  69. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  70. options.c_oflag &= ~OPOST; // Raw output
  71. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  72. if (tcsetattr(fd, TCSANOW, &options) == -1) {
  73. close(fd);
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. // Write to port. Returns number of characters sent, -1 on error
  79. ssize_t serialWrite(char *data, size_t length) {
  80. return write(fd, data, length);
  81. }
  82. // Read from port. Return number of characters read, 0 if none available, -1 on error
  83. ssize_t serialRead(char *data, size_t length) {
  84. return read(fd, data, length);
  85. }
  86. // Close the serial Port
  87. void serialClose(void) {
  88. close(fd);
  89. }
  90. char** namesInDev(int *siz) {
  91. DIR *dir;
  92. struct dirent *ent;
  93. int i = 0, size = 0;
  94. char **files = NULL;
  95. dir = opendir("/dev/");
  96. while ((ent = readdir(dir)) != NULL) {
  97. size++;
  98. }
  99. files = (char **)malloc((size + 1) * sizeof(char *));
  100. files[size++] = NULL;
  101. closedir(dir);
  102. dir = opendir("/dev/");
  103. while ((ent = readdir(dir)) != NULL) {
  104. files[i] = (char *)malloc((strlen(ent->d_name) + 1) * sizeof(char));
  105. files[i] = strcpy(files[i], ent->d_name);
  106. i++;
  107. }
  108. closedir(dir);
  109. char *tmp = NULL;
  110. // Fix every string, addin /dev/ in front of it...
  111. for (i = 0; i < (size - 1); i++) {
  112. tmp = (char *)malloc((strlen(files[i]) + 6) * sizeof(char));
  113. tmp[0] = '/';
  114. tmp[1] = 'd';
  115. tmp[2] = 'e';
  116. tmp[3] = 'v';
  117. tmp[4] = '/';
  118. files[i] = strncat(tmp, files[i], strlen(files[i]));
  119. }
  120. *siz = size;
  121. return files;
  122. }
  123. char** getSerialPorts(const char *search) {
  124. int size;
  125. char** files = namesInDev(&size);
  126. char **fin = NULL, **finish = NULL;
  127. int i = 0, j = 0, f, g;
  128. // printf("JNI: Got files in /dev (%d)\n", size);
  129. fin = (char **)malloc(size * sizeof(char *));
  130. // Has space for all files in dev!
  131. while (files[i] != NULL) {
  132. // Filter for SEARCH and if it is a serial port
  133. if (strstr(files[i], search) != NULL) {
  134. // We have a match
  135. // printf("JNI: %s matched %s", files[i], search);
  136. // Don't actually check if it is a serial port
  137. // It causes long delays while trying to connect
  138. // to Bluetooth devices...
  139. // f = serialOpen(files[i]);
  140. // if (f != -1) {
  141. // printf(" and is a serial port\n");
  142. fin[j++] = files[i];
  143. // serialClose();
  144. // } else {
  145. // printf(" and is not a serial port\n");
  146. // free(files[i]);
  147. // }
  148. } else {
  149. free(files[i]);
  150. }
  151. i++;
  152. }
  153. free(files);
  154. // printf("JNI: Found %d serial ports\n", j);
  155. // Copy in memory with matching size, NULL at end
  156. finish = (char **)malloc((j + 1) * sizeof(char *));
  157. finish[j] = NULL;
  158. for (i = 0; i < j; i++) {
  159. finish[i] = fin[i];
  160. }
  161. free(fin);
  162. return finish;
  163. }