summaryrefslogtreecommitdiff
path: root/n/lcd/src/lcd.c
blob: b81dcb08438ccf6f9487681622d5db7e29772861 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* lcd.c */
/* lcd - affiche des messages sur l'�cran lcd. {{{
 *
 * Copyright (C) 2006 Christophe Le Blanc
 *
 * Robot APB Team/Efrei 2006.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "modules/twi/twi.h"
#include "modules/utils/utils.h"
#include "modules/uart/uart.h"
#include "modules/proto/proto.h"
#include "io.h"

#include <string.h>

void lcd_send_command (u8 cmd);

char key_read (void);
/* Envois une commande transit **/
void
lcd_transit_command (u8 cmd)
{
    lcd_send_command(cmd>>4);
    lcd_send_command(cmd);
}

/** Envois une commande. */
void
lcd_send_command (u8 cmd)
{
    PORTC = (PORTC & 0xf0) | (cmd & 0x0f);
    // Pas de RS car on n'�crit pas sur le lcd, c'est une commande !
    PORTD = PORTD | 0x80; // puis E � 1
    PORTD = PORTD & 0x7f; //
    utils_delay_us (40);
}

/* Envois un caract�re. */
void
lcd_send_character (u8 c)
{
    PORTC = (PORTC & 0xf0) | (c>>4); // c'est �a !
    PORTD = (PORTD | 0x40); // 1 � RS
    PORTD = PORTD | 0x80; // puis 1 � E
    PORTD = PORTD & 0x7f; // puis on remet 0 � E
    utils_delay_us (40);


    PORTC = (PORTC & 0xf0) | (c & 0xf); // c'est �a !
    PORTD = PORTD | 0x80; // puis 1 � E
    PORTD = PORTD & 0x7f; // puis on remet 0 � E
    PORTD = (PORTD & 0xBF); // 0 � RS
    utils_delay_us (40);
}

/* Envois une cha�ne de caract�re. */
void
lcd_send_string_n (const char *s, u8 i)
{
    u8 cpt ;
    lcd_transit_command(0x01); // suffit d'envoyer les bits de gauche � droite : D7D6D5D4 en hexa
    utils_delay_ms(2);
    lcd_transit_command(0x02);
    utils_delay_ms(2);
    for (cpt = 0 ; cpt < i ; cpt++)
      {
	if (cpt == 16)
	  {
	    lcd_transit_command(0xc0);
	    utils_delay_us(40);
	  }
	lcd_send_character(s[cpt]);
      }
}

//lcd_send_line(const char *s,u8 i,u8 l)
//{
//    u8 cpt ;
//    lcd_transit_command(0x01); // suffit d'envoyer les bits de gauche � droite : D7D6D5D4 en hexa
//    utils_delay_ms(2);
//    if ( l == 0 )
//        lcd_transit_command(0x02);
//    else
//	lcd_transit_command(0xc0);
//    utils_delay_ms(2);
//    for (cpt = 0 ; cpt < 16  && cpt < i ; cpt++)
//      {
//	lcd_send_character(s[cpt]);
//      }
//
//}

/* Envois une cha�ne de caract�re. */
void
lcd_send_string (const char *s)
{
    lcd_send_string_n (s, strlen (s));
}

/* Initialisation du lcd.  */
void 
lcd_init (void)
{
    DDRC = 0x0f;
    DDRD = 0xC0;
    // PD0 RX ( uart )
    // PD1 TX ( uart )
    // PD2 - PD5 clavier
    // PD6 RS ( lcd )
    // PD7 E  ( lcd )
    PORTD = 0x3C; // pull up
    DDRB = 0x01;
    utils_delay_ms (15); // suffit d'envoyer les bits de gauche � droite : D7D6D5D4 en hexa
    lcd_transit_command (0x01); // efface ecran
    lcd_transit_command (0x01);
    lcd_transit_command (0x13); // force 8 bits
    utils_delay_ms (4.5);
    lcd_transit_command (0x33);
    utils_delay_ms (1);
    lcd_transit_command (0x22); // passage mode 4 bits // def afficheur
    lcd_transit_command (0x80);
    utils_delay_ms (2); // affichage fonction
    lcd_transit_command (0xc0); // d�placement curseur
    lcd_transit_command (0x60); // efface ecran
    lcd_transit_command (0x10);
}

