COSA
An Object-Oriented Platform for Arduino Programming
ST7565.cpp
Go to the documentation of this file.
1 
21 #include "ST7565.hh"
22 
23 // Initialization script
24 const uint8_t ST7565::script[] __PROGMEM = {
25  LCD_BIAS_9,
26  ADC_NORMAL,
27  COM_OUTPUT_REVERSE,
28  SET_DISPLAY_START | 0,
29  SET_POWER_CONTROL | 0x04,
30  SCRIPT_PAUSE , 50,
31  SET_POWER_CONTROL | 0x06,
32  SCRIPT_PAUSE , 50,
33  SET_POWER_CONTROL | 0x07,
34  SCRIPT_PAUSE , 10,
35  SET_RESISTOR_RATIO | 0x06,
36  SET_CONSTRAST , 0x08,
37  DISPLAY_ON,
38  DISPLAY_NORMAL,
39  DISPLAY_64X128_POINTS,
40  SCRIPT_END
41 };
42 
44  LCD::Device(),
45  m_io(io),
46  m_dc(dc, 1),
47  m_font(font)
48 {
49 }
50 
51 void
52 ST7565::set(uint8_t cmd)
53 {
54  m_io->begin();
55  asserted(m_dc) {
56  m_io->write(cmd);
57  }
58  m_io->end();
59 }
60 
61 void
62 ST7565::set(uint8_t x, uint8_t y)
63 {
64  m_io->begin();
65  asserted(m_dc) {
66  m_io->write(SET_X_ADDR | ((x >> 4) & X_ADDR_MASK));
67  m_io->write(x & X_ADDR_MASK);
68  m_io->write(SET_Y_ADDR | (y & Y_ADDR_MASK));
69  }
70  m_io->end();
71 }
72 
73 void
74 ST7565::fill(uint8_t data, uint16_t count)
75 {
76  m_io->begin();
77  while (count--) m_io->write(data);
78  m_io->end();
79 }
80 
81 bool
83 {
84  const uint8_t* bp = script;
85  uint8_t cmd;
86  m_io->begin();
87  asserted(m_dc) {
88  while ((cmd = pgm_read_byte(bp++)) != SCRIPT_END) {
89  if (cmd == SCRIPT_PAUSE) {
90  uint8_t ms = pgm_read_byte(bp++);
91  delay(ms);
92  }
93  else m_io->write(cmd);
94  }
95  }
96  m_io->end();
97  display_clear();
98  return (true);
99 }
100 
101 bool
103 {
104  set(DISPLAY_OFF);
105  return (true);
106 }
107 
108 void
110 {
111  m_io->begin();
112  asserted(m_dc) {
114  m_io->write(CONSTRAST_MASK & level);
115  }
116  m_io->end();
117 }
118 
119 void
121 {
122  set(DISPLAY_ON);
123 }
124 
125 void
127 {
128  set(DISPLAY_OFF);
129 }
130 
131 void
133 {
134  set(DISPLAY_NORMAL);
135 }
136 
137 void
139 {
140  set(DISPLAY_REVERSE);
141 }
142 
143 void
145 {
146  for (uint8_t y = 0; y < LINES; y++) {
147  set(0, y);
148  fill(m_mode, WIDTH);
149  }
150  set_cursor(0, 0);
151 }
152 
153 void
154 ST7565::set_cursor(uint8_t x, uint8_t y)
155 {
156  set(x, y);
157  m_x = (x & (WIDTH - 1));
158  m_y = (y & (LINES - 1));
159  if (UNLIKELY((m_x != 0) || (m_y != 0))) return;
160  m_line = 0;
161  set(SET_DISPLAY_START | m_line);
162 }
163 
164 void
165 ST7565::draw_icon(const uint8_t* bp)
166 {
167  uint8_t width = pgm_read_byte(bp++);
168  uint8_t height = pgm_read_byte(bp++);
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 ^ pgm_read_byte(bp++));
174  }
175  m_io->end();
176  set_cursor(m_x, m_y + 1);
177  }
178  set_cursor(m_x, m_y + 1);
179 }
180 
181 void
182 ST7565::draw_bitmap(uint8_t* bp, uint8_t width, uint8_t height)
183 {
184  uint8_t lines = (height >> 3);
185  for (uint8_t y = 0; y < lines; y++) {
186  m_io->begin();
187  for (uint8_t x = 0; x < width; x++) {
188  m_io->write(m_mode ^ (*bp++));
189  }
190  m_io->end();
191  set_cursor(m_x, m_y + 1);
192  }
193  set_cursor(m_x, m_y + 1);
194 }
195 
196 void
197 ST7565::draw_bar(uint8_t percent, uint8_t width, uint8_t pattern)
198 {
199  if (UNLIKELY(percent > 100)) percent = 100;
200  uint8_t filled = (percent * (width - 2U)) / 100;
201  uint8_t boarder = (m_y == 0 ? 0x81 : 0x80);
202  width -= (filled + 1);
203  m_io->begin();
204  m_io->write(m_mode ^ 0xff);
205  while (filled--) {
206  m_io->write(m_mode ^ (pattern | boarder));
207  pattern = ~pattern;
208  }
209  m_io->write(m_mode ^ 0xff);
210  width -= 1;
211  if (width > 0) {
212  while (width--)
213  m_io->write(m_mode ^ boarder);
214  }
215  m_io->write(m_mode ^ 0xff);
216  m_io->end();
217 }
218 
219 int
221 {
222  // Check for special characters
223  if (c < ' ') {
224 
225  // Carriage-return: move to start of line
226  if (c == '\r') {
227  set_cursor(0, m_y);
228  return (c);
229  }
230 
231  // Check line-feed: clear new line, Use display start line scroll
232  if (c == '\n') {
233  if (m_y == (LINES - 1)) {
235  set(SET_DISPLAY_START | m_line);
236  uint8_t y = m_line / CHARBITS;
237  if (y == 0) y = 7; else y = y - 1;
238  set(0, y);
239  fill(m_mode, WIDTH);
240  set(0, y);
241  m_x = 0;
242  }
243  else {
244  set_cursor(0, m_y + 1);
245  fill(m_mode, WIDTH);
246  set(m_x, m_y);
247  }
248  return (c);
249  }
250 
251  // Check for special character: horizontal tab
252  if (c == '\t') {
253  uint8_t tab = m_tab * (m_font->WIDTH + m_font->SPACING);
254  uint8_t x = m_x + tab - (m_x % tab);
255  uint8_t y = m_y + (x >= WIDTH);
256  set_cursor(x, y);
257  return (c);
258  }
259 
260  // Check for special character: form-feed
261  if (c == '\f') {
262  display_clear();
263  return (c);
264  }
265 
266  // Check for special character: back-space
267  if (c == '\b') {
268  uint8_t width = m_font->WIDTH + m_font->SPACING;
269  if (m_x < width) width = m_x;
270  set_cursor(m_x - width, m_y);
271  return (c);
272  }
273 
274  // Check for special character: alert
275  if (c == '\a') {
276  m_mode = ~m_mode;
277  return (c);
278  }
279  }
280 
281  // Write character to the display with an extra space
282  uint8_t width = m_font->WIDTH + m_font->SPACING;
283  Font::Glyph glyph(m_font, c);
284  m_x += width;
285  if (m_x > WIDTH) {
286  putchar('\n');
287  m_x = width;
288  }
289  m_io->begin();
290  while (--width)
291  m_io->write(m_mode ^ glyph.next());
292  m_io->write(m_mode);
293  m_io->end();
294 
295  return (c & 0xff);
296 }
297 
uint8_t m_y
Cursor position y.
Definition: LCD.hh:181
Init script pause (ms).
Definition: ST7565.hh:264
uint8_t m_x
Cursor position x.
Definition: LCD.hh:180
virtual int putchar(char c)
Definition: ST7565.cpp:220
uint8_t m_line
Display start line.
Definition: ST7565.hh:274
Definition: Font.hh:30
void draw_bar(uint8_t percent, uint8_t width, uint8_t pattern=0x55)
Definition: ST7565.cpp:197
#define asserted(pin)
Definition: Pin.hh:275
Set start line address.
Definition: ST7565.hh:230
static const uint8_t WIDTH
Definition: ST7565.hh:88
static const uint8_t LINES
Definition: ST7565.hh:90
virtual void begin()=0
uint8_t next()
Definition: Font.cpp:110
Set page address.
Definition: ST7565.hh:232
virtual void write(uint8_t data)=0
IOStream & tab(IOStream &outs)
Definition: IOStream.hh:805
void fill(uint8_t data, uint16_t count)
Definition: ST7565.cpp:74
void draw_bitmap(uint8_t *bp, uint8_t width, uint8_t height)
Definition: ST7565.cpp:182
Turn display off.
Definition: ST7565.hh:228
const uint8_t SPACING
Definition: Font.hh:37
virtual void display_inverse()
Definition: ST7565.cpp:138
static const uint8_t script[]
Definition: ST7565.hh:269
virtual void display_contrast(uint8_t level)
Definition: ST7565.cpp:109
virtual void display_clear()
Definition: ST7565.cpp:144
Turn display on.
Definition: ST7565.hh:229
uint8_t m_tab
Tab step.
Definition: LCD.hh:182
Init script end.
Definition: ST7565.hh:265
const uint8_t WIDTH
Definition: Font.hh:35
Normal display mode.
Definition: ST7565.hh:238
void(* delay)(uint32_t ms)
Reverse display mode.
Definition: ST7565.hh:239
OutputPin m_dc
Data(1) or command(0).
Definition: ST7565.hh:273
#define CHARBITS
Definition: Types.h:57
void draw_icon(const uint8_t *bp)
Definition: ST7565.cpp:165
Set column address (2x4 bits).
Definition: ST7565.hh:234
virtual void set_cursor(uint8_t x, uint8_t y)
Definition: ST7565.cpp:154
virtual bool begin()
Definition: ST7565.cpp:82
virtual void display_off()
Definition: ST7565.cpp:126
ST7565(LCD::IO *io, Board::DigitalPin dc=Board::D8, Font *font=&system5x7)
Definition: ST7565.cpp:43
Set output voltage volume register.
Definition: ST7565.hh:253
Definition: LCD.hh:190
void set(uint8_t cmd)
Definition: ST7565.cpp:52
uint8_t m_mode
Text mode.
Definition: LCD.hh:183
Definition: LCD.hh:36
virtual void display_on()
Definition: ST7565.cpp:120
Font * m_font
Font.
Definition: ST7565.hh:275
virtual bool end()
Definition: ST7565.cpp:102
LCD::IO * m_io
Display adapter.
Definition: ST7565.hh:272
virtual void end()=0
#define UNLIKELY(x)
Definition: Types.h:153
virtual void display_normal()
Definition: ST7565.cpp:132
const uint8_t ST7565::script[] __PROGMEM
Definition: ST7565.cpp:24