PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/NetduinoLEDControl/Classes/Animation.cs

#
C# | 87 lines | 42 code | 12 blank | 33 comment | 8 complexity | 1eb2607917f889734e8c0d859cb19359 MD5 | raw file
  1. //=======================================================================================
  2. //
  3. // Purpose: Povide a an animation to dipslay in the LED cube.
  4. //
  5. // Copyright (C) 2011 Mark Stevens
  6. //
  7. // This software is destributed under the MS-PL licence agreement a copy of which can
  8. // be found on the codeplex page http://netduinoledcontrol.codeplex.com and in the
  9. // Licence.txt file distributed with this project.
  10. //
  11. //=======================================================================================
  12. using System.Threading;
  13. namespace Coding4Fun.NetduinoLEDControl
  14. {
  15. /// <summary>
  16. /// Hold the frame information for an animation to be displayed in the LED cube.
  17. /// </summary>
  18. class Animation
  19. {
  20. #region Properties
  21. /// <summary>
  22. /// Frames holding the animation sequence.
  23. /// </summary>
  24. public byte[][] Frames { get; set; }
  25. /// <summary>
  26. /// Delay (in milliseconds) between the frames.
  27. /// </summary>
  28. public int FrameDelay { get; set; }
  29. /// <summary>
  30. /// Number of times to repeat the sequence.
  31. /// </summary>
  32. public int Repeat { get; set; }
  33. /// <summary>
  34. /// Should the cube be cleared before the animation is played?
  35. /// </summary>
  36. public bool ClearCubeBeforePlaying { get; set; }
  37. #endregion
  38. #region Constuctor(s)
  39. /// <summary>
  40. /// Default constructor for an instance of the animation class.
  41. /// </summary>
  42. public Animation()
  43. {
  44. Frames = null;
  45. FrameDelay = 50;
  46. Repeat = 1;
  47. ClearCubeBeforePlaying = true;
  48. }
  49. #endregion
  50. #region Methods
  51. /// <summary>
  52. /// Play this animation in the specified instance of the LED cube.
  53. /// </summary>
  54. /// <param name="cube">Instance of the LED cube to show the animation in.</param>
  55. public void Play(LEDCube cube)
  56. {
  57. if ((Frames != null) && (Frames.Length > 0) && (Repeat > 0) && (FrameDelay > 0))
  58. {
  59. if (ClearCubeBeforePlaying)
  60. {
  61. cube.ClearCube();
  62. }
  63. for (int counter = 0; counter < Repeat; counter++)
  64. {
  65. for (int currentFrame = 0; currentFrame < Frames.Length; currentFrame++)
  66. {
  67. cube.UpdateBuffer(Frames[currentFrame]);
  68. Thread.Sleep(FrameDelay);
  69. }
  70. }
  71. }
  72. }
  73. #endregion
  74. }
  75. }