void
lcd_clear(void)
{
    lcd_send_command (0x0);
    lcd_send_command (0x1);
    utils_delay_ms (20);
}



char
key_get (char old_key)
{
    char new_key = key_read ();
    if (old_key == 0)
      {
	old_key = new_key;
	
	if (old_key != 0)
	    proto_send1b ('R', new_key);
	
      }
    else if (new_key == 0)
      {
	char i = 0;
	while ( key_read () == 0 && i < 3 )
	  {
	    utils_delay_ms(100); 
	    if ( i == 2 )
	        old_key = 0;
	    i++;
	  }
      }
    else if ( old_key != new_key )
      {
 	proto_send1b ('R', new_key);
	old_key = new_key;
      }
    return old_key;
}

char
key_read (void)
{
    char key = 0;
    PORTC = (PORTC & 0xf0) | 0x0f;
    uint8_t i = 0;
     while ( key == 0 && i < 4 )
      {
	switch (i)
	  {
	  case 0 : PORTC = (PORTC & 0xf0) | 0x0E;
		   break;
	  case 1 : PORTC = (PORTC & 0xf0) | 0x0D;
		   break;
	  case 2 : PORTC = (PORTC & 0xf0) | 0x0B;
		   break;
	  case 3 : PORTC = (PORTC & 0xf0) | 0x07;
		   break;
	  }
	uint8_t key_tmp = PIND;
	if ( ~(key_tmp & 0x3c) & 0x04  )
    switch (i)
	      {
	      case 0 : key = '1';
		       break;
	      case 1 : key = '2';
		       break;
	      case 2 : key = '3';
		       break;
	      case 3 : key = 'F';
		       break;
	      }
	else if ( ~(key_tmp & 0x3c) & 0x08)
	    switch (i)
	      {
	      case 0 : key = '4';
		       break;
	      case 1 : key = '5';
		       break;
	      case 2 : key = '6';
		       break;
	      case 3 : key = 'E';
		       break;
	      }
	else if ( ~(key_tmp & 0x3c) & 0x10)
	    switch (i)
	      {
	      case 0 : key = '7';
		       break;
	      case 1 : key = '8';
		       break;
	      case 2 : key = '9';
		       break;
	      case 3 : key = 'D';
		       break;
	      }
	else if ( ~(key_tmp & 0x3c) & 0x20)
	    switch (i)
	      {
	      case 0 : key = 'A';
		       break;
	      case 1 : key = '0';
		       break;
	      case 2 : key = 'B';
		       break;
	      case 3 : key = 'C';
		       break;
	      }
	i++;
    }
    PORTC = (PORTC & 0xf0);
    return key;
}


void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
    switch (cmd)
      {
      // reset
      case 'z':
	if (size != 0)
	  {
	    proto_send0 ('?');
	    return;
	  }
	else
	  {
	    utils_reset ();
	  }
	break;
      // affiche un message 32 caract�re
      case 'p':
	lcd_send_string_n (args, size);
	break;
      // Efface l'�cran
      case 'C':
	if (size != 0)
	  {
	    proto_send0 ('?');
	    return;
	  }
	else
	  {
	    lcd_clear ();
	  }
	break;
      // Lit une touche au clavier (bloquant)
      // revoit un caract�re {1234567890ABCDEF}
      case 'R':
	proto_send1b ('R',key_read ());
	break;
      default:
	/* This is to handle default commands, return an error. */
	proto_send0 ('?');
	return;
      }
    /* When no error acknoledge. */
    proto_send (cmd, size, args);
}

/* Fonction principale */
int
main (void)
{
    char old_key = 0;
    uint8_t data_rcpt[TWI_SL_RCPT_SIZE];
    sei ();
    uart0_init ();
    lcd_init ();
    twi_init(0x02);
    proto_send0 ('z');
    lcd_send_string (" LCD initialise                 ");
    while (1)
      {
	if (uart0_poll ())
	    proto_accept (uart0_getc ());
	utils_delay_ms(3);
	old_key = key_get (old_key);

	if (twi_sl_poll (data_rcpt, TWI_SL_RCPT_SIZE))
	  {
	    lcd_send_string_n(data_rcpt,32);
	  }

	twi_sl_update (&old_key, TWI_SL_SEND_SIZE);
	
      }
    return 0;
}