#define LCD_FLAG_N 0x0040 /* 2-rows mode */
#define LCD_FLAG_L 0x0080 /* backlight enabled */
+/* LCD commands */
+#define LCD_CMD_DISPLAY_CLEAR 0x01 /* Clear entire display */
+
+#define LCD_CMD_ENTRY_MODE 0x04 /* Set entry mode */
+#define LCD_CMD_CURSOR_INC 0x02 /* Increment cursor */
+
+#define LCD_CMD_DISPLAY_CTRL 0x08 /* Display control */
+#define LCD_CMD_DISPLAY_ON 0x04 /* Set display on */
+#define LCD_CMD_CURSOR_ON 0x02 /* Set cursor on */
+#define LCD_CMD_BLINK_ON 0x01 /* Set blink on */
+
+#define LCD_CMD_SHIFT 0x10 /* Shift cursor/display */
+#define LCD_CMD_DISPLAY_SHIFT 0x08 /* Shift display instead of cursor */
+#define LCD_CMD_SHIFT_RIGHT 0x04 /* Shift display/cursor to the right */
+
+#define LCD_CMD_FUNCTION_SET 0x20 /* Set function */
+#define LCD_CMD_DATA_LEN_8BITS 0x10 /* Set data length to 8 bits */
+#define LCD_CMD_TWO_LINES 0x08 /* Set to two display lines */
+#define LCD_CMD_FONT_5X10_DOTS 0x04 /* Set char font to 5x10 dots */
+
+#define LCD_CMD_SET_CGRAM_ADDR 0x40 /* Set char generator RAM address */
+
+#define LCD_CMD_SET_DDRAM_ADDR 0x80 /* Set display data RAM address */
+
#define LCD_ESCAPE_LEN 24 /* max chars for LCD escape command */
#define LCD_ESCAPE_CHAR 27 /* use char 27 for escape command */
static void lcd_gotoxy(void)
{
- lcd_write_cmd(0x80 /* set DDRAM address */
+ lcd_write_cmd(LCD_CMD_SET_DDRAM_ADDR
| (lcd.addr.y ? lcd.hwidth : 0)
/* we force the cursor to stay at the end of the
line if it wants to go farther */
/* clears the display and resets X/Y */
static void lcd_clear_display(void)
{
- lcd_write_cmd(0x01); /* clear display */
+ lcd_write_cmd(LCD_CMD_DISPLAY_CLEAR);
lcd.addr.x = 0;
lcd.addr.y = 0;
/* we must wait a few milliseconds (15) */
long_sleep(20); /* wait 20 ms after power-up for the paranoid */
- lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
+ /* 8bits, 1 line, small fonts; let's do it 3 times */
+ lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
long_sleep(10);
- lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
+ lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
long_sleep(10);
- lcd_write_cmd(0x30); /* 8bits, 1 line, small fonts */
+ lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
long_sleep(10);
- lcd_write_cmd(0x30 /* set font height and lines number */
- | ((lcd.flags & LCD_FLAG_F) ? 4 : 0)
- | ((lcd.flags & LCD_FLAG_N) ? 8 : 0)
+ /* set font height and lines number */
+ lcd_write_cmd(LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS
+ | ((lcd.flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0)
+ | ((lcd.flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0)
);
long_sleep(10);
- lcd_write_cmd(0x08); /* display off, cursor off, blink off */
+ /* display off, cursor off, blink off */
+ lcd_write_cmd(LCD_CMD_DISPLAY_CTRL);
long_sleep(10);
- lcd_write_cmd(0x08 /* set display mode */
- | ((lcd.flags & LCD_FLAG_D) ? 4 : 0)
- | ((lcd.flags & LCD_FLAG_C) ? 2 : 0)
- | ((lcd.flags & LCD_FLAG_B) ? 1 : 0)
+ lcd_write_cmd(LCD_CMD_DISPLAY_CTRL /* set display mode */
+ | ((lcd.flags & LCD_FLAG_D) ? LCD_CMD_DISPLAY_ON : 0)
+ | ((lcd.flags & LCD_FLAG_C) ? LCD_CMD_CURSOR_ON : 0)
+ | ((lcd.flags & LCD_FLAG_B) ? LCD_CMD_BLINK_ON : 0)
);
lcd_backlight((lcd.flags & LCD_FLAG_L) ? 1 : 0);
long_sleep(10);
/* entry mode set : increment, cursor shifting */
- lcd_write_cmd(0x06);
+ lcd_write_cmd(LCD_CMD_ENTRY_MODE | LCD_CMD_CURSOR_INC);
lcd_clear_display();
}
if (lcd.addr.x > 0) {
/* back one char if not at end of line */
if (lcd.addr.x < lcd.bwidth)
- lcd_write_cmd(0x10);
+ lcd_write_cmd(LCD_CMD_SHIFT);
lcd.addr.x--;
}
processed = 1;
case 'r': /* shift cursor right */
if (lcd.addr.x < lcd.width) {
/* allow the cursor to pass the end of the line */
- if (lcd.addr.x <
- (lcd.bwidth - 1))
- lcd_write_cmd(0x14);
+ if (lcd.addr.x < (lcd.bwidth - 1))
+ lcd_write_cmd(LCD_CMD_SHIFT |
+ LCD_CMD_SHIFT_RIGHT);
lcd.addr.x++;
}
processed = 1;
break;
case 'L': /* shift display left */
- lcd_write_cmd(0x18);
+ lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT);
processed = 1;
break;
case 'R': /* shift display right */
- lcd_write_cmd(0x1C);
+ lcd_write_cmd(LCD_CMD_SHIFT | LCD_CMD_DISPLAY_SHIFT |
+ LCD_CMD_SHIFT_RIGHT);
processed = 1;
break;
case 'k': { /* kill end of line */
esc++;
}
- lcd_write_cmd(0x40 | (cgaddr * 8));
+ lcd_write_cmd(LCD_CMD_SET_CGRAM_ADDR | (cgaddr * 8));
for (addr = 0; addr < cgoffset; addr++)
lcd_write_data(cgbytes[addr]);
break;
}
+ /* TODO: This indent party here got ugly, clean it! */
/* Check whether one flag was changed */
if (oldflags != lcd.flags) {
/* check whether one of B,C,D flags were changed */
if ((oldflags ^ lcd.flags) &
(LCD_FLAG_B | LCD_FLAG_C | LCD_FLAG_D))
/* set display mode */
- lcd_write_cmd(0x08
- | ((lcd.flags & LCD_FLAG_D) ? 4 : 0)
- | ((lcd.flags & LCD_FLAG_C) ? 2 : 0)
- | ((lcd.flags & LCD_FLAG_B) ? 1 : 0));
+ lcd_write_cmd(LCD_CMD_DISPLAY_CTRL
+ | ((lcd.flags & LCD_FLAG_D)
+ ? LCD_CMD_DISPLAY_ON : 0)
+ | ((lcd.flags & LCD_FLAG_C)
+ ? LCD_CMD_CURSOR_ON : 0)
+ | ((lcd.flags & LCD_FLAG_B)
+ ? LCD_CMD_BLINK_ON : 0));
/* check whether one of F,N flags was changed */
else if ((oldflags ^ lcd.flags) & (LCD_FLAG_F | LCD_FLAG_N))
- lcd_write_cmd(0x30
- | ((lcd.flags & LCD_FLAG_F) ? 4 : 0)
- | ((lcd.flags & LCD_FLAG_N) ? 8 : 0));
+ lcd_write_cmd(LCD_CMD_FUNCTION_SET
+ | LCD_CMD_DATA_LEN_8BITS
+ | ((lcd.flags & LCD_FLAG_F)
+ ? LCD_CMD_TWO_LINES : 0)
+ | ((lcd.flags & LCD_FLAG_N)
+ ? LCD_CMD_FONT_5X10_DOTS
+ : 0));
/* check whether L flag was changed */
else if ((oldflags ^ lcd.flags) & (LCD_FLAG_L)) {
if (lcd.flags & (LCD_FLAG_L))
end of the line */
if (lcd.addr.x < lcd.bwidth)
/* back one char */
- lcd_write_cmd(0x10);
+ lcd_write_cmd(LCD_CMD_SHIFT);
lcd.addr.x--;
}
/* replace with a space */
lcd_write_data(' ');
/* back one char again */
- lcd_write_cmd(0x10);
+ lcd_write_cmd(LCD_CMD_SHIFT);
break;
case '\014':
/* quickly clear the display */