|
@@ -1,393 +0,0 @@
|
1
|
|
-#include "LiquidCrystalRus.h"
|
2
|
|
-
|
3
|
|
-#include <stdio.h>
|
4
|
|
-#include <string.h>
|
5
|
|
-#include <inttypes.h>
|
6
|
|
-#include <avr/pgmspace.h>
|
7
|
|
-
|
8
|
|
-#if defined(ARDUINO) && ARDUINO >= 100
|
9
|
|
- #include "Arduino.h"
|
10
|
|
-#else
|
11
|
|
- #include "WProgram.h"
|
12
|
|
-#endif
|
13
|
|
-
|
14
|
|
-// it is a Russian alphabet translation
|
15
|
|
-// except 0401 --> 0xa2 = ╗, 0451 --> 0xb5
|
16
|
|
-const PROGMEM uint8_t utf_recode[] =
|
17
|
|
- { 0x41,0xa0,0x42,0xa1,0xe0,0x45,0xa3,0xa4,
|
18
|
|
- 0xa5,0xa6,0x4b,0xa7,0x4d,0x48,0x4f,0xa8,
|
19
|
|
- 0x50,0x43,0x54,0xa9,0xaa,0x58,0xe1,0xab,
|
20
|
|
- 0xac,0xe2,0xad,0xae,0x62,0xaf,0xb0,0xb1,
|
21
|
|
- 0x61,0xb2,0xb3,0xb4,0xe3,0x65,0xb6,0xb7,
|
22
|
|
- 0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0x6f,0xbe,
|
23
|
|
- 0x70,0x63,0xbf,0x79,0xe4,0x78,0xe5,0xc0,
|
24
|
|
- 0xc1,0xe6,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7
|
25
|
|
- };
|
26
|
|
-
|
27
|
|
-// When the display powers up, it is configured as follows:
|
28
|
|
-//
|
29
|
|
-// 1. Display clear
|
30
|
|
-// 2. Function set:
|
31
|
|
-// DL = 1; 8-bit interface data
|
32
|
|
-// N = 0; 1-line display
|
33
|
|
-// F = 0; 5x8 dot character font
|
34
|
|
-// 3. Display on/off control:
|
35
|
|
-// D = 0; Display off
|
36
|
|
-// C = 0; Cursor off
|
37
|
|
-// B = 0; Blinking off
|
38
|
|
-// 4. Entry mode set:
|
39
|
|
-// I/D = 1; Increment by 1
|
40
|
|
-// S = 0; No shift
|
41
|
|
-//
|
42
|
|
-// Note, however, that resetting the Arduino doesn't reset the LCD, so we
|
43
|
|
-// can't assume that it's in that state when a sketch starts (and the
|
44
|
|
-// LiquidCrystal constructor is called).
|
45
|
|
-//
|
46
|
|
-// modified 27 Jul 2011
|
47
|
|
-// by Ilya V. Danilov http://mk90.ru/
|
48
|
|
-
|
49
|
|
-
|
50
|
|
-LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t rw, uint8_t enable,
|
51
|
|
- uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
52
|
|
- uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
53
|
|
-{
|
54
|
|
- init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
|
55
|
|
-}
|
56
|
|
-
|
57
|
|
-LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t enable,
|
58
|
|
- uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
59
|
|
- uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
60
|
|
-{
|
61
|
|
- init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
|
62
|
|
-}
|
63
|
|
-
|
64
|
|
-LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t rw, uint8_t enable,
|
65
|
|
- uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
|
66
|
|
-{
|
67
|
|
- init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
|
68
|
|
-}
|
69
|
|
-
|
70
|
|
-LiquidCrystalRus::LiquidCrystalRus(uint8_t rs, uint8_t enable,
|
71
|
|
- uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
|
72
|
|
-{
|
73
|
|
- init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
|
74
|
|
-}
|
75
|
|
-
|
76
|
|
-void LiquidCrystalRus::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
|
77
|
|
- uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
|
78
|
|
- uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
|
79
|
|
-{
|
80
|
|
- _rs_pin = rs;
|
81
|
|
- _rw_pin = rw;
|
82
|
|
- _enable_pin = enable;
|
83
|
|
-
|
84
|
|
- _data_pins[0] = d0;
|
85
|
|
- _data_pins[1] = d1;
|
86
|
|
- _data_pins[2] = d2;
|
87
|
|
- _data_pins[3] = d3;
|
88
|
|
- _data_pins[4] = d4;
|
89
|
|
- _data_pins[5] = d5;
|
90
|
|
- _data_pins[6] = d6;
|
91
|
|
- _data_pins[7] = d7;
|
92
|
|
-
|
93
|
|
- pinMode(_rs_pin, OUTPUT);
|
94
|
|
- // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
|
95
|
|
- if (_rw_pin != 255) {
|
96
|
|
- pinMode(_rw_pin, OUTPUT);
|
97
|
|
- }
|
98
|
|
- pinMode(_enable_pin, OUTPUT);
|
99
|
|
-
|
100
|
|
- if (fourbitmode)
|
101
|
|
- _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
|
102
|
|
- else
|
103
|
|
- _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
|
104
|
|
-
|
105
|
|
- begin(16, 1);
|
106
|
|
-}
|
107
|
|
-
|
108
|
|
-void LiquidCrystalRus::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
|
109
|
|
- if (lines > 1) {
|
110
|
|
- _displayfunction |= LCD_2LINE;
|
111
|
|
- }
|
112
|
|
- _numlines = lines;
|
113
|
|
- _currline = 0;
|
114
|
|
-
|
115
|
|
- // for some 1 line displays you can select a 10 pixel high font
|
116
|
|
- if ((dotsize != 0) && (lines == 1)) {
|
117
|
|
- _displayfunction |= LCD_5x10DOTS;
|
118
|
|
- }
|
119
|
|
-
|
120
|
|
- // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
|
121
|
|
- // according to datasheet, we need at least 40ms after power rises above 2.7V
|
122
|
|
- // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50
|
123
|
|
- delayMicroseconds(50000);
|
124
|
|
- // Now we pull both RS and R/W low to begin commands
|
125
|
|
- digitalWrite(_rs_pin, LOW);
|
126
|
|
- digitalWrite(_enable_pin, LOW);
|
127
|
|
- if (_rw_pin != 255) {
|
128
|
|
- digitalWrite(_rw_pin, LOW);
|
129
|
|
- }
|
130
|
|
-
|
131
|
|
- //put the LCD into 4 bit or 8 bit mode
|
132
|
|
- if (! (_displayfunction & LCD_8BITMODE)) {
|
133
|
|
- // this is according to the Hitachi HD44780 datasheet
|
134
|
|
- // figure 24, pg 46
|
135
|
|
-
|
136
|
|
- // we start in 8bit mode, try to set 4 bit mode
|
137
|
|
- writeNbits(0x03,4);
|
138
|
|
- delayMicroseconds(4500); // wait min 4.1ms
|
139
|
|
-
|
140
|
|
- // second try
|
141
|
|
- writeNbits(0x03,4);
|
142
|
|
- delayMicroseconds(4500); // wait min 4.1ms
|
143
|
|
-
|
144
|
|
- // third go!
|
145
|
|
- writeNbits(0x03,4);
|
146
|
|
- delayMicroseconds(150);
|
147
|
|
-
|
148
|
|
- // finally, set to 8-bit interface
|
149
|
|
- writeNbits(0x02,4);
|
150
|
|
- } else {
|
151
|
|
- // this is according to the Hitachi HD44780 datasheet
|
152
|
|
- // page 45 figure 23
|
153
|
|
-
|
154
|
|
- // Send function set command sequence
|
155
|
|
- command(LCD_FUNCTIONSET | _displayfunction);
|
156
|
|
- delayMicroseconds(4500); // wait more than 4.1ms
|
157
|
|
-
|
158
|
|
- // second try
|
159
|
|
- command(LCD_FUNCTIONSET | _displayfunction);
|
160
|
|
- delayMicroseconds(150);
|
161
|
|
-
|
162
|
|
- // third go
|
163
|
|
- command(LCD_FUNCTIONSET | _displayfunction);
|
164
|
|
- }
|
165
|
|
-
|
166
|
|
- // finally, set # lines, font size, etc.
|
167
|
|
- command(LCD_FUNCTIONSET | _displayfunction);
|
168
|
|
-
|
169
|
|
- // turn the display on with no cursor or blinking default
|
170
|
|
- _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
|
171
|
|
- display();
|
172
|
|
-
|
173
|
|
- // clear it off
|
174
|
|
- clear();
|
175
|
|
-
|
176
|
|
- // Initialize to default text direction (for romance languages)
|
177
|
|
- _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
|
178
|
|
- // set the entry mode
|
179
|
|
- command(LCD_ENTRYMODESET | _displaymode);
|
180
|
|
-
|
181
|
|
-}
|
182
|
|
-
|
183
|
|
-void LiquidCrystalRus::setDRAMModel(uint8_t model) {
|
184
|
|
- _dram_model = model;
|
185
|
|
-}
|
186
|
|
-
|
187
|
|
-/********** high level commands, for the user! */
|
188
|
|
-void LiquidCrystalRus::clear()
|
189
|
|
-{
|
190
|
|
- command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
|
191
|
|
- delayMicroseconds(2000); // this command takes a long time!
|
192
|
|
-}
|
193
|
|
-
|
194
|
|
-void LiquidCrystalRus::home()
|
195
|
|
-{
|
196
|
|
- command(LCD_RETURNHOME); // set cursor position to zero
|
197
|
|
- delayMicroseconds(2000); // this command takes a long time!
|
198
|
|
-}
|
199
|
|
-
|
200
|
|
-void LiquidCrystalRus::setCursor(uint8_t col, uint8_t row)
|
201
|
|
-{
|
202
|
|
- int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
|
203
|
|
- if ( row >= _numlines ) {
|
204
|
|
- row = _numlines-1; // we count rows starting w/0
|
205
|
|
- }
|
206
|
|
-
|
207
|
|
- command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
|
208
|
|
-}
|
209
|
|
-
|
210
|
|
-// Turn the display on/off (quickly)
|
211
|
|
-void LiquidCrystalRus::noDisplay() {
|
212
|
|
- _displaycontrol &= ~LCD_DISPLAYON;
|
213
|
|
- command(LCD_DISPLAYCONTROL | _displaycontrol);
|
214
|
|
-}
|
215
|
|
-void LiquidCrystalRus::display() {
|
216
|
|
- _displaycontrol |= LCD_DISPLAYON;
|
217
|
|
- command(LCD_DISPLAYCONTROL | _displaycontrol);
|
218
|
|
-}
|
219
|
|
-
|
220
|
|
-// Turns the underline cursor on/off
|
221
|
|
-void LiquidCrystalRus::noCursor() {
|
222
|
|
- _displaycontrol &= ~LCD_CURSORON;
|
223
|
|
- command(LCD_DISPLAYCONTROL | _displaycontrol);
|
224
|
|
-}
|
225
|
|
-void LiquidCrystalRus::cursor() {
|
226
|
|
- _displaycontrol |= LCD_CURSORON;
|
227
|
|
- command(LCD_DISPLAYCONTROL | _displaycontrol);
|
228
|
|
-}
|
229
|
|
-
|
230
|
|
-// Turn on and off the blinking cursor
|
231
|
|
-void LiquidCrystalRus::noBlink() {
|
232
|
|
- _displaycontrol &= ~LCD_BLINKON;
|
233
|
|
- command(LCD_DISPLAYCONTROL | _displaycontrol);
|
234
|
|
-}
|
235
|
|
-void LiquidCrystalRus::blink() {
|
236
|
|
- _displaycontrol |= LCD_BLINKON;
|
237
|
|
- command(LCD_DISPLAYCONTROL | _displaycontrol);
|
238
|
|
-}
|
239
|
|
-
|
240
|
|
-// These commands scroll the display without changing the RAM
|
241
|
|
-void LiquidCrystalRus::scrollDisplayLeft(void) {
|
242
|
|
- command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
|
243
|
|
-}
|
244
|
|
-void LiquidCrystalRus::scrollDisplayRight(void) {
|
245
|
|
- command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
|
246
|
|
-}
|
247
|
|
-
|
248
|
|
-// This is for text that flows Left to Right
|
249
|
|
-void LiquidCrystalRus::leftToRight(void) {
|
250
|
|
- _displaymode |= LCD_ENTRYLEFT;
|
251
|
|
- command(LCD_ENTRYMODESET | _displaymode);
|
252
|
|
-}
|
253
|
|
-
|
254
|
|
-// This is for text that flows Right to Left
|
255
|
|
-void LiquidCrystalRus::rightToLeft(void) {
|
256
|
|
- _displaymode &= ~LCD_ENTRYLEFT;
|
257
|
|
- command(LCD_ENTRYMODESET | _displaymode);
|
258
|
|
-}
|
259
|
|
-
|
260
|
|
-// This will 'right justify' text from the cursor
|
261
|
|
-void LiquidCrystalRus::autoscroll(void) {
|
262
|
|
- _displaymode |= LCD_ENTRYSHIFTINCREMENT;
|
263
|
|
- command(LCD_ENTRYMODESET | _displaymode);
|
264
|
|
-}
|
265
|
|
-
|
266
|
|
-// This will 'left justify' text from the cursor
|
267
|
|
-void LiquidCrystalRus::noAutoscroll(void) {
|
268
|
|
- _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
|
269
|
|
- command(LCD_ENTRYMODESET | _displaymode);
|
270
|
|
-}
|
271
|
|
-
|
272
|
|
-// Allows us to fill the first 8 CGRAM locations
|
273
|
|
-// with custom characters
|
274
|
|
-void LiquidCrystalRus::createChar(uint8_t location, uint8_t charmap[]) {
|
275
|
|
- location &= 0x7; // we only have 8 locations 0-7
|
276
|
|
- command(LCD_SETCGRAMADDR | (location << 3));
|
277
|
|
- for (int i=0; i<8; i++) {
|
278
|
|
- write(charmap[i]);
|
279
|
|
- }
|
280
|
|
-}
|
281
|
|
-
|
282
|
|
-/*********** mid level commands, for sending data/cmds */
|
283
|
|
-
|
284
|
|
-inline void LiquidCrystalRus::command(uint8_t value) {
|
285
|
|
- send(value, LOW);
|
286
|
|
-}
|
287
|
|
-
|
288
|
|
-#if defined(ARDUINO) && ARDUINO >= 100
|
289
|
|
- size_t LiquidCrystalRus::write(uint8_t value)
|
290
|
|
-#else
|
291
|
|
- void LiquidCrystalRus::write(uint8_t value)
|
292
|
|
-#endif
|
293
|
|
-{
|
294
|
|
- uint8_t out_char=value;
|
295
|
|
-
|
296
|
|
- if (_dram_model == LCD_DRAM_WH1601) {
|
297
|
|
- uint8_t ac=recv(LOW) & 0x7f;
|
298
|
|
- if (ac>7 && ac<0x14) command(LCD_SETDDRAMADDR | (0x40+ac-8));
|
299
|
|
- }
|
300
|
|
-
|
301
|
|
- if (value>=0x80) { // UTF-8 handling
|
302
|
|
- if (value >= 0xc0) {
|
303
|
|
- utf_hi_char = value - 0xd0;
|
304
|
|
- } else {
|
305
|
|
- value &= 0x3f;
|
306
|
|
- if (!utf_hi_char && (value == 1))
|
307
|
|
- send(0xa2,HIGH); // ╗
|
308
|
|
- else if ((utf_hi_char == 1) && (value == 0x11))
|
309
|
|
- send(0xb5,HIGH); // ╦
|
310
|
|
- else
|
311
|
|
- send(pgm_read_byte_near(utf_recode + value + (utf_hi_char<<6) - 0x10), HIGH);
|
312
|
|
- }
|
313
|
|
- } else send(out_char, HIGH);
|
314
|
|
-#if defined(ARDUINO) && ARDUINO >= 100
|
315
|
|
- return 1; // assume success
|
316
|
|
-#endif
|
317
|
|
-}
|
318
|
|
-
|
319
|
|
-/************ low level data pushing commands **********/
|
320
|
|
-
|
321
|
|
-// write either command or data, with automatic 4/8-bit selection
|
322
|
|
-void LiquidCrystalRus::send(uint8_t value, uint8_t mode) {
|
323
|
|
- digitalWrite(_rs_pin, mode);
|
324
|
|
-
|
325
|
|
- // if there is a RW pin indicated, set it low to Write
|
326
|
|
- if (_rw_pin != 255) {
|
327
|
|
- digitalWrite(_rw_pin, LOW);
|
328
|
|
- }
|
329
|
|
-
|
330
|
|
- if (_displayfunction & LCD_8BITMODE) {
|
331
|
|
- writeNbits(value,8);
|
332
|
|
- } else {
|
333
|
|
- writeNbits(value>>4,4);
|
334
|
|
- writeNbits(value,4);
|
335
|
|
- }
|
336
|
|
-}
|
337
|
|
-
|
338
|
|
-// read data, with automatic 4/8-bit selection
|
339
|
|
-uint8_t LiquidCrystalRus::recv(uint8_t mode) {
|
340
|
|
- uint8_t retval;
|
341
|
|
- digitalWrite(_rs_pin, mode);
|
342
|
|
-
|
343
|
|
- // if there is a RW pin indicated, set it low to Write
|
344
|
|
- if (_rw_pin != 255) {
|
345
|
|
- digitalWrite(_rw_pin, HIGH);
|
346
|
|
- }
|
347
|
|
-
|
348
|
|
- if (_displayfunction & LCD_8BITMODE) {
|
349
|
|
- retval = readNbits(8);
|
350
|
|
- } else {
|
351
|
|
- retval = readNbits(4) << 4;
|
352
|
|
- retval |= readNbits(4);
|
353
|
|
- }
|
354
|
|
- return retval;
|
355
|
|
-}
|
356
|
|
-void LiquidCrystalRus::pulseEnable() {
|
357
|
|
- digitalWrite(_enable_pin, LOW);
|
358
|
|
- delayMicroseconds(1);
|
359
|
|
- digitalWrite(_enable_pin, HIGH);
|
360
|
|
- delayMicroseconds(1); // enable pulse must be >450ns
|
361
|
|
- digitalWrite(_enable_pin, LOW);
|
362
|
|
- delayMicroseconds(100); // commands need > 37us to settle
|
363
|
|
-}
|
364
|
|
-
|
365
|
|
-void LiquidCrystalRus::writeNbits(uint8_t value, uint8_t n) {
|
366
|
|
- for (int i = 0; i < n; i++) {
|
367
|
|
- pinMode(_data_pins[i], OUTPUT);
|
368
|
|
- digitalWrite(_data_pins[i], (value >> i) & 0x01);
|
369
|
|
- }
|
370
|
|
-
|
371
|
|
- pulseEnable();
|
372
|
|
-}
|
373
|
|
-
|
374
|
|
-uint8_t LiquidCrystalRus::readNbits(uint8_t n) {
|
375
|
|
- uint8_t retval=0;
|
376
|
|
- for (int i = 0; i < n; i++) {
|
377
|
|
- pinMode(_data_pins[i], INPUT);
|
378
|
|
- }
|
379
|
|
-
|
380
|
|
- digitalWrite(_enable_pin, LOW);
|
381
|
|
- delayMicroseconds(1);
|
382
|
|
- digitalWrite(_enable_pin, HIGH);
|
383
|
|
- delayMicroseconds(1); // enable pulse must be >450ns
|
384
|
|
-
|
385
|
|
- for (int i = 0; i < n; i++) {
|
386
|
|
- retval |= (digitalRead(_data_pins[i]) == HIGH)?(1 << i):0;
|
387
|
|
- }
|
388
|
|
-
|
389
|
|
- digitalWrite(_enable_pin, LOW);
|
390
|
|
-
|
391
|
|
- return retval;
|
392
|
|
-}
|
393
|
|
-
|