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.

serialHelper.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. void usage(char *name);
  35. int parseNumber(char *s);
  36. int power(int num, int pow);
  37. void printFileData(size_t size);
  38. void printSerialPorts(char ** ports);
  39. /*
  40. Return values:
  41. 0: Success
  42. 1: Usage error
  43. 2: Serial Port Error
  44. 3: Data File Error
  45. */
  46. int main(int argc, char *argv[]) {
  47. size_t length, written;
  48. printf("Debugging Worker... Ignore me!\n");
  49. if (argc < 2) {
  50. usage(argv[0]);
  51. return 1;
  52. }
  53. if (argv[1][0] == 'p') {
  54. if (argc != 2) {
  55. usage(argv[0]);
  56. return 1;
  57. }
  58. char** ports = getSerialPorts();
  59. printSerialPorts(ports);
  60. free(ports);
  61. return 0;
  62. } else if (argv[1][0] == 'w') {
  63. if (argc != 4) {
  64. usage(argv[0]);
  65. return 1;
  66. }
  67. if (serialOpen(argv[2]) != 0) {
  68. printf("Error: Could not open %s\n", argv[1]);
  69. return 2;
  70. }
  71. // write file to com port
  72. length = readFile(argv[3]);
  73. if (length == 0) {
  74. printf("Error while reading %s\n", argv[2]);
  75. return 3;
  76. }
  77. written = serialWrite(fileData, length);
  78. while (written < length) {
  79. removeFromBeginning(length, written);
  80. length -= written;
  81. written = serialWrite(fileData, length);
  82. }
  83. } else {
  84. if (argc != 4) {
  85. usage(argv[0]);
  86. return 1;
  87. }
  88. if (serialOpen(argv[2]) != 0) {
  89. printf("Error: Could not open %s\n", argv[1]);
  90. return 2;
  91. }
  92. // Read from com port to file
  93. if (argc < 5) {
  94. printf("No size given. ");
  95. usage(argv[0]);
  96. return 1;
  97. }
  98. length = parseNumber(argv[3]);
  99. fileData = (char *)malloc(length * sizeof(char));
  100. // fill fileData with serial Port data
  101. written = 0;
  102. while (written < length) {
  103. written = serialRead(fileData + written, length - written);
  104. }
  105. printFileData(length);
  106. }
  107. free(fileData);
  108. fileData = NULL;
  109. serialClose();
  110. return 0;
  111. // }
  112. }
  113. void removeFromBeginning(size_t size, size_t remove) {
  114. size_t i;
  115. char *tmp = (char *)malloc((size - remove) * sizeof(char));
  116. for (i = 0; i < (size - remove); i++) {
  117. tmp[i] = fileData[i + remove];
  118. }
  119. free(fileData);
  120. fileData = tmp;
  121. }
  122. size_t readFile(char *path) {
  123. size_t size, i;
  124. FILE *fp;
  125. fp = fopen(path, "r");
  126. if (!fp) {
  127. return 0;
  128. }
  129. size = getFileSize(fp);
  130. fileData = (char *)malloc(size * sizeof(char));
  131. for (i = 0; i < size; i++) {
  132. fileData[i] = fgetc(fp);
  133. }
  134. fclose(fp);
  135. return size;
  136. }
  137. size_t getFileSize(FILE *fp) {
  138. size_t size = 0;
  139. int c;
  140. fseek(fp, 0, 0); // Set file pointer to beginning
  141. do { // Calculate size
  142. c = fgetc(fp);
  143. size++;
  144. } while (c != EOF);
  145. size--;
  146. fseek(fp, 0, 0);
  147. return size;
  148. }
  149. void usage(char *name) {
  150. #ifdef winHelper
  151. printf("Usage:\n%s r|w|p [COM1 [C:\\file\\to\\send.txt] [sizeToRead]]\n", name);
  152. #else
  153. printf("Usage:\n%s r|w|p [/dev/SerialPort [/file/to/send] [sizeToRead]]\n", name);
  154. #endif
  155. }
  156. int parseNumber(char *s) {
  157. int i, size = 0, result = 0;
  158. while (*(s++) != '\0') {
  159. size++;
  160. }
  161. for (i = 0; i < size; i++) {
  162. result += (s[i] - '0') * power(10, (size - i));
  163. }
  164. return result;
  165. }
  166. int power(int num, int pow) {
  167. int result = 1;
  168. while (pow > 0) {
  169. result *= num;
  170. pow--;
  171. }
  172. return result;
  173. }
  174. void printFileData(size_t size) {
  175. int i;
  176. for (i = 0; i < size; i++) {
  177. printf("%x", fileData[i]);
  178. if (i % 8 == 0) {
  179. printf("\n");
  180. }
  181. }
  182. }
  183. void printSerialPorts(char ** ports) {
  184. int i = 0;
  185. while (ports[i] != NULL) {
  186. printf("%s\n", ports[i]);
  187. i++;
  188. }
  189. }