-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOLED_Text.hh
266 lines (234 loc) · 5.85 KB
/
OLED_Text.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @file ?/OLED_Text.hh
* @version 0.1
*
* @section License
* Copyright (C) 2014-2015, jeditekunum
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#ifndef COSA_X_OLED_TEXT_HH
#define COSA_X_OLED_TEXT_HH
#include "Cosa/LCD.hh"
#include <Font.h>
#include "OLED_IO.hh"
/**
* Base class for OLED Text.
*/
class OLED_Text : public LCD::Device {
public:
/**
* @override LCD::Device
* Text display mode.
*/
enum {
NORMAL_TEXT_MODE = 0,
INVERTED_TEXT_MODE = _BV(0),
UNDERLINED_TEXT_MODE = _BV(1),
RAW_TEXT_MODE = _BV(7)
} __attribute__((packed));
typedef uint8_t TextMode;
/**
* Construct OLED_Text OLED connected to given io port handler.
* Note that these devices only accept data in bands of 8 rows.
* Therefore each line must start at a %8 boundary. Some fonts
* will display less lines than expected. (For example, a 20
* high font will only display as 2 lines on a 64 high display.)
* LINE_SPACING is ignored in these devices.
* @param[in] io handler.
* @param[in] font font to display.
* @param[in] screen_width.
* @param[in] screen_height.
* @param[in] column_offset.
*/
OLED_Text(OLED_IO* io, Font* font,
uint8_t screen_width, uint8_t screen_height,
uint8_t column_offset = 0) :
LCD::Device(),
m_initialized(false),
m_io(io),
m_font(font),
m_screen_width(screen_width),
m_column_offset(column_offset),
m_screen_height(screen_height),
m_columns(screen_width / (m_font->WIDTH + m_font->SPACING)),
m_rows(screen_height / (BYTES(m_font->HEIGHT)*8))
{}
/**
* @override LCD::Device
* Start display for text output. Returns true if successful
* otherwise false.
* @return boolean.
*/
virtual bool begin() = 0;
/**
* @override LCD::Device
* Stop display and power down. Returns true if successful
* otherwise false.
*/
virtual bool end()
{
display_clear();
display_off();
return (true);
}
/**
* @override LCD::Device
* Set cursor position to given position.
* @param[in] x.
* @param[in] y.
*/
virtual void set_cursor(uint8_t x, uint8_t y)
{
if (x >= m_columns) x = 0;
if (y >= m_rows) y = 0;
m_x = x;
m_y = y;
}
/**
* @override IOStream::Device
* Write character to display. Handles carriage-return-line-feed, back-
* space, alert, horizontal tab and form-feed. Returns character or EOF
* on error.
* @param[in] c character to write.
* @return character written or EOF(-1).
*/
virtual int putchar(char c);
/**
* Until Cosa LCD::Device has get_text_mode...
* Get text mode.
* @return mode.
*/
TextMode text_mode()
__attribute__((always_inline))
{
LCD::Device::TextMode mode = LCD::Device::text_mode(LCD::Device::NORMAL_TEXT_MODE);
text_mode(mode);
return (mode);
}
/**
* @override LCD::Device until it changes
* Set text mode. Return previous text mode.
* @param[in] mode new text mode.
* @return previous text mode.
*/
TextMode text_mode(TextMode mode)
__attribute__((always_inline))
{
return (LCD::Device::text_mode((LCD::Device::TextMode)mode));
}
/** Overloaded virtual member function write. */
using IOStream::Device::write;
/**
* @override IOStream::Device
* Write data from buffer with given size to device.
* @param[in] buf buffer to write.
* @param[in] size number of bytes to write.
* @return number of bytes written or EOF(-1).
*/
virtual int write(const void* buf, size_t size);
/**
* Clear to end of line.
*/
virtual void line_clear()
{
uint8_t x = m_x;
uint8_t y = m_x;
while (m_x < m_columns) putchar(' ');
set_cursor(x, y);
}
/**
* Get width in characters.
*/
uint8_t width()
__attribute__((always_inline))
{
return (m_columns);
}
/**
* Get height in characters.
*/
uint8_t height()
__attribute__((always_inline))
{
return (m_rows);
}
protected:
uint8_t m_initialized;
/** Display IO. */
OLED_IO* m_io;
/** Font. */
Font* m_font;
/** Width of display. */
uint8_t m_screen_width;
/** Column offset. */
uint8_t m_column_offset;
/** Height of display. */
uint8_t m_screen_height;
/** Display columns (characters per line). */
uint8_t m_columns;
/** Display rows (lines). */
uint8_t m_rows;
/**
* Scripts are a sequence of bytes of the form
* {command, #args, args...}
* Command may be a special script control.
*/
/**
* Script control options.
* (Cannot be real commands.)
*/
enum Command {
SW_DELAY = 0xf0,
SCRIPT_END = 0xf1
} __attribute__((packed));
/**
* Begin with script execution.
* @param[in] script.
*/
bool oled_begin(const uint8_t* script);
/**
* Set communication in data stream mode.
*/
void set_data_mode()
__attribute__((always_inline))
{
m_io->mode(OLED_IO::DATA);
}
/**
* Set communication in instruction stream mode.
*/
void set_instruction_mode()
__attribute__((always_inline))
{
m_io->mode(OLED_IO::INSTRUCTION);
}
/**
* Write character to display.
* @param[in] c character.
*/
virtual void print(char c);
/**
* Transform pixels based on text mode.
* @param[in] byte.
* @return possibly inverted byte.
*/
uint8_t transform(uint8_t p)
__attribute__((always_inline))
{
if (m_mode & INVERTED_TEXT_MODE)
return (~p);
else
return (p);
}
};
#endif