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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 if (argv[1][0] == 'r') {
  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. } else {
  107. printf("Unrecognized Option: %s\n", argv[1]);
  108. usage(argv[0]);
  109. return 1;
  110. }
  111. free(fileData);
  112. fileData = NULL;
  113. serialClose();
  114. return 0;
  115. // }
  116. }
  117. void removeFromBeginning(size_t size, size_t remove) {
  118. size_t i;
  119. char *tmp = (char *)malloc((size - remove) * sizeof(char));
  120. for (i = 0; i < (size - remove); i++) {
  121. tmp[i] = fileData[i + remove];
  122. }
  123. free(fileData);
  124. fileData = tmp;
  125. }
  126. size_t readFile(char *path) {
  127. size_t size, i;
  128. FILE *fp;
  129. fp = fopen(path, "r");
  130. if (!fp) {
  131. return 0;
  132. }
  133. size = getFileSize(fp);
  134. fileData = (char *)malloc(size * sizeof(char));
  135. for (i = 0; i < size; i++) {
  136. fileData[i] = fgetc(fp);
  137. }
  138. fclose(fp);
  139. return size;
  140. }
  141. size_t getFileSize(FILE *fp) {
  142. size_t size = 0;
  143. int c;
  144. fseek(fp, 0, 0); // Set file pointer to beginning
  145. do { // Calculate size
  146. c = fgetc(fp);
  147. size++;
  148. } while (c != EOF);
  149. size--;
  150. fseek(fp, 0, 0);
  151. return size;
  152. }
  153. void usage(char *name) {
  154. #ifdef winHelper
  155. printf("Usage:\n%s r|w|p [COM1 [C:\\file\\to\\send.txt] [sizeToRead]]\n", name);
  156. #else
  157. printf("Usage:\n%s r|w|p [/dev/SerialPort [/file/to/send] [sizeToRead]]\n", name);
  158. #endif
  159. }
  160. int parseNumber(char *s) {
  161. int i, size = 0, result = 0;
  162. while (*(s++) != '\0') {
  163. size++;
  164. }
  165. for (i = 0; i < size; i++) {
  166. result += (s[i] - '0') * power(10, (size - i));
  167. }
  168. return result;
  169. }
  170. int power(int num, int pow) {
  171. int result = 1;
  172. while (pow > 0) {
  173. result *= num;
  174. pow--;
  175. }
  176. return result;
  177. }
  178. void printFileData(size_t size) {
  179. int i;
  180. for (i = 0; i < size; i++) {
  181. printf("%x", fileData[i]);
  182. if (i % 8 == 0) {
  183. printf("\n");
  184. }
  185. }
  186. }
  187. void printSerialPorts(char ** ports) {
  188. int i = 0;
  189. while (ports[i] != NULL) {
  190. printf("%s\n", ports[i]);
  191. i++;
  192. }
  193. }