-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay_helper.h
175 lines (157 loc) · 5.7 KB
/
display_helper.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
/*
* Copyright 2018 Justin Schoeman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _DISPLAY_HELPER_H_
#define _DISPLAY_HELPER_H_
/*
* helpers for the various display modules...
* make sure libraries are included before
* including this file
*
* TODO:
* When using multiple displays, we will need to share the frame buffer - so we
* need to add a flag to tell display modules when they need to do a complete
* redraw, and when they can just update the required areas...
*
*/
#include "module.h"
// default display update rate 10Hz
#define DISPLAY_INTERVAL 100L
/*
* main process defines an array of display modules
* which will be rendered in display loop
*
* A display module is just a module with an extra display attribute
* (used to link the module to the display it should render on)
*
*/
class display;
class display_module: public module {
public:
display_module(const char * id, display& d_): module(id), d(d_) {};
display& d;
};
extern display_module * display_modules[];
/*
*
* this is ugly shit...
*
* Adafruit_GFX class does not have sufficient methods to make the PCD8544 class even display anything
* so we can't abstract display types directly.
* Introspection does not work on arduino, so can't cast back up.
* I can't figure out how to get siblings to inherit a single base class copy (diamond problem)
* So end result is a container object which holds the base class and abstract
* container object which contains the abstract view of it (with additional required methods)
*
* display is the generalised display class, which must have all functions required to operate all displays
* (note: only operate - instantiate and configure is done in the derived class)
*/
class display: public module {
public:
display(const char * id, Adafruit_GFX& g): module(id), gfx(g) {
nextflush = millis(); // render immediately
dm = -1; // no current module
};
// standard Arduino setup and loop, called from ModuleHelper
virtual void setup(void) {}; // for module.h
virtual void loop(unsigned long& ms) {
// by default, only update display every 100ms
// dm is the last module index which was updated
if(dm >= 0 || ms >= nextflush) {
//Serial.print("foo "); Serial.println(dm);
if(dm < 0) {
dm = 0;
clear();
} else dm++;
// find next our module
for(;;) {
if(!display_modules[dm]) {
// no more modules
flush(); // render display
dm = -1;
nextflush = ms + DISPLAY_INTERVAL;
return;
}
if(&(display_modules[dm]->d) == this) {
// if this is our module, run it
display_modules[dm]->loop(ms);
return;
}
dm++;
}
}
};
// these are our abstracted functions to make devices useful
// flush framebuffer to display (actually display what we have been drawing)
// default empty implementation, so only override if the display driver
// requires such a function...
virtual void flush(void) {};
// clear display - default is basic blank rectangle - override if driver provides
// a more efficient method
virtual void clear(void) {gfx.fillRect(0, 0, gfx.width(), gfx.height(), 0);}; // default clear - write white to entire area
Adafruit_GFX& gfx;
unsigned long nextflush;
int16_t dm;
};
#ifdef _ADAFRUIT_PCD8544_H
/*
* provide an abstraction of PCD8544 driver
* add extra constructors as required.
*
*/
class display_pcd8544: public display {
public:
//display_pcd8544(const char * id, Adafruit_PCD8544& d): priv(d), display(id, d) {};
display_pcd8544(const char * id, uint8_t dc, uint8_t cs, uint8_t rs): priv(dc, cs, rs), display(id, priv) {};
void setup(void) {
// setup display
priv.begin();
// FIXME - read from config
// FIXME - add UI to adjust
priv.setContrast(60);
priv.clearDisplay(); // clears the screen and buffer
}
void flush(void) {priv.display();};
void clear(void) {priv.clearDisplay();}
Adafruit_PCD8544 priv;
};
#endif
/*
* a display module to render text+number to display
*
* FIXME: should not be in this file...
*/
class number_display: public display_module {
public:
number_display(display& d, float& p, const char * txt, int16_t x, int16_t y): display_module(txt, d), p(p), txt(txt), x(x), y(y){};
void setup(void) {};
void loop(unsigned long& ms) {
//d.gfx.fillRect(x, y, d.gfx.width(), 8, 0);
d.gfx.setCursor(x, y);
d.gfx.print(txt);
d.gfx.print(" ");
d.gfx.println(p);
};
float& p;
const char * txt;
int16_t x;
int16_t y;
};
#endif