-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextAnchor.cpp
615 lines (451 loc) · 14.4 KB
/
TextAnchor.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#pragma once
#include "TextAnchor.hpp"
/*
MAKE_TYPE_INFO( int )
MAKE_TYPE_INFO( int* )
MAKE_TYPE_INFO( float )
MAKE_TYPE_INFO( float* )
MAKE_TYPE_INFO( long )
MAKE_TYPE_INFO( unsigned long )
MAKE_TYPE_INFO( long* )
MAKE_TYPE_INFO( short* )
MAKE_TYPE_INFO( short )
MAKE_TYPE_INFO( double )
MAKE_TYPE_INFO( double* )
MAKE_TYPE_INFO( char )
MAKE_TYPE_INFO( char* )
MAKE_TYPE_INFO( char& )
//MAKE_TYPE_INFO( String )
//MAKE_TYPE_INFO( char[5] )
//MAKE_TYPE_INFO( char(*)[5] )
*/
/*
https://github.com/adafruit/Adafruit-GFX-Library/blob/master/Adafruit_GFX.cpp
*/
TextAnchor::TextAnchor(Adafruit_SSD1351 &GFXHandler) : GFXHandler (&GFXHandler) {}
// If user were to set/change the printer to a CHAR...
TextAnchor * TextAnchor::print( char *charValue ) {
//std::cout << "You provided the char: " << charValue << std::endl;
//if ( strcmp( _printerVal, _printerValPrev ) != 0 ) _lastChangeMs = millis();
//_printerVal = charValue;
strcpy(_printerVal, charValue);
print();
return this;
}
TextAnchor * TextAnchor::print(int intValue){
_printInt(intValue);
return this;
}
TextAnchor * TextAnchor::print(unsigned int intValue){
_printInt(intValue);
return this;
}
TextAnchor * TextAnchor::print(long intValue){
_printLong(intValue);
return this;
}
TextAnchor * TextAnchor::print(unsigned long intValue){
_printLong(intValue);
return this;
}
TextAnchor * TextAnchor::print(float intValue){
_printFloat(intValue);
return this;
}
TextAnchor * TextAnchor::print(unsigned float intValue){
_printFloat(intValue);
return this;
}
TextAnchor::~TextAnchor(void) {
}
TextAnchor * TextAnchor::setAutoPrint( bool autoPrint ){
_autoReprint = autoPrint;
return this;
}
TextAnchor * TextAnchor::setCursor( int x, int y ) {
_x = x;
_y = y;
print();
return this;
}
// A specific x/y location is provided, and the test will always end there
// whenever updated.
TextAnchor * TextAnchor::rightAlign( void ){
return rightAlign( true );
}
TextAnchor * TextAnchor::rightAlign( bool rightAlign ){
if ( rightAlign == _rightAlign )
return this;
_rightAlign = rightAlign;
print();
return this;
}
// This would print the stored value in the specified area, but for this forum
// post, I'm just outputting the value to serial
TextAnchor * TextAnchor::print( void ){
// width, height, getRotation, getCursorX, getCursorY
/*
std::cout << "width: " << GFXHandler->width() << std::endl;
std::cout << "height: " << GFXHandler->height() << std::endl;
std::cout << "getRotation: " << GFXHandler->getRotation() << std::endl;
std::cout << "getCursorX: " << GFXHandler->getCursorX() << std::endl;
std::cout << "getCursorY: " << GFXHandler->getCursorY() << std::endl;
*/
//std::cout << _printerVal << " has charWidth: "
//<< GFXHandler->charWidth(_printerVal) << " and textWidth: "
//<< GFXHandler->textWidth(_printerVal) << std::endl;
uint16_t colFg = _fgColor,
colBg = _bgColor;
// If highlight is true, then set the fg/bg colors to the highlight colors
if ( _highlightStatus == true ) {
// If no fg/bg highlight color is set, then just swap the values
if ( _fgHlColor == NULL && _bgHlColor == NULL ){
colFg = _bgColor;
colBg = _fgColor;
}
// But if only one of the fg or bg highlight color is set, then use that
// color only for that fg/bg (and leave the other as-is)
else {
if ( _fgHlColor != NULL ) colFg = _fgHlColor;
if ( _bgHlColor != NULL ) colBg = _bgHlColor;
}
}
int diff = 0;
// If the printed text isn't the same as the text printed last time, then check the difference
// in length (to override the text that would otherwise show from the side), and update the
// _printerValPrev
if ( strcmp( _printerVal, _printerValPrev ) != 0 ) {
//std::cout << "\t_printerVal is different than _printerValPrev: " << std::endl;
if ( strlen(_printerValPrev) > strlen( _printerVal ) ){
diff = strlen(_printerValPrev) - strlen(_printerVal );
//std::cout << "\tdiff: " << diff << std::endl;
}
}
// Pause interrupts while we set the font details and placement. This prevents other
// updates that may be triggered via an interrupt from unintentionally injecting
// print values into this location. For example, this happens if a text anchor is
// updated in an interrupt function triggered by an encoder (if the encoder is spun
// too quickly).
if ( _allowInterruptPauses == true ) noInterrupts();
//std::cout << "getBuffer: " << GFXHandler->getBuffer() << std::endl;
//GFXHandler.setRotation(1);
GFXHandler->setTextSize( _textSize );
GFXHandler->setFont( NULL );
//int realX = 0;
// If right align is enabled, then set X to be n characters to left of _x,
// where n = length of _printerVal
if ( _rightAlign == true ){
GFXHandler->setCursor( _x - (strlen(_printerVal) + diff) * _fontCharWidth, _y );
}
else {
GFXHandler->setCursor( _x, _y );
}
GFXHandler->setTextColor( colFg, colBg );
/*
std::cout << "Printing: " << _printerVal << std::endl;
std::cout << "\t_x: " << _x << std::endl;
std::cout << "\t_y: " << _y << std::endl;
std::cout << "\t_fgColor: " << _fgColor << std::endl;
std::cout << "\t_bgColor: " << _bgColor << std::endl;
std::cout << "\t_printerValPrev: " << _printerValPrev << std::endl;
*/
if ( diff > 0 && _rightAlign == true ){
for ( int i = 0; diff > i; i++ ){
//std::cout << "\tAdding prefix space #" << i << std::endl;
GFXHandler->print(" ");
}
}
GFXHandler->print( _printerVal );
if ( diff > 0 && _rightAlign == false ){
for ( int i = 0; diff > i; i++ ){
//std::cout << "\tAdding suffix space #" << i << std::endl;
GFXHandler->print(" ");
}
}
// If the printed text isn't the same as the text printed last time, then check the difference
// in length (to override the text that would otherwise show from the side), and update the
// _printerValPrev
if ( strcmp( _printerVal, _printerValPrev ) != 0 ) {
_lastChangeMs = millis();
//std::cout << "\t_printerVal is different than _printerValPrev: " << std::endl;
/*
if ( strlen(_printerValPrev) > strlen( _printerVal ) ){
int diff = strlen(_printerValPrev) - strlen(_printerVal );
//std::cout << "\tdiff: " << diff << std::endl;
for ( int i = 0; diff > i; i++ ){
//std::cout << "\tAdding space #" << i << std::endl;
GFXHandler->print(" ");
}
}
*/
strcpy(_printerValPrev, _printerVal);
}
if ( _allowInterruptPauses == true ) interrupts();
return this;
}
// User wants to prepend (or append, in a diff function) a string
// to the beginning of the char value..
TextAnchor * TextAnchor::prepend( char *prependTxt ){
if ( strlen( _printerVal ) == 0 )
{
// Nothing to append anything to, just switch to a regular print
return print( prependTxt );
//return ;
}
char newCharValue[ strlen(prependTxt) + strlen(_printerVal) +1 ]; // add +1 for end?...
//char newCharValue[ MAX_CHAR_LENGTH ];
strcpy( newCharValue, prependTxt );
//strncpy( newCharValue, prependTxt, MAX_CHAR_LENGTH );
strcat( newCharValue, _printerVal );
// Print the new char (with the prefix prepended)
print( newCharValue );
return this;
}
TextAnchor * TextAnchor::append( char *appendTxt ){
if ( strlen( _printerVal ) == 0 )
{
// Nothing to append anything to, just switch to a regular print
return print( appendTxt );
}
char newCharValue[ strlen(appendTxt) + strlen(_printerVal) +1 ]; // add +1 for end?...
//char newCharValue[ MAX_CHAR_LENGTH ];
strcpy( newCharValue, _printerVal );
//strcpy( newCharValue, prependTxt );
//strncpy( newCharValue, prependTxt, MAX_CHAR_LENGTH );
strcat( newCharValue, appendTxt );
// Print the new char (with the prefix prepended)
print( newCharValue );
return this;
}
TextAnchor * TextAnchor::clear(){
return this;
}
TextAnchor * TextAnchor::clearFromDisplay(){
GFXHandler->setTextSize( _textSize );
//GFXHandler->setFont( NULL );
GFXHandler->setTextColor( _bgColor, _bgColor );
GFXHandler->setCursor( _x, _y );
GFXHandler->print(_printerVal);
GFXHandler->setTextColor( _fgColor, _bgColor );
return this;
}
TextAnchor * TextAnchor::incrementPos ( int x, int y ){
return move( _x+x, _y+y);
}
// Move a text box to a new location. This will involve clearing the current location (covering it with
// the background color), moving the cursor to the new location, and printing the content.
TextAnchor * TextAnchor::move( int x, int y ){
clearFromDisplay();
setCursor( x, y );
print();
return this;
}
/*
void setFont(const GFXfont *f = NULL);
GFXHandler->setFont(GFXfont);
//GFXHandler->setFont(&FreeMonoBoldOblique12pt7b);
}
*/
TextAnchor * TextAnchor::setFontSize( int ftSize ){
_textSize = ftSize;
print();
return this;
}
TextAnchor * TextAnchor::setFontSize( int ftSize, bool reprint ){
_textSize = ftSize;
print();
return this;
}
TextAnchor * TextAnchor::setColor( uint16_t fgColor ){
_fgColor = fgColor;
print();
return this;
}
TextAnchor * TextAnchor::setColor( uint16_t fgColor, uint16_t bgColor ){
_fgColor = fgColor;
_bgColor = bgColor;
//std::cout << "Inside: TextAnchor::setColor" << std::endl;
//std::cout << "\t_fgColor: " << _fgColor << std::endl;
//std::cout << "\t_bgColor: " << _bgColor << std::endl;
print();
return this;
}
TextAnchor * TextAnchor::setColor( uint16_t fgColor, uint16_t bgColor, uint16_t fgHlColor, uint16_t bgHlColor ){
return this;
}
TextAnchor * TextAnchor::setHighlightColor( uint16_t fgHlColor ){
_fgHlColor = fgHlColor;
if ( _highlightStatus == true ) print();
return this;
}
TextAnchor * TextAnchor::setHighlightColor( uint16_t fgHlColor, uint16_t bgHlColor ){
_fgHlColor = fgHlColor;
_bgHlColor = bgHlColor;
//std::cout << "Inside: TextAnchor::setColor" << std::endl;
//std::cout << "\t_fgColor: " << _fgColor << std::endl;
//std::cout << "\t_bgColor: " << _bgColor << std::endl;
if ( _highlightStatus == true ) print();
return this;
}
TextAnchor * TextAnchor::highlight( void ){
_highlightStatus = !_highlightStatus;
//std::cout << "In highlight - highlight:" << _highlightStatus << std::endl;
/*
if ( _highlightStatus == true ){
setColor( _bgColor, _fgColor );
}
else {
setColor( _fgColor, _bgColor );
}
*/
return print();
//return this;
}
// Set highlight status
TextAnchor * TextAnchor::highlight( bool highlight ){
_highlightStatus = highlight;
if ( _highlightStatus == true ){
setColor( _bgColor, _fgColor );
}
else {
setColor( _fgColor, _bgColor );
}
print();
return this;
}
uint32_t TextAnchor::lastChangeMs(void){
return _lastChangeMs;
}
void TextAnchor::_appendInt( const int intValue ){
char cstr[MAX_CHAR_LENGTH];
itoa( intValue, cstr, 10 );
append( cstr );
}
void TextAnchor::_prependInt( const int intValue ){
char cstr[MAX_CHAR_LENGTH];
itoa( intValue, cstr, 10 );
prepend( cstr );
}
void TextAnchor::_appendDouble( const double intValue ){
char cstr[MAX_CHAR_LENGTH];
dtostrf(intValue, 2, 2, cstr);
append( cstr );
}
void TextAnchor::_prependDouble( const double intValue ){
char cstr[MAX_CHAR_LENGTH];
dtostrf(intValue, 2, 2, cstr);
prepend( cstr );
}
void TextAnchor::_printInt( const int intValue ){
//std::cout << "The value " << intValue << " is a REGULAR int" << std::endl;
char cstr[MAX_CHAR_LENGTH];
itoa( intValue, cstr, 10 );
print(cstr);
}
void TextAnchor::_printUInt( const unsigned int intValue ){
//std::cout << "The value " << intValue << " is an UNSIGNED REGULAR int" << std::endl;
char cstr[MAX_CHAR_LENGTH];
itoa( intValue, cstr, 10 );
print(cstr);
//print(cstr);
}
void TextAnchor::_printLong( const long intValue ){
//std::cout << "The value " << intValue << " is a LONG int" << std::endl;
char cstr[MAX_CHAR_LENGTH];
ltoa( intValue, cstr, 10 );
if ( _autoReprint == true ) print(cstr);
}
void TextAnchor::_printULong( const unsigned long intValue ){
//std::cout << "The value " << intValue << " is a UNSIGNED LONG int" << std::endl;
char cstr[MAX_CHAR_LENGTH];
ltoa( intValue, cstr, 10 );
print(cstr);
}
void TextAnchor::_printFloat( const float intValue ){
//std::cout << "The value " << intValue << " is a FLOAT int" << std::endl;
char cstr[MAX_CHAR_LENGTH];
dtostrf(intValue, 2, 2, cstr);
print(cstr);
}
void TextAnchor::_printDouble( const double intValue ){
//std::cout << "The value " << intValue << " is a DOUBLE int" << std::endl;
char cstr[MAX_CHAR_LENGTH];
dtostrf(intValue, 2, 2, cstr);
print(cstr);
}
uint16_t TextAnchor::charWidth(const char ch)
{
char onecharstring[2] = {ch, 0};
int16_t x1, y1;
uint16_t w, h;
GFXHandler->getTextBounds(onecharstring, 0, 0, &x1, &y1, &w, &h);
return w;
}
uint16_t TextAnchor::textWidth(const char* text)
{
int16_t x1, y1;
uint16_t w, h;
GFXHandler->getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
return w;
}
/*
// User provides a new NUMERICAL value, store it as a char
void TextAnchor::print( int intValue )
{
//typeof( intValue ) MyType1;
std::cout << "printing int :" << intValue << std::endl;
//std::cout << "MyType1:" << std::endl;
//this->TypeOf(intValue);
//typeof( 12345 ) an_int;
//typedef typeof( intValue ) MyType2;
//std::cout << "MyType2:" << MyType2 << std::endl;
char cstr[MAX_CHAR_LENGTH];
ltoa( intValue, cstr, 10 );
print(cstr);
}
*/
/*
// User provides a new NUMERICAL value, store it as a char
void TextAnchor::print( float intValue )
{
//std::cout << "Type of intValue::" << typeid(intValue).name() << std::endl;
std::cout << "Printing a DOUBLE:" << intValue << std::endl;
char cstr[MAX_CHAR_LENGTH];
//ltoa( intValue, cstr, 10 );
// dtostrf(float_value, min_width, num_digits_after_decimal, where_to_store_string)
dtostrf(intValue, 2, 2, cstr);
print(cstr);
}
*/
/*
char* TextAnchor::TypeOf( const double& )
{
static const char type[] = "double";
return type;
}
char* TextAnchor::TypeOf( const double* )
{
static const char type[] = "double*";
return type;
}
char* TextAnchor::TypeOf( const float& )
{
static const char type[] = "float";
return type;
}
char* TextAnchor::TypeOf( const float* )
{
static const char type[] = "float*";
return type;
}
char* TextAnchor::TypeOf(const int&)
{
static const char type[] = "int";
return type;
}
char* TextAnchor::TypeOf(const int*)
{
static const char type[] = "int*";
return type;
}
*/