PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/Modules/SytechDesigns/Button/Software/Button/Button.cs

#
C# | 314 lines | 133 code | 37 blank | 144 comment | 40 complexity | 4fa15c432b2f44af06a03f1a7cef14e6 MD5 | raw file
  1. using System;
  2. using Gadgeteer.Interfaces;
  3. using Microsoft.SPOT;
  4. using GT = Gadgeteer;
  5. using GTM = Gadgeteer.Modules;
  6. using GTI = Gadgeteer.Interfaces;
  7. namespace Gadgeteer.Modules.Sytech
  8. {
  9. /// <summary>
  10. /// A Button Gadgeteer module.
  11. /// </summary>
  12. /// <example>
  13. /// <para>The following example show intialization of a <see cref="Button"/> object and the delegate that handles the
  14. /// <see cref="Button.ButtonPressed"/> event.</para>
  15. /// <code>
  16. /// using System;
  17. /// using Microsoft.SPOT;
  18. /// using Microsoft.SPOT.Presentation;
  19. /// using Microsoft.SPOT.Presentation.Controls;
  20. /// using Microsoft.SPOT.Presentation.Media;
  21. ///
  22. /// using GT = Gadgeteer;
  23. /// using GTM = Gadgeteer.Modules;
  24. ///
  25. /// using Gadgeteer.Modules.Sytech;
  26. ///
  27. /// namespace GadgeteerAppGettingStarted
  28. /// {
  29. /// public partial class Program
  30. /// {
  31. /// // This template uses the NANAO mainboard from Sytech Designs
  32. ///
  33. /// // Define and initialize GTM.Modules here, specifying their socket numbers.
  34. /// GTM.Sytech.UsbDevice usbClient = new UsbDevice(1);
  35. /// GTM.Sytech.Button button = new Button(2);
  36. ///
  37. /// void ProgramStarted()
  38. /// {
  39. /// // Initialize event handlers here.
  40. /// button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
  41. ///
  42. /// // Do one-time tasks here
  43. /// Debug.Print("Program Started");
  44. /// }
  45. ///
  46. /// void button_ButtonPressed(Button sender, Button.ButtonState state)
  47. /// {
  48. /// Debug.Print("Button pressed.");
  49. /// }
  50. /// }
  51. /// }
  52. ///
  53. /// </code>
  54. /// </example>
  55. public class Button : GTM.Module
  56. {
  57. // This example implements a driver in managed code for a simple Gadgeteer module. The module uses a
  58. // single GTI.InterruptInput to interact with a sensor that can be in either of two states: low or high.
  59. // The example code shows the recommended code pattern for exposing the property (IsHigh).
  60. // The example also uses the recommended code pattern for exposing two events: ButtonHigh, ButtonLow.
  61. // The triple-slash "///" comments shown will be used in the build process to create an XML file named
  62. // GTM.Sytech.Button. This file will provide Intellisense and documention for the
  63. // interface and make it easier for developers to use the Button module.
  64. // Note: A constructor summary is auto-generated by the doc builder.
  65. /// <summary></summary>
  66. /// <param name="socketNumber">The socket that this module is plugged in to.</param>
  67. public Button(int socketNumber)
  68. {
  69. // This finds the Socket instance from the user-specified socket number.
  70. // This will generate user-friendly error messages if the socket is invalid.
  71. // If there is more than one socket on this module, then instead of "null" for the last parameter,
  72. // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
  73. Socket socket = Socket.GetSocket(socketNumber, true, this, null);
  74. // validate the socket
  75. socket.EnsureTypeIsSupported(new char[] { 'X', 'Y' }, this);
  76. // This creates an GTI.InterruptInput interface. The interfaces under the GTI namespace provide easy ways to build common modules.
  77. // This also generates user-friendly error messages automatically, e.g. if the user chooses a socket incompatible with an interrupt input.
  78. this.input = new GTI.InterruptInput(socket, GT.Socket.Pin.Three, GTI.GlitchFilterMode.On, GTI.ResistorMode.PullUp, GTI.InterruptMode.RisingAndFallingEdge, this);
  79. // This registers a handler for the interrupt event of the interrupt input (which is below)
  80. this.input.Interrupt += new GTI.InterruptInput.InterruptEventHandler(this._input_Interrupt);
  81. //Create the LED output
  82. led = new DigitalOutput(socket,GT.Socket.Pin.Four,false,this);
  83. // intialise with the LED off
  84. LEDMode = LEDModes.Off;
  85. }
  86. private void _input_Interrupt(GTI.InterruptInput input, bool value)
  87. {
  88. ButtonState buttonState = value ? ButtonState.Released : ButtonState.Pressed;
  89. //Handle if button pressed or released
  90. switch (buttonState)
  91. {
  92. case ButtonState.Released:
  93. if (LEDMode == LEDModes.OnWhilePressed)
  94. TurnLEDOff();
  95. else if (LEDMode == LEDModes.OnWhileReleased)
  96. TurnLEDOn();
  97. else if (LEDMode == LEDModes.ToggleWhenReleased)
  98. ToggleLED();
  99. break;
  100. case ButtonState.Pressed:
  101. if (LEDMode == LEDModes.OnWhilePressed)
  102. TurnLEDOn();
  103. else if (LEDMode == LEDModes.OnWhileReleased)
  104. TurnLEDOff();
  105. else if (LEDMode == LEDModes.ToggleWhenPressed)
  106. ToggleLED();
  107. break;
  108. }
  109. this.OnButtonEvent(this, buttonState);
  110. }
  111. /// <summary>
  112. /// Gets a value that indicates whether the state of this Button is Pressed.
  113. /// </summary>
  114. public bool IsPressed
  115. {
  116. get
  117. {
  118. return !this.input.Read();
  119. }
  120. }
  121. private GTI.InterruptInput input;
  122. private GTI.DigitalOutput led;
  123. #region LED Handler
  124. private LEDModes _ledMode;
  125. /// <summary>
  126. /// Gets or sets the LED's current mode of operation.
  127. /// </summary>
  128. public LEDModes LEDMode
  129. {
  130. get
  131. {
  132. return _ledMode;
  133. }
  134. set
  135. {
  136. _ledMode = value;
  137. if (_ledMode == LEDModes.On || _ledMode == LEDModes.OnWhilePressed && IsPressed || _ledMode == LEDModes.OnWhileReleased && !IsPressed)
  138. TurnLEDOn();
  139. else if (_ledMode == LEDModes.Off || _ledMode == LEDModes.OnWhileReleased && IsPressed || _ledMode == LEDModes.OnWhilePressed && !IsPressed)
  140. TurnLEDOff();
  141. }
  142. }
  143. /// <summary>
  144. /// Turns on the module's LED.
  145. /// </summary>
  146. public void TurnLEDOn()
  147. {
  148. led.Write(true);
  149. }
  150. /// <summary>
  151. /// Turns off the module's LED.
  152. /// </summary>
  153. public void TurnLEDOff()
  154. {
  155. led.Write(false);
  156. }
  157. /// <summary>
  158. /// Toggles the module's LED. If the LED is currently on, it is turned off. If it is currently off, it is turned on.
  159. /// </summary>
  160. public void ToggleLED()
  161. {
  162. if (IsLedOn)
  163. TurnLEDOff();
  164. else
  165. TurnLEDOn();
  166. }
  167. /// <summary>
  168. /// Gets a boolean value that indicates whether the module's LED is currently lit (true = lit, false = off).
  169. /// </summary>
  170. public bool IsLedOn
  171. {
  172. get
  173. {
  174. return led.Read();
  175. }
  176. }
  177. #endregion
  178. /// <summary>
  179. /// Represents the delegate that is used to handle the <see cref="ButtonReleased"/>
  180. /// and <see cref="ButtonPressed"/> events.
  181. /// </summary>
  182. /// <param name="sender">The <see cref="Button"/> object that raised the event.</param>
  183. /// <param name="state">The state of the Button</param>
  184. public delegate void ButtonEventHandler(Button sender, ButtonState state);
  185. /// <summary>
  186. /// Raised when the state of <see cref="Button"/> is high.
  187. /// </summary>
  188. /// <remarks>
  189. /// Implement this event handler and the <see cref="ButtonPressed"/> event handler
  190. /// when you want to provide an action associated with Button activity.
  191. /// The state of the Button is passed to the <see cref="ButtonEventHandler"/> delegate,
  192. /// so you can use the same event handler for both Button states.
  193. /// </remarks>
  194. public event ButtonEventHandler ButtonReleased;
  195. /// <summary>
  196. /// Raised when the state of <see cref="Button"/> is low.
  197. /// </summary>
  198. /// <remarks>
  199. /// Implement this event handler and the <see cref="ButtonReleased"/> event handler
  200. /// when you want to provide an action associated with Button activity.
  201. /// Since the state of the Button is passed to the <see cref="ButtonEventHandler"/> delegate,
  202. /// you can use the same event handler for both Button states.
  203. /// </remarks>
  204. public event ButtonEventHandler ButtonPressed;
  205. private ButtonEventHandler onButton;
  206. /// <summary>
  207. /// Raises the <see cref="ButtonPressed"/> or <see cref="ButtonReleased"/> event.
  208. /// </summary>
  209. /// <param name="sender">The <see cref="Button"/> that raised the event.</param>
  210. /// <param name="ButtonState">The state of the Button.</param>
  211. protected virtual void OnButtonEvent(Button sender, ButtonState ButtonState)
  212. {
  213. if (this.onButton == null)
  214. {
  215. this.onButton = new ButtonEventHandler(this.OnButtonEvent);
  216. }
  217. if (Program.CheckAndInvoke((ButtonState == ButtonState.Released ? this.ButtonReleased : this.ButtonPressed), this.onButton, sender, ButtonState))
  218. {
  219. switch (ButtonState)
  220. {
  221. case ButtonState.Released:
  222. this.ButtonReleased(sender, ButtonState);
  223. break;
  224. case ButtonState.Pressed:
  225. this.ButtonPressed(sender, ButtonState);
  226. break;
  227. }
  228. }
  229. }
  230. #region enums
  231. /// <summary>
  232. /// Represents the state of the <see cref="Button"/> object.
  233. /// </summary>
  234. public enum ButtonState
  235. {
  236. /// <summary>
  237. /// The state of Button is low - pressed.
  238. /// </summary>
  239. Pressed = 0,
  240. /// <summary>
  241. /// The state of Button is high - released.
  242. /// </summary>
  243. Released = 1
  244. }
  245. /// <summary>
  246. /// Enuerates the various modes a LED can be set to.
  247. /// </summary>
  248. public enum LEDModes
  249. {
  250. /// <summary>
  251. /// The LED is on regardless of the button state.
  252. /// </summary>
  253. On,
  254. /// <summary>
  255. /// The LED is off regardless of the button state.
  256. /// </summary>
  257. Off,
  258. /// <summary>
  259. /// The LED changes state whenever the button is pressed.
  260. /// </summary>
  261. ToggleWhenPressed,
  262. /// <summary>
  263. /// The LED changes state whenever the button is released.
  264. /// </summary>
  265. ToggleWhenReleased,
  266. /// <summary>
  267. /// The LED is on while the button is pressed.
  268. /// </summary>
  269. OnWhilePressed,
  270. /// <summary>
  271. /// The LED is on except when the button is pressed.
  272. /// </summary>
  273. OnWhileReleased
  274. }
  275. #endregion
  276. }
  277. }