ActuBoardApp.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using ActuBoardAPI;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace ActuBoardConsole
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class ActuBoardApp : Window
  23. {
  24. private ActuBoardInterface _abi;
  25. private int _comport = 0;
  26. private bool _outputEnabled = true;
  27. public ActuBoardApp()
  28. {
  29. InitializeComponent();
  30. foreach (String port in SerialPort.GetPortNames())
  31. {
  32. drop_com.Items.Add(port);
  33. }
  34. _abi = new ActuBoardInterface();
  35. _abi.OnConnectionChanged += _abi_OnConnectionChanged;
  36. _abi.OnMessageReceived += _abi_OnMessageReceived;
  37. _abi.OnStatusUpdate += _abi_OnStatusUpdate;
  38. drop_com.SelectionChanged += Drop_com_SelectionChanged;
  39. if (drop_com.Items.Count > 0)
  40. drop_com.SelectedIndex = 0;
  41. btn_connect.Click += Btn_connect_Click;
  42. btn_list.Click += Btn_list_Click;
  43. btn_send.Click += Btn_send_Click;
  44. btn_read.Click += Btn_read_Click;
  45. btn_debug.Click += Btn_debug_Click;
  46. btn_output.Click += Btn_output_Click;
  47. btn_send_value.Click += Btn_send_value_Click;
  48. btn_group_m.Click += Btn_group_m_Click;
  49. btn_group_n.Click += Btn_group_n_Click;
  50. btn_test.Click += Btn_test_Click;
  51. }
  52. private void Btn_send_value_Click(object sender, RoutedEventArgs e)
  53. {
  54. if (_abi.IsConnected() && txt_channel.Text != "")
  55. {
  56. int channel = 0;
  57. Int32.TryParse(txt_channel.Text, out channel);
  58. int value = 0;
  59. Int32.TryParse(txt_value.Text, out value);
  60. if (value > 255 || value < 0)
  61. value = 0;
  62. _abi.SetChannel(channel, value);
  63. }
  64. }
  65. private void Btn_group_n_Click(object sender, RoutedEventArgs e)
  66. {
  67. if (_abi.IsConnected())
  68. {
  69. int[] channels = { 30, 31, 32 };
  70. int[] pwm = { 128, 15, 255 };
  71. _abi.SetChannels(channels, pwm);
  72. }
  73. }
  74. private void Btn_group_m_Click(object sender, RoutedEventArgs e)
  75. {
  76. if (_abi.IsConnected())
  77. {
  78. int[] channels = { 30, 31, 32 };
  79. _abi.SetChannels(channels, 128);
  80. }
  81. }
  82. private void _abi_OnStatusUpdate(StatusCode e)
  83. {
  84. Dispatcher.BeginInvoke(new Action(delegate ()
  85. {
  86. if (e > 0)
  87. Console.Beep();
  88. // error released
  89. if (e == StatusCode.NoError)
  90. {
  91. el_error.Fill = new SolidColorBrush(Colors.LimeGreen);
  92. }
  93. // hardware controller could not interpret command
  94. else if (e == StatusCode.HardwareCommandError)
  95. {
  96. el_error.Fill = new SolidColorBrush(Colors.Red);
  97. }
  98. // selected channel(s) are invalid or out of range
  99. else if (e == StatusCode.ChannelInvalid)
  100. {
  101. el_error.Fill = new SolidColorBrush(Colors.BlueViolet);
  102. }
  103. // pwm value(s) are out of range (0-255)
  104. else if (e == StatusCode.PwmValueInvalid)
  105. {
  106. el_error.Fill = new SolidColorBrush(Colors.Coral);
  107. }
  108. // hardware is not connected
  109. else if (e == StatusCode.ActuBoardNotConnected)
  110. {
  111. el_error.Fill = new SolidColorBrush(Colors.DarkRed);
  112. }
  113. // unknown error ID
  114. else
  115. {
  116. el_error.Fill = new SolidColorBrush(Colors.Red);
  117. }
  118. }));
  119. }
  120. private void Btn_test_Click(object sender, RoutedEventArgs e)
  121. {
  122. if (_abi.IsConnected())
  123. {
  124. _abi.SetChannel("30", "240");
  125. }
  126. }
  127. private void Btn_output_Click(object sender, RoutedEventArgs e)
  128. {
  129. if (_abi.IsConnected())
  130. {
  131. _abi.EnableOutput(!_outputEnabled);
  132. _outputEnabled = !_outputEnabled;
  133. if (_outputEnabled)
  134. {
  135. SetStatusColor(1);
  136. btn_output.Content = "disable output";
  137. }
  138. else
  139. {
  140. SetStatusColor(3);
  141. btn_output.Content = "enable output";
  142. }
  143. }
  144. }
  145. private void Btn_debug_Click(object sender, RoutedEventArgs e)
  146. {
  147. if (_abi.IsConnected())
  148. _abi.ToggleDebug();
  149. }
  150. private void Btn_read_Click(object sender, RoutedEventArgs e)
  151. {
  152. if (_abi.IsConnected() && txt_channel.Text != "")
  153. {
  154. int channel = 0;
  155. Int32.TryParse(txt_channel.Text, out channel);
  156. _abi.GetChannel(channel);
  157. }
  158. }
  159. private void Btn_send_Click(object sender, RoutedEventArgs e)
  160. {
  161. if (_abi.IsConnected() && txt_command.Text != "")
  162. {
  163. _abi.SendCustomCommand(txt_command.Text);
  164. }
  165. }
  166. private void Btn_list_Click(object sender, RoutedEventArgs e)
  167. {
  168. if (_abi.IsConnected())
  169. _abi.ListDevices();
  170. }
  171. private void _abi_OnMessageReceived(string e)
  172. {
  173. Console.WriteLine("MSG: " + e);
  174. Dispatcher.BeginInvoke(new Action(delegate ()
  175. {
  176. txt_console.Text += e;
  177. txt_console.ScrollToEnd();
  178. }));
  179. }
  180. private void _abi_OnConnectionChanged(bool e)
  181. {
  182. if (e)
  183. {
  184. SetStatusColor(1);
  185. btn_connect.Content = "Disconnect";
  186. }
  187. else
  188. {
  189. SetStatusColor(2);
  190. btn_connect.Content = "Connect";
  191. }
  192. }
  193. private void SetStatusColor(int status)
  194. {
  195. // unknown status
  196. if (status == 0)
  197. el_status.Fill = new SolidColorBrush(Colors.Black);
  198. // connected
  199. else if (status == 1)
  200. el_status.Fill = new SolidColorBrush(Colors.LimeGreen);
  201. // disconnected
  202. else if (status == 2)
  203. el_status.Fill = new SolidColorBrush(Colors.Red);
  204. // output disabled
  205. else if (status == 3)
  206. el_status.Fill = new SolidColorBrush(Colors.GreenYellow);
  207. }
  208. private void Drop_com_SelectionChanged(object sender, SelectionChangedEventArgs e)
  209. {
  210. _comport = ComToInt(drop_com.SelectedItem.ToString());
  211. _abi.SetComPort(_comport);
  212. }
  213. private int ComToInt(String scom)
  214. {
  215. Console.WriteLine("COMPORT: " + scom);
  216. if (scom.StartsWith("COM"))
  217. return Int32.Parse(scom.Substring(3));
  218. else
  219. return 0;
  220. }
  221. private void Btn_connect_Click(object sender, RoutedEventArgs e)
  222. {
  223. Console.WriteLine("Connecting to " + _comport);
  224. if (_comport > 0 && !_abi.IsConnected())
  225. _abi.Connect();
  226. else if (_abi.IsConnected())
  227. _abi.Disconnect();
  228. }
  229. private void txt_command_KeyDown(object sender, KeyEventArgs e)
  230. {
  231. if (e.Key == Key.Enter)
  232. {
  233. Console.WriteLine("sender " + sender.ToString());
  234. btn_send.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
  235. }
  236. }
  237. }
  238. }