COSA
An Object-Oriented Platform for Arduino Programming
HD44780.cpp
Go to the documentation of this file.
1 
21 #include "HD44780.hh"
22 
23 // DDR offset table
24 // 0: 40X2, 20X4, 20X2, 16X2, 16X1
25 // 1: 16X4
26 const uint8_t HD44780::offset0[] __PROGMEM = { 0x00, 0x40, 0x14, 0x54 };
27 const uint8_t HD44780::offset1[] __PROGMEM = { 0x00, 0x40, 0x10, 0x50 };
28 
29 bool
31 {
32  // Initiate display; See fig. 24, 4-bit interface, pp. 46.
33  // http://web.alfredstate.edu/weimandn/lcd/lcd_initialization/-
34  // LCD%204-bit%20Initialization%20v06.pdf
35  const uint8_t FS0 = (FUNCTION_SET | DATA_LENGTH_8BITS);
36  const uint8_t FS1 = (FUNCTION_SET | DATA_LENGTH_4BITS);
37  bool mode = m_io->setup();
39  if (!mode) {
40  m_io->write4b(FS0 >> 4);
42  m_io->write4b(FS0 >> 4);
44  m_io->write4b(FS0 >> 4);
46  m_io->write4b(FS1 >> 4);
48  }
49  // 8-bit initialization
50  else {
52  }
53 
54  // Initialization with the function, control and mode setting
55  write(m_func);
56  write(m_cntl);
57  display_clear();
58  write(m_mode);
59 
60  // Initialization completed. Turn on the display and backlight
61  display_on();
62  backlight_on();
63  return (true);
64 }
65 
66 bool
68 {
69  display_off();
70  return (true);
71 }
72 
73 void
75 {
76  m_io->set_backlight(1);
77 }
78 
79 void
81 {
82  m_io->set_backlight(0);
83 }
84 
85 void
87 {
88  set(m_cntl, DISPLAY_ON);
89 }
90 
91 void
93 {
95 }
96 
97 void
99 {
101  m_x = 0;
102  m_y = 0;
103  m_mode |= INCREMENT;
105 }
106 
107 void
109 {
111  m_x = 0;
112  m_y = 0;
114 }
115 
116 void
117 HD44780::set_cursor(uint8_t x, uint8_t y)
118 {
119  if (x >= WIDTH) x = 0;
120  if (y >= HEIGHT) y = 0;
121  uint8_t offset = (uint8_t) pgm_read_byte(&m_offset[y]);
122  write(SET_DDRAM_ADDR | ((x + offset) & SET_DDRAM_MASK));
123  m_x = x;
124  m_y = y;
125 }
126 
127 void
128 HD44780::set_custom_char(uint8_t id, const uint8_t* bitmap)
129 {
130  write(SET_CGRAM_ADDR | ((id << 3) & SET_CGRAM_MASK));
131  set_data_mode();
132  {
133  for (uint8_t i = 0; i < BITMAP_MAX; i++)
134  write(*bitmap++);
135  }
137 }
138 
139 void
140 HD44780::set_custom_char_P(uint8_t id, const uint8_t* bitmap)
141 {
142  write(SET_CGRAM_ADDR | ((id << 3) & SET_CGRAM_MASK));
143  set_data_mode();
144  {
145  for (uint8_t i = 0; i < BITMAP_MAX; i++, bitmap++)
146  write(pgm_read_byte(bitmap));
147  }
149 }
150 
151 int
153 {
154  // Check for special characters
155  if (c < ' ') {
156 
157  // Carriage-return: move to start of line
158  if (c == '\r') {
159  set_cursor(0, m_y);
160  return (c);
161  }
162 
163  // New-line: clear line
164  if (c == '\n') {
165  uint8_t x, y;
166  set_cursor(0, m_y + 1);
167  get_cursor(x, y);
168  set_data_mode();
169  {
170  for (uint8_t i = 0; i < WIDTH; i++) write(' ');
171  }
173  set_cursor(x, y);
174  return (c);
175  }
176 
177  // Horizontal tab
178  if (c == '\t') {
179  uint8_t x = m_x + m_tab - (m_x % m_tab);
180  uint8_t y = m_y + (x >= WIDTH);
181  set_cursor(x, y);
182  return (c);
183  }
184 
185  // Form-feed: clear the display
186  if (c == '\f') {
187  display_clear();
188  return (c);
189  }
190 
191  // Back-space: move cursor back one step (if possible)
192  if (c == '\b') {
193  set_cursor(m_x - 1, m_y);
194  return (c);
195  }
196 
197  // Alert: blink the backlight
198  if (c == '\a') {
199  backlight_off();
200  delay(32);
201  backlight_on();
202  return (c);
203  }
204  }
205 
206  // Write character
207  if (m_x == WIDTH) putchar('\n');
208  m_x += 1;
209  set_data_mode();
210  {
211  write(c);
212  }
214 
215  return (c & 0xff);
216 }
217 
218 int
219 HD44780::write(const void* buf, size_t size)
220 {
221  set_data_mode();
222  {
223  m_io->write8n(buf, size);
224  }
226  m_x += size;
227  return (size);
228 }
229 
virtual void display_clear()
Definition: HD44780.cpp:98
uint8_t m_y
Cursor position y.
Definition: LCD.hh:181
uint8_t m_x
Cursor position x.
Definition: LCD.hh:180
virtual void set_cursor(uint8_t x, uint8_t y)
Definition: HD44780.cpp:117
void set_instruction_mode()
Definition: HD44780.hh:959
virtual void backlight_on()
Definition: HD44780.cpp:74
const uint8_t HD44780::offset0[] __PROGMEM
Definition: HD44780.cpp:26
void clear(uint8_t &cmd, uint8_t mask)
Definition: HD44780.hh:941
virtual bool end()
Definition: HD44780.cpp:67
void set_custom_char(uint8_t id, const uint8_t *bitmap)
Definition: HD44780.cpp:128
Sets DDRAM address.
Definition: HD44780.hh:852
static const uint16_t LONG_EXEC_TIME
Definition: HD44780.hh:835
virtual int putchar(char c)
Definition: HD44780.cpp:152
virtual void display_off()
Definition: HD44780.cpp:92
Increment (right) on write.
Definition: HD44780.hh:867
virtual void write8n(const void *buf, size_t size)
Definition: HD44780_IO.cpp:31
static const uint16_t INIT1_TIME
Definition: HD44780.hh:838
static const uint8_t offset0[]
Definition: HD44780.hh:905
IO * m_io
IO port handler.
Definition: HD44780.hh:909
#define DELAY(us)
Definition: Types.h:280
The display is on.
Definition: HD44780.hh:877
uint8_t m_tab
Tab step.
Definition: LCD.hh:182
uint8_t m_cntl
Control.
Definition: HD44780.hh:911
uint8_t m_mode
Entry mode.
Definition: HD44780.hh:910
static const uint16_t POWER_ON_TIME
Definition: HD44780.hh:836
Sets the interface data length, 4-bit or.
Definition: HD44780.hh:894
void(* delay)(uint32_t ms)
void get_cursor(uint8_t &x, uint8_t &y) const
Definition: LCD.hh:133
void set_data_mode()
Definition: HD44780.hh:950
const uint8_t * m_offset
Row offset table.
Definition: HD44780.hh:913
const uint8_t HEIGHT
Definition: HD44780.hh:99
virtual void display_on()
Definition: HD44780.cpp:86
static const uint8_t offset1[]
Definition: HD44780.hh:906
Clears entrire display and return home.
Definition: HD44780.hh:844
virtual int write(const void *buf, size_t size)
Definition: HD44780.cpp:219
virtual bool begin()
Definition: HD44780.cpp:30
static const uint16_t INIT0_TIME
Definition: HD44780.hh:837
Sets interface data length, line and font.
Definition: HD44780.hh:849
virtual void write4b(uint8_t data)=0
uint8_t m_func
Function set.
Definition: HD44780.hh:912
void cursor_home()
Definition: HD44780.cpp:108
virtual void backlight_off()
Definition: HD44780.cpp:80
virtual bool setup()=0
Sets CGRAM address.
Definition: HD44780.hh:850
static const uint8_t BITMAP_MAX
Definition: HD44780.hh:93
virtual void set_backlight(uint8_t flag)=0
void set_custom_char_P(uint8_t id, const uint8_t *bitmap)
Definition: HD44780.cpp:140
Sets DDRAM 0 in address counter.
Definition: HD44780.hh:845
const uint8_t WIDTH
Definition: HD44780.hh:96