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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * POSIX compatible serial port library
  3. * Uses 8 databits, no parity, 1 stop bit, no handshaking
  4. * By: Thomas Buck <taucher.bodensee@gmail.com>
  5. * Visit: www.xythobuz.org
  6. *
  7. * serial.c
  8. *
  9. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  10. *
  11. * This file is part of LED-Cube.
  12. *
  13. * LED-Cube is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * LED-Cube is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.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. #include "serial.h"
  35. int fd = -1;
  36. // Open the serial port
  37. int serialOpen(char *port) {
  38. struct termios options;
  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. fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
  47. tcgetattr(fd, &options);
  48. cfsetispeed(&options, BAUD); // Set speed
  49. cfsetospeed(&options, BAUD);
  50. options.c_cflag |= (CLOCAL | CREAD);
  51. options.c_cflag &= ~PARENB; // 8N1
  52. options.c_cflag &= ~CSTOPB;
  53. options.c_cflag &= ~CSIZE;
  54. options.c_cflag |= CS8;
  55. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  56. options.c_oflag &= ~OPOST; // Raw output
  57. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  58. tcsetattr(fd, TCSANOW, &options);
  59. return 0;
  60. }
  61. // Write to port. Returns number of characters sent, -1 on error
  62. ssize_t serialWrite(char *data, size_t length) {
  63. return write(fd, data, length);
  64. }
  65. // Read from port. Return number of characters read, 0 if none available, -1 on error
  66. ssize_t serialRead(char *data, size_t length) {
  67. ssize_t temp = read(fd, data, length);
  68. if ((temp == -1) && (errno == EAGAIN)) {
  69. return 0;
  70. } else {
  71. return temp;
  72. }
  73. }
  74. // Close the serial Port
  75. void serialClose(void) {
  76. close(fd);
  77. }
  78. char** namesInDev(int *siz) {
  79. DIR *dir;
  80. struct dirent *ent;
  81. int i = 0, size = 0;
  82. char **files = NULL;
  83. dir = opendir("/dev/");
  84. while ((ent = readdir(dir)) != NULL) {
  85. size++;
  86. }
  87. files = (char **)malloc((size + 1) * sizeof(char *));
  88. files[size++] = NULL;
  89. closedir(dir);
  90. dir = opendir("/dev/");
  91. while ((ent = readdir(dir)) != NULL) {
  92. files[i] = (char *)malloc((strlen(ent->d_name) + 1) * sizeof(char));
  93. files[i] = strcpy(files[i], ent->d_name);
  94. i++;
  95. }
  96. closedir(dir);
  97. char *tmp = NULL;
  98. // Fix every string, addin /dev/ in front of it...
  99. for (i = 0; i < (size - 1); i++) {
  100. tmp = (char *)malloc((strlen(files[i]) + 6) * sizeof(char));
  101. tmp[0] = '/';
  102. tmp[1] = 'd';
  103. tmp[2] = 'e';
  104. tmp[3] = 'v';
  105. tmp[4] = '/';
  106. files[i] = strncat(tmp, files[i], strlen(files[i]));
  107. }
  108. *siz = size;
  109. return files;
  110. }
  111. char** getSerialPorts() {
  112. int size;
  113. char** files = namesInDev(&size);
  114. char **fin = NULL, **finish = NULL;
  115. int i = 0, j = 0, f, g;
  116. fin = (char **)malloc(size * sizeof(char *));
  117. // Has space for all files in dev!
  118. while (files[i] != NULL) {
  119. // Filter for SEARCH and if it is a serial port
  120. if (strstr(files[i], SEARCH) != NULL) {
  121. fin[j++] = files[i];
  122. } else {
  123. free(files[i]);
  124. }
  125. i++;
  126. }
  127. free(files);
  128. // Copy in memory with matching size, NULL at end
  129. finish = (char **)malloc((j + 1) * sizeof(char *));
  130. finish[j] = NULL;
  131. for (i = 0; i < j; i++) {
  132. finish[i] = fin[i];
  133. }
  134. free(fin);
  135. return finish;
  136. }