COSA
An Object-Oriented Platform for Arduino Programming
VLCD.cpp
Go to the documentation of this file.
1 
21 #include "VLCD.hh"
22 #include "Cosa/Watchdog.hh"
23 
24 void
25 VLCD::Slave::on_request(void* buf, size_t size)
26 {
27  UNUSED(buf);
28 
29  // Check for non command prefix
30  char c = (char) m_buf[0];
31  if (c != (char) COMMAND) {
32  m_lcd->putchar(c);
33  for (uint8_t i = 1; i < size; i++)
34  m_lcd->putchar(m_buf[i]);
35  return;
36  }
37  // Handle command: COMMAND(uint8_t cmd)
38  if (size == 2) {
39  uint8_t cmd = m_buf[1];
40  switch (cmd) {
41  case BACKLIGHT_OFF_CMD: m_lcd->backlight_off(); return;
42  case BACKLIGHT_ON_CMD: m_lcd->backlight_on(); return;
43  case DISPLAY_OFF_CMD: m_lcd->display_off(); return;
44  case DISPLAY_ON_CMD: m_lcd->display_on(); return;
45  case INIT_CMD:
46  m_lcd->display_clear();
47  m_lcd->display_on();
48  m_lcd->backlight_on();
49  info_t* info = (info_t*) m_buf;
50  info->major = MAJOR;
51  info->minor = MINOR;
52  // Fix: Should be defined by LCD
53  info->width = 16;
54  info->height = 2;
55  return;
56  }
57  }
58  // Handle command: SET_CURSOR(uint8_t x, uint8_t y)
59  if (size == 3) {
60  uint8_t x = m_buf[1];
61  uint8_t y = m_buf[2];
62  m_lcd->set_cursor(x, y);
63  return;
64  }
65 }
66 
67 void
68 VLCD::write(uint8_t cmd)
69 {
70  uint8_t buf[2];
71  buf[0] = Slave::COMMAND;
72  buf[1] = cmd;
73  twi.acquire(this);
74  uint8_t retry = 3;
75  do {
76  int res = twi.write(buf, sizeof(buf));
77  if (res == sizeof(buf)) break;
78  } while (--retry);
79  twi.release();
80 }
81 
82 bool
84 {
85  sleep(1);
86  write(Slave::INIT_CMD);
87  twi.acquire(this);
88  uint8_t retry = 3;
89  info_t info;
90  do {
91  int res = twi.read(&info, sizeof(info));
92  if (res == sizeof(info)) break;
93  } while (--retry);
94  twi.release();
95  MAJOR = info.major;
96  MINOR = info.minor;
97  WIDTH = info.width;
98  HEIGHT = info.height;
99  return (true);
100 }
101 
102 bool
104 {
105  display_off();
106  return (true);
107 }
108 
109 void
111 {
112  write(Slave::BACKLIGHT_OFF_CMD);
113 }
114 
115 void
117 {
118  write(Slave::BACKLIGHT_ON_CMD);
119 }
120 
121 void
123 {
124  write(Slave::DISPLAY_OFF_CMD);
125 }
126 
127 void
129 {
130  write(Slave::DISPLAY_ON_CMD);
131 }
132 
133 void
135 {
136  putchar('\f');
137 }
138 
139 void
140 VLCD::set_cursor(uint8_t x, uint8_t y)
141 {
142  uint8_t buf[3];
143  buf[0] = 0;
144  buf[1] = x;
145  buf[2] = y;
146  twi.acquire(this);
147  twi.write(buf, sizeof(buf));
148  twi.release();
149  m_x = x;
150  m_y = y;
151 }
152 
153 int
155 {
156  twi.acquire(this);
157  int n = twi.write(&c, sizeof(c));
158  twi.release();
159  if (n != 1) return (-1);
160  if (c >= ' ') return (c & 0xff);
161  switch (c) {
162  case '\b':
163  if (m_x > 0) m_x -= 1;
164  break;
165  case '\f':
166  m_x = 0;
167  m_y = 0;
168  break;
169  case '\n':
170  m_x = 0;
171  m_y += 1;
172  if (m_y > HEIGHT) m_y = 0;
173  break;
174  case '\r':
175  m_x = 0;
176  break;
177  case '\t':
178  m_x += m_tab - (m_x % m_tab);
179  m_y += (m_x >= WIDTH);
180  if (m_x >= WIDTH) m_x = 0;
181  break;
182  }
183  return (c & 0xff);
184 }
185 
186 int
187 VLCD::write(const void* buf, size_t size)
188 {
189  twi.acquire(this);
190  int n = twi.write((void*) buf, size);
191  twi.release();
192  if (n < 0) return (-1);
193  m_x += size;
194  return (n);
195 }
uint8_t m_y
Cursor position y.
Definition: LCD.hh:181
TWI twi
Definition: TWI.cpp:27
virtual int putchar(char c)
uint8_t m_x
Cursor position x.
Definition: LCD.hh:180
virtual bool begin()
Definition: VLCD.cpp:83
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
virtual void display_off()=0
virtual void backlight_off()
Definition: LCD.hh:86
virtual int putchar(char c)
Definition: VLCD.cpp:154
uint8_t m_tab
Tab step.
Definition: LCD.hh:182
virtual void set_cursor(uint8_t x, uint8_t y)=0
void release()
Definition: TWI.cpp:58
virtual void display_on()=0
virtual void backlight_off()
Definition: VLCD.cpp:110
int read(void *buf, size_t size)
Definition: TWI.hh:326
virtual void backlight_on()
Definition: LCD.hh:80
virtual void display_off()
Definition: VLCD.cpp:122
virtual void display_clear()=0
int write(void *buf, size_t size)
Definition: TWI.hh:282
#define UNUSED(x)
Definition: ATmega328P.hh:31
virtual void display_clear()
Definition: VLCD.cpp:134
void(* sleep)(uint16_t s)
uint8_t HEIGHT
Definition: VLCD.hh:39
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
uint8_t WIDTH
Definition: VLCD.hh:38
virtual bool end()
Definition: VLCD.cpp:103
virtual void set_cursor(uint8_t x, uint8_t y)
Definition: VLCD.cpp:140
virtual void on_request(void *buf, size_t size)
Definition: VLCD.cpp:25