COSA
An Object-Oriented Platform for Arduino Programming
PCD8544.cpp
Go to the documentation of this file.
1 
21 #include "PCD8544.hh"
22 
23 // Startup script
24 const uint8_t PCD8544::script[] __PROGMEM = {
25  SET_FUNC | EXTENDED_INST,
26  SET_VOP | 0x38,
27  SET_TEMP_COEFF | 0x00,
28  SET_BIAS_SYS | 0x04,
29  SET_FUNC | BASIC_INST | HORIZONTAL_ADDR,
30  DISPLAY_CNTL | NORMAL_MODE,
31  SET_X_ADDR | 0,
32  SET_Y_ADDR | 0,
33  SCRIPT_END
34 };
35 
37  LCD::Device(),
38  m_io(io),
39  m_dc(dc, 1),
40  m_font(font)
41 {
42 }
43 
44 void
45 PCD8544::set(uint8_t cmd)
46 {
47  m_io->begin();
48  asserted(m_dc) {
49  m_io->write(cmd);
50  }
51  m_io->end();
52 }
53 
54 void
55 PCD8544::set(uint8_t x, uint8_t y)
56 {
57  m_io->begin();
58  asserted(m_dc) {
59  m_io->write(SET_X_ADDR | (x & X_ADDR_MASK));
60  m_io->write(SET_Y_ADDR | (y & Y_ADDR_MASK));
61  }
62  m_io->end();
63 }
64 
65 void
66 PCD8544::fill(uint8_t data, uint16_t count)
67 {
68  m_io->begin();
69  while (count--) m_io->write(data);
70  m_io->end();
71 }
72 
73 bool
75 {
76  const uint8_t* bp = script;
77  uint8_t cmd;
78  m_io->begin();
79  asserted(m_dc) {
80  while ((cmd = pgm_read_byte(bp++)) != SCRIPT_END)
81  m_io->write(cmd);
82  }
83  m_io->end();
84  display_clear();
85  return (true);
86 }
87 
88 bool
90 {
92  return (true);
93 }
94 
95 void
97 {
98  set(DISPLAY_CNTL | DISPLAY_ON);
99 }
100 
101 void
103 {
104  set(DISPLAY_CNTL | DISPLAY_OFF);
105 }
106 
107 void
109 {
110  set(DISPLAY_CNTL | NORMAL_MODE);
111 }
112 
113 void
115 {
116  set(DISPLAY_CNTL | INVERSE_MODE);
117 }
118 
119 void
121 {
122  set_cursor(0, 0);
123  fill(m_mode, LINES * WIDTH);
124  set(m_x, m_y);
125 }
126 
127 void
129 {
130  m_io->begin();
131  asserted(m_dc) {
133  m_io->write(SET_VOP | (level & VOP_MASK));
135  }
136  m_io->end();
137 }
138 
139 void
140 PCD8544::set_cursor(uint8_t x, uint8_t y)
141 {
142  // For x on character boundary (and not pixel)
143  // x *= m_font->WIDTH + m_font->SPACING;
144  set(x, y);
145  m_x = x;
146  m_y = y;
147 }
148 
149 void
150 PCD8544::draw_icon(const uint8_t* bp)
151 {
152  uint8_t width = pgm_read_byte(bp++);
153  uint8_t height = pgm_read_byte(bp++);
154  uint8_t lines = (height >> 3);
155  for (uint8_t y = 0; y < lines; y++) {
156  m_io->begin();
157  for (uint8_t x = 0; x < width; x++) {
158  m_io->write(m_mode ^ pgm_read_byte(bp++));
159  }
160  m_io->end();
161  set_cursor(m_x, m_y + 1);
162  }
163  set_cursor(m_x, m_y + 1);
164 }
165 
166 void
167 PCD8544::draw_bitmap(uint8_t* bp, uint8_t width, uint8_t height)
168 {
169  uint8_t lines = (height >> 3);
170  for (uint8_t y = 0; y < lines; y++) {
171  m_io->begin();
172  for (uint8_t x = 0; x < width; x++) {
173  m_io->write(m_mode ^ (*bp++));
174  }
175  m_io->end();
176  set_cursor(m_x, m_y + 1);
177  }
178  m_y += 1;
179  set_cursor(m_x, m_y + 1);
180 }
181 
182 void
183 PCD8544::draw_bar(uint8_t percent, uint8_t width, uint8_t pattern)
184 {
185  if (UNLIKELY(percent > 100)) percent = 100;
186  uint8_t filled = (percent * (width - 2U)) / 100;
187  uint8_t boarder = (m_y == 0 ? 0x81 : 0x80);
188  width -= (filled + 1);
189  m_io->begin();
190  m_io->write(m_mode ^ 0xff);
191  while (filled--) {
192  m_io->write(m_mode ^ (pattern | boarder));
193  pattern = ~pattern;
194  }
195  m_io->write(m_mode ^ 0xff);
196  width -= 1;
197  if (width > 0) {
198  while (width--)
199  m_io->write(m_mode ^ boarder);
200  }
201  m_io->write(m_mode ^ 0xff);
202  m_io->end();
203 }
204 
205 int
207 {
208  // Check for special characters
209  if (c < ' ') {
210 
211  // Carriage-return: move to start of line
212  if (c == '\r') {
213  set_cursor(0, m_y);
214  return (c);
215  }
216 
217  // Check for line-feed: clear new line
218  if (c == '\n') {
219  m_y += 1;
220  if (m_y == LINES) m_y = 0;
221  set_cursor(0, m_y);
222  fill(m_mode, WIDTH);
223  set(m_x, m_y);
224  return (c);
225  }
226 
227  // Check for horizontal tab
228  if (c == '\t') {
229  uint8_t x = m_x + m_tab - (m_x % m_tab);
230  uint8_t y = m_y + (x >= WIDTH);
231  set_cursor(x, y);
232  return (c);
233  }
234 
235  // Check for special character: form-feed
236  if (c == '\f') {
237  display_clear();
238  return (c);
239  }
240 
241  // Check for special character: back-space
242  if (c == '\b') {
243  uint8_t width = m_font->WIDTH + m_font->SPACING;
244  if (m_x < width) width = m_x;
245  set_cursor(m_x - width, m_y);
246  return (c);
247  }
248 
249  // Check for special character: alert
250  if (c == '\a') {
251  m_mode = ~m_mode;
252  return (c);
253  }
254  }
255 
256  // Access font for character width and bitmap
257  uint8_t width = m_font->WIDTH + m_font->SPACING;
258  Font::Glyph glyph(m_font, c);
259  m_x += width;
260 
261  // Check that the character is not clipped
262  if (m_x > WIDTH) {
263  putchar('\n');
264  m_x = width;
265  }
266 
267  // Write character to the display memory and an extra byte
268  m_io->begin();
269  while (--width)
270  m_io->write(m_mode ^ glyph.next());
271  m_io->write(m_mode);
272  m_io->end();
273 
274  return (c);
275 }
Mask X-addres.
Definition: PCD8544.hh:230
uint8_t m_y
Cursor position y.
Definition: LCD.hh:181
virtual void display_on()
Definition: PCD8544.cpp:96
Sets X-address of RAM (0..83).
Definition: PCD8544.hh:229
void fill(uint8_t data, uint16_t count)
Definition: PCD8544.cpp:66
PCD8544(LCD::IO *io, Board::DigitalPin dc=Board::D8, Font *font=&system5x7)
Definition: PCD8544.cpp:36
uint8_t m_x
Cursor position x.
Definition: LCD.hh:180
Definition: Font.hh:30
#define asserted(pin)
Definition: Pin.hh:275
virtual void begin()=0
Display control.
Definition: PCD8544.hh:222
Turn display on.
Definition: PCD8544.hh:224
virtual int putchar(char c)
Definition: PCD8544.cpp:206
uint8_t next()
Definition: Font.cpp:110
OutputPin m_dc
Data/command output pin.
Definition: PCD8544.hh:243
const uint8_t PCD8544::script[] __PROGMEM
Definition: PCD8544.cpp:24
Font * m_font
Font.
Definition: PCD8544.hh:244
virtual void write(uint8_t data)=0
Horizontal addressing.
Definition: PCD8544.hh:218
Extended instruction set control.
Definition: PCD8544.hh:217
void draw_bitmap(uint8_t *bp, uint8_t width, uint8_t height)
Definition: PCD8544.cpp:167
const uint8_t SPACING
Definition: Font.hh:37
Turn display off.
Definition: PCD8544.hh:223
uint8_t m_tab
Tab step.
Definition: LCD.hh:182
virtual void display_contrast(uint8_t level)
Definition: PCD8544.cpp:128
const uint8_t WIDTH
Definition: Font.hh:35
Power down mode.
Definition: PCD8544.hh:221
virtual bool begin()
Definition: PCD8544.cpp:74
LCD::IO * m_io
Display adapter.
Definition: PCD8544.hh:242
static const uint8_t LINES
Definition: PCD8544.hh:76
void draw_icon(const uint8_t *bp)
Definition: PCD8544.cpp:150
Normal display mode.
Definition: PCD8544.hh:225
virtual bool end()
Definition: PCD8544.cpp:89
virtual void display_normal()
Definition: PCD8544.cpp:108
Definition: LCD.hh:190
Mask Vop.
Definition: PCD8544.hh:234
Write Vop to register.
Definition: PCD8544.hh:233
Sets Y-address of RAM (0..5).
Definition: PCD8544.hh:227
virtual void display_inverse()
Definition: PCD8544.cpp:114
Mask Y-address.
Definition: PCD8544.hh:228
Set function.
Definition: PCD8544.hh:215
Basic instruction set.
Definition: PCD8544.hh:216
uint8_t m_mode
Text mode.
Definition: LCD.hh:183
static const uint8_t WIDTH
Definition: PCD8544.hh:74
Definition: LCD.hh:36
virtual void end()=0
#define UNLIKELY(x)
Definition: Types.h:153
void set(uint8_t cmd)
Definition: PCD8544.cpp:45
static const uint8_t script[]
Definition: PCD8544.hh:239
virtual void set_cursor(uint8_t x, uint8_t y)
Definition: PCD8544.cpp:140
Init script end.
Definition: PCD8544.hh:235
Inverse display mode.
Definition: PCD8544.hh:226
virtual void display_off()
Definition: PCD8544.cpp:102
void draw_bar(uint8_t percent, uint8_t width, uint8_t pattern=0x55)
Definition: PCD8544.cpp:183
virtual void display_clear()
Definition: PCD8544.cpp:120