COSA
An Object-Oriented Platform for Arduino Programming
HD44780.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_HD44780_HH
22 #define COSA_HD44780_HH
23 
24 #include "Cosa/SPI.hh"
25 #include "Cosa/LCD.hh"
26 #include "Cosa/OutputPin.hh"
27 
37 class HD44780 : public LCD::Device {
38 public:
44  class IO {
45  public:
52  virtual bool setup() = 0;
53 
59  virtual void write4b(uint8_t data) = 0;
60 
66  virtual void write8b(uint8_t data);
67 
74  virtual void write8n(const void* buf, size_t size);
75 
82  virtual void set_mode(uint8_t flag) = 0;
83 
89  virtual void set_backlight(uint8_t flag) = 0;
90  };
91 
93  static const uint8_t BITMAP_MAX = 8;
94 
96  const uint8_t WIDTH;
97 
99  const uint8_t HEIGHT;
100 
108  HD44780(IO* io, uint8_t width = 16, uint8_t height = 2) :
109  LCD::Device(),
110  WIDTH(width),
111  HEIGHT(height),
112  m_io(io),
116  m_offset((height == 4) && (width == 16) ? offset1 : offset0)
117  {}
118 
125  virtual bool begin();
126 
132  virtual bool end();
133 
138  virtual void backlight_on();
139 
144  virtual void backlight_off();
145 
150  virtual void display_on();
151 
156  virtual void display_off();
157 
162  __attribute__((always_inline))
163  {
165  }
166 
171  __attribute__((always_inline))
172  {
174  }
175 
180  virtual void display_clear();
181 
185  void line_clear()
186  {
187  while (m_x < WIDTH) putchar(' ');
188  }
189 
193  void cursor_home();
194 
199  __attribute__((always_inline))
200  {
201  set(m_cntl, CURSOR_ON);
202  }
203 
208  __attribute__((always_inline))
209  {
211  }
212 
217  __attribute__((always_inline))
218  {
219  set(m_cntl, BLINK_ON);
220  }
221 
226  __attribute__((always_inline))
227  {
229  }
230 
237  virtual void set_cursor(uint8_t x, uint8_t y);
238 
243  __attribute__((always_inline))
244  {
245  set(m_mode, INCREMENT);
246  }
247 
252  __attribute__((always_inline))
253  {
255  }
256 
261  __attribute__((always_inline))
262  {
263  set(m_mode, DISPLAY_SHIFT);
264  }
265 
270  __attribute__((always_inline))
271  {
273  }
274 
280  void set_custom_char(uint8_t id, const uint8_t* bitmap);
281 
288  void set_custom_char_P(uint8_t id, const uint8_t* bitmap);
289 
298  virtual int putchar(char c);
299 
302 
310  virtual int write(const void* buf, size_t size);
311 
312 #if !defined(BOARD_ATTINYX5)
313 
350  class Port4b : public IO {
351  public:
371  m_d0(d0, 0),
372  m_d1(d1, 0),
373  m_d2(d2, 0),
374  m_d3(d3, 0),
375  m_rs(rs, 0),
376  m_en(en, 0),
377  m_bt(bt, 1)
378  {}
379 
385  virtual bool setup();
386 
392  virtual void write4b(uint8_t data);
393 
399  virtual void write8b(uint8_t data);
400 
407  virtual void set_mode(uint8_t flag);
408 
414  virtual void set_backlight(uint8_t flag);
415 
416  protected:
418  static const uint16_t SHORT_EXEC_TIME = 32;
419 
427  };
428 #endif
429 
483  class SR3W : public IO {
484  public:
492 #if !defined(BOARD_ATTINY)
496 #else
500 #endif
501 
507  virtual bool setup();
508 
514  virtual void write4b(uint8_t data);
515 
521  virtual void write8b(uint8_t data);
522 
529  virtual void set_mode(uint8_t flag);
530 
536  virtual void set_backlight(uint8_t flag);
537 
538  protected:
540  static const uint16_t SHORT_EXEC_TIME = 20;
541 
543  union port_t {
544  uint8_t as_uint8;
545  struct {
546  uint8_t data:4;
547  uint8_t rs:1;
548  uint8_t bt:1;
549  uint8_t app2:1;
550  uint8_t app1:1;
551  };
552  operator uint8_t()
553  {
554  return (as_uint8);
555  }
557  {
558  as_uint8 = 0;
559  }
560  };
565  };
566 
618  class SR3WSPI : public IO, public SPI::Driver {
619  public:
625 #if !defined(BOARD_ATTINY)
627 #else
629 #endif
630 
636  virtual bool setup();
637 
643  virtual void write4b(uint8_t data);
644 
650  virtual void write8b(uint8_t data);
651 
658  virtual void set_mode(uint8_t flag);
659 
665  virtual void set_backlight(uint8_t flag);
666 
667  protected:
669 #if !defined(BOARD_ATTINY)
670  static const uint16_t SHORT_EXEC_TIME = 24;
671 #else
672  static const uint16_t SHORT_EXEC_TIME = 8;
673 #endif
674 
676  union port_t {
677  uint8_t as_uint8;
678  struct {
679  uint8_t data:4;
680  uint8_t rs:1;
681  uint8_t bt:1;
682  uint8_t app2:1;
683  uint8_t app1:1;
684  };
685  operator uint8_t()
686  {
687  return (as_uint8);
688  }
690  {
691  as_uint8 = 0;
692  }
693  };
695  };
696 
763  class SR4W : public IO {
764  public:
773 #if !defined(BOARD_ATTINY)
778 #else
783 #endif
784 
790  virtual bool setup();
791 
797  virtual void write4b(uint8_t data);
798 
804  virtual void write8b(uint8_t data);
805 
812  virtual void set_mode(uint8_t flag);
813 
819  virtual void set_backlight(uint8_t flag);
820 
821  protected:
823  static const uint16_t SHORT_EXEC_TIME = 16;
824 
829  uint8_t m_rs;
830  };
831 
835  static const uint16_t LONG_EXEC_TIME = 1600;
836  static const uint16_t POWER_ON_TIME = 48;
837  static const uint16_t INIT0_TIME = 4500;
838  static const uint16_t INIT1_TIME = 150;
839 
843  enum {
844  CLEAR_DISPLAY = 0x01,
845  RETURN_HOME = 0x02,
846  ENTRY_MODE_SET = 0x04,
847  CONTROL_SET = 0x08,
848  SHIFT_SET = 0x10,
849  FUNCTION_SET = 0x20,
850  SET_CGRAM_ADDR = 0x40,
851  SET_CGRAM_MASK = 0x3f,
852  SET_DDRAM_ADDR = 0x80,
853  SET_DDRAM_MASK = 0x7f,
856  COM_SEG_SET = 0x40,
857  COM_SET_MASK = 0x0f,
860  } __attribute__((packed));
861 
865  enum {
866  DISPLAY_SHIFT = 0x01,
867  INCREMENT = 0x02,
868  DECREMENT = 0x00
869  } __attribute__((packed));
870 
874  enum {
875  BLINK_ON = 0x01,
876  CURSOR_ON = 0x02,
877  DISPLAY_ON = 0x04,
878  } __attribute__((packed));
879 
883  enum {
884  MOVE_LEFT = 0x00,
885  MOVE_RIGHT = 0x04,
886  CURSOR_MODE = 0x00,
888  } __attribute__((packed));
889 
893  enum {
896  NR_LINES_1 = 0x00,
897  NR_LINES_2 = 0x08,
898  FONT_5X8DOTS = 0x00,
899  FONT_5X10DOTS = 0x04,
900  BASIC_SET = 0x00,
901  EXTENDED_SET = 0x04
902  } __attribute__((packed));
903 
905  static const uint8_t offset0[] PROGMEM;
906  static const uint8_t offset1[] PROGMEM;
907 
909  IO* m_io;
910  uint8_t m_mode;
911  uint8_t m_cntl;
912  uint8_t m_func;
913  const uint8_t* m_offset;
914 
919  void write(uint8_t data)
920  __attribute__((always_inline))
921  {
922  m_io->write8b(data);
923  }
924 
930  void set(uint8_t& cmd, uint8_t mask)
931  __attribute__((always_inline))
932  {
933  m_io->write8b(cmd |= mask);
934  }
935 
941  void clear(uint8_t& cmd, uint8_t mask)
942  __attribute__((always_inline))
943  {
944  m_io->write8b(cmd &= ~mask);
945  }
946 
951  __attribute__((always_inline))
952  {
953  m_io->set_mode(1);
954  }
955 
960  __attribute__((always_inline))
961  {
962  m_io->set_mode(0);
963  }
964 };
965 #endif
uint8_t m_rs
Command/Data select.
Definition: HD44780.hh:829
OutputPin m_d1
Data pin; d1.
Definition: HD44780.hh:421
virtual void display_clear()
Definition: HD44780.cpp:98
without changing DDRAM contents.
Definition: HD44780.hh:885
uint8_t as_uint8
Unsigned byte access.
Definition: HD44780.hh:544
void text_flow_right_to_left()
Definition: HD44780.hh:251
OutputPin m_bt
Back-light control (0/on, 1/off).
Definition: HD44780.hh:426
uint8_t m_x
Cursor position x.
Definition: LCD.hh:180
void text_scroll_right_adjust()
Definition: HD44780.hh:269
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
void clear(uint8_t &cmd, uint8_t mask)
Definition: HD44780.hh:941
Set display, cursor and blinking controls.
Definition: HD44780.hh:847
Sets the number of display lines, 1 or.
Definition: HD44780.hh:896
virtual bool end()
Definition: HD44780.cpp:67
void set_custom_char(uint8_t id, const uint8_t *bitmap)
Definition: HD44780.cpp:128
virtual int write(const void *buf, size_t size)
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
void line_clear()
Definition: HD44780.hh:185
Decrement (left) on write.
Definition: HD44780.hh:868
Increment (right) on write.
Definition: HD44780.hh:867
virtual void write8n(const void *buf, size_t size)
Definition: HD44780_IO.cpp:31
void write(uint8_t data)
Definition: HD44780.hh:919
OutputPin m_d2
Data pin; d2.
Definition: HD44780.hh:422
Sets cursor move direction and display shift.
Definition: HD44780.hh:846
OutputPin m_d0
Data pin; d0.
Definition: HD44780.hh:420
void cursor_underline_on()
Definition: HD44780.hh:198
Port4b(Board::DigitalPin d0=Board::D4, Board::DigitalPin d1=Board::D5, Board::DigitalPin d2=Board::D6, Board::DigitalPin d3=Board::D7, Board::DigitalPin rs=Board::D8, Board::DigitalPin en=Board::D9, Board::DigitalPin bt=Board::D10)
Definition: HD44780.hh:364
OutputPin m_en
Starts data read/write.
Definition: HD44780.hh:564
HD44780(IO *io, uint8_t width=16, uint8_t height=2)
Definition: HD44780.hh:108
static const uint16_t INIT1_TIME
Definition: HD44780.hh:838
Bias resistor select.
Definition: HD44780.hh:854
static const uint8_t offset0[]
Definition: HD44780.hh:905
IO * m_io
IO port handler.
Definition: HD44780.hh:909
The display is on.
Definition: HD44780.hh:877
void display_scroll_right()
Definition: HD44780.hh:170
virtual void write8b(uint8_t data)
Definition: HD44780_IO.cpp:24
OutputPin m_sda
Serial data output.
Definition: HD44780.hh:825
uint8_t m_cntl
Control.
Definition: HD44780.hh:911
COM SEG direction select.
Definition: HD44780.hh:856
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
uint8_t as_uint8
Unsigned byte access.
Definition: HD44780.hh:677
void set_data_mode()
Definition: HD44780.hh:950
const uint8_t * m_offset
Row offset table.
Definition: HD44780.hh:913
Set cursor and shifts display.
Definition: HD44780.hh:848
const uint8_t HEIGHT
Definition: HD44780.hh:99
virtual void display_on()
Definition: HD44780.cpp:86
void text_scroll_left_adjust()
Definition: HD44780.hh:260
OutputPin m_rs
Register select (0/instruction, 1/data).
Definition: HD44780.hh:424
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
OutputPin m_scl
Serial clock.
Definition: HD44780.hh:826
Shift the entire display not cursor.
Definition: HD44780.hh:866
OutputPin m_sda
Serial data output.
Definition: HD44780.hh:562
Sets basic instruction set.
Definition: HD44780.hh:900
virtual void set_mode(uint8_t flag)=0
Sets interface data length, line and font.
Definition: HD44780.hh:849
port_t m_port
Port setting.
Definition: HD44780.hh:561
virtual void write4b(uint8_t data)=0
uint8_t m_func
Function set.
Definition: HD44780.hh:912
OutputPin m_en
Starts data read/write.
Definition: HD44780.hh:425
void text_flow_left_to_right()
Definition: HD44780.hh:242
OutputPin m_d3
Data pin; d3.
Definition: HD44780.hh:423
void cursor_blink_on()
Definition: HD44780.hh:216
void cursor_home()
Definition: HD44780.cpp:108
virtual void backlight_off()
Definition: HD44780.cpp:80
Moves cursor and shifts display.
Definition: HD44780.hh:884
OutputPin m_en
Starts data read/write.
Definition: HD44780.hh:827
virtual bool setup()=0
Set display data length.
Definition: HD44780.hh:858
Sets CGRAM address.
Definition: HD44780.hh:850
Definition: LCD.hh:36
static const uint8_t BITMAP_MAX
Definition: HD44780.hh:93
void cursor_blink_off()
Definition: HD44780.hh:225
virtual void set_backlight(uint8_t flag)=0
void cursor_underline_off()
Definition: HD44780.hh:207
The character indicated by cursor blinks.
Definition: HD44780.hh:875
Sets the character font, 5X8 dots or.
Definition: HD44780.hh:898
Device()
Definition: LCD.hh:53
OutputPin m_scl
Serial clock.
Definition: HD44780.hh:563
void display_scroll_left()
Definition: HD44780.hh:161
OutputPin m_bt
Backlight control.
Definition: HD44780.hh:828
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
port_t m_port
Port setting.
Definition: HD44780.hh:694
const uint8_t WIDTH
Definition: HD44780.hh:96
The cursor is displayed.
Definition: HD44780.hh:876