summaryrefslogtreecommitdiff
path: root/samples/dashboard_4884/LCD4884.cpp
blob: 522826d2ec933fc32f88d9bd7da300d1c2cb87c0 (plain)
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
/*
Modified by Lauren
version 0.3

Any suggestions are welcome.

Editors    : Lauren from DFRobot <Lauran.pan@gmail.com>
             Stanley Huang <stanleyhuangyc@gmail.com>
Date       : Feb. 11, 2012

* Added LCD_putchar for basic console display
* Have the back light under control.
* Update the library and sketch to compatible with IDE V1.0 and earlier

*/

#include "LCD4884.h"
#include "font_6x8.h"
#include "font_big.h"

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WConstants.h"
#endif


extern "C" 
{
#include <avr/pgmspace.h>
#include <avr/io.h>
}

#define NUM_COL 14
#define NUM_ROW 5

LCD4884::LCD4884():prev_char(0),char_mode(MENU_NORMAL)
{};

LCD4884 lcd = LCD4884();

void LCD4884::backlight(unsigned char dat)
{
	if(dat==1)
		digitalWrite(LCD_BL,HIGH);
	else
		digitalWrite(LCD_BL,LOW);
}

void LCD4884::LCD_init(void)
{
	for(unsigned char i = 2; i < 8; i++) {
		pinMode(i,OUTPUT);
		digitalWrite(i,LOW);
	}

	digitalWrite(LCD_RST,LOW);
	delayMicroseconds(1);
	digitalWrite(LCD_RST,HIGH);
	
	digitalWrite(SPI_CS,LOW);
	delayMicroseconds(1);
	digitalWrite(SPI_CS,HIGH);
	delayMicroseconds(1);
	digitalWrite(LCD_BL,HIGH);

	LCD_write_byte(0x21, 0);
	LCD_write_byte(0xc0, 0);
	LCD_write_byte(0x06, 0);
	LCD_write_byte(0x13, 0);
	LCD_write_byte(0x20, 0);
	LCD_clear();
	LCD_write_byte(0x0c, 0);
	
	digitalWrite(SPI_CS,LOW);
}
  
void LCD4884::LCD_write_byte(unsigned char dat, unsigned char dat_type)
{
	digitalWrite(SPI_CS,LOW);
    if (dat_type == 0)
		digitalWrite(LCD_DC,LOW);
    else
		digitalWrite(LCD_DC,HIGH);

	for(unsigned char i=0; i<8; i++) { 
		if(dat & 0x80) {
			digitalWrite(SPI_MOSI,HIGH);
		} else {
			digitalWrite(SPI_MOSI,LOW);
		} 
		digitalWrite(SPI_SCK,LOW);
		dat = dat << 1; 
		digitalWrite(SPI_SCK,HIGH);
	} 
	digitalWrite(SPI_CS,HIGH);
}

void LCD4884::LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,unsigned char *map,
                  unsigned char Pix_x,unsigned char Pix_y)
{
    unsigned int i,n;
    unsigned char row;
    
    if (Pix_y%8==0)
		row=Pix_y/8;
	else
        row=Pix_y/8+1;
    
    for (n=0;n<row;n++) {
		LCD_set_XY(X,Y);
		for(i=0; i<Pix_x; i++) {
			LCD_write_byte(map[i+n*Pix_x], 1);
		}
        Y++;                       
	}      
}

void LCD4884::LCD_write_string(unsigned char X,unsigned char Y,char *s, char mode)
  {
    LCD_set_XY(X,Y);
    while (*s) {
		LCD_write_char(*s, mode);
		s++;
	}
  }

void LCD4884::LCD_write_string(char *s, char mode)
  {
    while (*s) {
		LCD_write_char(*s, mode);
		s++;
	}
  }

