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.

main.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. volatile int keepRunning = 1;
  14. #define suicide intHandler(0)
  15. int main(int argc, char *argv[]) {
  16. char c;
  17. int i, f;
  18. int animationCount = 1;
  19. int frameCount = 2;
  20. int duration = 3;
  21. int data = -1;
  22. if (argc < 2) {
  23. printf("Usage:\n%s /dev/port [-d 0xff]\n", argv[0]);
  24. return 0;
  25. }
  26. if (serialOpen(argv[1]) != 0) {
  27. printf("Could not open port: %s\n", argv[1]);
  28. return 0;
  29. }
  30. if ((argc >= 4) && (strcmp(argv[2], "-d") == 0)) {
  31. sscanf(argv[3], "%x", &data);
  32. frameCount = 1;
  33. }
  34. signal(SIGINT, intHandler);
  35. signal(SIGQUIT, intHandler);
  36. printf("Port opened. Sending test data:\n");
  37. printf("\tSending command 's'...");
  38. c = 's';
  39. if (serialWriteTry(&c, 1) != 0) {
  40. printf(" Could not send it!\n");
  41. suicide;
  42. }
  43. printf(" Success!\n");
  44. readAck();
  45. printf("\tSending anim count (1)...");
  46. c = (char)animationCount;
  47. if (serialWriteTry(&c, 1) != 0) {
  48. printf(" Could not send it!\n");
  49. suicide;
  50. }
  51. printf(" Success!\n");
  52. readAck();
  53. printf("\tSending frame count (2)...");
  54. c = (char)frameCount;
  55. if (serialWriteTry(&c, 1) != 0) {
  56. printf(" Could not send it!\n");
  57. suicide;
  58. }
  59. printf(" Success!\n");
  60. readAck();
  61. for (f = 0; f < frameCount; f++) {
  62. printf("\tSending duration (3)...");
  63. c = (char)duration;
  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 data");
  71. for(i = 0; i < 64; i++) {
  72. if (data == -1) {
  73. if (f == 0) {
  74. c = (char)i; // Some test data
  75. } else {
  76. c = (char)(63 - i);
  77. }
  78. } else {
  79. c = (char)data;
  80. }
  81. if (serialWriteTry(&c, 1) != 0) {
  82. printf(" Error while sending!\n");
  83. suicide;
  84. } else {
  85. printf(".");
  86. }
  87. }
  88. printf(" Success!\n");
  89. readAck();
  90. }
  91. printf("\tSending end sequence");
  92. for (i = 0; i < 4; i++) {
  93. // Send 4 OKs
  94. c = OK;
  95. if (serialWriteTry(&c, 1) != 0) {
  96. printf(" Error while sending!\n");
  97. suicide;
  98. } else {
  99. printf(".");
  100. }
  101. }
  102. printf(" Success!\n");
  103. readAck();
  104. printf("Finished. Success!\n");
  105. serialClose();
  106. return 0;
  107. }
  108. void readAck() {
  109. char c;
  110. printf("Awaiting acknowledge...");
  111. if (serialReadTry(&c, 1) != 0) {
  112. printf(" Could not read!\n");
  113. suicide;
  114. }
  115. if (c != OK) {
  116. printf(" Was not OK!\n");
  117. suicide;
  118. }
  119. printf(" OK\n");
  120. }
  121. int serialWriteString(char *s) {
  122. return serialWriteTry(s, strlen(s));
  123. }
  124. // 1 on error, 0 on success
  125. int serialReadTry(char *data, size_t length) {
  126. int i = 0;
  127. int written = 0;
  128. int ret;
  129. time_t start, end;
  130. start = time(NULL);
  131. while (keepRunning) {
  132. ret = serialRead((data + written), (length - written));
  133. if (ret == -1) {
  134. i++;
  135. } else {
  136. written += ret;
  137. }
  138. if (i > 10) {
  139. return 1;
  140. }
  141. if (written == length) {
  142. break;
  143. }
  144. end = time(NULL);
  145. if (difftime(start, end) > 2) {
  146. printf("Timeout went by. Exit!");
  147. suicide;
  148. }
  149. }
  150. return 0;
  151. }
  152. // 1 on error, 0 on success
  153. int serialWriteTry(char *data, size_t length) {
  154. int i = 0;
  155. int written = 0;
  156. int ret;
  157. while (keepRunning) {
  158. ret = serialWrite((data + written), (length - written));
  159. if (ret == -1) {
  160. i++;
  161. } else {
  162. written += ret;
  163. }
  164. if (i > 10) {
  165. return 1;
  166. }
  167. if (written == length) {
  168. break;
  169. }
  170. }
  171. return 0;
  172. }
  173. void intHandler(int dummy) {
  174. keepRunning = 0;
  175. serialClose();
  176. exit(0);
  177. }