Simple single-color 8x8x8 LED Cube with AVRs
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

serialHelper.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * unixHelper.c
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #ifdef winHelper
  26. #include "helper/winSerial.c"
  27. #else
  28. #include "helper/unixSerial.c"
  29. #endif
  30. char *fileData = NULL;
  31. void removeFromBeginning(size_t size, size_t remove);
  32. size_t readFile(char *path);
  33. size_t getFileSize(FILE *fp);
  34. /*
  35. Return values:
  36. 0: Success
  37. 1: Usage error
  38. 2: Serial Port Error
  39. 3: Data File Error
  40. */
  41. int main(int argc, char *argv[]) {
  42. size_t length, written;
  43. if (argc < 3) {
  44. #ifdef winHelper
  45. printf("Usage:\n%s COM1 C:\\file\\to\\send.txt\n", argv[0]);
  46. #else
  47. printf("Usage:\n%s /dev/SerialPort /file/to/send\n", argv[0]);
  48. #endif
  49. return 1;
  50. } else {
  51. if (serialOpen(argv[1]) != 0) {
  52. printf("Error: Could not open %s\n", argv[1]);
  53. return 2;
  54. }
  55. length = readFile(argv[2]);
  56. if (length == 0) {
  57. printf("Error while reading %s\n", argv[2]);
  58. return 3;
  59. }
  60. written = serialWrite(fileData, length);
  61. while (written < length) {
  62. removeFromBeginning(length, written);
  63. length -= written;
  64. written = serialWrite(fileData, length);
  65. }
  66. free(fileData);
  67. fileData = NULL;
  68. serialClose();
  69. return 0;
  70. }
  71. }
  72. void removeFromBeginning(size_t size, size_t remove) {
  73. size_t i;
  74. char *tmp = (char *)malloc((size - remove) * sizeof(char));
  75. for (i = 0; i < (size - remove); i++) {
  76. tmp[i] = fileData[i + remove];
  77. }
  78. free(fileData);
  79. fileData = tmp;
  80. }
  81. size_t readFile(char *path) {
  82. size_t size, i;
  83. FILE *fp;
  84. fp = fopen(path, "r");
  85. if (!fp) {
  86. return 0;
  87. }
  88. size = getFileSize(fp);
  89. fileData = (char *)malloc(size * sizeof(char));
  90. for (i = 0; i < size; i++) {
  91. fileData[i] = fgetc(fp);
  92. }
  93. fclose(fp);
  94. return size;
  95. }
  96. size_t getFileSize(FILE *fp) {
  97. size_t size = 0;
  98. int c;
  99. fseek(fp, 0, 0); // Set file pointer to beginning
  100. do { // Calculate size
  101. c = fgetc(fp);
  102. size++;
  103. } while (c != EOF);
  104. size--;
  105. fseek(fp, 0, 0);
  106. return size;
  107. }