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.

transmit.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * transmit.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. #include <avr/io.h>
  22. #include <util/delay.h>
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <avr/wdt.h>
  26. #include "memLayer.h"
  27. #include "serial.h"
  28. #include "time.h"
  29. #include "strings.h"
  30. #include "audio.h"
  31. #define OK 0x42
  32. #define ERROR 0x23
  33. #define TIMEOUTPERFRAME 500
  34. // These are global variables from main.c
  35. extern char buffer[11];
  36. void recieveAnimations(void) {
  37. uint8_t animCount, a, frameCount, f, i, c;
  38. uint16_t completeCount = 0;
  39. uint8_t *frame = (uint8_t *)malloc(65);
  40. uint64_t timestamp = getSystemTime();
  41. uint64_t timeout = TIMEOUTPERFRAME;
  42. serialWrite(OK); // We are ready...
  43. while (!serialHasChar()) { // Wait for answer
  44. if ((getSystemTime() - timestamp) <= timeout) {
  45. wdt_reset();
  46. } else {
  47. setAnimationCount(0);
  48. return;
  49. }
  50. }
  51. c = serialGet();
  52. animCount = c; // Got animation count
  53. serialWrite(OK);
  54. for (a = 0; a < animCount; a++) {
  55. while (!serialHasChar()) { // Wait for answer
  56. if ((getSystemTime() - timestamp) <= timeout) {
  57. wdt_reset();
  58. } else {
  59. setAnimationCount(0);
  60. return;
  61. }
  62. }
  63. c = serialGet();
  64. frameCount = c; // Got frame count
  65. serialWrite(OK);
  66. for (f = 0; f < frameCount; f++) {
  67. timeout += TIMEOUTPERFRAME;
  68. while (!serialHasChar()) { // Wait for answer
  69. if ((getSystemTime() - timestamp) <= timeout) {
  70. wdt_reset();
  71. } else {
  72. setAnimationCount(0);
  73. return;
  74. }
  75. }
  76. c = serialGet();
  77. frame[64] = c; // Got duration
  78. serialWrite(OK);
  79. for (i = 0; i < 64; i++) {
  80. while (!serialHasChar()) { // Wait for answer
  81. if ((getSystemTime() - timestamp) <= timeout) {
  82. wdt_reset();
  83. } else {
  84. setAnimationCount(0);
  85. return;
  86. }
  87. }
  88. c = serialGet();
  89. frame[i] = c; // Got data byte
  90. }
  91. serialWrite(OK);
  92. setFrame(completeCount++, frame);
  93. }
  94. }
  95. free(frame);
  96. while (!serialHasChar()) { // Wait for answer
  97. if ((getSystemTime() - timestamp) <= timeout) {
  98. wdt_reset();
  99. } else {
  100. setAnimationCount(0);
  101. return;
  102. }
  103. }
  104. c = serialGet();
  105. while (!serialHasChar()) { // Wait for answer
  106. if ((getSystemTime() - timestamp) <= timeout) {
  107. wdt_reset();
  108. } else {
  109. setAnimationCount(0);
  110. return;
  111. }
  112. }
  113. c = serialGet();
  114. while (!serialHasChar()) { // Wait for answer
  115. if ((getSystemTime() - timestamp) <= timeout) {
  116. wdt_reset();
  117. } else {
  118. setAnimationCount(0);
  119. return;
  120. }
  121. }
  122. c = serialGet();
  123. while (!serialHasChar()) { // Wait for answer
  124. if ((getSystemTime() - timestamp) <= timeout) {
  125. wdt_reset();
  126. } else {
  127. setAnimationCount(0);
  128. return;
  129. }
  130. }
  131. c = serialGet();
  132. serialWrite(OK);
  133. setAnimationCount(completeCount);
  134. }
  135. void transmitAnimations(void) {
  136. serialWrite(ERROR);
  137. }
  138. void sendAudioData(void) {
  139. uint8_t i;
  140. uint8_t *audioData = getAudioData();
  141. if (audioData == NULL) {
  142. serialWriteString(getString(21));
  143. } else {
  144. serialWriteString(getString(22));
  145. for (i = 0; i < 7; i++) {
  146. serialWrite(i + '0');
  147. serialWriteString(": ");
  148. itoa(audioData[i], buffer, 10);
  149. serialWriteString(buffer);
  150. serialWrite('\n');
  151. }
  152. }
  153. }
  154. void printTime(void) {
  155. serialWriteString(getString(14));
  156. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  157. serialWriteString("ms");
  158. if (getSystemTime() > 60000) {
  159. serialWriteString(" (");
  160. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  161. serialWriteString(" min)");
  162. }
  163. if (getSystemTime() > 1000) {
  164. serialWriteString(" (");
  165. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  166. itoa(getSystemTime() % 1000, buffer, 10);
  167. if (buffer[0] != '\0')
  168. serialWrite('.');
  169. if (buffer[2] == '\0')
  170. serialWrite('0');
  171. if (buffer[1] == '\0')
  172. serialWrite('0');
  173. if (buffer[0] != '\0')
  174. serialWriteString(buffer);
  175. serialWriteString("s)\n");
  176. } else {
  177. serialWrite('\n');
  178. }
  179. }
  180. void dumpFrame(uint8_t *f) {
  181. uint8_t zeile, spalte;
  182. for (zeile = 0; zeile < 8; zeile++) {
  183. serialWrite(zeile + '0');
  184. serialWriteString(": ");
  185. for (spalte = 0; spalte < 8; spalte++) {
  186. itoa(f[(8 * zeile) + spalte], buffer, 16);
  187. serialWriteString(buffer);
  188. serialWrite(' ');
  189. }
  190. serialWrite('\n');
  191. }
  192. serialWriteString(getString(35));
  193. itoa(f[64], buffer, 10);
  194. serialWriteString(buffer);
  195. serialWrite('\n');
  196. }
  197. uint8_t *readLine(void) {
  198. uint8_t ptr = 0;
  199. while(1) {
  200. wdt_reset();
  201. if (serialHasChar()) {
  202. buffer[ptr] = serialGet();
  203. serialWrite(buffer[ptr]);
  204. if ((buffer[ptr] == '\n') || (ptr == sizeof(buffer) - 1)) {
  205. buffer[ptr] = '\0';
  206. return (uint8_t *)buffer;
  207. }
  208. ptr++;
  209. }
  210. }
  211. }
  212. uint8_t readNumber(uint8_t base) {
  213. uint8_t *s = readLine();
  214. uint8_t val = (uint8_t)strtoul((char *)s, NULL, base);
  215. return val;
  216. }
  217. void writeNumber(uint8_t num, uint8_t base) {
  218. itoa(num, buffer, base);
  219. serialWriteString(buffer);
  220. }
  221. uint8_t *readAFrame(void) {
  222. uint8_t *frame = (uint8_t *)malloc(65);
  223. uint8_t byte;
  224. serialWriteString(getString(35)); // "Duration: "
  225. frame[64] = readNumber(10);
  226. for (byte = 0; byte < 64; byte++) {
  227. writeNumber(byte, 10);
  228. serialWriteString(": "); // "xx: "
  229. frame[byte] = readNumber(16);
  230. }
  231. serialWriteString(getString(33));
  232. return frame;
  233. }
  234. void simpleAnimationInput(void) {
  235. uint8_t start, j, d;
  236. int8_t i;
  237. uint8_t data[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  238. uint8_t *frame = (uint8_t *)malloc(65);
  239. frame[64] = 2;
  240. serialWriteString(getString(36));
  241. start = readNumber(10);
  242. serialWriteString(getString(37));
  243. for (i = 0; i < 8; i++) {
  244. writeNumber(i, 10);
  245. serialWriteString(": ");
  246. data[i] = readNumber(16);
  247. }
  248. serialWriteString(getString(38));
  249. for (i = 0; i < 8; i++) {
  250. d = 0;
  251. for (j = 0; j < 64; j++) {
  252. if ((j < (8 * (i + 1))) && (j >= (8 * i))) {
  253. frame[j] = data[d++];
  254. } else {
  255. frame[j] = 0;
  256. }
  257. }
  258. setFrame(i + start, frame);
  259. }
  260. serialWriteString(getString(33)); // Done
  261. free(frame);
  262. }