-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst7789_spi_dc_cs_rst_bw.cpp
137 lines (120 loc) · 3.37 KB
/
st7789_spi_dc_cs_rst_bw.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
133
134
135
136
137
//
// Made by a second year student of Wouter van Ooijen
//
using namespace hwlib;
namespace target = hwlib::target;
class st7789_spi_dc_cs_rst_bw : public st7789, public window {
private:
static auto constexpr wsize = xy(240, 240);
uint8_t buffer[(wsize.x * wsize.y) / 8];
void write_implementation(
xy pos,
color col
) override {
if (col == white) {
buffer[(pos.x + wsize.x * pos.y) / 8] |= (0x01 << (pos.x % 8));
} else {
buffer[(pos.x + wsize.x * pos.y) / 8] &= ~(0x01 << (pos.x % 8));
}
}
spi_bus &bus;
pin_out &dc;
pin_out &cs;
pin_out &rst;
public:
void command(
commands c
) {
dc.write(0);
dc.flush();
auto transaction = bus.transaction(cs);
transaction.write(static_cast< uint8_t >( c ));
dc.write(1);
dc.flush();
}
void command(
commands c,
uint8_t d0
) {
dc.write(0);
dc.flush();
auto transaction = bus.transaction(cs);
transaction.write(static_cast< uint8_t >( c ));
dc.write(1);
dc.flush();
transaction.write(d0);
}
void command(
commands c,
uint8_t d0,
uint8_t d1,
uint8_t d2,
uint8_t d3
) {
dc.write(0);
dc.flush();
auto transaction = bus.transaction(cs);
transaction.write(static_cast< uint8_t >( c ));
dc.write(1);
dc.flush();
transaction.write(d0);
transaction.write(d1);
transaction.write(d2);
transaction.write(d3);
}
st7789_spi_dc_cs_rst_bw(
spi_bus &bus,
pin_out &dc,
pin_out &cs,
pin_out &rst
) :
window(wsize, white, black),
bus(bus),
dc(dc),
cs(cs),
rst(rst) {
rst.write(0);
rst.flush();
wait_ms(200);
rst.write(1);
rst.flush();
wait_ms(200);
command(commands::SWRESET);
wait_ms(150);
command(commands::SLPOUT);
wait_ms(10);
command(commands::COLMOD, 0x66);
wait_ms(10);
command(commands::MADCTL, 0x10);
command(commands::CASET, 0, 0, wsize.x >> 8, wsize.x & 0xFF);
command(commands::RASET, 0, 0, wsize.y >> 8, wsize.y & 0xFF);
command(commands::INVON);
wait_ms(10);
command(commands::NORON);
wait_ms(100);
command(commands::DISPON);
wait_ms(100);
}
void flush() override {
dc.write(0);
dc.flush();
auto transaction = bus.transaction(cs);
transaction.write(static_cast< uint8_t >( commands::RAMWR ));
dc.write(1);
dc.flush();
for (int i = 0; i < 240 * 240; ++i) {
// transaction.write( ( buffer[ i ] << 2 ) & 0xC0 );
// transaction.write( ( buffer[ i ] << 4 ) & 0xC0 );
// transaction.write( ( buffer[ i ] << 6 ) & 0xC0 );
if ((buffer[i / 8] >> (i % 8)) & 0x01) {
transaction.write(0xFF);
transaction.write(0xFF);
transaction.write(0xFF);
} else {
transaction.write(0x00);
transaction.write(0x00);
transaction.write(0x00);
}
}
}
}; // class st7789_spi_dc_cs_rst