Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * main.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. // Allows to send test data or finished animations from CubeControl
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <strings.h>
  25. #include <time.h>
  26. #include "serial.h"
  27. #define OK 0x42
  28. #define ERROR 0x23
  29. void readAck(void);
  30. int serialWriteString(char *s);
  31. int serialReadTry(char *data, size_t length);
  32. int serialWriteTry(char *data, size_t length);
  33. void intHandler(int dummy);
  34. void transferFile(char *file);
  35. volatile int keepRunning = 1;
  36. FILE *fp = NULL;
  37. #define suicide intHandler(0)
  38. int main(int argc, char *argv[]) {
  39. char c;
  40. int i, f;
  41. int animationCount = 1;
  42. int frameCount = 2;
  43. int duration = 3;
  44. int data = -1;
  45. char command = -1;
  46. if (argc < 2) {
  47. printf("Usage:\n%s /dev/port [-d 0xff] | [-f /file]\n", argv[0]);
  48. return 0;
  49. }
  50. if (serialOpen(argv[1]) != 0) {
  51. printf("Could not open port: %s\n", argv[1]);
  52. return 0;
  53. }
  54. signal(SIGINT, intHandler);
  55. signal(SIGQUIT, intHandler);
  56. if ((argc == 4) && (strcmp(argv[2], "-d") == 0)) {
  57. sscanf(argv[3], "%x", &data);
  58. frameCount = 1;
  59. }
  60. if ((argc == 4) && (strcmp(argv[2], "-f") == 0)) {
  61. printf("Trying to send from file.\n");
  62. transferFile(argv[3]);
  63. }
  64. if (argc == 3) {
  65. command = argv[2][0];
  66. }
  67. printf("Port opened. Sending test data:\n");
  68. if (command != -1) {
  69. c = command;
  70. } else {
  71. c = 's';
  72. }
  73. printf("\tSending command '%c'...", c);
  74. if (serialWriteTry(&c, 1) != 0) {
  75. printf(" Could not send it!\n");
  76. suicide;
  77. }
  78. printf(" Success!\n");
  79. if (command != -1) {
  80. suicide;
  81. }
  82. readAck();
  83. printf("\tSending anim count (%d)...", animationCount);
  84. c = (char)animationCount;
  85. if (serialWriteTry(&c, 1) != 0) {
  86. printf(" Could not send it!\n");
  87. suicide;
  88. }
  89. printf(" Success!\n");
  90. readAck();
  91. printf("\tSending frame count (%d)...", frameCount);
  92. c = (char)frameCount;
  93. if (serialWriteTry(&c, 1) != 0) {
  94. printf(" Could not send it!\n");
  95. suicide;
  96. }
  97. printf(" Success!\n");
  98. readAck();
  99. for (f = 0; f < frameCount; f++) {
  100. printf("\tSending duration (%d)...", duration);
  101. c = (char)duration;
  102. if (serialWriteTry(&c, 1) != 0) {
  103. printf(" Could not send it!\n");
  104. suicide;
  105. }
  106. printf(" Success!\n");
  107. readAck();
  108. printf("\tSending data");
  109. for(i = 0; i < 64; i++) {
  110. if (data == -1) {
  111. if (f == 0) {
  112. c = (char)i; // Some test data
  113. } else {
  114. c = (char)(63 - i);
  115. }
  116. } else {
  117. c = (char)data;
  118. }
  119. if (serialWriteTry(&c, 1) != 0) {
  120. printf(" Error while sending!\n");
  121. suicide;
  122. } else {
  123. printf(".");
  124. }
  125. }
  126. printf(" Success!\n");
  127. readAck();
  128. }
  129. printf("\tSending end sequence");
  130. for (i = 0; i < 4; i++) {
  131. // Send 4 OKs
  132. c = OK;
  133. if (serialWriteTry(&c, 1) != 0) {
  134. printf(" Error while sending!\n");
  135. suicide;
  136. } else {
  137. printf(".");
  138. }
  139. }
  140. printf(" Success!\n");
  141. readAck();
  142. printf("Finished. Success!\n");
  143. serialClose();
  144. return 0;
  145. }
  146. void writeS(char c) {
  147. if (serialWriteTry(&c, 1) != 0) {
  148. printf("Error while sending!\n");
  149. suicide;
  150. }
  151. }
  152. int readFrame(char *frame) {
  153. char lineBuffer[80];
  154. char buff[3];
  155. int lines, byte, i = 0, tmp;
  156. buff[2] = '\0';
  157. fgets(lineBuffer, 80, fp);// Frame name, ignore
  158. for (lines = 0; lines < 8; lines++) {
  159. fgets(lineBuffer, 80, fp);
  160. for(byte = 0; byte < 8; byte++) {
  161. buff[0] = lineBuffer[(2 * byte)];
  162. buff[1] = lineBuffer[(2 * byte) + 1];
  163. sscanf(buff, "%x", &tmp);
  164. frame[i++] = (char)tmp;
  165. }
  166. }
  167. fgets(lineBuffer, 80, fp);
  168. if(lineBuffer[strlen(lineBuffer)-1] == '\n') {
  169. lineBuffer[strlen(lineBuffer)-1] = '\0';
  170. }
  171. sscanf(lineBuffer, "%d", &lines);
  172. return lines;
  173. }
  174. void transferFile(char *file) {
  175. char lineBuffer[80];
  176. int f, i;
  177. int fMax = 0;
  178. char frame[64];
  179. int duration;
  180. fp = fopen(file, "r");
  181. if (fp != NULL) {
  182. printf("File opened...\n");
  183. fgets(lineBuffer, 80, fp);
  184. if (lineBuffer[strlen(lineBuffer)-1] == '\n') {
  185. lineBuffer[strlen(lineBuffer)-1] = '\0';
  186. }
  187. fMax = atoi(lineBuffer);
  188. printf("Has %d frames.\n", fMax);
  189. fgets(lineBuffer, 80, fp);
  190. printf("Sendind command.\n");
  191. writeS('s');
  192. readAck();
  193. printf("Sendind animation count.\n");
  194. writeS(1); // Animation count
  195. readAck();
  196. printf("Sending frame count.\n");
  197. writeS(fMax); // Frame count
  198. readAck();
  199. for (f = 0; f < fMax; f++) {
  200. printf("Reading frame from file... ");
  201. duration = readFrame(frame);
  202. printf("Done!\nSending duration.\n");
  203. writeS(duration);
  204. readAck();
  205. printf("Sending frame (");
  206. for (i = 0; i < 64; i++) {
  207. writeS(frame[i]);
  208. printf("%x", frame[i]);
  209. if (i < 63) {
  210. printf(" ");
  211. }
  212. }
  213. printf(") Done!\n");
  214. readAck();
  215. printf("Wrote frame %d\n\n", f);
  216. }
  217. printf("Sending end sequence!\n");
  218. writeS(0x42);
  219. writeS(0x42);
  220. writeS(0x42);
  221. writeS(0x42);
  222. readAck();
  223. } else {
  224. printf("File not found\n");
  225. suicide;
  226. }
  227. printf("Success!\n");
  228. suicide;
  229. }
  230. void readAck() {
  231. char c;
  232. printf("Awaiting acknowledge...");
  233. if (serialReadTry(&c, 1) != 0) {
  234. printf(" Could not read!\n");
  235. suicide;
  236. }
  237. if (c != OK) {
  238. printf(" Was not OK!\n");
  239. suicide;
  240. }
  241. printf(" OK\n");
  242. }
  243. int serialWriteString(char *s) {
  244. return serialWriteTry(s, strlen(s));
  245. }
  246. // 1 on error, 0 on success
  247. int serialReadTry(char *data, size_t length) {
  248. int i = 0;
  249. int written = 0;
  250. int ret;
  251. time_t start, end;
  252. start = time(NULL);
  253. while (keepRunning) {
  254. ret = serialRead((data + written), (length - written));
  255. if (ret == -1) {
  256. i++;
  257. } else {
  258. written += ret;
  259. }
  260. if (i > 10) {
  261. return 1;
  262. }
  263. if (written == length) {
  264. break;
  265. }
  266. end = time(NULL);
  267. if (difftime(start, end) > 2) {
  268. printf("Timeout went by. Exit!");
  269. suicide;
  270. }
  271. }
  272. return 0;
  273. }
  274. // 1 on error, 0 on success
  275. int serialWriteTry(char *data, size_t length) {
  276. int i = 0;
  277. int written = 0;
  278. int ret;
  279. while (keepRunning) {
  280. ret = serialWrite((data + written), (length - written));
  281. if (ret == -1) {
  282. i++;
  283. } else {
  284. written += ret;
  285. }
  286. if (i > 10) {
  287. return 1;
  288. }
  289. if (written == length) {
  290. break;
  291. }
  292. }
  293. return 0;
  294. }
  295. void intHandler(int dummy) {
  296. keepRunning = 0;
  297. if (fp != NULL) {
  298. fclose(fp);
  299. }
  300. serialClose();
  301. exit(0);
  302. }