forked from fedetft/mxgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.h
646 lines (569 loc) · 19.7 KB
/
display.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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/***************************************************************************
* Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 by Terraneo Federico *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or use *
* macros or inline functions from this file, or you compile this file *
* and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with the GNU General *
* Public License. This exception does not invalidate any other reasons *
* why a work based on this file might be covered by the GNU General *
* Public License. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#ifndef DISPLAY_H
#define DISPLAY_H
#include <vector>
#include <string>
#include <utility>
#include <pthread.h>
#include <config/mxgui_settings.h>
#include "point.h"
#include "color.h"
#include "font.h"
#include "image.h"
namespace mxgui {
class Display; //Forward declaration
class DrawingContext;
/**
* \ingroup pub_iface
* This class allows to register and retrieve one or more displays in the
* system. Selected boards supported by miosix already have a display driver
* provided. When working with those boards, the driver will be already
* registered.
* This design also allows to support multiple displays connected to the same
* microcontroller.
*/
class DisplayManager
{
public:
/**
* \return a reference to the instance of the DisplayManager
* Multiple calls return the same display instance (singleton)
*/
static DisplayManager& instance();
/**
* \param id display id. This allows to select one of the registered
* displays for boards that have multiple displays.
* \return a reference to the display class with the given id
* \throws exception if no display with the given id exists
*/
Display& getDisplay(unsigned int id=0);
/**
* Register a display to the display manager.
* \param display a pointer to the new display to be registered. Note that
* there is no way to unregister a display. This is done on purpose, as
* the applications may cache the reference returned by getDisplay(),
* and so removing displays may cause dangling pointers. Moreover, display
* subclasses are expected to be singletons.
* \return the display id associated with the registered display
*/
int registerDisplay(Display *display);
private:
DisplayManager(const DisplayManager&)=delete;
DisplayManager& operator=(const DisplayManager&)=delete;
/**
* Constructor
*/
DisplayManager();
pthread_mutex_t mutex; ///< Mutex to support concurrent access
std::vector<Display*> displays; ///< Registered displays
};
/**
* If a board has a display driver, this function should be implemented by it
* to register the display. An attempt to compile mxgui for a board for which
* no display driver is provided will result in an undefined reference to this
* function. It can be implemented by writing a custom display driver and
* registering it.
*/
void registerDisplayHook(DisplayManager& dm);
/**
* \ingroup pub_iface
* Display class. This is the base class from which all display drivers should
* derive. Contains member functions to to turn the display on or off.
* For drawing onto the display, you need to instantiate a DrawingContext that
* will lock the display mutex and allow thread-safe display access.
*/
class Display
{
public:
/**
* Constructor
*/
Display();
/**
* Turn the display On after it has been turned Off.
* Display initial state is On.
*/
void turnOn();
/**
* Turn the display Off. It can be later turned back On.
*/
void turnOff();
/**
* Set display brightness. Depending on the underlying driver,
* may do nothing.
* \param brt from 0 to 100
*/
void setBrightness(int brt);
/**
* \return true if the display is on
*/
bool isOn() const { return isDisplayOn; }
/**
* \return a pair with the display height and width
*/
std::pair<short int, short int> getSize() const
{
return doGetSize();
}
/**
* \return the display's height
*/
short int getHeight() const { return doGetSize().first; }
/**
* \return the display's width
*/
short int getWidth() const { return doGetSize().second; }
/**
* Destructor
*/
virtual ~Display();
protected:
/**
* Turn the display On after it has been turned Off.
* Display initial state is On.
*/
virtual void doTurnOn()=0;
/**
* Turn the display Off. It can be later turned back On.
*/
virtual void doTurnOff()=0;
/**
* Set display brightness. Depending on the underlying driver,
* may do nothing.
* \param brt from 0 to 100
*/
virtual void doSetBrightness(int brt)=0;
/**
* \return a pair with the display height and width
*/
virtual std::pair<short int, short int> doGetSize() const=0;
/**
* Write text to the display. If text is too long it will be truncated
* \param p point where the upper left corner of the text will be printed
* \param text, text to print.
*/
virtual void write(Point p, const char *text)=0;
/**
* Write part of text to the display
* \param p point of the upper left corner where the text will be drawn.
* Negative coordinates are allowed, as long as the clipped view has
* positive or zero coordinates
* \param a Upper left corner of clipping rectangle
* \param b Lower right corner of clipping rectangle
* \param text text to write
*/
virtual void clippedWrite(Point p, Point a, Point b, const char *text)=0;
/**
* Clear the Display. The screen will be filled with the desired color
* \param color fill color
*/
virtual void clear(Color color)=0;
/**
* Clear an area of the screen
* \param p1 upper left corner of area to clear
* \param p2 lower right corner of area to clear
* \param color fill color
*/
virtual void clear(Point p1, Point p2, Color color)=0;
/**
* This member function is used on some target displays to reset the
* drawing window to its default value. You have to call beginPixel() once
* before calling setPixel(). You can then make any number of calls to
* setPixel() without calling beginPixel() again, as long as you don't
* call any other member function in this class. If you call another
* member function, for example line(), you have to call beginPixel() again
* before calling setPixel().
*/
virtual void beginPixel()=0;
/**
* Draw a pixel with desired color. You have to call beginPixel() once
* before calling setPixel()
* \param p point where to draw pixel
* \param color pixel color
*/
virtual void setPixel(Point p, Color color)=0;
/**
* Draw a line between point a and point b, with color c
* \param a first point
* \param b second point
* \param color line color
*/
virtual void line(Point a, Point b, Color color)=0;
/**
* Draw an horizontal line on screen.
* Instead of line(), this member function takes an array of colors to be
* able to individually set pixel colors of a line.
* \param p starting point of the line
* \param colors an array of pixel colors whoase size must be b.x()-a.x()+1
* \param length length of colors array.
* p.x()+length must be <= display.width()
*/
virtual void scanLine(Point p, const Color *colors, unsigned short length)=0;
/**
* \return a buffer of length equal to this->getWidth() that can be used to
* render a scanline.
*/
virtual Color *getScanLineBuffer()=0;
/**
* Draw the content of the last getScanLineBuffer() on an horizontal line
* on the screen.
* \param p starting point of the line
* \param length length of colors array.
* p.x()+length must be <= display.width()
*/
virtual void scanLineBuffer(Point p, unsigned short length)=0;
/**
* Draw an image on the screen
* \param p point of the upper left corner where the image will be drawn
* \param img image to draw
*/
virtual void drawImage(Point p, const ImageBase& img)=0;
/**
* Draw part of an image on the screen
* \param p point of the upper left corner where the image will be drawn.
* Negative coordinates are allowed, as long as the clipped view has
* positive or zero coordinates
* \param a Upper left corner of clipping rectangle
* \param b Lower right corner of clipping rectangle
* \param img Image to draw
*/
virtual void clippedDrawImage(Point p, Point a, Point b, const ImageBase& img)=0;
/**
* Draw a rectangle (not filled) with the desired color
* \param a upper left corner of the rectangle
* \param b lower right corner of the rectangle
* \param c color of the line
*/
virtual void drawRectangle(Point a, Point b, Color c)=0;
/**
* Set colors used for writing text
* \param colors a pair with the text foreground and background colors
*/
void setTextColor(std::pair<Color,Color> colors);
/**
* \return a pair with the foreground and background colors.
* Those colors are used to draw text on screen
*/
std::pair<Color,Color> getTextColor() const;
/**
* Set the font used for writing text
* \param font new font
*/
void setFont(const Font& font);
/**
* \return the current font used to draw text
*/
Font getFont() const;
/**
* Make all changes done to the display since the last call to update()
* visible. Backends that require it may override this.
*/
virtual void update();
private:
Display(const Display&)=delete;
Display& operator=(const Display&)=delete;
pthread_mutex_t dispMutex; ///< To lock concurrent access to the display
bool isDisplayOn; ///< True if display is on
protected:
Font font; ///< Current font selected for writing text
/// textColors[0] is the background color, textColor[3] the foreground
/// while the other two are the intermediate colors for drawing antialiased
/// fonts. They remain just for compatibilty, as this screen in monochrome
Color textColor[4];
friend class DrawingContext;
};
/**
* \ingroup pub_iface
* A drawing context is a class that is instantiated whenever there is the
* need to draw something on a display. Its primary purpose is to lock a mutex
* allowing safe concurrent access to a display from multiple threads, but
* avoiding the overhead of locking a mutex for each single graphic primitive
* call.
*/
class DrawingContext
{
public:
/**
* Constructor
* \param display the display on which you want to draw
*/
DrawingContext(Display& display) : display(display)
{
pthread_mutex_lock(&display.dispMutex);
}
/**
* \return a pair with the display height and width
*/
std::pair<short int, short int> getSize() const
{
return display.doGetSize();
}
/**
* \return the display's height
*/
short int getHeight() const
{
return display.doGetSize().first;
}
/**
* \return the display's width
*/
short int getWidth() const
{
return display.doGetSize().second;
}
/**
* Write text to the display. If text is too long it will be truncated
* \param p point where the upper left corner of the text will be printed
* \param text, text to print.
*/
void write(Point p, const char *text)
{
display.write(p,text);
}
/**
* Write text to the display. If text is too long it will be truncated
* \param p point where the upper left corner of the text will be printed
* \param text, text to print.
*/
void write(Point p, const std::string& text)
{
display.write(p,text.c_str());
}
/**
* Write part of text to the display
* \param p point of the upper left corner where the text will be drawn.
* Negative coordinates are allowed, as long as the clipped view has
* positive or zero coordinates
* \param a Upper left corner of clipping rectangle
* \param b Lower right corner of clipping rectangle
* \param text text to write
*/
void clippedWrite(Point p, Point a, Point b, const char *text)
{
display.clippedWrite(p,a,b,text);
}
/**
* Write part of text to the display
* \param p point of the upper left corner where the text will be drawn.
* Negative coordinates are allowed, as long as the clipped view has
* positive or zero coordinates
* \param a Upper left corner of clipping rectangle
* \param b Lower right corner of clipping rectangle
* \param text text to write
*/
void clippedWrite(Point p, Point a, Point b, const std::string& text)
{
display.clippedWrite(p,a,b,text.c_str());
}
/**
* Clear the Display. The screen will be filled with the desired color
* \param color fill color
*/
void clear(Color color)
{
display.clear(color);
}
/**
* Clear an area of the screen
* \param p1 upper left corner of area to clear
* \param p2 lower right corner of area to clear
* \param color fill color
*/
void clear(Point p1, Point p2, Color color)
{
display.clear(p1,p2,color);
}
/**
* This member function is used on some target displays to reset the
* drawing window to its default value. You have to call beginPixel() once
* before calling setPixel(). You can then make any number of calls to
* setPixel() without calling beginPixel() again, as long as you don't
* call any other member function in this class. If you call another
* member function, for example line(), you have to call beginPixel() again
* before calling setPixel().
*/
void beginPixel()
{
display.beginPixel();
}
/**
* Draw a pixel with desired color. You have to call beginPixel() once
* before calling setPixel()
* \param p point where to draw pixel
* \param color pixel color
*/
void setPixel(Point p, Color color)
{
display.setPixel(p,color);
}
/**
* Draw a line between point a and point b, with color c
* \param a first point
* \param b second point
* \param color line color
*/
void line(Point a, Point b, Color color)
{
display.line(a,b,color);
}
/**
* Draw an horizontal line on screen.
* Instead of line(), this member function takes an array of colors to be
* able to individually set pixel colors of a line.
* \param p starting point of the line
* \param colors an array of pixel colors whoase size must be b.x()-a.x()+1
* \param length length of colors array.
* p.x()+length must be <= display.width()
*/
void scanLine(Point p, const Color *colors, unsigned short length)
{
display.scanLine(p,colors,length);
}
/**
* \return a buffer of length equal to this->getWidth() that can be used to
* render a scanline.
*/
Color *getScanLineBuffer()
{
return display.getScanLineBuffer();
}
/**
* Draw the content of the last getScanLineBuffer() on an horizontal line
* on the screen.
* \param p starting point of the line
* \param length length of colors array.
* p.x()+length must be <= display.width()
*/
void scanLineBuffer(Point p, unsigned short length)
{
display.scanLineBuffer(p,length);
}
/**
* Draw an image on the screen
* \param p point of the upper left corner where the image will be drawn
* \param img image to draw
*/
void drawImage(Point p, const ImageBase& img)
{
display.drawImage(p,img);
}
/**
* Draw part of an image on the screen
* \param p point of the upper left corner where the image will be drawn.
* Negative coordinates are allowed, as long as the clipped view has
* positive or zero coordinates
* \param a Upper left corner of clipping rectangle
* \param b Lower right corner of clipping rectangle
* \param img Image to draw
*/
void clippedDrawImage(Point p, Point a, Point b, const ImageBase& img)
{
display.clippedDrawImage(p,a,b,img);
}
/**
* Draw a rectangle (not filled) with the desired color
* \param a upper left corner of the rectangle
* \param b lower right corner of the rectangle
* \param c color of the line
*/
void drawRectangle(Point a, Point b, Color c)
{
display.drawRectangle(a,b,c);
}
/**
* Set colors used for writing text
* \param fgcolor text color
* \param bgcolor background color
*/
void setTextColor(Color fgcolor, Color bgcolor)
{
display.setTextColor(std::make_pair(fgcolor,bgcolor));
}
/**
* Set colors used for writing text
* \param colors a pair where first is the foreground color, and second the
* background one
*/
void setTextColor(std::pair<Color,Color> colors)
{
display.setTextColor(colors);
}
/**
* \return the current foreground color.
* The foreground color is used to draw text on screen
*/
Color getForeground() const
{
return display.getTextColor().first;
}
/**
* \return the current background color.
* The foreground color is used to draw text on screen
*/
Color getBackground() const
{
return display.getTextColor().second;
}
/**
* \return a pari with the foreground and background color
*/
std::pair<Color,Color> getTextColor() const
{
return display.getTextColor();
}
/**
* Set the font used for writing text
* \param font new font
*/
void setFont(const Font& font)
{
display.setFont(font);
}
/**
* \return the current font used to draw text
*/
Font getFont() const
{
return display.getFont();
}
/**
* Destructor
*/
~DrawingContext()
{
display.update();
pthread_mutex_unlock(&display.dispMutex);
}
private:
DrawingContext(const DrawingContext&)=delete;
DrawingContext& operator=(DrawingContext&)=delete;
Display& display; ///< Underlying display object
};
} //namespace mxgui
#endif //DISPLAY_H