/
SerialLCD.cpp
220 lines (197 loc) · 5.74 KB
/
SerialLCD.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
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
/*
* SerialLCD.cpp
* Serial LCD driver Library
*
* Copyright (c) 2010 seeed technology inc.
* Author : Jimbo.We
* Contribution : Visweswara R
* Create Time : Dec 2010
* Change Log : Modified 15 March,2012 for Arduino 1.0 IDE by Frankie.Chu
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "SerialLCD.h"
SerialLCD::SerialLCD(uint8_t rx, uint8_t tx):SoftwareSerial(rx,tx)
{
}
/********** High level commands, for the user! **********/
// Initialize the Serial LCD Driver. SerialLCD Module initiates the communication.
void SerialLCD::begin()
{
SoftwareSerial::begin(9600);
/*while(1)
{
if ( SoftwareSerial::available() > 0 && SoftwareSerial::read()==SLCD_INIT )
{
SoftwareSerial::write(SLCD_INIT_ACK);
break;
}
}*/
delay(2);
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_POWER_OFF);
delay(1);
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_POWER_ON);
delay(1);
SoftwareSerial::write(SLCD_INIT_ACK);
while(1)
{
if (SoftwareSerial::available() > 0 &&SoftwareSerial::read()==SLCD_INIT_DONE)
break;
}
delay(2);
}
// Clear the display
void SerialLCD::clear()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_CLEAR_DISPLAY);
}
// Return to home(top-left corner of LCD)
void SerialLCD::home()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_RETURN_HOME);
delay(2);//this command needs more time;
}
// Set Cursor to (Column,Row) Position
void SerialLCD::setCursor(uint8_t column, uint8_t row)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_CURSOR_HEADER); //cursor header command
SoftwareSerial::write(column);
SoftwareSerial::write(row);
}
// Switch the display off without clearing RAM
void SerialLCD::noDisplay()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_DISPLAY_OFF);
}
// Switch the display on
void SerialLCD::display()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_DISPLAY_ON);
}
// Switch the underline cursor off
void SerialLCD::noCursor()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_CURSOR_OFF);
}
// Switch the underline cursor on
void SerialLCD::cursor()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_CURSOR_ON);
}
// Switch off the blinking cursor
void SerialLCD::noBlink()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_BLINK_OFF);
}
// Switch on the blinking cursor
void SerialLCD::blink()
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_BLINK_ON);
}
// Scroll the display left without changing the RAM
void SerialLCD::scrollDisplayLeft(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_SCROLL_LEFT);
}
// Scroll the display right without changing the RAM
void SerialLCD::scrollDisplayRight(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_SCROLL_RIGHT);
}
// Set the text flow "Left to Right"
void SerialLCD::leftToRight(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_LEFT_TO_RIGHT);
}
// Set the text flow "Right to Left"
void SerialLCD::rightToLeft(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_RIGHT_TO_LEFT);
}
// This will 'right justify' text from the cursor
void SerialLCD::autoscroll(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_AUTO_SCROLL);
}
// This will 'left justify' text from the cursor
void SerialLCD::noAutoscroll(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_NO_AUTO_SCROLL);
}
void SerialLCD::Power(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_POWER_ON);
}
void SerialLCD::noPower(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_POWER_OFF);
}
//Turn off the backlight
void SerialLCD::noBacklight(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_BACKLIGHT_OFF);
}
//Turn on the back light
void SerialLCD::backlight(void)
{
SoftwareSerial::write(SLCD_CONTROL_HEADER);
SoftwareSerial::write(SLCD_BACKLIGHT_ON);
}
// Print Commands
void SerialLCD::print(uint8_t b)
{
SoftwareSerial::write(SLCD_CHAR_HEADER);
SoftwareSerial::write(b);
}
void SerialLCD::print(const char b[])
{
SoftwareSerial::write(SLCD_CHAR_HEADER);
SoftwareSerial::write(b);
}
void SerialLCD::print(unsigned long n, uint8_t base)
{
SoftwareSerial::write(SLCD_CHAR_HEADER);
SoftwareSerial::print(n, base);
}
void SerialLCD::write(const char b[], uint8_t len)
{
SoftwareSerial::write(SLCD_CHAR_HEADER);
SoftwareSerial::write(b, len);
}