LEDBase.cs 7.3 KB

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