-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ST7920_Simulator.c
159 lines (131 loc) · 3.28 KB
/
ST7920_Simulator.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
#include "ST7920_Simulator.h"
#include "includes.h"
#include "GUI.h"
#include "../../Configuration.h"
#ifdef ST7920_SPI
ST7920_PIXEL st7920 = {ST7920_XSTART, ST7920_YSTART, 0};
ST7920_CTRL_STATUS status = ST7920_IDLE;
void ST7920_DrawPixel(int16_t x, int16_t y, uint16_t color)
{
// GUI_DrawPixel(x, y, color);
GUI_FillRectColor(SIMULATOR_XSTART + PIXEL_XSIZE*x,
SIMULATOR_YSTART + PIXEL_YSIZE*y,
SIMULATOR_XSTART + PIXEL_XSIZE*(x+1),
SIMULATOR_YSTART + PIXEL_YSIZE*(y+1), color);
}
int16_t ST7920_MapCoordinateX(void)
{
return (st7920.x & 0x07) * 16 + st7920.x_record;
}
int16_t ST7920_MapCoordinateY(void)
{
return ((st7920.x & 0x08)>>3)*32 + (st7920.y - ST7920_YSTART);
}
void ST7920_SetCursor(int16_t x, int16_t y)
{
st7920.x = x;
st7920.y = y;
st7920.x_record = 0;
}
void ST7920_DrawByte(u8 data)
{
uint8_t i = 0;
int16_t x = ST7920_MapCoordinateX(),
y = ST7920_MapCoordinateY();
for(; i<8; i++)
{
if(data & 0x80)
ST7920_DrawPixel(x, y, infoSettings.font_color);
else
ST7920_DrawPixel(x, y, infoSettings.bg_color);
data <<= 1;
x++;
}
st7920.x_record += 8;
if(st7920.x_record == 16)
{
st7920.x_record = 0;
st7920.x = (st7920.x + 1) & 0x8F;
}
}
u8 ST7920_IsCtrlByte(u8 data)
{
if(data == ST7920_WCMD || data == ST7920_WDATA || data == ST7920_RCMD || data == ST7920_RDATA)
return true;
else
return false;
}
u8 rcvData[64];
u8 rcvIndex = 0;
void ST7920_ParseRecv(u8 val)
{
if (ST7920_IsCtrlByte(val))
{
status = (ST7920_CTRL_STATUS)val;
rcvIndex = 0;
}
else
{
rcvData[rcvIndex++] = val;
if(rcvIndex == 1) return; //high 4 bits in first byte and
rcvIndex = 0; //low 4 bits in second byte is valid
switch (status)
{
case ST7920_WCMD:
ST7920_ST7920_ParseWCmd(rcvData[0] | (rcvData[1]>>4));
break;
case ST7920_WDATA:
ST7920_DrawByte(rcvData[0] | (rcvData[1]>>4));
break;
case ST7920_RCMD:
break;
case ST7920_RDATA:
break;
default:
break;
}
}
}
void ST7920_WriteXY(u8 xy)
{
static uint8_t i = 0;
if(i == 0) //y first
st7920.y = xy;
else
st7920.x = xy;
i = (i + 1) % 2;
}
void ST7920_ST7920_ParseWCmd(u8 cmd)
{
if(cmd & 0x80)
{
ST7920_WriteXY(cmd);
}
}
void menuST7920(void)
{
GUI_Clear(infoSettings.bg_color);
GUI_SetColor(infoSettings.font_color);
GUI_SetBkColor(infoSettings.bg_color);
#if defined(ST7920_BANNER_TEXT)
GUI_DispStringInRect(0, 0, LCD_WIDTH, SIMULATOR_YSTART, (u8*)ST7920_BANNER_TEXT);
#endif
SPI_Slave();
SPI_Slave_CS_Config();
while(infoMenu.menu[infoMenu.cur] == menuST7920)
{
while(SPISlave.rIndex != SPISlave.wIndex)
{
ST7920_ParseRecv(SPISlave.data[SPISlave.rIndex]);
SPISlave.rIndex = (SPISlave.rIndex + 1) % SPI_SLAVE_MAX;
}
Touch_Sw(LCD_ReadTouch());
if(LCD_BtnTouch(LCD_BUTTON_INTERVALS))
Touch_Sw(1);
#if LCD_ENCODER_SUPPORT
loopCheckMode();
#endif
}
SPI_SlaveDeInit();
}
#endif