forked from moononournation/Arduino_GFX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino_ST7735.cpp
132 lines (114 loc) · 3.56 KB
/
Arduino_ST7735.cpp
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
/*
* start rewrite from:
* https://github.com/adafruit/Adafruit-GFX-Library.git
*/
#include "Arduino_ST7735.h"
Arduino_ST7735::Arduino_ST7735(
Arduino_DataBus *bus, int8_t rst, uint8_t r,
bool ips, int16_t w, int16_t h,
uint8_t col_offset1, uint8_t row_offset1, uint8_t col_offset2, uint8_t row_offset2,
bool bgr)
: Arduino_TFT(bus, rst, r, ips, w, h, col_offset1, row_offset1, col_offset2, row_offset2)
{
_bgr = bgr;
}
void Arduino_ST7735::begin(int speed)
{
#if defined(ESP8266) || defined(ESP32)
if (speed == 0)
{
speed = 27000000; // ST7735 Maximum supported speed
}
#endif
Arduino_TFT::begin(speed);
}
// Companion code to the above tables. Reads and issues
// a series of LCD commands stored in PROGMEM byte array.
void Arduino_ST7735::tftInit()
{
// if (_rst < 0)
// {
_bus->sendCommand(ST7735_SWRESET); // 1: Software reset
delay(ST7735_RST_DELAY);
// }
_bus->sendCommand(ST7735_SLPOUT); // 2: Out of sleep mode, no args, w/delay
delay(ST7735_SLPOUT_DELAY);
_bus->sendCommand(ST7735_COLMOD); // 3: Set color mode, 1 arg + delay:
_bus->sendData(0x05); // 16-bit color
if (_ips)
{
_bus->sendCommand(ST7735_INVON);
}
_bus->sendCommand(ST7735_NORON); // 4: Normal display on, no args, w/delay
delay(10);
_bus->sendCommand(ST7735_DISPON); // 5: Main screen turn on, no args, w/delay
}
void Arduino_ST7735::writeAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
if ((x != _currentX) || (w != _currentW))
{
uint16_t x_start = x + _xStart, x_end = x + w - 1 + _xStart;
_bus->writeCommand(ST7735_CASET); // Column addr set
_bus->write(x_start >> 8);
_bus->write(x_start & 0xFF); // XSTART
_bus->write(x_end >> 8);
_bus->write(x_end & 0xFF); // XEND
_currentX = x;
_currentW = w;
}
if ((y != _currentY) || (h != _currentH))
{
uint16_t y_start = y + _yStart, y_end = y + h - 1 + _yStart;
_bus->writeCommand(ST7735_RASET); // Row addr set
_bus->write(y_start >> 8);
_bus->write(y_start & 0xFF); // YSTART
_bus->write(y_end >> 8);
_bus->write(y_end & 0xFF); // YEND
_currentY = y;
_currentH = h;
}
_bus->writeCommand(ST7735_RAMWR); // write to RAM
}
/**************************************************************************/
/*!
@brief Set origin of (0,0) and orientation of TFT display
@param m The index for rotation, from 0-3 inclusive
*/
/**************************************************************************/
void Arduino_ST7735::setRotation(uint8_t r)
{
Arduino_TFT::setRotation(r);
switch (_rotation)
{
case 0:
r = ST7735_MADCTL_MX | ST7735_MADCTL_MY | (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB);
break;
case 1:
r = ST7735_MADCTL_MY | ST7735_MADCTL_MV | (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB);
break;
case 2:
r = (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB);
break;
case 3:
r = ST7735_MADCTL_MX | ST7735_MADCTL_MV | (_bgr ? ST7735_MADCTL_BGR : ST7735_MADCTL_RGB);
break;
}
_bus->beginWrite();
_bus->writeCommand(ST7735_MADCTL);
_bus->write(r);
_bus->endWrite();
}
void Arduino_ST7735::invertDisplay(bool i)
{
_bus->sendCommand(_ips ? (i ? ST7735_INVOFF : ST7735_INVON) : (i ? ST7735_INVON : ST7735_INVOFF));
}
void Arduino_ST7735::displayOn(void)
{
_bus->sendCommand(ST7735_SLPOUT);
delay(ST7735_SLPOUT_DELAY);
}
void Arduino_ST7735::displayOff(void)
{
_bus->sendCommand(ST7735_SLPIN);
delay(ST7735_SLPIN_DELAY);
}