/
Ili9486.cs
153 lines (135 loc) · 5.33 KB
/
Ili9486.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Meadow.Hardware;
using Meadow.Peripherals.Displays;
namespace Meadow.Foundation.Displays
{
/// <summary>
/// Represents a Ili9486 TFT color display
/// </summary>
public class Ili9486 : TftSpiBase, IRotatableDisplay
{
/// <summary>
/// The default display color mode
/// </summary>
public override ColorMode DefaultColorMode => ColorMode.Format12bppRgb444;
/// <summary>
/// The color modes supported by the display
/// </summary>
public override ColorMode SupportedColorModes => ColorMode.Format16bppRgb565 | ColorMode.Format12bppRgb444;
/// <summary>
/// Create a new Ili9486 color display object
/// </summary>
/// <param name="spiBus">SPI bus connected to display</param>
/// <param name="chipSelectPin">Chip select pin</param>
/// <param name="dcPin">Data command pin</param>
/// <param name="resetPin">Reset pin</param>
/// <param name="width">Width of display in pixels</param>
/// <param name="height">Height of display in pixels</param>
/// <param name="colorMode">The color mode to use for the display buffer</param>
public Ili9486(ISpiBus spiBus, IPin chipSelectPin, IPin dcPin, IPin resetPin,
int width = 320, int height = 480, ColorMode colorMode = ColorMode.Format12bppRgb444)
: base(spiBus, chipSelectPin, dcPin, resetPin, width, height, colorMode)
{
Initialize();
SetRotation(RotationType.Normal);
}
/// <summary>
/// Create a new Ili9486 color display object
/// </summary>
/// <param name="spiBus">SPI bus connected to display</param>
/// <param name="chipSelectPort">Chip select output port</param>
/// <param name="dataCommandPort">Data command output port</param>
/// <param name="resetPort">Reset output port</param>
/// <param name="width">Width of display in pixels</param>
/// <param name="height">Height of display in pixels</param>
/// <param name="colorMode">The color mode to use for the display buffer</param>
public Ili9486(ISpiBus spiBus, IDigitalOutputPort chipSelectPort,
IDigitalOutputPort dataCommandPort, IDigitalOutputPort resetPort,
int width = 320, int height = 480, ColorMode colorMode = ColorMode.Format12bppRgb444) :
base(spiBus, chipSelectPort, dataCommandPort, resetPort, width, height, colorMode)
{
Initialize();
SetRotation(RotationType.Normal);
}
/// <summary>
/// Initialize the display
/// </summary>
protected override void Initialize()
{
SendCommand(Register.SLPOUT); // Sleep out, also SW reset
DelayMs(120);
SendCommand(Register.COLOR_MODE);
if (ColorMode == ColorMode.Format16bppRgb565)
SendData(0x55);
else
SendData(0x53);
SendCommand(0xC2);
SendData(0x44);
SendCommand(0xC5);
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendData(0x00);
SendCommand(0xE0);
SendData(0x0F);
SendData(0x1F);
SendData(0x1C);
SendData(0x0C);
SendData(0x0F);
SendData(0x08);
SendData(0x48);
SendData(0x98);
SendData(0x37);
SendData(0x0A);
SendData(0x13);
SendData(0x04);
SendData(0x11);
SendData(0x0D);
SendData(0x00);
SendCommand(0xE1);
SendData(0x0F);
SendData(0x32);
SendData(0x2E);
SendData(0x0B);
SendData(0x0D);
SendData(0x05);
SendData(0x47);
SendData(0x75);
SendData(0x37);
SendData(0x06);
SendData(0x10);
SendData(0x03);
SendData(0x24);
SendData(0x20);
SendData(0x00);
SendCommand(Register.INVOFF); // display inversion OFF
SendCommand(Register.MADCTL);
SendData(0x48);
SendCommand(0x29); // display on
DelayMs(150);
}
/// <summary>
/// Set the display rotation
/// </summary>
/// <param name="rotation">The rotation value</param>
public void SetRotation(RotationType rotation)
{
SendCommand(Register.MADCTL);
switch (Rotation = rotation)
{
case RotationType.Normal:
SendData((byte)Register.MADCTL_MX | (byte)Register.MADCTL_BGR);
break;
case RotationType._90Degrees:
SendData((byte)Register.MADCTL_MV | (byte)Register.MADCTL_BGR);
break;
case RotationType._180Degrees:
SendData((byte)Register.MADCTL_BGR | (byte)Register.MADCTL_MY);
break;
case RotationType._270Degrees:
SendData((byte)Register.MADCTL_BGR | (byte)Register.MADCTL_MV | (byte)Register.MADCTL_MX | (byte)Register.MADCTL_MY);
break;
}
UpdateBuffer();
}
}
}