-
Notifications
You must be signed in to change notification settings - Fork 0
/
st7735.c
335 lines (310 loc) · 9.96 KB
/
st7735.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "st7735.h"
#include <stdarg.h>
#include <stdio.h>
void st7735_delay(uint32_t i) {
volatile uint32_t j;
for (j=0; j!= i * 100; j++) ;
}
void st7735_sendByte(uint8_t data){
SPI_I2S_SendData(ST7735_SPI, data);
while(SPI_I2S_GetFlagStatus(ST7735_SPI, SPI_I2S_FLAG_BSY) == SET);
}
void st7735_sendCommand(uint8_t data){
GPIO_ResetBits(GPIOA, ST7735_PIN_A0);
st7735_sendByte(data);
}
void st7735_sendData(uint8_t data){
GPIO_SetBits(GPIOA, ST7735_PIN_A0);
st7735_sendByte(data);
}
void st7735_sendColor(Color c){
st7735_sendData(c.r);
st7735_sendData(c.g);
st7735_sendData(c.b);
}
void st7735_setMemoryWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
{
st7735_sendCommand(ST7735_CASET);
st7735_sendData(0x00);
st7735_sendData(x0);
st7735_sendData(0x00);
st7735_sendData(x1);
st7735_sendCommand(ST7735_RASET);
st7735_sendData(0x00);
st7735_sendData(y0);
st7735_sendData(0x00);
st7735_sendData(y1);
}
void st7735_drawPixel(uint8_t x, uint8_t y, Color c)
{
st7735_setMemoryWindow(x,y,x+1,y+1);
st7735_sendCommand(ST7735_RAMWR);
st7735_sendColor(c);
}
void st7735_drawRectangle(uint8_t x, uint8_t y, uint8_t width, uint8_t height, Color c)
{
uint16_t i, length;
st7735_setMemoryWindow(x,y,x+width-1,y+height-1);
st7735_sendCommand(ST7735_RAMWR);
length = width*height;
for(i=0; i<length; i++){
st7735_sendColor(c);
}
}
void st7735_clearScreen(Color color){
st7735_drawRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
}
char st7735_convertChar(char c){
if((c >= ' ') && (c<='z')){
return c-32;
}
return 0;
}
uint8_t st7735_printChar(uint8_t x, uint8_t y, Color fontColor, Color backgroundColor, char value){ //!TODO Add limitations for values
uint8_t j, i;
if(value == '\n')
return 0;
st7735_setMemoryWindow(x, y,x+ ST7735_FONT_WIDTH-1,y+ ST7735_FONT_HEIGHT-1);
st7735_sendCommand(ST7735_RAMWR);
uint8_t row = 1;
char converted = st7735_convertChar(value);
for( j = 0 ; j < ST7735_FONT_HEIGHT ; j++ ){
for( i = 0 ; i < ST7735_FONT_WIDTH ; i++ ){
if( ST7735_FONT_CHARS[converted*5+i] & row ){
st7735_sendColor(fontColor);
}else{
st7735_sendColor(backgroundColor);
}
}
row <<= 1;
}
return 1;
}
uint8_t st7735_printString(uint8_t x, uint8_t y, Color fontColor, Color backgroundColor, Orientation orientation, char *str){
uint8_t i, length, cursorX, cursorY;
length = 0;
cursorX = x;
cursorY = y;
for( i = 0 ; (i < ST7735_STRING_MAX_LENGTH) && (str[i] != 0x00); i++ ){
if(str[i]=='\n'){
cursorX = x;
cursorY += ST7735_FONT_HEIGHT;
}else{
length += st7735_printChar(cursorX, cursorY, fontColor, backgroundColor, str[i]);
if(orientation == ST7735_FONT_ORIENTATION_HORIZONTAL)
cursorX += ST7735_FONT_WIDTH;
else
cursorY += ST7735_FONT_HEIGHT;
}
}
return length;
}
uint8_t st7735_printFormattedString(uint8_t x, uint8_t y, Color fontColor, Color backgroundColor, Orientation orientation, char *str, ...){
uint8_t length;
va_list args;
va_start(args, str);
char buf[ST7735_STRING_MAX_LENGTH];
vsprintf(buf, str, args);
length = st7735_printString(x, y, fontColor, backgroundColor, orientation, buf);
va_end(args);
return length;
}
uint8_t st7735_calculateStringWidth(Orientation orientation, uint8_t numberOfChars){//!TODO check for multiline strings
if(orientation == ST7735_FONT_ORIENTATION_HORIZONTAL)
return numberOfChars * ST7735_FONT_WIDTH;
else
return ST7735_FONT_WIDTH;
}
uint8_t st7735_calculateStringHeight(Orientation orientation, uint8_t numberOfChars){
if(orientation == ST7735_FONT_ORIENTATION_HORIZONTAL)
return ST7735_HEIGHT;
else
return numberOfChars * ST7735_HEIGHT;
}
uint8_t st7735_calculateMultilineStringWidth(Orientation orientation, char *str){
uint8_t width, i, x;
width = 0;
x = 0;
if(orientation == ST7735_FONT_ORIENTATION_HORIZONTAL){
for( i = 0 ; (i < ST7735_STRING_MAX_LENGTH) && (str[i] != 0x00); i++ ){
if( str[i] == '\n' ){
x = 0;
}else{
x += ST7735_FONT_WIDTH;
if ( x > width )
width = x;
}
}
}else{
if (str[0] != 0x00)
width = ST7735_FONT_WIDTH;
}
return width;
}
uint8_t st7735_calculateMultilineStringHeight(Orientation orientation, char *str){
uint8_t height, i;
height = 0;
if(orientation == ST7735_FONT_ORIENTATION_HORIZONTAL){
height = ST7735_FONT_HEIGHT;
for( i = 0 ; (i < ST7735_STRING_MAX_LENGTH) && (str[i] != 0x00); i++ ){
if( str[i] == '\n' )
height += ST7735_FONT_HEIGHT;
}
}else{
for( i = 0 ; (i < ST7735_STRING_MAX_LENGTH) && (str[i] != 0x00); i++ ){
height += ST7735_FONT_HEIGHT;
}
}
return height;
}
uint8_t st7735_calculateMultilineFormattedStringWidth(Orientation orientation, char *str, ...){
uint8_t width;
va_list args;
va_start(args, str);
char buf[ST7735_STRING_MAX_LENGTH];
vsprintf(buf, str, args);
width = st7735_calculateMultilineStringWidth(orientation, buf);
va_end(args);
return width;
}
uint8_t st7735_calculateMultilineFormattedStringHeight(Orientation orientation, char *str, ...){
uint8_t height;
va_list args;
va_start(args, str);
char buf[ST7735_STRING_MAX_LENGTH];
vsprintf(buf, str, args);
height = st7735_calculateMultilineStringHeight(orientation, buf);
va_end(args);
return height;
}
void st7735_initGPIO(void){
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = ST7735_PIN_A0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ST7735_PIN_SCK | ST7735_PIN_SDA;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(ST7735_SPI, &SPI_InitStructure);
SPI_Cmd(ST7735_SPI, ENABLE);
SPI_NSSInternalSoftwareConfig(ST7735_SPI, SPI_NSSInternalSoft_Set);
}
void st7735_initDisplay(){
st7735_sendCommand(ST7735_SWRESET); //reset
st7735_delay(500);
st7735_sendCommand(ST7735_SLPOUT); //sleep out
st7735_delay(500);
st7735_sendCommand(ST7735_DISPON); //display on
st7735_delay(500);
}
void st7735_init(void){
st7735_initGPIO();
st7735_initDisplay();
}
const uint8_t ST7735_FONT_CHARS[] = {
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x9E,0x00,0x00,
0x00,0x0E,0x00,0x0E,0x00,
0x28,0xFE,0x28,0xFE,0x28,
0x48,0x54,0xFE,0x54,0x24,
0x46,0x26,0x10,0xC8,0xC4,
0x6C,0x92,0xAA,0x44,0x80,
0x00,0x0A,0x06,0x00,0x00,
0x00,0x38,0x44,0x82,0x00,
0x00,0x82,0x44,0x38,0x00,
0x28,0x10,0x7C,0x10,0x28,
0x10,0x10,0x7C,0x10,0x10,
0x00,0xA0,0x60,0x00,0x00,
0x10,0x10,0x10,0x10,0x10,
0x00,0xC0,0xC0,0x00,0x00,
0x40,0x20,0x10,0x08,0x04,
0x7C,0xA2,0x92,0x8A,0x7C,
0x00,0x84,0xFE,0x80,0x00,
0x84,0xC2,0xA2,0x92,0x8C,
0x42,0x82,0x8A,0x96,0x62,
0x30,0x28,0x24,0xFE,0x20,
0x4E,0x8A,0x8A,0x8A,0x72,
0x78,0x94,0x92,0x92,0x60,
0x02,0xE2,0x12,0x0A,0x06,
0x6C,0x92,0x92,0x92,0x6C,
0x0C,0x92,0x92,0x52,0x3C,
0x00,0x6C,0x6C,0x00,0x00,
0x00,0xAC,0x6C,0x00,0x00,
0x10,0x28,0x44,0x82,0x00,
0x28,0x28,0x28,0x28,0x28,
0x82,0x44,0x28,0x10,0x00,
0x04,0x02,0xA2,0x12,0x0C,
0x64,0x92,0xF2,0x82,0x7C,
0xFC,0x22,0x22,0x22,0xFC,
0xFE,0x92,0x92,0x92,0x6C,
0x7C,0x82,0x82,0x82,0x44,
0xFE,0x82,0x82,0x82,0x7C,
0xFE,0x92,0x92,0x92,0x82,
0xFE,0x12,0x12,0x12,0x02,
0x7C,0x82,0x92,0x92,0xF4,
0xFE,0x10,0x10,0x10,0xFE,
0x00,0x82,0xFE,0x82,0x00,
0x40,0x80,0x82,0x7E,0x02,
0xFE,0x10,0x28,0x44,0x82,
0xFE,0x80,0x80,0x80,0x80,
0xFE,0x04,0x08,0x04,0xFE,
0xFE,0x08,0x10,0x20,0xFE,
0x7C,0x82,0x82,0x82,0x7C,
0xFE,0x12,0x12,0x12,0x0C,
0x7C,0x82,0xA2,0x42,0xBC,
0xFE,0x12,0x32,0x52,0x8C,
0x8C,0x92,0x92,0x92,0x62,
0x02,0x02,0xFE,0x02,0x02,
0x7E,0x80,0x80,0x80,0x7E,
0x3E,0x40,0x80,0x40,0x3E,
0x7E,0x80,0x70,0x80,0x7E,
0xC6,0x28,0x10,0x28,0xC6,
0x0E,0x10,0xE0,0x10,0x0E,
0xC2,0xA2,0x92,0x8A,0x86,
0x00,0xFE,0x82,0x82,0x00,
0x0E,0x10,0xE0,0x10,0x0E,
0x00,0x82,0x82,0xFE,0x00,
0x08,0x04,0x02,0x04,0x08,
0x80,0x80,0x80,0x80,0x80,
0x00,0x02,0x04,0x08,0x00,
0x40,0xA8,0xA8,0xA8,0xF0,
0xFE,0x90,0x88,0x88,0x70,
0x70,0x88,0x88,0x88,0x40,
0x70,0x88,0x88,0x90,0xFE,
0x70,0xA8,0xA8,0xA8,0x30,
0x10,0xFC,0x12,0x02,0x04,
0x0C,0x92,0x92,0x92,0x7E,
0xFE,0x10,0x08,0x08,0xF0,
0x00,0x88,0xFA,0x80,0x00,
0x40,0x80,0x88,0x7A,0x00,
0xFE,0x20,0x50,0x88,0x00,
0x00,0x82,0xFE,0x80,0x00,
0xF8,0x08,0xF0,0x08,0xF0,
0xF8,0x10,0x08,0x08,0xF0,
0x70,0x88,0x88,0x88,0x70,
0xF8,0x28,0x28,0x28,0x10,
0x10,0x28,0x28,0x30,0xF8,
0xF8,0x10,0x08,0x08,0x10,
0x90,0xA8,0xA8,0xA8,0x48,
0x04,0x7E,0x84,0x80,0x40,
0x78,0x80,0x80,0x40,0xF8,
0x38,0x40,0x80,0x40,0x38,
0x78,0x80,0x60,0x80,0x78,
0x88,0x50,0x20,0x50,0x88,
0x18,0xA0,0xA0,0xA0,0x78,
0x88,0xC8,0xA8,0x98,0x88,
};