animation.ino 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Animation Rendering Logics */
  2. int frameCount = 0;
  3. char previousAnicode = 'a';
  4. //delay with early breakout if current animation code changed
  5. void delayWithEarlyBreakout(int delayDuration) {
  6. delayDuration = delayDuration / 100;
  7. for (int i = 0; i < delayDuration; i++) {
  8. delay(100);
  9. if (getAnimationCode() != previousAnicode) {
  10. break;
  11. }
  12. }
  13. }
  14. //Handle animation rendering
  15. void handleAnimationRendering(char anicode) {
  16. if (!SD_exists){
  17. //SD card not exists. Use what is inside the buffer.
  18. Serial.println("SD card not exists. Rendering internal frame buffer");
  19. renderFrame();
  20. delay(1000);
  21. return;
  22. }
  23. if (previousAnicode != anicode) {
  24. previousAnicode = anicode;
  25. frameCount = 0;
  26. }
  27. //Check if the target animation frame is static
  28. String targetFrame = "/" + String(anicode) + ".bin";
  29. if (SD.exists(targetFrame)) {
  30. //This is a static frame. Load and render it
  31. loadFrameAndRender(targetFrame);
  32. int delayDuration = getFrameDuration(anicode, 0);
  33. delayWithEarlyBreakout(delayDuration);
  34. return;
  35. }
  36. //It is not a static frame. Check if x0.bin exists
  37. targetFrame = "/" + String(anicode) + String(frameCount) + ".bin";
  38. if (SD.exists(targetFrame)) {
  39. loadFrameAndRender(targetFrame);
  40. frameCount++;
  41. int delayDuration = getFrameDuration(anicode, frameCount);
  42. delayWithEarlyBreakout(delayDuration);
  43. return;
  44. } else {
  45. //Not found.
  46. if (frameCount != 0) {
  47. loadFrameAndRender("/" + String(anicode) + "0.bin");
  48. int delayDuration = getFrameDuration(anicode, 0);
  49. delayWithEarlyBreakout(delayDuration);
  50. frameCount = 1;
  51. return;
  52. } else {
  53. //frameCount = 0. x0.bin not exists. Report error
  54. Serial.println("Target frame buffer not found: " + String(anicode) + ".bin");
  55. setAnimationCode('a');
  56. return;
  57. }
  58. }
  59. }
  60. //Get how long the frame shd last.
  61. //Default 500 unless specially programmed
  62. int getFrameDuration(char anicode, int framecount) {
  63. if ((anicode == 'a' || anicode == 'b' || anicode == 'g')) {
  64. //Blinking
  65. if (framecount == 0) {
  66. return 3000;
  67. }
  68. return 300;
  69. }
  70. return 500;
  71. }