Simple single-color 8x8x8 LED Cube with AVRs
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

unixSerial.c 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <unistd.h>
  29. #include <fcntl.h>
  30. #include <termios.h>
  31. #define BAUD B19200
  32. int fd = -1;
  33. // Open the serial port
  34. int serialOpen(char *port) {
  35. struct termios options;
  36. if (fd != -1) {
  37. close(fd);
  38. }
  39. fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
  40. if (fd == -1) {
  41. return -1;
  42. }
  43. fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
  44. tcgetattr(fd, &options);
  45. cfsetispeed(&options, BAUD); // Set speed
  46. cfsetospeed(&options, BAUD);
  47. options.c_cflag |= (CLOCAL | CREAD);
  48. options.c_cflag &= ~PARENB; // 8N1
  49. options.c_cflag &= ~CSTOPB;
  50. options.c_cflag &= ~CSIZE;
  51. options.c_cflag |= CS8;
  52. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  53. options.c_oflag &= ~OPOST; // Raw output
  54. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  55. tcsetattr(fd, TCSANOW, &options);
  56. return 0;
  57. }
  58. // Write to port. Returns number of characters sent, -1 on error
  59. ssize_t serialWrite(char *data, size_t length) {
  60. return write(fd, data, length);
  61. }
  62. // Read from port. Return number of characters read, 0 if none available, -1 on error
  63. ssize_t serialRead(char *data, size_t length) {
  64. return read(fd, data, length);
  65. }
  66. // Close the serial Port
  67. void serialClose(void) {
  68. close(fd);
  69. }