Program.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. namespace DreamCheekyUSB {
  6. class Program {
  7. static void Main(string[] args) {
  8. int actions = 0; //Track if any actions are executed
  9. DreamCheekyLED led = null;
  10. try {
  11. //if (args.ContainsInsensitive("debug"))
  12. {
  13. Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  14. }
  15. string devicearg = args.StartsWith("device=").FirstOrDefault();
  16. devicearg = "\\\\?\\hid#vid_1d34&pid_0004#6&1067c3dc&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
  17. if (string.IsNullOrEmpty(devicearg)) {
  18. Trace.WriteLine("\r\nConnecting to DreamCheekyLED using default values...");
  19. led = new DreamCheekyLED();
  20. } else {
  21. Trace.WriteLine("\r\nConnecting to DreamCheekyLED using specified device...");
  22. string[] deviceSplit = devicearg.Substring(7).Split(',');
  23. if (deviceSplit.Length == 1) {
  24. led = new DreamCheekyLED(deviceSplit[0]); //One argument = device path
  25. } else {
  26. //Two or Three arguments = VID,PID,Count=0
  27. int devicecount = 0;
  28. if (deviceSplit.Length > 2) {
  29. devicecount = int.Parse(deviceSplit[2]);
  30. }
  31. int VID = int.Parse(deviceSplit[0].Substring(2), System.Globalization.NumberStyles.HexNumber);
  32. int PID = int.Parse(deviceSplit[1].Substring(2), System.Globalization.NumberStyles.HexNumber);
  33. led = new DreamCheekyLED(VID, PID, devicecount);
  34. }
  35. }
  36. if (args.ContainsInsensitive("test")) {
  37. actions++;
  38. Trace.WriteLine("Testing USBLED...");
  39. bool result = led.Test();
  40. Trace.WriteLine("Testing USBLED Result: " + result);
  41. System.Threading.Thread.Sleep(1000);
  42. }
  43. if (args.ContainsInsensitive("testblink")) {
  44. actions++;
  45. Trace.WriteLine("Blinking test...");
  46. bool result = led.TestBlink();
  47. Trace.WriteLine("Blinking test result: " + result);
  48. }
  49. string rgbarg = args.StartsWith("rgb=").FirstOrDefault();
  50. if (!string.IsNullOrEmpty(rgbarg)) {
  51. actions++;
  52. string rgb = rgbarg.Substring(4);
  53. Trace.WriteLine("Setting RGB Color: {0}", rgb);
  54. bool result = led.SetColor(rgb);
  55. Trace.WriteLine("Result: " + result);
  56. }
  57. string colorarg = args.StartsWith("color=").FirstOrDefault();
  58. string fadearg = args.StartsWith("fade=").FirstOrDefault();
  59. string blinkarg = args.StartsWith("blink=").FirstOrDefault();
  60. System.Drawing.Color color = System.Drawing.Color.Empty;
  61. if (!string.IsNullOrEmpty(colorarg)) {
  62. string strcolor = colorarg.Substring(6);
  63. Trace.WriteLine("Setting System Color: {0}", strcolor);
  64. if (led.TryParseNametoColor(strcolor, out color)) {
  65. if (string.IsNullOrEmpty(fadearg) && string.IsNullOrEmpty(blinkarg)) { //Only set color if fade and blink options are not set
  66. actions++;
  67. bool result = led.SetColor(color);
  68. Trace.WriteLine("Result: " + result);
  69. }
  70. } else {
  71. Trace.WriteLine("Error: Unknown color '{0}'. See http://www.flounder.com/csharp_color_table.htm.", strcolor);
  72. }
  73. }
  74. if (!string.IsNullOrEmpty(fadearg)) {
  75. if (string.IsNullOrEmpty(colorarg)) {
  76. Trace.WriteLine("Error... Must set color when using fade options\r\n");
  77. return;
  78. } else {
  79. actions++;
  80. string[] fadesplit = fadearg.Substring(5).Split(',');
  81. int totalms = int.Parse(fadesplit[0]);
  82. int inout = 0;
  83. if (fadesplit.ContainsInsensitive("in")) {
  84. inout = 1;
  85. } else if (fadesplit.ContainsInsensitive("out")) {
  86. inout = 2;
  87. }
  88. switch (inout) {
  89. case 1:
  90. Trace.WriteLine("Fading color in. TotalMS=" + totalms);
  91. led.FadeIn(color, totalms);
  92. break;
  93. case 2:
  94. Trace.WriteLine("Fading color out. TotalMS=" + totalms);
  95. led.FadeOut(color, totalms);
  96. break;
  97. default:
  98. Trace.WriteLine("Fading color in and then out. TotalMS=" + totalms);
  99. led.FadeInOut(color, totalms);
  100. break;
  101. }
  102. }
  103. }
  104. if (!string.IsNullOrEmpty(blinkarg)) {
  105. if (string.IsNullOrEmpty(colorarg)) {
  106. Trace.WriteLine("Error... Must set color when using blink option\r\n");
  107. return;
  108. } else {
  109. actions++;
  110. string[] blinksplit = blinkarg.Substring(6).Split(',');
  111. int count = int.Parse(blinksplit[0]);
  112. int blinkms = 500;
  113. if (blinksplit.Length > 1) {
  114. blinkms = int.Parse(blinksplit[1]);
  115. }
  116. Trace.WriteLine(String.Format("Blinking color. Count={0} Blinkms={1}", count, blinkms));
  117. led.Blink(color, count, blinkms);
  118. }
  119. }
  120. string delayarg = args.StartsWith("delay=").FirstOrDefault();
  121. if (!string.IsNullOrEmpty(delayarg)) {
  122. actions++;
  123. int delay = int.Parse(delayarg.Substring(6));
  124. Trace.WriteLine("Delay for {0}ms", Convert.ToString(delay));
  125. System.Threading.Thread.Sleep(delay);
  126. }
  127. if (args.ContainsInsensitive("off")) {
  128. actions++;
  129. Trace.WriteLine("Turning led off");
  130. led.Off();
  131. }
  132. } catch (Exception ex) {
  133. Trace.WriteLine("\r\n\r\nError: " + ex.Message + "\r\n\r\n");
  134. } finally {
  135. if (led != null) {
  136. led.Dispose();
  137. }
  138. }
  139. //Pause on exit or display usage syntax
  140. if (actions > 0) {
  141. Trace.WriteLine("Finished\r\n");
  142. } else { //No actions specified, show help
  143. Trace.WriteLine("\r\nUsage:");
  144. Trace.WriteLine(" DreamCheekyLED.exe [device=...] [options] [rgb=xxx,xxx,xxx] [color=....]");
  145. Trace.WriteLine(" [fade=...] [blink=...] [delay=xxxx] [off]");
  146. Trace.WriteLine("\r\nExamples:");
  147. Trace.WriteLine(" DreamCheekyLED.exe debug nopause color=red");
  148. Trace.WriteLine(" DreamCheekyLED.exe debug nopause color=green fade=3000");
  149. Trace.WriteLine(" DreamCheekyLED.exe debug nopause color=green fade=\"3000,in\"");
  150. Trace.WriteLine(" DreamCheekyLED.exe debug nopause color=blue blink=2");
  151. Trace.WriteLine(" DreamCheekyLED.exe debug nopause color=blue blink=\"5,250\"");
  152. Trace.WriteLine(" DreamCheekyLED.exe debug nopause rgb=\"255,255,0\" delay=5000 off");
  153. Trace.WriteLine(" DreamCheekyLED.exe debug nopause color=yellow fade=\"3000,in\" delay=5000 off");
  154. Trace.WriteLine("\r\n\r\nDevice Path:");
  155. Trace.WriteLine(" Optional, Defaults to first USB device with VID=0x1D34 and PID=0x0004");
  156. Trace.WriteLine(" Example (VID,PID,Index): device=\"0x1D34,0x0004,0\"");
  157. Trace.WriteLine(" Example (Path): device=" + @"""\\?\hid#vid_1d34&pid_0004#6&1067c3dc&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}""");
  158. Trace.WriteLine("\r\nOptions:");
  159. Trace.WriteLine(" debug = Print trace statements to Console.Out");
  160. Trace.WriteLine(" test = Cycle Red/Green/Blue then fade Red/Green/Blue");
  161. Trace.WriteLine(" testblink = test a few blink cycles");
  162. Trace.WriteLine(" nopause = omit the 'Press enter to exit...' message at the end");
  163. Trace.WriteLine("\r\nColors: (See http://www.flounder.com/csharp_color_table.htm )");
  164. Trace.WriteLine(" Use rgb=xxx,xxx,xxx or one of the .NET System Colors");
  165. Trace.WriteLine(" Example (yellow): rgb=\"255,255,0\"");
  166. Trace.WriteLine(" Example (System.Drawing.Color.Pink): color=DeepPink");
  167. Trace.WriteLine("\r\n");
  168. Trace.WriteLine("\r\nFade: set to fade in, out, or both");
  169. Trace.WriteLine(" Makes the colors fade in or out instead of instantly on or off.");
  170. Trace.WriteLine(" Example (Fade in and out in 2 seconds): color=Indigo fade=2000");
  171. Trace.WriteLine(" Example (Fade in 1 second): color=Indigo fade=\"1000,in\"");
  172. Trace.WriteLine(" Example (Fade out 3 seconds): color=Indigo fade=\"3000,out\"");
  173. Trace.WriteLine("\r\nBlink: will blink specified color");
  174. Trace.WriteLine(" Example (Blink twice at default 500ms): color=LimeGreen blink=5");
  175. Trace.WriteLine(" Example (Blink 5 times at 250ms each): color=LimeGreen blink=\"5,250\"");
  176. Trace.WriteLine("\r\nDelay: Add a delay in milliseconds.");
  177. Trace.WriteLine("\r\n");
  178. }
  179. if (!args.ContainsInsensitive("nopause")) {
  180. Trace.WriteLine("\r\nPress enter to exit...");
  181. Console.ReadLine();
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// Extenstions for working with string arrays
  187. /// </summary>
  188. public static class StringArrayExtenstions {
  189. public static bool ContainsInsensitive(this string[] args, string name) {
  190. return args.Contains(name, StringComparer.CurrentCultureIgnoreCase);
  191. }
  192. public static IEnumerable<string> StartsWith(this string[] args, string value, StringComparison options = StringComparison.CurrentCultureIgnoreCase) {
  193. var q = from a in args
  194. where a.StartsWith(value, options)
  195. select a;
  196. return q;
  197. }
  198. }
  199. }