This entry's latest version is outdated and must be revised. Please see the documentation for the latest API.

Devantech LCD03 driver by Gus_ghielectroncs

Jul. 27, 2012   |   Snippet   |   Licensed as Apache 2.0   |   1400 views

This driver allows the use of the LCD03 display using either serial or I2C port.

Comments or questions?   Discuss on the forum.



Author Version Date
Gus_ghielectroncs 1 07/27 '12 at 12:53am
1 — Source
  1. /* Devantech LCD03 driver
  2. *
  3. * Author : Christophe Gerbier, 2010
  4. *
  5. */
  6.  
  7. using Microsoft.SPOT.Hardware;
  8. using System.IO.Ports;
  9.  
  10. namespace Becafuel
  11. {
  12. /// <summary>
  13. /// Defines the different cursor types
  14. /// </summary>
  15. public enum LCD03CursorType
  16. {
  17. /// <summary>
  18. /// Hides the cursor
  19. /// </summary>
  20. Hide,
  21. /// <summary>
  22. /// Displays an underscore cursor
  23. /// </summary>
  24. Underline,
  25. /// <summary>
  26. /// Displays a blinking block
  27. /// </summary>
  28. Blink
  29. };
  30.  
  31. /// <summary>
  32. /// This class implement different functions of the Devantech LCD03 LCD display. It's a 4x20 display
  33. /// that can be controlled either via a serial link (UART) or via I²C.
  34. /// The interface is determined by the class constructor used, which is overriden.
  35. /// <para></para>
  36. /// <para>Author : Christophe Gerbier, 2010</para>
  37. /// </summary>
  38. public class LCD03
  39. {
  40.  
  41. private static bool Serial;
  42. private static SerialPort UART;
  43. private static I2CDevice.Configuration I2CConfig;
  44. private static I2CDevice I2C;
  45.  
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="LCD03"/> class using serial communication.
  48. /// <para>This constructor defaults to use Serial COM1</para>
  49. /// </summary>
  50. /// <example>This sample shows how to call the default constructor
  51. /// <code>
  52. ///using Becafuel;
  53. ///
  54. ///namespace MFConsoleApplication
  55. ///{
  56. /// public class Program
  57. /// {
  58. /// public static void Main()
  59. /// {
  60. /// LCD03 LCD = new LCD03(); // Default constructor using Serial port COM1
  61. /// LCD.Cursor(LCD03CursorType.Hide);
  62. /// LCD.BackLight(true);
  63. /// LCD.ClearScreen();
  64. /// }
  65. /// }
  66. ///}
  67. /// </code>
  68. /// </example>
  69. public LCD03() : this("COM1") {}
  70.  
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="LCD03"/> class using serial communication.
  73. /// </summary>
  74. /// <param name="ComPort">The COM port on the board.</param>
  75. /// <example>This sample shows how to call the constructor with a serial port parameter
  76. /// <code>
  77. ///using Becafuel;
  78. ///
  79. ///namespace MFConsoleApplication
  80. ///{
  81. /// public class Program
  82. /// {
  83. /// public static void Main()
  84. /// {
  85. /// LCD03 LCD = new LCD03("COM2");
  86. /// LCD.Cursor(LCD03CursorType.Hide);
  87. /// LCD.BackLight(true);
  88. /// LCD.ClearScreen();
  89. /// }
  90. /// }
  91. ///}
  92. /// </code>
  93. /// </example>
  94. public LCD03(string ComPort)
  95. {
  96. Serial = true;
  97. UART = new SerialPort(ComPort, 9600);
  98. UART.Parity = Parity.None;
  99. UART.StopBits = StopBits.Two;
  100. UART.Open();
  101. }
  102.  
  103. /// <summary>
  104. /// Initializes a new instance of the <see cref="LCD03"/> class using I²C communication.
  105. /// </summary>
  106. /// <param name="I2C_Address">The i²c address of the display.</param>
  107. /// <example>This sample shows how to call the constructor for I²C communication
  108. /// <code>
  109. ///using Becafuel;
  110. ///
  111. ///namespace MFConsoleApplication
  112. ///{
  113. /// public class Program
  114. /// {
  115. /// public static void Main()
  116. /// {
  117. /// LCD03 LCD = new LCD03(0xC6);
  118. /// LCD.Cursor(LCD03CursorType.Hide);
  119. /// LCD.BackLight(true);
  120. /// LCD.ClearScreen();
  121. /// }
  122. /// }
  123. ///}
  124. /// </code>
  125. /// </example>
  126. public LCD03(byte I2C_Address)
  127. {
  128. Serial = false;
  129. I2CConfig = new I2CDevice.Configuration((ushort)(I2C_Address>>1), 91); // 91 KHz
  130. I2C = new I2CDevice(I2CConfig);
  131. }
  132.  
  133.  
  134. /// <summary>
  135. /// Sets the cursor's type
  136. /// </summary>
  137. /// <param name="pCursor">Cursor's type</param>
  138. /// <example>This sample shows how change the cursor type
  139. /// <code>
  140. ///using Becafuel;
  141. ///
  142. ///namespace MFConsoleApplication
  143. ///{
  144. /// public class Program
  145. /// {
  146. /// public static void Main()
  147. /// {
  148. /// LCD03 LCD = new LCD03(0xC6);
  149. /// LCD.Cursor(LCD03CursorType.Hide);
  150. /// }
  151. /// }
  152. ///}
  153. /// </code>
  154. /// </example>
  155. public void Cursor(LCD03CursorType pCursor)
  156. {
  157. if (Serial) { UART.Write(new byte[1] { (byte)(4 + pCursor) }, 0, 1); }
  158. else
  159. {
  160. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  161. xActions[0] = I2C.CreateWriteTransaction(new byte[2] { 0, (byte)(4 + pCursor) });
  162. I2C.Execute(xActions, 1000);
  163. }
  164. }
  165.  
  166. /// <summary>
  167. /// Sets cursor position using two bytes, where first byte is the line and the second byte is the column
  168. /// </summary>
  169. /// <param name="x">Column, starting at 1</param>
  170. /// <param name="y">Raw, starting at 1</param>
  171. /// <example>This sample shows how to set the cursor's position
  172. /// <code>
  173. ///using Becafuel;
  174. ///
  175. ///namespace MFConsoleApplication
  176. ///{
  177. /// public class Program
  178. /// {
  179. /// public static void Main()
  180. /// {
  181. /// LCD03 LCD = new LCD03(0xC6);
  182. /// LCD.Cursor(LCD03CursorType.Blink);
  183. /// LCD.Cursor(10,2); // Cursor is now on column 10, raw 2
  184. /// }
  185. /// }
  186. ///}
  187. /// </code>
  188. /// </example>
  189. public void Cursor(byte x, byte y)
  190. {
  191. if (x <= 0 || x > 20 || y <= 0 || y > 4) { return; }
  192. if (Serial) { UART.Write(new byte[3] { 3, y, x }, 0, 3); }
  193. else
  194. {
  195. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  196. xActions[0] = I2C.CreateWriteTransaction(new byte[4] { 0, 3, y, x });
  197. I2C.Execute(xActions, 1000);
  198. }
  199. }
  200.  
  201. /// <summary>
  202. /// Turns the backlight ON or OFF
  203. /// </summary>
  204. /// <param name="State">if set to <c>true</c> [state].</param>
  205. /// <example>This sample shows how to turn the LCD backlight ON
  206. /// <code>
  207. ///using Becafuel;
  208. ///
  209. ///namespace MFConsoleApplication
  210. ///{
  211. /// public class Program
  212. /// {
  213. /// public static void Main()
  214. /// {
  215. /// LCD03 LCD = new LCD03();
  216. /// LCD.Cursor(LCD03CursorType.Blink);
  217. /// LCD.BackLight(true);
  218. /// }
  219. /// }
  220. ///}
  221. /// </code>
  222. /// </example>
  223. public void BackLight(bool State)
  224. {
  225. if (Serial) { UART.Write(new byte[1] { State ? (byte)19 : (byte)20 }, 0, 1); }
  226. else
  227. {
  228. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  229. xActions[0] = I2C.CreateWriteTransaction(new byte[2] { 0, State ? (byte)19 : (byte)20 });
  230. I2C.Execute(xActions, 1000);
  231. }
  232. }
  233.  
  234. /// <summary>
  235. /// Writes the specified string at the current cursor position.
  236. /// </summary>
  237. /// <param name="Text">The string to display.</param>
  238. /// <example>This sample shows how to display a string at the current cursor position
  239. /// <code>
  240. ///using Becafuel;
  241. ///
  242. ///namespace MFConsoleApplication
  243. ///{
  244. /// public class Program
  245. /// {
  246. /// public static void Main()
  247. /// {
  248. /// LCD03 LCD = new LCD03();
  249. /// LCD.Cursor(LCD03CursorType.Blink);
  250. /// LCD.Write ("FEZ rocks");
  251. /// }
  252. /// }
  253. ///}
  254. /// </code>
  255. /// </example>
  256. public void Write(string Text)
  257. {
  258. if (Serial)
  259. {
  260. byte[] Str = System.Text.Encoding.UTF8.GetBytes(Text);
  261. UART.Write(Str, 0, Str.Length);
  262. }
  263. else
  264. {
  265. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  266. xActions[0] = I2C.CreateWriteTransaction(System.Text.Encoding.UTF8.GetBytes((byte)0 + Text));
  267. I2C.Execute(xActions, 1000);
  268. }
  269. }
  270.  
  271. /// <summary>
  272. /// Writes the specified string at coordinates (x,y)
  273. /// </summary>
  274. /// <param name="x">Column, starting at 1</param>
  275. /// <param name="y">Row, starting at 1</param>
  276. /// <param name="Text">The string to display</param>
  277. /// <example>This sample shows how to display a string at the specified cursor position
  278. /// <code>
  279. ///using Becafuel;
  280. ///
  281. ///namespace MFConsoleApplication
  282. ///{
  283. /// public class Program
  284. /// {
  285. /// public static void Main()
  286. /// {
  287. /// LCD03 LCD = new LCD03();
  288. /// LCD.Cursor(LCD03CursorType.Blink);
  289. /// LCD.Write (5,2,"FEZ rocks");
  290. /// }
  291. /// }
  292. ///}
  293. /// </code>
  294. /// </example>
  295. public void Write(byte x, byte y, string Text)
  296. {
  297. if (x <= 0 || x > 20 || y <= 0 || y > 4) { return; }
  298. if (Serial)
  299. {
  300. Cursor(x, y);
  301. byte[] Str = System.Text.Encoding.UTF8.GetBytes(Text);
  302. UART.Write(Str, 0, Str.Length);
  303. }
  304. else
  305. {
  306. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
  307. xActions[0] = I2C.CreateWriteTransaction(new byte[4] { 0, 3, y, x });
  308. xActions[1] = I2C.CreateWriteTransaction(System.Text.Encoding.UTF8.GetBytes((byte)0 + Text));
  309. I2C.Execute(xActions, 1000);
  310. }
  311. }
  312.  
  313. /// <summary>
  314. /// Clears the screen.
  315. /// </summary>
  316. /// <example>This sample shows how to clear the LCD display
  317. /// <code>
  318. ///using Becafuel;
  319. ///
  320. ///namespace MFConsoleApplication
  321. ///{
  322. /// public class Program
  323. /// {
  324. /// public static void Main()
  325. /// {
  326. /// LCD03 LCD = new LCD03();
  327. /// LCD.ClearScreen();
  328. /// }
  329. /// }
  330. ///}
  331. /// </code>
  332. /// </example>
  333. public void ClearScreen()
  334. {
  335. if (Serial) { UART.Write(new byte[1] { 12 }, 0, 1); }
  336. else
  337. {
  338. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  339. xActions[0] = I2C.CreateWriteTransaction(new byte[2] { 0, 12 });
  340. I2C.Execute(xActions, 1000);
  341. }
  342. }
  343. }
  344. }
  345.