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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include "serial.h"
  5. #define OK 0x42
  6. #define ERROR 0x23
  7. void readAck(void);
  8. int serialWriteString(char *s);
  9. int serialReadTry(char *data, size_t length);
  10. int serialWriteTry(char *data, size_t length);
  11. void intHandler(int dummy);
  12. volatile int keepRunning = 1;
  13. #define suicide intHandler(0)
  14. int main(int argc, char *argv[]) {
  15. char c;
  16. int i;
  17. if (argc != 2) {
  18. printf("Usage: %s /dev/port\n", argv[0]);
  19. return 0;
  20. }
  21. if (serialOpen(argv[1]) != 0) {
  22. printf("Could not open port: %s\n", argv[1]);
  23. return 0;
  24. }
  25. signal(SIGINT, intHandler);
  26. signal(SIGQUIT, intHandler);
  27. printf("Port opened. Sending test data:\n");
  28. printf("\tSending command 's'...");
  29. c = 's';
  30. if (serialWriteTry(&c, 1) != 0) {
  31. printf(" Could not send it!\n");
  32. suicide;
  33. }
  34. printf(" Success!\n");
  35. readAck();
  36. printf("\tSending anim count (1)...");
  37. c = 1;
  38. if (serialWriteTry(&c, 1) != 0) {
  39. printf(" Could not send it!\n");
  40. suicide;
  41. }
  42. printf(" Success!\n");
  43. readAck();
  44. printf("\tSending frame count (1)...");
  45. c = 1;
  46. if (serialWriteTry(&c, 1) != 0) {
  47. printf(" Could not send it!\n");
  48. suicide;
  49. }
  50. printf(" Success!\n");
  51. readAck();
  52. printf("\tSending duration (1)...");
  53. c = 1;
  54. if (serialWriteTry(&c, 1) != 0) {
  55. printf(" Could not send it!\n");
  56. suicide;
  57. }
  58. printf(" Success!\n");
  59. readAck();
  60. printf("\tSending data");
  61. for(i = 0; i < 64; i++) {
  62. c = (char)i; // Some test data
  63. if (serialWriteTry(&c, 1) != 0) {
  64. printf(" Error while sending!\n");
  65. suicide;
  66. } else {
  67. printf(".");
  68. }
  69. }
  70. printf(" Success!\n");
  71. readAck();
  72. printf("\tSending end sequence");
  73. for (i = 0; i < 4; i++) {
  74. // Send 4 OKs
  75. c = OK;
  76. if (serialWriteTry(&c, 1) != 0) {
  77. printf(" Error while sending!\n");
  78. suicide;
  79. } else {
  80. printf(".");
  81. }
  82. }
  83. printf(" Success!\n");
  84. readAck();
  85. printf("Finished. Success!\n");
  86. serialClose();
  87. return 0;
  88. }
  89. void readAck() {
  90. char c;
  91. printf("Awaiting acknowledge...");
  92. if (serialReadTry(&c, 1) != 0) {
  93. printf(" Could not read!\n");
  94. suicide;
  95. }
  96. if (c != OK) {
  97. printf(" Was not OK!\n");
  98. suicide;
  99. }
  100. printf(" OK\n");
  101. }
  102. int serialWriteString(char *s) {
  103. return serialWriteTry(s, strlen(s));
  104. }
  105. // 1 on error, 0 on success
  106. int serialReadTry(char *data, size_t length) {
  107. int i = 0;
  108. int written = 0;
  109. int ret;
  110. while (keepRunning) {
  111. ret = serialRead((data + written), (length - written));
  112. if (ret == -1) {
  113. i++;
  114. } else {
  115. written += ret;
  116. }
  117. if (i > 10) {
  118. return 1;
  119. }
  120. if (written == length) {
  121. break;
  122. }
  123. }
  124. return 0;
  125. }
  126. // 1 on error, 0 on success
  127. int serialWriteTry(char *data, size_t length) {
  128. int i = 0;
  129. int written = 0;
  130. int ret;
  131. while (keepRunning) {
  132. ret = serialWrite((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. }
  145. return 0;
  146. }
  147. void intHandler(int dummy) {
  148. keepRunning = 0;
  149. serialClose();
  150. exit(0);
  151. }