-
Notifications
You must be signed in to change notification settings - Fork 0
/
saba_i2clcd.h
381 lines (327 loc) · 11.9 KB
/
saba_i2clcd.h
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* saba_LcdText.h
*
* Created: 17.03.2018 17:33:47
* Author: Joerg
*/
#ifndef SABA_LcdText_H_
#define SABA_LcdText_H_
#include "saba_timing.h"
#include "saba_i2cm.h"
// The I2C Input/Output Bits to the LCD Controller
#define LCD_BUSYFLAG 7
#define LCD_BACKLIGHT 3
#define LCD_ENABLE 2
#define LCD_RW 1
#define LCD_RS 0
#ifdef DEBUG_LCDTEXT_MESSAGE
extern void putch(uint8_t c);
extern SABA::OStream <&putch> out;
#endif
namespace SABA
{
namespace I2C
{
// \brief I2C Lcd Text Display Driver
/**
A template class to access a I2C Text LCD.
@tparam FIFO_SIZE the putch Fifo size for non blocking printing
@tparam address the I2C Device Adress, tpyicalle 0x4e
@tparam DIPLAY_LINES false for 1 line, false for 2 lines (see LCD Datasheet)
@tparam FONT false = 5*7 font
*/
template<bool DISPLAY_LINES=false,bool FONT=false>
class LcdText
{
public:
LcdText(SABA::I2C::I2CMaster& master, uint8_t address) : master(&master), address(address) { } //! Constructs the Driver, set the I2CMaster
typedef void(*ERROR_RETURN)(uint8_t errorCode);
typedef void(*WRITE_RETURN)(void *env);
typedef void(*Callback)(void *env);
/** Initialize the Display, function blocks only, if the I2CMaster is busy.
* @param initReturn: callback in case of successful initialization.
* @param errorReturn: callback in case of any Error, the error callback is stored and also called from other methods
*/
void initialize(Callback initReturn, ERROR_RETURN errorReturn, void *callbackEnv = nullptr)
{
// wait, if I2C Master is busy
while((*master)());
context2= (void*)initReturn;
contextEnv= callbackEnv;
this->errorReturn= errorReturn;
master->startWrite(address, 1, &orMask,[](void *env, SABA::I2C::CMD* cmd)
{
LcdText *me= (LcdText*)env;
me->callbackDelay.start(100, env, [](void *env)
{
LcdText *me= (LcdText*)env;
me->writeNibble(0x30, [](void *env)
{
LcdText *me= (LcdText*)env;
me->callbackDelay.start(5, env, [](void *env)
{
LcdText *me= (LcdText*)env;
me->writeNibble(0x30, [](void *env)
{
LcdText *me= (LcdText*)env;
me->callbackDelay.start(5, env, [](void *env)
{
LcdText *me= (LcdText*)env;
me->writeNibble(0x30, [](void *env)
{
LcdText *me= (LcdText*)env;
me->callbackDelay.start(1, env, [](void *env)
{
LcdText *me= (LcdText*)env;
me->writeNibble(0x20, [](void *env)
{
LcdText *me= (LcdText*)env;
me->write(0x28 | (DISPLAY_LINES ? 8 : 0) | (FONT ? 4 : 0), [](void *env)
{
LcdText *me= (LcdText*)env;
me->write( 0x0c, [](void *env)
{
LcdText *me= (LcdText*)env;
me->write( 0x01, [](void *env)
{
LcdText *me= (LcdText*)env;
me->write( 0x6, [](void *env)
{
LcdText *me= (LcdText*)env;
me->initialized= true;
((Callback)me->context2)( me->contextEnv );
});
});
});
});
});
});
});
});
});
});
});
});
},(void*)this);
}
/** Turn on or off the Backlight
* @param on: true= Backlight on, false= Backlight off
* @param callback: the optional callback, called on successful command
* @return false, if I2C Hardware is busy, command will not be sent
*/
bool backlight( bool on, Callback callback= nullptr, void *callbackEnv = nullptr )
{
if((*master)())
return false;
if(on)
orMask |= _BV(LCD_BACKLIGHT);
else
orMask &= ~_BV(LCD_BACKLIGHT);
context2= (void*)callback;
contextEnv= callbackEnv;
return master->startWrite(address, 1, &orMask, [](void *env, SABA::I2C::CMD* cmd)
{
LcdText *me= (LcdText*)env;
if( cmd->error != 0 )
me->errorReturn(cmd->error);
else if(me->context2 != nullptr)
((Callback)me->context2)(me->contextEnv);
}, (void*)this);
}
/** Clear the LCD screen
* @param callback: the optional callback, called on successful command
* @return false, if I2C Hardware is busy, command will not be sent
*/
bool clearScreen(Callback callback= nullptr, void *callbackEnv = nullptr)
{
return command(1, callback, callbackEnv);
}
/** Cursor home
* @param callback: the optional callback, called on successful command
* @return false, if I2C Hardware is busy, command will not be sent
*/
bool home(Callback callback= nullptr, void *callbackEnv = nullptr)
{
return command(2, callback, callbackEnv);
}
/** Display options
* @param on: Display on (true) or off (false)
* @param cursorOn: Cursor on (true) or off (false)
* @param cursorBlink: Cursor is blinking (true) or not (false)
* @param callback: the optional callback, called on successful command
* @return false, if I2C Hardware is busy, command will not be sent
*/
bool display(bool on, bool cursorOn, bool cursorBlink, Callback callback= nullptr, void *callbackEnv = nullptr)
{
uint8_t data= 8;
if(on)
data |= 4;
if(cursorOn)
data |= 2;
if(cursorBlink)
data |= 1;
return command(data, callback, callbackEnv);
}
/** Set the Display Data Ram Address
* @param address: The data Ram address
* @param callback: the optional callback, called on successful command
* @return false, if I2C Hardware is busy, command will not be sent
*/
bool ddram(uint8_t address, Callback callback= nullptr, void *callbackEnv = nullptr)
{
return command( 0x80 | address, callback, callbackEnv);
}
/** Cyclic has to be called regularly, it is the internal state machine
*/
void cyclic()
{
callbackDelay.cyclic();
}
/** test, if the display has been initialized
* @return true, if the display has been successfully initialized
*/
bool isInitialized()
{
return initialized;
}
bool isBusy()
{
return (*master)() || callbackDelay();
}
/** non blocking putch function
* @param ch: char to print
* @param callback: the optional callback, called on successful command
* @return false, if I2C Hardware is busy, command will not be sent
*/
bool putch(uint8_t ch, Callback putchReturn = nullptr, void *callbackEnv = nullptr)
{
if((*master)())
return false;
context2= (void*)putchReturn;
contextEnv= callbackEnv;
orMask |= _BV(LCD_RS);
return write(ch, [](void *env)
{
LcdText *me= (LcdText*)env;
if(me->context2 != nullptr)
((Callback)me->context2)( me->contextEnv );
});
}
private:
bool command(uint8_t cmd, Callback callback, void *callbackEnv)
{
if((*master)())
return false;
context2= (void*)callback;
contextEnv= callbackEnv;
orMask &= ~_BV(LCD_RS);
return write(cmd, [](void *env)
{
LcdText *me= (LcdText*)env;
me->callbackDelay.start( 3, me, [](void *env)
{
LcdText *me= (LcdText*)env;
if(me->context2 != nullptr)
((Callback)me->context2)( me->contextEnv );
});
});
}
/*bool readBusy(BUSY_RETURN busyReturn)
{
if((*master)())
return false;
orMask &= ~_BV(LCD_RS);
writeData[0]= orMask | _BV(LCD_RW);
writeData[1]= orMask | _BV(LCD_BUSYFLAG) | _BV(LCD_RW) | _BV(LCD_ENABLE);
context= (void*)busyReturn;
return master->startWriteAndRead(address, 2, writeData, 1, &readData, [](void *env, SABA::I2C::CMD* cmd)
{
LcdText *me= (LcdText*)env;
if( cmd->error != 0 )
{
me->errorReturn(cmd->error);
}
else
{
cmd->bytesToWrite= 1;
cmd->bytesToRead= 0;
me->master->continueWriteAndRead([](void *env, SABA::I2C::CMD* cmd)
{
LcdText *me= (LcdText*)env;
if( cmd->error != 0 )
me->errorReturn(cmd->error);
else
((BUSY_RETURN)me->context)(env, me->readData & _BV(LCD_BUSYFLAG));
} );
}
}, (void *)this);
}*/
bool writeNibble( uint8_t d, WRITE_RETURN writeReturn )
{
if( (*master)())
return false;
d &= 0xf0;
d |= orMask;
writeData[0]= d | _BV(LCD_ENABLE);
writeData[1]= d;
#ifdef DEBUG_LCDTEXT_MESSAGE
out << PSTR("N:") << writeData[0] << ' ' << writeData[1] << SABA::endl;
#endif
context= (void*)writeReturn;
return master->startWrite(address, 2, writeData, [](void *env, SABA::I2C::CMD* cmd)
{
LcdText *me= (LcdText*)env;
if( cmd->error != 0 )
{
me->errorReturn(cmd->error);
}
else
{
((WRITE_RETURN)me->context)(env);
}
}, (void *)this);
}
bool write( uint8_t d, WRITE_RETURN writeReturn )
{
if( (*master)())
return false;
uint8_t tmp= (d & 0xf0) | orMask;
writeData[0]= tmp | _BV(LCD_ENABLE);
writeData[1]= tmp;
tmp= ((d <<= 4) & 0xf0) | orMask;
writeData[2]= tmp | _BV(LCD_ENABLE);
writeData[3]= tmp;
#ifdef DEBUG_LCDTEXT_MESSAGE
out << PSTR("B:") << d << ' ' << writeData[0] << ' ' << writeData[1] << ' ' << writeData[2] << ' ' << writeData[3] << SABA::endl;
#endif
context= (void *)writeReturn;
return master->startWrite(address, 4, writeData, [](void *env, SABA::I2C::CMD* cmd)
{
LcdText *me= (LcdText*)env;
if( cmd->error != 0 )
{
me->errorReturn(cmd->error);
}
else
{
((WRITE_RETURN)me->context)(env);
}
}, (void *)this);
}
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60972
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58798
SABA::I2C::I2CMaster *master;
uint8_t writeData[4];
uint8_t address;
uint8_t readData;
uint8_t orMask = 0;
bool initialized= false;
void *context = nullptr; // inner callback
void *context2= nullptr; // public method callback - init, putch, ...
void *contextEnv= nullptr;
ERROR_RETURN errorReturn = nullptr;
SABA::Timing::CallbackDelay<uint8_t> callbackDelay;
};
}
}
#endif /* SABA_LcdText_H_ */