/
lcdLib.c
295 lines (253 loc) · 11.9 KB
/
lcdLib.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
/*-----------------------------------------------------------------------------*/
/* */
/* Copyright (c) James Pearman */
/* 2013 */
/* All Rights Reserved */
/* */
/*-----------------------------------------------------------------------------*/
/* */
/* Module: lcdLib.c */
/* Author: James Pearman */
/* Created: 17 October 2013 */
/* */
/* Revisions: */
/* V1.00 17 Oct 2013 - Initial release */
/*-----------------------------------------------------------------------------*/
/* */
/* The author is supplying this software for use with the VEX cortex */
/* control system. This file can be freely distributed and teams are */
/* authorized to freely use this program , however, it is requested that */
/* improvements or additions be shared with the Vex community via the vex */
/* forum. Please acknowledge the work of the authors when appropriate. */
/* Thanks. */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/* The author can be contacted on the vex forums as jpearman */
/* or electronic mail using jbpearman_at_mac_dot_com */
/* Mentor for team 8888 RoboLancers, Pasadena CA. */
/* */
/*-----------------------------------------------------------------------------*/
/* A port back of some of the ConVEX LCD code which was based on the */
/* example lcd code I wrote for ROBOTC back in 2011 */
/*-----------------------------------------------------------------------------*/
#ifndef _LCD_LIB__
#define _LCD_LIB__
// hide globals
#pragma systemFile
#define VEX_LCD_BACKLIGHT 0x01 ///< bit for turning backlight on
/*-----------------------------------------------------------------------------*/
/** @brief Holds all data for a single LCD display */
/*-----------------------------------------------------------------------------*/
typedef struct _LcdData {
short port; ///< The uart port
unsigned char flags; ///< flags, currently backlight on/off
short buttons; ///< button data read from lcd
char line1[20]; ///< stirage for line 1 lcd data
char line2[20]; ///< stirage for line 2 lcd data
char txbuf[32]; ///< the serial transmit buffer
char rxbuf[16]; ///< the serial receive buffer
} LcdData;
// Only one LCD available to this code
static LcdData vexLcdData;
// A global into which we update the buttons to make this work like
// Standard ROBOTC code
TControllerButtons nLCDButtons2;
// prototypes for the task
void vexLcdSendMessage( LcdData *lcd, short line );
void vexLcdCheckReceiveMessage( LcdData *lcd );
/*-----------------------------------------------------------------------------*/
/** @brief task sending data constantly to the LCD */
/*-----------------------------------------------------------------------------*/
task
vexLcdPollTask()
{
while(1)
{
vexLcdSendMessage( &vexLcdData, 0 );
wait1Msec(25);
vexLcdCheckReceiveMessage( &vexLcdData );
wait1Msec(10);
vexLcdSendMessage( &vexLcdData, 1 );
wait1Msec(25);
vexLcdCheckReceiveMessage( &vexLcdData );
wait1Msec(10);
}
}
/*-----------------------------------------------------------------------------*/
/** @brief Initializa the second LCD */
/** @param[in] port The uart on which this LCD is installed */
/*-----------------------------------------------------------------------------*/
void
vexLcdInit( int port )
{
// init the lcd structure
vexLcdData.port = port;
vexLcdData.flags = 0;
vexLcdData.buttons = 0;
// Init the baud rate
setBaudRate( port, baudRate19200 );
// start task slightly higher priority then default
StartTask( vexLcdPollTask, kDefaultTaskPriority + 1 );
}
/*-----------------------------------------------------------------------------*/
/** @brief Show text from beginning of an LCD line */
/** @param[in] line The line to display the text on, 0 or 1 */
/** @param[in] buf Pointer to char buffer with test to display */
/*-----------------------------------------------------------------------------*/
void
vexLcdSet( short line, char *buf )
{
if( !line )
strncpy( vexLcdData.line1, buf, 16 );
else
strncpy( vexLcdData.line2, buf, 16 );
}
/*-----------------------------------------------------------------------------*/
/** @brief Show text on LCD line and column */
/** @param[in] line The line to display the text on, 0 or 1 */
/** @param[in] col The position at which to start displaying text */
/** @param[in] buf Pointer to char buffer with test to display */
/*-----------------------------------------------------------------------------*/
void
vexLcdSetAt( short line, short col, char *buf )
{
if( (col < 0) || (col > 15 ) )
return;
if( !line )
strncpy( &vexLcdData.line1[col], buf, 16-col );
else
strncpy( &vexLcdData.line2[col], buf, 16-col );
}
/*-----------------------------------------------------------------------------*/
/** @brief Clear a given lcd display line */
/** @param[in] line The line to display the text on, 0 or 1 */
/*-----------------------------------------------------------------------------*/
void
vexLcdClearLine( short line )
{
vexLcdSet( line, " " );
}
/*-----------------------------------------------------------------------------*/
/** @brief Turn on or off the backlight */
/** @param[in] value 1 for backlight on, 0 for off */
/*-----------------------------------------------------------------------------*/
void
vexLcdBacklight( short value )
{
vexLcdData.flags = (value == 1) ? vexLcdData.flags | VEX_LCD_BACKLIGHT : vexLcdData.flags & ~VEX_LCD_BACKLIGHT;
}
/*-----------------------------------------------------------------------------*/
/** @brief Get the current button status */
/** @returns The button (or buttons) pressed */
/*-----------------------------------------------------------------------------*/
TControllerButtons
vexLcdButtonGet( )
{
return( (TControllerButtons)vexLcdData.buttons );
}
/*-----------------------------------------------------------------------------*/
/** @brief Form and send message to LCD */
/** @param[in] lcd Pointer to LcdData structure */
/** @param[in] line The line to display the text on, 0 or 1 */
/** @note Internal driver use only */
/*-----------------------------------------------------------------------------*/
void
vexLcdSendMessage( LcdData *lcd, short line )
{
// create message
short i, cs;
short timeout;
// bounds check line variable
if( ( line < 0 ) || (line > 1) )
return;
// Header for LCD communication
lcd->txbuf[0] = 0xAA;
lcd->txbuf[1] = 0x55;
lcd->txbuf[2] = 0x1e;
lcd->txbuf[3] = 0x12;
lcd->txbuf[4] = (lcd->flags & VEX_LCD_BACKLIGHT) ? 0x02 + line : line;
// fill with spaces
for(i=0;i<16;i++)
lcd->txbuf[ 5+i ] = 0x20;
// Copy data transmit buffer
if(!line) {
for(i=0;(i<16)&&(lcd->line1[i]!=0);i++)
lcd->txbuf[ 5+i ] = lcd->line1[i];
}
else {
for(i=0;(i<16)&&(lcd->line2[i]!=0);i++)
lcd->txbuf[ 5+i ] = lcd->line2[i];
}
// calculate checksum
cs = 0;
for(i=4;i<21;i++)
cs = cs + lcd->txbuf[i];
lcd->txbuf[21] = 0x100 - cs;
// send to port
for(i=0;i<22;i++)
{
// wait for transmit complete or timeout
// Under normal circumstances this should not be needed, there is
// a transmit buffer larger than most messages
// timeout here is set at about 200uS (from experimentation)
timeout = 0;
while( !bXmitComplete(lcd->port) && (timeout < 20) )
timeout++;
// send char
sendChar( lcd->port, lcd->txbuf[i] );
}
}
/*-----------------------------------------------------------------------------*/
/** @brief Check for receive data and store */
/** @param[in] lcd Pointer to LcdData structure */
/** @note Internal driver use only */
/*-----------------------------------------------------------------------------*/
void
vexLcdCheckReceiveMessage( LcdData *lcd )
{
short i;
short c;
// any characters ?
c = getChar(lcd->port);
if( c < 0 )
return;
// read up to 16 bytes from serial port
lcd->rxbuf[0] = c;
for(i=1;i<16;i++)
{
c = getChar(lcd->port);
if( c >= 0 )
lcd->rxbuf[i] = c;
else
break;
}
// 6 chars ?
if( i == 6 )
{
// lcd message ?
if( (lcd->rxbuf[0] == 0xAA) && (lcd->rxbuf[1] == 0x55) && (lcd->rxbuf[2] == 0x16))
{
// verify checksum
if( !((lcd->rxbuf[4] + lcd->rxbuf[5]) & 0xFF) )
{
lcd->buttons = lcd->rxbuf[4];
nLCDButtons2 = (TControllerButtons)lcd->buttons;
}
}
}
// Flush any remaining characters
while(getChar(lcd->port) >= 0)
i++; // to remove warning
}
#endif /* _LCD_LIB_ */