void LCD4884::LCD_write_chinese(unsigned char X, unsigned char Y,unsigned char *c,unsigned char ch_with,unsigned char num,unsigned char line,unsigned char row) 
{ 
    LCD_set_XY(X,Y);                             
    for (unsigned char i=0;i<num;) { 
      for (unsigned char n=0; n<ch_with*2; n++) {  
          if (n==ch_with) { 
              if (i==0)
				  LCD_set_XY(X,Y+1);
              else
                  LCD_set_XY((X+(ch_with+row)*i),Y+1); 
          } 
          LCD_write_byte(c[(i*ch_with*2)+n],1); 
        } 
      i++; 
      LCD_set_XY((X+(ch_with+row)*i),Y); 
    } 
}


void LCD4884::LCD_write_string_big ( unsigned char X,unsigned char Y, char *string, char mode )
{
    while ( *string ){
        LCD_write_char_big( X, Y, *string , mode );
        if(*string++ == '.')
          X += 5;
        else
          X += 12;
    }
}

/* write char in big font */ 
void LCD4884::LCD_write_char_big (unsigned char X,unsigned char Y, unsigned char ch, char mode)
{
   unsigned char i, j;
   unsigned char *pFont;
   unsigned char ch_dat;
   
   pFont = (unsigned char *) big_number;
   
   switch (ch) {
   case '.':
    ch = 10;
	break;
   case '+':
    ch = 11;
	break;
   case '-':
    ch = 12;
	break;
   case ' ':
	   for(i=0;i<3;i++) {	
		 LCD_set_XY ( X, Y+i);
		 for(j=0; j<16; j++) {
		   LCD_write_byte( (mode == MENU_NORMAL)? 0 : 0xff, 1);
		 }
	   }
	   return;
	break;
   default:
    ch = ch & 0x0f;
   }
	
   for(i=0;i<3;i++) {	
	 LCD_set_XY ( X, Y+i);
     for(j=0; j<16; j++){
       ch_dat =  pgm_read_byte(pFont+ch*48 + i*16 +j);
       LCD_write_byte( (mode == MENU_NORMAL)? ch_dat : (ch_dat^0xff), 1);
     }
   } 
   
  
}
  
void LCD4884::LCD_write_char(unsigned char c, char mode)
{
    unsigned char line;
    unsigned char *pFont;
    byte ch;
    
    pFont = (unsigned char *)font6_8;
    c -= 32;

    for (line=0; line<6; line++) {
		ch = pgm_read_byte(pFont+c*6+line);
		LCD_write_byte( (mode==MENU_NORMAL)? ch: (ch^ 0xff) , 1);
    }
	 x = (x + 1) % NUM_COL;
}

  
void LCD4884::LCD_set_XY(unsigned char X, unsigned char Y)
  {
    LCD_write_byte(0x40 | Y, 0);		// column
    LCD_write_byte(0x80 | X, 0);         // row
	x = X;
  }


void LCD4884::LCD_clear(void)
  {
    unsigned int i;

    LCD_write_byte(0x0c, 0);			
    LCD_write_byte(0x80, 0);			

    for (i=0; i<504; i++)
    LCD_write_byte(0, 1);

	x = 0;
  }

void LCD4884::LCD_write_title(char* title)
{
	LCD_set_XY(0, 0);
	for (char n = 0; n < NUM_COL; n++)
       LCD_write_char(' ', MENU_HIGHLIGHT);
	LCD_write_string((NUM_COL - strlen(title)) * 3, 0, title, MENU_HIGHLIGHT);
	LCD_set_XY(0, 1);
}

void LCD4884::LCD_putchar(char c)
{
	if (prev_char == 27) {
		switch (c) {
		case '0':
			LCD_clear();
			break;
		case '1':
			LCD_set_XY(0, 0);
			break;
		case '2':
			char_mode = MENU_NORMAL;
			break;
		case '3':
			char_mode = MENU_HIGHLIGHT;
			break;
		case '4':
			backlight(ON);
			break;
		case '5':
			backlight(OFF);
			break;
		}
	} else {
		switch (c) {
		case '\r':
			for (char n = x; n < NUM_COL; n++) {
				LCD_write_char(' ', char_mode);
			}
			break;
		case '\n':
			break;
		default:
			LCD_write_char(c, char_mode);
		}
	}
	prev_char = c;
}