forked from antiprism/mpd_oled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.cpp
295 lines (254 loc) · 9.01 KB
/
display.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
/*
Copyright (c) 2018, Adrian Rossiter
Antiprism - http://www.antiprism.com
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.
*/
#include "display.h"
#include <vector>
#include <string>
#include <algorithm>
using std::vector;
using std::string;
void print(ArduiPi_OLED &display, const char *str)
{
int sz = strlen(str);
for(int i=0; i<sz; i++)
display.write((uint8_t)str[i]);
}
int draw_spectrum(ArduiPi_OLED &display, int x_start, int y_start, int width,
int height, const spect_graph &spect)
{
const int num_bars = spect.heights.size();
const int gap = spect.gap;
int total_bar_pixes = width-(num_bars-1)*gap;
int bar_width = total_bar_pixes / num_bars;
int bar_height_max = height - 1;
int graph_width = num_bars*bar_width + (num_bars-1)*gap;
if(bar_width < 1 || bar_height_max < 1) // bars too small to draw
return -1;
// Draw spectrum graph axes
display.drawFastHLine(x_start, height - 1 - y_start, graph_width, WHITE);
for (int i=0; i<num_bars; i++) {
// map vals range to graph ht
int val = bar_height_max * spect.heights[i] / 255.0 + 0.5;
int x = x_start + i*(bar_width+gap);
// int y = y_start+2;
if(val)
display.fillRect(x, y_start + height - val - 2, bar_width, val, WHITE);
}
return 0;
}
// Draw mpd state
void draw_mpd_state(ArduiPi_OLED &display, const display_info &disp_info, int x_start, int y_start,
int width, int height, enum mpd_state state)
{
string state_text = "";
if (state == MPD_STATE_STOP)
state_text = "STOP";
else if (state == MPD_STATE_PLAY)
state_text = "PLAY";
else if (state == MPD_STATE_PAUSE)
state_text = "PAUSED";
else
state_text = "STATE UNKNOWN";
vector<double> scroll_origin(disp_info.scroll.begin()+2,
disp_info.scroll.begin()+4);
draw_text_scroll(display, 1, 3, 5, state_text, scroll_origin, disp_info.text_change.secs(), 2);
display.drawFastHLine(x_start, height-1-y_start, width, WHITE);
}
// Draw moOde logo
void draw_moode_logo(ArduiPi_OLED &display)
{
string moode_str = "moOde";
string tm_str = "TM";
string version_str = "v 6.4.2"
display.setFont(&FreeSansBold18pt7b);
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(2, 35);
print(display, moode_str.c_str());
display.setFont();
display.setCursor(117, 8);
print(display, tm_str.c_str());
display.setCursor(80, 40);
print(display, version_str.c_str());
}
// Draw time, according to clock_format: 0-3
void draw_time(ArduiPi_OLED &display, int start_x, int start_y, int sz,
int clock_format)
{
display.setTextColor(WHITE);
time_t t = time(0);
struct tm *now = localtime(&t);
const size_t STR_SZ = 32;
char str[STR_SZ];
const char *fmts[] = { "%H:%M", "%k:%M", "%I:%M", "%l:%M" };
if (clock_format >= 0 || clock_format < (int)sizeof(fmts))
strftime(str, STR_SZ, fmts[clock_format], now);
else
str[0] = '\0';
display.setCursor(start_x, start_y);
display.setTextSize(sz);
print(display, str);
int W = 6; // width of a character box
int N = 5; // number of character
if (clock_format > 1 && now->tm_hour >= 12)
display.fillRect(start_x+W*N*sz, start_y, sz, sz, WHITE);
}
// Draw date - DD-MM-YYYY
void draw_date(ArduiPi_OLED &display, int start_x, int start_y, int sz)
{
display.setTextColor(WHITE);
time_t t = time(0);
struct tm *now = localtime(&t);
const size_t STR_SZ = 32;
char str[STR_SZ];
strftime(str, STR_SZ, "%d-%m-%Y", now);
display.setCursor(start_x, start_y);
display.setTextSize(sz);
print(display, str);
}
// Draw a connection indicator, 12x8
void draw_connection(ArduiPi_OLED &display, int x_start, int y_start,
const connection_info &conn)
{
const int char_ht = 8;
if (conn.get_type() == connection_info::TYPE_WIFI) {
for(int i=0; i<4; i++) {
int ht = 2*i + 1;
int x_off = 3*i + 1;
if (conn.get_link() > 20*i)
display.fillRect(x_start+x_off, y_start+char_ht-(1+ht), 2, ht, WHITE);
else
break;
}
}
else if (conn.get_type() == connection_info::TYPE_ETH) {
int w = 10;
display.drawPixel(x_start+3, y_start, WHITE);
display.drawFastHLine(x_start+1+1, y_start+1, w-1, WHITE);
display.drawFastHLine(x_start+1, y_start+2, w, WHITE);
display.drawFastHLine(x_start+1, y_start+4, w, WHITE);
display.drawFastHLine(x_start+1, y_start+5, w-1, WHITE);
display.drawPixel(x_start+1+w-3, y_start+6, WHITE);
}
}
// Draw a slider
void draw_slider(ArduiPi_OLED &display, int x_start, int y_start, int width,
int height, float percent)
{
const int in_width = (width - 2) * percent/100.0 + 0.5;
display.drawRect(x_start, y_start, width, height, WHITE);
display.fillRect(x_start+1, y_start+1, in_width, height-2, WHITE);
}
// Draw solid slider
void draw_solid_slider(ArduiPi_OLED &display, int x_start, int y_start,
int width, int height, float percent)
{
const int bar_width = width * percent/100.0 + 0.5;
display.fillRect(x_start, y_start, bar_width, height, WHITE);
}
// Draw triangle slider
void draw_triangle_slider(ArduiPi_OLED &display, int x_start, int y_start,
int width, int height, float percent)
{
const float frac = percent/100;
display.fillTriangle(
x_start, y_start + height-1,
x_start + (width-1)*frac, y_start + height-1,
x_start + (width-1)*frac, y_start + (height-1)*(1-frac), WHITE);
}
// Draw text
void draw_text(ArduiPi_OLED &display, int x_start, int y_start, int max_len,
string str, int sz)
{
if ((int)str.size() > max_len)
str.resize(max_len);
display.setTextColor(WHITE);
display.setCursor(x_start, y_start);
display.setTextSize(sz);
print(display, str.c_str());
}
// Draw text
void draw_text_scroll(ArduiPi_OLED &display, int x_start, int y_start,
int max_len, string str, vector<double> scroll, double secs, int sz)
{
if ((int)str.size() <= max_len) {
draw_text(display, x_start, y_start, max_len, str);
return;
}
const double pixels_per_sec = scroll[0];
const double scroll_after_secs = scroll[1];
int size = sz;
int W = 6*size;
str += " ";
double elapsed = secs - scroll_after_secs;
int pix_shift = (elapsed<0) ? 0.0
: int(elapsed*pixels_per_sec + 0.5) % (str.size()*W);
int pix_offset = pix_shift % W;
int char_pix_offset = (W-pix_offset)%W;
int char_shift = pix_shift / W + (char_pix_offset>0);
std::rotate(str.begin(), str.begin()+char_shift, str.end());
//fprintf(stderr, "%c : %4d : %4d : %4d : %4d\n", str[0], pix_shift, pix_offset, char_pix_offset, char_shift);
display.setTextColor(WHITE);
display.setTextSize(size);
display.setCursor(x_start, y_start);
// Draw first partial character
if(char_pix_offset>0)
display.drawCharPart(x_start, y_start, W - char_pix_offset, W,
str[str.size()-1], WHITE, BLACK, size);
str.resize(max_len+1);
display.setCursor(x_start+char_pix_offset, y_start);
// Draw intermediate characters
print(display, str.substr(0, max_len-1).c_str());
// Draw last partial character
display.drawCharPart(x_start + (max_len-1)*W + char_pix_offset, y_start,
0, pix_offset ? pix_offset : W, str[max_len-1], WHITE, BLACK, size);
}
static void set_rotation(ArduiPi_OLED &display, bool upside_down)
{
if(upside_down) {
display.sendCommand(0xA0);
display.sendCommand(0xC0);
}
else {
display.sendCommand(0xA1);
display.sendCommand(0xC8);
}
}
bool init_display(ArduiPi_OLED &display, int oled, unsigned char i2c_addr,
int i2c_bus, int reset_gpio, int spi_dc_gpio, int spi_cs, bool rotate180)
{
if (display.oled_is_spi_proto(oled)) {
// SPI change parameters to fit to your LCD
if ( !display.init(spi_dc_gpio, reset_gpio, spi_cs, oled) )
return false;
bcm2835_spi_set_speed_hz(1e6); // ~1MHz
}
else {
// I2C change parameters to fit to your LCD
if ( !display.init(reset_gpio, oled, i2c_addr, i2c_bus) )
return false;
}
display.begin();
set_rotation(display, rotate180);
display.setTextWrap(false);
// init done
display.clearDisplay(); // clears the screen buffer
display.display(); // display it (clear display)
return true;
}