LEDBase.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Diagnostics;
  3. using HidSharp;
  4. namespace DreamCheekyUSB {
  5. public abstract class LEDBase : IDisposable {
  6. /// <summary>
  7. /// HidLibray.HidDevice for this USBLED instance
  8. /// </summary>
  9. public HidDevice HidLED;
  10. internal HidStream Stream;
  11. internal HidDeviceLoader Loader;
  12. internal bool initialized = false;
  13. internal virtual bool Initialize() { //Should be extended by base classes
  14. if (initialized) {
  15. return true;
  16. }
  17. if (HidLED == null) {
  18. throw new NullReferenceException("hidLED not initialized");
  19. }
  20. Stream = HidLED.Open();
  21. initialized = true;
  22. return true;
  23. }
  24. public void Dispose() {
  25. Trace.WriteLine("Releasing control of USB device...");
  26. if (Stream != null) {
  27. Stream.Close();
  28. Stream.Dispose();
  29. Stream = null;
  30. }
  31. }
  32. public abstract bool Write(byte[] data);
  33. public abstract bool SetColor(System.Drawing.Color color);
  34. public abstract bool SetColor(byte red, byte green, byte blue);
  35. /// <summary>
  36. /// Accepts rgb value as a string in xxx,xxx,xxx format. Will convert to byte values.
  37. /// </summary>
  38. /// <param name="rgb">Example: 255,255,0 for Yellow</param>
  39. /// <returns>True if success, False otherwise.</returns>
  40. public bool SetColor(string rgb) {
  41. try {
  42. var rxRGB = System.Text.RegularExpressions.Regex.Match(rgb, "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$");
  43. if (rxRGB.Success) {
  44. byte red = byte.Parse(rxRGB.Groups[1].Value);
  45. byte green = byte.Parse(rxRGB.Groups[2].Value);
  46. byte blue = byte.Parse(rxRGB.Groups[3].Value);
  47. return SetColor(red, green, blue);
  48. }
  49. System.Diagnostics.Trace.WriteLine("Invalid RGB string: " + rgb);
  50. return false;
  51. } catch (Exception ex) {
  52. System.Diagnostics.Trace.WriteLine("Error in SetColor: " + ex.Message);
  53. return false;
  54. }
  55. }
  56. public bool Off() {
  57. return SetColor(0, 0, 0);
  58. }
  59. /// <summary>
  60. /// Cycle Red, Green, Blue, then fade Red, Green Blue
  61. /// </summary>
  62. /// <returns></returns>
  63. public virtual bool Test() {
  64. bool bReturn = true;
  65. bReturn &= SetColor(255, 0, 0);
  66. System.Threading.Thread.Sleep(250);
  67. bReturn &= SetColor(0, 255, 0);
  68. System.Threading.Thread.Sleep(250);
  69. bReturn &= SetColor(0, 0, 255);
  70. System.Threading.Thread.Sleep(250);
  71. bReturn &= Off();
  72. System.Threading.Thread.Sleep(250);
  73. bReturn &= FadeInOut(System.Drawing.Color.Red, 1000);
  74. System.Threading.Thread.Sleep(100);
  75. bReturn &= FadeInOut(System.Drawing.Color.Green, 1000);
  76. System.Threading.Thread.Sleep(100);
  77. bReturn &= FadeInOut(System.Drawing.Color.Blue, 1000);
  78. return bReturn;
  79. }
  80. public virtual bool TestBlink() {
  81. bool bReturn = true;
  82. bReturn &= Blink(System.Drawing.Color.Red);
  83. bReturn &= Blink(System.Drawing.Color.Orange);
  84. bReturn &= Blink(System.Drawing.Color.Yellow);
  85. bReturn &= Blink(System.Drawing.Color.Green);
  86. bReturn &= Blink(System.Drawing.Color.Blue);
  87. bReturn &= Blink(System.Drawing.Color.Indigo);
  88. bReturn &= Blink(System.Drawing.Color.Violet);
  89. return Off() && bReturn;
  90. }
  91. public bool FadeInOut(System.Drawing.Color toColor, int totalMs = 2000) {
  92. if (totalMs <= 0) {
  93. throw new ArgumentOutOfRangeException("totalMs", "must be greater than zero");
  94. }
  95. FadeIn(toColor, totalMs / 2);
  96. return FadeOut(toColor, totalMs / 2);
  97. }
  98. public void FadeIn(System.Drawing.Color toColor, int totalMs = 1000) {
  99. if (totalMs <= 0) {
  100. throw new ArgumentOutOfRangeException("totalMs", "must be greater than zero");
  101. }
  102. int t = 0;
  103. const int step = 35;
  104. float ratio;
  105. while (t < totalMs) {
  106. System.Threading.Thread.Sleep(step);
  107. ratio = (float)t / (float)totalMs;
  108. byte red = (byte)(toColor.R * ratio);
  109. byte green = (byte)(toColor.G * ratio);
  110. byte blue = (byte)(toColor.B * ratio);
  111. SetColor(red, green, blue);
  112. t += step;
  113. }
  114. }
  115. public bool FadeOut(System.Drawing.Color fromColor, int totalMs = 1000) {
  116. if (totalMs <= 0) {
  117. throw new ArgumentOutOfRangeException("totalMs", "must be greater than zero");
  118. }
  119. var t = 0;
  120. const int step = 35;
  121. float ratio;
  122. while (t < totalMs) {
  123. System.Threading.Thread.Sleep(step);
  124. ratio = (float)t / (float)totalMs;
  125. byte red = (byte)(fromColor.R - (fromColor.R * ratio));
  126. byte green = (byte)(fromColor.G - (fromColor.G * ratio));
  127. byte blue = (byte)(fromColor.B - (fromColor.B * ratio));
  128. SetColor(red, green, blue);
  129. t += step;
  130. }
  131. return Off();
  132. }
  133. public bool Blink(System.Drawing.Color color, int count = 1, int blinkMs = 500) {
  134. if (count <= 0) {
  135. throw new ArgumentOutOfRangeException("count", "Count cannot be less than zero");
  136. }
  137. if (blinkMs <= 0) {
  138. throw new ArgumentOutOfRangeException("blinkMs", "BlinkMs cannot be less than zero");
  139. }
  140. int i = 0;
  141. bool bReturn = true;
  142. while (i < count) {
  143. bReturn &= FadeInOut(color, blinkMs);
  144. i++;
  145. }
  146. return bReturn;
  147. }
  148. public bool TryParseNametoColor(string name, out System.Drawing.Color result) {
  149. result = System.Drawing.Color.FromName(name);
  150. if (result.IsKnownColor || result.IsSystemColor) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. }
  156. }