You have reached the legacy GHI Electronics, LLC website, for the new website please visit here. For the new forum please visit here.

This legacy website will be taken offline at the end of this year. If there is anything that you would like to archive and save for future reference please do so.

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

FEZ Cerberus and N18 Display Library by GHI Electronics

Dec. 12, 2013   |   Snippet   |   Licensed as Apache 2.0   |   1339 views

This simple class performs all the initialization needed to use bitmap objects with their flush methods on the Display N18 with a Cerberus based device in a non-Gadgeteer context. It does support rotating the display. It requires the GHI.OSHW.Hardware and Microsoft.SPOT.Hardware libraries.

In order to use the flush functionality, the bitmap must be the size of the display in its current rotation. So if the display is in Portrait or PortraitFlipped mode, width is 128 and height is 160. If the display is in Landscape or LandscapeFlipped mode, width is 160 and height is 128.

Comments or questions?   Discuss on the forum.



Author Version Date
John_ghielectroncs 1 12/12 '13 at 04:58pm
1 — Source
  1. using GHI.OSHW.Hardware;
  2. using Microsoft.SPOT.Hardware;
  3. using System;
  4. using System.Threading;
  5.  
  6. public class FEZCerberusN18Display
  7. {
  8. private Orientations currentOrientation;
  9. private byte[] byteArray = new byte[1];
  10. private OutputPort backlight;
  11. private OutputPort reset;
  12. private OutputPort rs;
  13. private SPI.Configuration config;
  14. private SPI spi;
  15.  
  16. /// <summary>
  17. /// Constructs a new instance of the N18 display for the FEZ Cerberus.
  18. /// </summary>
  19. /// <param name="resetPin">The reset pin (Pin 3 on the socket)</param>
  20. /// <param name="backlightPin">The backlight pin (Pin 4 on the socket)</param>
  21. /// <param name="rsPin">The rs pin (Pin 5 on the socket)</param>
  22. /// <param name="chipSelectPin">The SPI chip select pin (Pin 6 on the socket)</param>
  23. /// <param name="spiModule">The SPI module</param>
  24. /// <param name="defaultOrientation">The default orientation of the display</param>
  25. public FEZCerberusN18Display(Cpu.Pin resetPin, Cpu.Pin backlightPin, Cpu.Pin rsPin, Cpu.Pin chipSelectPin, SPI.SPI_module spiModule, Orientations defaultOrientation = Orientations.Portrait)
  26. {
  27. this.backlight = new OutputPort(backlightPin, true);
  28. this.reset = new OutputPort(resetPin, false);
  29. this.rs = new OutputPort(rsPin, false);
  30. this.config = new SPI.Configuration(chipSelectPin, false, 0, 0, false, true, 12000, spiModule);
  31. this.spi = new SPI(this.config);
  32.  
  33. this.reset.Write(false);
  34. Thread.Sleep(150);
  35. this.reset.Write(true);
  36.  
  37. this.Orientation = defaultOrientation;
  38.  
  39. this.ConfigureDisplay();
  40. }
  41.  
  42. /// <summary>
  43. /// The possible orientations of the display.
  44. /// </summary>
  45. public enum Orientations
  46. {
  47. /// <summary>
  48. /// Portrait orientation (rotated 0 degrees).
  49. /// </summary>
  50. Portrait = 0,
  51. /// <summary>
  52. /// Landscape orientation (rotated 90 degrees clockwise).
  53. /// </summary>
  54. Landscape = 1,
  55. /// <summary>
  56. /// Portrait orientation (rotated 180 degrees).
  57. /// </summary>
  58. PortraitFlipped = 2,
  59. /// <summary>
  60. /// Landscape orientation (rotated 90 degrees counter-clockwise).
  61. /// </summary>
  62. LandscapeFlipped = 3
  63. }
  64.  
  65. /// <summary>
  66. /// The current orientation of the display.
  67. /// </summary>
  68. public Orientations Orientation
  69. {
  70. get
  71. {
  72. return this.currentOrientation;
  73. }
  74. set
  75. {
  76. if (Util.SetSpecialDisplayConfig(this.config, Util.BPP_Type.BPP16_BGR_BE, (int)value, this.rs.Id))
  77. this.currentOrientation = value;
  78. else
  79. throw new ArgumentException();
  80. }
  81. }
  82.  
  83. /// <summary>
  84. /// The current width of the display based on its orientation.
  85. /// </summary>
  86. public int Width
  87. {
  88. get
  89. {
  90. return (this.Orientation == Orientations.Portrait || this.Orientation == Orientations.PortraitFlipped) ? 128 : 160;
  91. }
  92. }
  93.  
  94. /// <summary>
  95. /// The current height of the display based on its orientation.
  96. /// </summary>
  97. public int Height
  98. {
  99. get
  100. {
  101. return (this.Orientation == Orientations.Portrait || this.Orientation == Orientations.PortraitFlipped) ? 160 : 128;
  102. }
  103. }
  104.  
  105. private void WriteCommand(byte command)
  106. {
  107. this.byteArray[0] = command;
  108. this.rs.Write(false);
  109. this.spi.Write(byteArray);
  110. }
  111.  
  112. private void WriteData(byte data)
  113. {
  114. this.byteArray[0] = data;
  115. this.rs.Write(true);
  116. this.spi.Write(byteArray);
  117. }
  118.  
  119. private void ConfigureDisplay()
  120. {
  121. this.WriteCommand(0x11);//Sleep exit
  122. Thread.Sleep(120);
  123.  
  124. //ST7735R Frame Rate
  125. this.WriteCommand(0xB1);
  126. this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D);
  127. this.WriteCommand(0xB2);
  128. this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D);
  129. this.WriteCommand(0xB3);
  130. this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D);
  131. this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D);
  132.  
  133. this.WriteCommand(0xB4); //Column inversion
  134. this.WriteData(0x07);
  135.  
  136. //ST7735R Power Sequence
  137. this.WriteCommand(0xC0);
  138. this.WriteData(0xA2); this.WriteData(0x02); this.WriteData(0x84);
  139. this.WriteCommand(0xC1); this.WriteData(0xC5);
  140. this.WriteCommand(0xC2);
  141. this.WriteData(0x0A); this.WriteData(0x00);
  142. this.WriteCommand(0xC3);
  143. this.WriteData(0x8A); this.WriteData(0x2A);
  144. this.WriteCommand(0xC4);
  145. this.WriteData(0x8A); this.WriteData(0xEE);
  146.  
  147. this.WriteCommand(0xC5); //VCOM
  148. this.WriteData(0x0E);
  149.  
  150. this.WriteCommand(0x36); //MX, MY, RGB mode
  151. this.WriteData(0xC8);
  152.  
  153. //ST7735R Gamma Sequence
  154. this.WriteCommand(0xe0);
  155. this.WriteData(0x0f); this.WriteData(0x1a);
  156. this.WriteData(0x0f); this.WriteData(0x18);
  157. this.WriteData(0x2f); this.WriteData(0x28);
  158. this.WriteData(0x20); this.WriteData(0x22);
  159. this.WriteData(0x1f); this.WriteData(0x1b);
  160. this.WriteData(0x23); this.WriteData(0x37); this.WriteData(0x00);
  161.  
  162. this.WriteData(0x07);
  163. this.WriteData(0x02); this.WriteData(0x10);
  164. this.WriteCommand(0xe1);
  165. this.WriteData(0x0f); this.WriteData(0x1b);
  166. this.WriteData(0x0f); this.WriteData(0x17);
  167. this.WriteData(0x33); this.WriteData(0x2c);
  168. this.WriteData(0x29); this.WriteData(0x2e);
  169. this.WriteData(0x30); this.WriteData(0x30);
  170. this.WriteData(0x39); this.WriteData(0x3f);
  171. this.WriteData(0x00); this.WriteData(0x07);
  172. this.WriteData(0x03); this.WriteData(0x10);
  173.  
  174. this.WriteCommand(0x2a);
  175. this.WriteData(0x00); this.WriteData(0x00);
  176. this.WriteData(0x00); this.WriteData(0x7f);
  177. this.WriteCommand(0x2b);
  178. this.WriteData(0x00); this.WriteData(0x00);
  179. this.WriteData(0x00); this.WriteData(0x9f);
  180.  
  181. this.WriteCommand(0xF0); //Enable test command
  182. this.WriteData(0x01);
  183. this.WriteCommand(0xF6); //Disable ram power save mode
  184. this.WriteData(0x00);
  185.  
  186. this.WriteCommand(0x3A); //65k mode
  187. this.WriteData(0x05);
  188.  
  189. this.WriteCommand(0x29);//Display on
  190. }
  191. }