summaryrefslogtreecommitdiffstats
path: root/app/lcd.c
blob: 911b798a2e6728f189df5741fa2cce54c639f21c (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
338
339
340
341
342
343
344
345
346
347
#include "project.h"

#ifndef SLIM

#define FOO do { printf("lcd:%x\r\n",__LINE__); usart1_drain(); } while (0)

#define PCF8574_I2C_ADDRESS 0x27

#define LINE_RS		0x1
#define LINE_RnW	0x2
#define LINE_EN		0x4
#define LINE_BACKLIGHT	0x8

#define LCD_CLEAR	0x1
#define LCD_HOME	0x2

#define LCD_DISP 	0x8
#define LCD_DISP_ON 	0x4
#define LCD_DISP_CURSOR 0x2
#define LCD_DISP_CURSOR_BLINK 0x1

#define LCD_FUNC        0x20
#define LCD_FUNC_8BIT   0x10
#define LCD_FUNC_4BIT	0x00
#define LCD_FUNC_2ROWS	0x08
#define LCD_FUNC_1ROW 	0x00
#define LCD_FUNC_5x10 	0x04
#define LCD_FUNC_5X7	0x00

#define LCD_SET_DDRAM_ADDR 0x80

#define LCD_H_SHIFT 5
#define LCD_RS (1 << LCD_H_SHIFT)
#define LCD_W 16
#define LCD_W_MASK (LCD_RS -1 )
#define LCD_H 2
#define LCD_SZ (LCD_H << LCD_H_SHIFT)
#define LCD_POS(c,r) (((r) << LCD_H_SHIFT ) +(c))

#define BYTES_PER_BYTE 6

#define DMA_BUF_SZ (BYTES_PER_BYTE * ( (1 + LCD_W) *LCD_H  + 1))


static int backlight;
uint8_t fb[LCD_H][LCD_W];
uint8_t shadow[LCD_H][LCD_W];

static int pos;


static void
clock_nibble (uint8_t n)
{
  if (backlight)
    n |= LINE_BACKLIGHT;
  i2c_bb_send_data (n);
  i2c_bb_send_data (LINE_EN | n);
  i2c_bb_send_data (n);
}


static void
write_reg (uint8_t c, int r)
{
  uint8_t b;

  b = c & 0xf0;
  if (r)
    b |= LINE_RS;

  clock_nibble (b);

  b = (c & 0xf) << 4;
  if (r)
    b |= LINE_RS;

  clock_nibble (b);
}

static void
send_data (uint8_t c)
{
  write_reg (c, 1);
}


static void
send_command (uint8_t c)
{
  write_reg (c, 0);
}

static void
send_one_command (uint8_t cmd, int delay)
{
  i2c_bb_start_transaction (PCF8574_I2C_ADDRESS, I2C_WRITE);
  send_command (cmd);
  i2c_bb_stop ();
  if (delay)
    delay_ms (delay);
}



static void
cls (void)
{
  send_one_command (LCD_CLEAR, 2);
}

#if 0
static void
home (void)
{
  send_one_command (LCD_HOME, 2);
}
#endif

static void
on (void)
{
  send_one_command (LCD_DISP | LCD_DISP_ON /* | LCD_DISP_CURSOR */ , 4);
}

static void
off (void)
{
  send_one_command (LCD_DISP, 4);
}

static int
lcd_addr (int x, int y)
{
  return x + ((y & 1) << 6) + ((y & 2) ? 20 : 0);
}


#if 0
static void
move (uint8_t c, uint8_t r)
{
  send_one_command (LCD_SET_DDRAM_ADDR | lcd_addr (c, r), 2);
}
#endif


void
lcd_refresh (void)
{
  int c, r;
  int addr;


  i2c_bb_start_transaction (PCF8574_I2C_ADDRESS, I2C_WRITE);


  for (r = 0; r < LCD_H; ++r)
    {
      for (c = 0; c < LCD_W; ++c)
        {

          if (shadow[r][c] != fb[r][c])
            {

              addr = lcd_addr (c, r);

              if (addr != pos)
                {
                  send_command (LCD_SET_DDRAM_ADDR | addr);
                  pos = addr;
                }

              send_data (shadow[r][c]);
              fb[r][c] = shadow[r][c];

              pos++;
            }

        }
    }

  i2c_bb_stop ();
}

void
lcd_tick (void)
{
  static int u;

  u++;

  if (u < 100)
    return;

  u = 0;

  if (!memcmp (shadow, fb, sizeof (fb)))
    return;

  lcd_refresh ();
}






void
lcd_write_char (uint8_t c, int x, int y)
{
  shadow[y][x] = c;
}


void
lcd_erase (int x, int y, int w)
{
  uint8_t *ptr = &shadow[y][x];
  while (w--)
    *ptr = ' ';
}

void
lcd_erase_line (int w, int y)
{
  lcd_erase (0, y, w);
}

void
lcd_erase_all (void)
{
  int y;
  for (y = 0; y < LCD_H; ++y)
    {
      lcd_erase (0, 0, LCD_W);
    }
}

void
lcd_write (char *c, int x, int y)
{
  while (*c)
    {
      lcd_write_char (*(c++), x++, y);
      if (x == LCD_W)
        break;
    }
}

#if 0
static void
lcd_scroll (uint8_t * p)
{
  int i;
  for (i = 0; i < 3; ++i)
    {
      memcpy (p, p + LCD_RS, LCD_W);
      p += LCD_RS;
    }
  memset (p, ' ', LCD_W);
}

void
lcd_putc (uint8_t c)
{

  switch (c)
    {
    case '\r':
      pos &= ~LCD_W_MASK;
      break;
    case '\n':
      pos &= ~LCD_W_MASK;
      pos += LCD_RS;
      break;
    default:
      buf[pos] = c;
      pos++;
    }


  if ((pos & LCD_W_MASK) == LCD_W)
    {
      pos &= ~LCD_W_MASK;
      pos += LCD_RS;
    }

  if (pos == LCD_SZ)
    {
      lcd_scroll (buf);
      pos -= LCD_RS;
    }


}
#endif

void
lcd_backlight (int i)
{
  backlight = i;

  i2c_bb_start_transaction (PCF8574_I2C_ADDRESS, I2C_WRITE);
  i2c_bb_send_data (backlight ? LINE_BACKLIGHT : 0);
  i2c_bb_stop ();
}






void
lcd_reset (void)
{
  i2c_bb_start_transaction (PCF8574_I2C_ADDRESS, I2C_WRITE);
  clock_nibble (0x30);
  delay_ms (5);
  clock_nibble (0x30);
  delay_us (64);
  clock_nibble (0x30);
  delay_us (64);
  clock_nibble (0x20);

  send_command (LCD_FUNC | LCD_FUNC_4BIT | LCD_FUNC_2ROWS | LCD_FUNC_5X7);
  i2c_bb_stop ();
  on ();
  cls ();
}

void
lcd_init (void)
{
  lcd_backlight (0);
  lcd_reset ();
  lcd_backlight (1);

}

void
lcd_shutdown (void)
{
  lcd_backlight (0);
  off ();
}
#endif