Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * POSIX compatible
  3. * Compatible API to libSerial (SerialHelper)
  4. * Opens a pseudoterminal, lets you write to it,
  5. * gives name of slave side.
  6. * By: Thomas Buck <taucher.bodensee@gmail.com>
  7. * Visit: www.xythobuz.org
  8. *
  9. * serial.c
  10. *
  11. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  12. *
  13. * This file is part of LED-Cube.
  14. *
  15. * LED-Cube is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * LED-Cube is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <termios.h>
  34. #include <dirent.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include "serial.h"
  38. int fd = -1;
  39. // Open pseudo terminal
  40. // return name of slave side or NULL on error.
  41. char *serialOpen() {
  42. struct termios options;
  43. if (fd != -1) {
  44. close(fd);
  45. }
  46. fd = posix_openpt(O_RDWR | O_NOCTTY);
  47. if (fd == -1) {
  48. return NULL;
  49. }
  50. // Set rigths
  51. if (grantpt(fd) == -1) {
  52. return NULL;
  53. }
  54. // Unlock slave
  55. if (unlockpt(fd) == -1) {
  56. return NULL;
  57. }
  58. // fcntl(fd, F_SETFL, FNDELAY); // read non blocking
  59. tcgetattr(fd, &options);
  60. cfsetispeed(&options, BAUD); // Set speed
  61. cfsetospeed(&options, BAUD);
  62. options.c_cflag |= (CLOCAL | CREAD);
  63. options.c_cflag &= ~PARENB; // 8N1
  64. options.c_cflag &= ~CSTOPB;
  65. options.c_cflag &= ~CSIZE;
  66. options.c_cflag |= CS8;
  67. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Raw input
  68. options.c_oflag &= ~OPOST; // Raw output
  69. options.c_iflag &= ~(IXON | IXOFF | IXANY); // No flow control
  70. tcsetattr(fd, TCSANOW, &options);
  71. return ptsname(fd);
  72. }
  73. // Write to port. Returns number of characters sent, -1 on error
  74. ssize_t serialWrite(char *data, size_t length) {
  75. return write(fd, data, length);
  76. }
  77. // Read from port. Return number of characters read, 0 if none available, -1 on error
  78. ssize_t serialRead(char *data, size_t length) {
  79. ssize_t temp = read(fd, data, length);
  80. if ((temp == -1) && (errno == EAGAIN)) {
  81. return 0;
  82. } else {
  83. return temp;
  84. }
  85. }
  86. // Close the serial Port
  87. void serialClose(void) {
  88. close(fd);
  89. }