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.

animations.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * animations.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 <stdint.h>
  24. #include <stdlib.h>
  25. #include <avr/io.h>
  26. #include <avr/wdt.h>
  27. #include <buffhelp.h>
  28. #include <cube.h>
  29. /*
  30. HOW TO Write a new Animation
  31. A) Think of a name, create a function Prototype right below this comment.
  32. B) Increment NUMOFANIMATION by the number of animations you are going to add.
  33. C) Put a function pointer to your new animation into the animations[] Array.
  34. D) Implement your animation!
  35. --> Get a buffer with buffNew(); Display it with setImage(); Free Buffer after usage!
  36. */
  37. // Prototypes for all animations
  38. void upWave(uint8_t i);
  39. void downWave(uint8_t i);
  40. void xWave1(uint8_t i);
  41. void xWave2(uint8_t i);
  42. void zWave1(uint8_t i);
  43. void zWave2(uint8_t i);
  44. // Array of animation functions
  45. #define NUMOFANIMATIONS 0
  46. void (*animations[NUMOFANIMATIONS])(void) = { };
  47. #define WAVELENGTH 2
  48. uint8_t numOfAnimations(void) {
  49. return NUMOFANIMATIONS + 24;
  50. }
  51. void executeAnimation(uint8_t id) {
  52. if (id < (6*4)) {
  53. if (id < 4) {
  54. upWave(id);
  55. } else if (id < 8) {
  56. downWave(id - 4);
  57. } else if (id < 12) {
  58. xWave1(id - 8);
  59. } else if (id < 16) {
  60. xWave2(id - 12);
  61. } else if (id < 20) {
  62. zWave1(id - 16);
  63. } else {
  64. zWave2(id - 20);
  65. }
  66. } else if ((id - (6*4)) < NUMOFANIMATIONS) {
  67. animations[id - (6*4)](); // Call animation
  68. }
  69. }
  70. void upWave(uint8_t i) {
  71. uint8_t *buff;
  72. int8_t x, y, z;
  73. buff = buffNew();
  74. // Up-wave
  75. for(y = (i * 2); y < ((i * 2) + 2); y++) {
  76. for(x = 0; x < 8; x++) {
  77. for(z = 0; z < 8; z++) {
  78. buffSetPixel(buff, x, y, z);
  79. }
  80. }
  81. setImage(buff);
  82. while(isFinished() < WAVELENGTH) {
  83. wdt_reset();
  84. }
  85. buffClearAllPixels(buff);
  86. }
  87. free(buff);
  88. }
  89. void downWave(uint8_t i) {
  90. uint8_t *buff;
  91. int8_t x, y, z;
  92. buff = buffNew();
  93. // Down-wave (Frames 9-15 of showoff.cube)
  94. for(y = (7 - (2 * i)); y >= ((7 - (2 * i)) - 1); y--) {
  95. for(x = 0; x < 8; x++) {
  96. for(z = 0; z < 8; z++) {
  97. buffSetPixel(buff, x, y, z);
  98. }
  99. }
  100. setImage(buff);
  101. while(isFinished() < WAVELENGTH) {
  102. wdt_reset();
  103. }
  104. buffClearAllPixels(buff);
  105. }
  106. free(buff);
  107. }
  108. void xWave1(uint8_t i) {
  109. uint8_t *buff;
  110. int8_t x, y, z;
  111. buff = buffNew();
  112. // x-axis wave
  113. for(x = (i * 2); x < ((i * 2) + 2); x++) {
  114. for(y = 0; y < 8; y++) {
  115. for(z = 0; z < 8; z++) {
  116. buffSetPixel(buff, x, y, z);
  117. }
  118. }
  119. setImage(buff);
  120. while(isFinished() < WAVELENGTH) {
  121. wdt_reset();
  122. }
  123. buffClearAllPixels(buff);
  124. }
  125. free(buff);
  126. }
  127. void xWave2(uint8_t i) {
  128. uint8_t *buff;
  129. int8_t x, y, z;
  130. buff = buffNew();
  131. for(x = (7 - (2 * i)); x >= ((7 - (2 * i)) - 1); x--) {
  132. for(y = 0; y < 8; y++) {
  133. for(z = 0; z < 8; z++) {
  134. buffSetPixel(buff, x, y, z);
  135. }
  136. }
  137. setImage(buff);
  138. while(isFinished() < WAVELENGTH) {
  139. wdt_reset();
  140. }
  141. buffClearAllPixels(buff);
  142. }
  143. free(buff);
  144. }
  145. void zWave1(uint8_t i) {
  146. uint8_t *buff;
  147. int8_t x, y, z;
  148. buff = buffNew();
  149. // z-axis-wave
  150. for(z = (i * 2); z < ((i * 2) + 2); z++) {
  151. for(y = 0; y < 8; y++) {
  152. for(x = 0; x < 8; x++) {
  153. buffSetPixel(buff, x, y, z);
  154. }
  155. }
  156. setImage(buff);
  157. while(isFinished() < WAVELENGTH) {
  158. wdt_reset();
  159. }
  160. buffClearAllPixels(buff);
  161. }
  162. free(buff);
  163. }
  164. void zWave2(uint8_t i) {
  165. uint8_t *buff;
  166. int8_t x, y, z;
  167. buff = buffNew();
  168. for(z = (7 - (2 * i)); z >= ((7 - (2 * i)) - 1); z--) {
  169. for(x = 0; x < 8; x++) {
  170. for(y = 0; y < 8; y++) {
  171. buffSetPixel(buff, x, y, z);
  172. }
  173. }
  174. setImage(buff);
  175. while(isFinished() < WAVELENGTH) {
  176. wdt_reset();
  177. }
  178. buffClearAllPixels(buff);
  179. }
  180. free(buff);
  181. }