Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

transmit.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * transmit.c
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <avr/io.h>
  24. #include <util/delay.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <avr/wdt.h>
  28. #include "memLayer.h"
  29. #include "serial.h"
  30. #include "time.h"
  31. #include "strings.h"
  32. #include "audio.h"
  33. #define OK 0x42
  34. #define ERROR 0x23
  35. #define TRANSTIMEOUT 10000
  36. // These are global variables from main.c
  37. extern char buffer[11];
  38. extern uint8_t refreshAnimationCount;
  39. void recieveAnimations(void) {
  40. uint8_t animCount, a, frameCount, f, i, c;
  41. uint16_t completeCount = 0;
  42. uint8_t *frame = (uint8_t *)malloc(65);
  43. uint64_t timestamp = getSystemTime();
  44. serialWrite(OK); // We are ready...
  45. while (!serialHasChar()) { // Wait for answer
  46. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  47. wdt_reset();
  48. }
  49. }
  50. c = serialGet();
  51. animCount = c; // Got animation count
  52. serialWrite(OK);
  53. for (a = 0; a < animCount; a++) {
  54. while (!serialHasChar()) { // Wait for answer
  55. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  56. wdt_reset();
  57. }
  58. }
  59. c = serialGet();
  60. frameCount = c; // Got frame count
  61. serialWrite(OK);
  62. for (f = 0; f < frameCount; f++) {
  63. while (!serialHasChar()) { // Wait for answer
  64. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  65. wdt_reset();
  66. }
  67. }
  68. c = serialGet();
  69. frame[64] = c; // Got duration
  70. serialWrite(OK);
  71. for (i = 0; i < 64; i++) {
  72. while (!serialHasChar()) { // Wait for answer
  73. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  74. wdt_reset();
  75. }
  76. }
  77. c = serialGet();
  78. frame[i] = c; // Got data byte
  79. }
  80. serialWrite(OK);
  81. setFrame(completeCount++, frame);
  82. }
  83. }
  84. free(frame);
  85. while (!serialHasChar()) { // Wait for answer
  86. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  87. wdt_reset();
  88. }
  89. }
  90. c = serialGet();
  91. while (!serialHasChar()) { // Wait for answer
  92. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  93. wdt_reset();
  94. }
  95. }
  96. c = serialGet();
  97. while (!serialHasChar()) { // Wait for answer
  98. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  99. wdt_reset();
  100. }
  101. }
  102. c = serialGet();
  103. while (!serialHasChar()) { // Wait for answer
  104. if ((getSystemTime() - timestamp) <= TRANSTIMEOUT) {
  105. wdt_reset();
  106. }
  107. }
  108. c = serialGet();
  109. serialWrite(OK);
  110. setAnimationCount(completeCount);
  111. refreshAnimationCount = 1;
  112. }
  113. void transmitAnimations(void) {
  114. // We store no animation information in here
  115. // So we have to place all frames in one or more
  116. // animations... We need 8 animations max...
  117. uint8_t animationsToGo;
  118. uint16_t framesToGo = getAnimationCount();
  119. uint16_t character;
  120. uint8_t a;
  121. uint8_t f, fMax, i;
  122. uint8_t *frame;
  123. if ((framesToGo % 255) == 0) {
  124. animationsToGo = framesToGo / 255;
  125. } else {
  126. animationsToGo = (framesToGo / 255) + 1;
  127. }
  128. serialWrite(OK);
  129. serialWrite(animationsToGo);
  130. while ((character = serialGet()) & 0xFF00); // Wait for answer
  131. if ((character & 0x00FF) != OK) { // Error code recieved
  132. return;
  133. }
  134. for (a = 0; a < animationsToGo; a++) {
  135. if (framesToGo > 255) {
  136. fMax = 255;
  137. } else {
  138. fMax = framesToGo;
  139. }
  140. serialWrite(fMax); // Number of Frames in current animation
  141. while ((character = serialGet()) & 0xFF00); // Wait for answer
  142. if ((character & 0x00FF) != OK) { // Error code recieved
  143. return;
  144. }
  145. for (f = 0; f < fMax; f++) {
  146. frame = getFrame(f + (255 * a));
  147. serialWrite(frame[64]); // frame duration
  148. while ((character = serialGet()) & 0xFF00); // Wait for answer
  149. if ((character & 0x00FF) != OK) { // Error code recieved
  150. free(frame);
  151. return;
  152. }
  153. for (i = 0; i < 64; i++) {
  154. serialWrite(frame[i]);
  155. }
  156. while ((character = serialGet()) & 0xFF00); // Wait for answer
  157. if ((character & 0x00FF) != OK) { // Error code recieved
  158. free(frame);
  159. return;
  160. }
  161. free(frame);
  162. }
  163. framesToGo -= fMax;
  164. }
  165. serialWrite(OK);
  166. serialWrite(OK);
  167. serialWrite(OK);
  168. serialWrite(OK);
  169. while ((character = serialGet()) & 0xFF00); // Wait for answer
  170. // Error code ignored...
  171. }
  172. void sendAudioData(void) {
  173. uint8_t i;
  174. uint8_t *audioData = getAudioData();
  175. if (audioData == NULL) {
  176. serialWriteString(getString(21));
  177. } else {
  178. serialWriteString(getString(22));
  179. for (i = 0; i < 7; i++) {
  180. serialWrite(i + '0');
  181. serialWriteString(": ");
  182. itoa(audioData[i], buffer, 10);
  183. serialWriteString(buffer);
  184. serialWrite('\n');
  185. }
  186. }
  187. }