Dec. 12, 2013 | Snippet | Licensed as Apache 2.0 | 1497 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 |
using GHI.OSHW.Hardware; using Microsoft.SPOT.Hardware; using System; using System.Threading; public class FEZCerberusN18Display { private Orientations currentOrientation; private byte[] byteArray = new byte[1]; private OutputPort backlight; private OutputPort reset; private OutputPort rs; private SPI.Configuration config; private SPI spi; /// <summary> /// Constructs a new instance of the N18 display for the FEZ Cerberus. /// </summary> /// <param name="resetPin">The reset pin (Pin 3 on the socket)</param> /// <param name="backlightPin">The backlight pin (Pin 4 on the socket)</param> /// <param name="rsPin">The rs pin (Pin 5 on the socket)</param> /// <param name="chipSelectPin">The SPI chip select pin (Pin 6 on the socket)</param> /// <param name="spiModule">The SPI module</param> /// <param name="defaultOrientation">The default orientation of the display</param> public FEZCerberusN18Display(Cpu.Pin resetPin, Cpu.Pin backlightPin, Cpu.Pin rsPin, Cpu.Pin chipSelectPin, SPI.SPI_module spiModule, Orientations defaultOrientation = Orientations.Portrait) { this.backlight = new OutputPort(backlightPin, true); this.reset = new OutputPort(resetPin, false); this.rs = new OutputPort(rsPin, false); this.config = new SPI.Configuration(chipSelectPin, false, 0, 0, false, true, 12000, spiModule); this.spi = new SPI(this.config); this.reset.Write(false); Thread.Sleep(150); this.reset.Write(true); this.Orientation = defaultOrientation; this.ConfigureDisplay(); } /// <summary> /// The possible orientations of the display. /// </summary> public enum Orientations { /// <summary> /// Portrait orientation (rotated 0 degrees). /// </summary> Portrait = 0, /// <summary> /// Landscape orientation (rotated 90 degrees clockwise). /// </summary> Landscape = 1, /// <summary> /// Portrait orientation (rotated 180 degrees). /// </summary> PortraitFlipped = 2, /// <summary> /// Landscape orientation (rotated 90 degrees counter-clockwise). /// </summary> LandscapeFlipped = 3 } /// <summary> /// The current orientation of the display. /// </summary> public Orientations Orientation { get { return this.currentOrientation; } set { if (Util.SetSpecialDisplayConfig(this.config, Util.BPP_Type.BPP16_BGR_BE, (int)value, this.rs.Id)) this.currentOrientation = value; else throw new ArgumentException(); } } /// <summary> /// The current width of the display based on its orientation. /// </summary> public int Width { get { return (this.Orientation == Orientations.Portrait || this.Orientation == Orientations.PortraitFlipped) ? 128 : 160; } } /// <summary> /// The current height of the display based on its orientation. /// </summary> public int Height { get { return (this.Orientation == Orientations.Portrait || this.Orientation == Orientations.PortraitFlipped) ? 160 : 128; } } private void WriteCommand(byte command) { this.byteArray[0] = command; this.rs.Write(false); this.spi.Write(byteArray); } private void WriteData(byte data) { this.byteArray[0] = data; this.rs.Write(true); this.spi.Write(byteArray); } private void ConfigureDisplay() { this.WriteCommand(0x11);//Sleep exit Thread.Sleep(120); //ST7735R Frame Rate this.WriteCommand(0xB1); this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D); this.WriteCommand(0xB2); this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D); this.WriteCommand(0xB3); this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D); this.WriteData(0x01); this.WriteData(0x2C); this.WriteData(0x2D); this.WriteCommand(0xB4); //Column inversion this.WriteData(0x07); //ST7735R Power Sequence this.WriteCommand(0xC0); this.WriteData(0xA2); this.WriteData(0x02); this.WriteData(0x84); this.WriteCommand(0xC1); this.WriteData(0xC5); this.WriteCommand(0xC2); this.WriteData(0x0A); this.WriteData(0x00); this.WriteCommand(0xC3); this.WriteData(0x8A); this.WriteData(0x2A); this.WriteCommand(0xC4); this.WriteData(0x8A); this.WriteData(0xEE); this.WriteCommand(0xC5); //VCOM this.WriteData(0x0E); this.WriteCommand(0x36); //MX, MY, RGB mode this.WriteData(0xC8); //ST7735R Gamma Sequence this.WriteCommand(0xe0); this.WriteData(0x0f); this.WriteData(0x1a); this.WriteData(0x0f); this.WriteData(0x18); this.WriteData(0x2f); this.WriteData(0x28); this.WriteData(0x20); this.WriteData(0x22); this.WriteData(0x1f); this.WriteData(0x1b); this.WriteData(0x23); this.WriteData(0x37); this.WriteData(0x00); this.WriteData(0x07); this.WriteData(0x02); this.WriteData(0x10); this.WriteCommand(0xe1); this.WriteData(0x0f); this.WriteData(0x1b); this.WriteData(0x0f); this.WriteData(0x17); this.WriteData(0x33); this.WriteData(0x2c); this.WriteData(0x29); this.WriteData(0x2e); this.WriteData(0x30); this.WriteData(0x30); this.WriteData(0x39); this.WriteData(0x3f); this.WriteData(0x00); this.WriteData(0x07); this.WriteData(0x03); this.WriteData(0x10); this.WriteCommand(0x2a); this.WriteData(0x00); this.WriteData(0x00); this.WriteData(0x00); this.WriteData(0x7f); this.WriteCommand(0x2b); this.WriteData(0x00); this.WriteData(0x00); this.WriteData(0x00); this.WriteData(0x9f); this.WriteCommand(0xF0); //Enable test command this.WriteData(0x01); this.WriteCommand(0xF6); //Disable ram power save mode this.WriteData(0x00); this.WriteCommand(0x3A); //65k mode this.WriteData(0x05); this.WriteCommand(0x29);//Display on } }