Simple single-color 8x8x8 LED Cube with AVRs
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

main.c 5.8KB

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