-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathst7789display.h
139 lines (119 loc) · 4.94 KB
/
st7789display.h
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
//
/// \file st7789display.h
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2021 R. Stange <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef _display_st7789display_h
#define _display_st7789display_h
#include <circle/spimaster.h>
#include <circle/gpiopin.h>
#include <circle/chargenerator.h>
#include <circle/timer.h>
#include <circle/util.h>
#include <circle/types.h>
class CST7789Display /// Driver for ST7789-based dot-matrix displays
{
public:
static const unsigned None = GPIO_PINS;
typedef u16 TST7789Color;
// really ((green) & 0x3F) << 5, but to have a 0-31 range for all colors
#define ST7789_COLOR(red, green, blue) bswap16 (((red) & 0x1F) << 11 \
| ((green) & 0x1F) << 6 \
| ((blue) & 0x1F))
#define ST7789_BLACK_COLOR 0
#define ST7789_RED_COLOR 0x00F8
#define ST7789_GREEN_COLOR 0xE007
#define ST7789_BLUE_COLOR 0x1F00
#define ST7789_WHITE_COLOR 0xFFFF
public:
/// \param pSPIMaster Pointer to SPI master object
/// \param nDCPin GPIO pin number for DC pin
/// \param nResetPin GPIO pin number for Reset pin (optional)
/// \param nBackLightPin GPIO pin number for backlight pin (optional)
/// \param nWidth Display width in number of pixels (default 240)
/// \param nHeight Display height in number of pixels (default 240)
/// \param CPOL SPI clock polarity (0 or 1, default 0)
/// \param CPHA SPI clock phase (0 or 1, default 0)
/// \param nClockSpeed SPI clock frequency in Hz
/// \param nChipSelect SPI chip select (if connected, otherwise don't care)
/// \note GPIO pin numbers are SoC number, not header positions.
/// \note If SPI chip select is not connected, CPOL should probably be 1.
CST7789Display (CSPIMaster *pSPIMaster,
unsigned nDCPin, unsigned nResetPin = None, unsigned nBackLightPin = None,
unsigned nWidth = 240, unsigned nHeight = 240,
unsigned CPOL = 0, unsigned CPHA = 0, unsigned nClockSpeed = 15000000,
unsigned nChipSelect = 0);
/// \return Display width in number of pixels
unsigned GetWidth (void) const { return m_nWidth; }
/// \return Display height in number of pixels
unsigned GetHeight (void) const { return m_nHeight; }
/// \return Operation successful?
boolean Initialize (void);
/// \brief Set the global rotation of the display
/// \param nRot (0, 90, 180, 270)
void SetRotation (unsigned nRot);
/// \return Rotation in degrees (0,90,180,270)
unsigned GetRotation (void) const { return m_nRotation; }
/// \brief Set display on
void On (void);
/// \brief Set display off
void Off (void);
/// \brief Clear entire display with color
/// \param Color RGB565 color with swapped bytes (see: ST7789_COLOR())
void Clear (TST7789Color Color = ST7789_BLACK_COLOR);
/// \brief Set a single pixel to color
/// \param nPosX X-position (0..width-1)
/// \param nPosY Y-postion (0..height-1)
/// \param Color RGB565 color with swapped bytes (see: ST7789_COLOR())
void SetPixel (unsigned nPosX, unsigned nPosY, TST7789Color Color);
/// \brief Draw an ISO8859-1 string at a specific pixel position
/// \param nPosX X-position (0..width-1)
/// \param nPosY Y-postion (0..height-1)
/// \param pString 0-terminated string of printable characters
/// \param Color RGB565 foreground color with swapped bytes (see: ST7789_COLOR())
/// \param BgColor RGB565 background color with swapped bytes (see: ST7789_COLOR())
/// \param bDoubleWidth default TRUE for thicker characters on screen
/// \param bDoubleHeight default TRUE for higher characters on screen
void DrawText (unsigned nPosX, unsigned nPosY, const char *pString,
TST7789Color Color, TST7789Color BgColor = ST7789_BLACK_COLOR,
bool bDoubleWidth = TRUE, bool bDoubleHeight = TRUE);
private:
void SetWindow (unsigned x0, unsigned y0, unsigned x1, unsigned y1);
void SendByte (u8 uchByte, boolean bIsData);
void Command (u8 uchByte) { SendByte (uchByte, FALSE); }
void Data (u8 uchByte) { SendByte (uchByte, TRUE); }
void SendData (const void *pData, size_t nLength);
unsigned RotX (unsigned x, unsigned y);
unsigned RotY (unsigned x, unsigned y);
private:
CSPIMaster *m_pSPIMaster;
unsigned m_nResetPin;
unsigned m_nBackLightPin;
unsigned m_nWidth;
unsigned m_nHeight;
unsigned m_CPOL;
unsigned m_CPHA;
unsigned m_nClockSpeed;
unsigned m_nChipSelect;
unsigned m_nRotation;
CGPIOPin m_DCPin;
CGPIOPin m_ResetPin;
CGPIOPin m_BackLightPin;
CCharGenerator m_CharGen;
CTimer *m_pTimer;
};
#endif