COSA
An Object-Oriented Platform for Arduino Programming
VLCD.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_VLCD_HH
22 #define COSA_VLCD_HH
23 
24 #include "Cosa/LCD.hh"
25 #include "Cosa/TWI.hh"
26 
31 class VLCD : public LCD::Device, private TWI::Driver {
32 public:
33  // Display protocol version (valid after initialization, begin())
34  uint8_t MAJOR;
35  uint8_t MINOR;
36 
37  // Display width (characters per line) and height (lines)
38  uint8_t WIDTH;
39  uint8_t HEIGHT;
40 
45  VLCD(uint8_t addr = 0x5a) :
46  LCD::Device(),
47  TWI::Driver(addr),
48  MAJOR(0),
49  MINOR(0),
50  WIDTH(0),
51  HEIGHT(0)
52  {}
53 
61  virtual bool begin();
62 
68  virtual bool end();
69 
74  virtual void backlight_on();
75 
80  virtual void backlight_off();
81 
86  virtual void display_on();
87 
92  virtual void display_off();
93 
98  virtual void display_clear();
99 
106  virtual void set_cursor(uint8_t x, uint8_t y);
107 
116  virtual int putchar(char c);
117 
120 
128  virtual int write(const void* buf, size_t size);
129 
133  class Slave : public TWI::Slave {
134  friend class VLCD;
135  public:
136  // Max size of buffer
137  static const uint8_t BUF_MAX = 40;
138 
144  Slave(LCD::Device* lcd, uint8_t addr = 0x5a) :
145  TWI::Slave(addr),
146  m_lcd(lcd)
147  {
148  write_buf(m_buf, sizeof(m_buf));
149  read_buf(m_buf, sizeof(m_buf));
150  }
151 
158  virtual void on_request(void* buf, size_t size);
159 
160  private:
161  // Version code
162  static const uint8_t MAJOR = 1;
163  static const uint8_t MINOR = 0;
164 
165  // Command prefix byte
166  static const uint8_t COMMAND = 255;
167 
168  // Command codes
169  enum {
170  BACKLIGHT_OFF_CMD = 0,
171  BACKLIGHT_ON_CMD,
172  DISPLAY_OFF_CMD,
173  DISPLAY_ON_CMD,
174  INIT_CMD = 0xff
175  } __attribute__((packed));
176 
177  // Buffer for transactions (max string write)
178  uint8_t m_buf[BUF_MAX];
179 
180  // The actual LCD implementation
181  LCD::Device* m_lcd;
182  };
183 
184 private:
185  // Version information
186  struct info_t {
187  uint8_t major;
188  uint8_t minor;
189  uint8_t width;
190  uint8_t height;
191  };
192 
197  void write(uint8_t cmd);
198 };
199 
200 #endif
Definition: TWI.hh:51
virtual bool begin()
Definition: VLCD.cpp:83
VLCD(uint8_t addr=0x5a)
Definition: VLCD.hh:45
uint8_t MAJOR
Definition: VLCD.hh:34
virtual int write(const void *buf, size_t size)
virtual void backlight_on()
Definition: VLCD.cpp:116
virtual void display_on()
Definition: VLCD.cpp:128
virtual int write(const void *buf, size_t size)
Definition: VLCD.cpp:187
void write_buf(void *buf, size_t size)
Definition: TWI.cpp:312
virtual int putchar(char c)
Definition: VLCD.cpp:154
virtual void backlight_off()
Definition: VLCD.cpp:110
Slave(LCD::Device *lcd, uint8_t addr=0x5a)
Definition: VLCD.hh:144
virtual void display_off()
Definition: VLCD.cpp:122
void read_buf(void *buf, size_t size)
Definition: TWI.cpp:319
Definition: VLCD.hh:31
virtual void display_clear()
Definition: VLCD.cpp:134
Driver(uint8_t addr)
Definition: TWI.hh:70
uint8_t HEIGHT
Definition: VLCD.hh:39
uint8_t MINOR
Definition: VLCD.hh:35
uint8_t WIDTH
Definition: VLCD.hh:38
virtual bool end()
Definition: VLCD.cpp:103
Definition: LCD.hh:36
virtual void set_cursor(uint8_t x, uint8_t y)
Definition: VLCD.cpp:140
Device()
Definition: LCD.hh:53
virtual void on_request(void *buf, size_t size)
Definition: VLCD.cpp:25
static const uint8_t BUF_MAX
Definition: VLCD.hh:137