-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathST7789.c
138 lines (121 loc) · 2.59 KB
/
ST7789.c
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
#include "ST7789.h"
//88
void ST7789_Init(){
LCD_SCK1;
LCD_CS0;
LCD_RST0;
HAL_Delay(10);
LCD_RST1;
HAL_Delay(10);
ST7789_sendCmd(0x11);
HAL_Delay(10);
ST7789_sendCmd(0x3A);
ST7789_sendData(0x05);
ST7789_sendCmd(0x29);
}
void ST7789_senddata(unsigned char data){
unsigned char i;
LCD_CS0;
for(i=0;i<8;i++){
if(data & 0x80){LCD_MOSI1;}
else LCD_MOSI0;
data=data<<1;
LCD_SCK0;
LCD_SCK1;
}
LCD_CS1;
}
void ST7789_sendCmd(unsigned char cmd){
LCD_DC0;
ST7789_senddata(cmd);
}
void ST7789_sendData(unsigned char data){
LCD_DC1;
ST7789_senddata(data);
}
void ST7789_pix(unsigned char x,unsigned char y, unsigned int color){
ST7789_sendCmd(0x2A);
ST7789_sendData(0x00);
ST7789_sendData(x);
ST7789_sendCmd(0x2B);
ST7789_sendData(0x00);
ST7789_sendData(y);
ST7789_sendCmd(0x2C);
ST7789_sendData((color & 0xFF00)>>8);
ST7789_sendData(color & 0x00FF);
}
void ST7789_at(unsigned char startx,unsigned char starty,unsigned char stopx,unsigned char stopy){
ST7789_sendCmd(0x2A);
ST7789_sendData(0x00);
ST7789_sendData(startx);
ST7789_sendData(0x00);
ST7789_sendData(stopx);
ST7789_sendCmd(0x2B);
ST7789_sendData(0x00);
ST7789_sendData(starty);
ST7789_sendData(0x00);
ST7789_sendData(stopy);
}
void ST7789_drawImage(uint32_t xs, uint32_t ys, const uint32_t *image,unsigned int color)
{
uint32_t i, x, y, sx, sy;
sx = image[0];
sy = image[1];
for (i = 0; i < sx * sy; i++)
{
x = xs + (i % sx);
y = ys + (i / sx);
if ((image[2 + (i / 32)] >> (31 - (i % 32))) & 0x1)
ST7789_pix(x, y,color);
else
ST7789_pix(x, y,0x0);
}
}
void ST7789_drawChar(uint32_t xs, uint32_t ys, const uint32_t ** font, uint8_t ascii,unsigned int color)
{
if ((ascii < 32) || (ascii > 126))
ascii = 0;
else
ascii -= 32;
ST7789_drawImage(xs, ys, font[ascii],color);
}
void ST7789_drawInt(uint32_t xs, uint32_t ys, const uint32_t ** font, int n,unsigned int color)
{
int r = 1;
int offset = 0;
if (n == 0)
{
ST7789_drawChar(xs, ys, font, '0',color);
return;
}
if (n < 0)
{
ST7789_drawChar(xs, ys, font, '-',color);
offset += font[0][0];
n = -n;
}
while (n / r)
r *= 10;
while (r != 1)
{
r /= 10;
ST7789_drawChar(xs + offset, ys, font, (uint8_t)((n % (r * 10)) / r + '0'),color);
offset += font[0][0];
}
}
void ST7789_drawString(uint32_t xs, uint32_t ys, const uint32_t ** font, uint8_t *str,unsigned int color)
{
while (*str != 0)
{
ST7789_drawChar(xs, ys, font, *(str++),color);
xs += font[0][0];
};
}
void ST7789_Clear(uint32_t x1,uint32_t x2,uint32_t y1,uint32_t y2){
int y,x;
for(y=y1;y<y2;y++){
for(x=x1;x<x2;x++){
ST7789_pix(x,y,0x0);
}
}
}