-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTft.cpp
693 lines (570 loc) · 21.1 KB
/
Tft.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#include "Tft.h"
#include "DisplayTable.h"
#include "EnergyMonitorConfig.h"
#include "EnergyData.h"
#include "OmnikGetStats.h"
#include "PvOutput.h"
#include <sstream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <wiringPi.h>
#include <signal.h>
#include <cairo/cairo-svg.h>
using namespace std;
namespace
{
const string cPictureOutputConfigNameBase = "PictureOutput";
const string cEstimateJanConfigName = "EstimateJan";
const string cEstimateFebConfigName = "EstimateFeb";
const string cEstimateMarConfigName = "EstimateMar";
const string cEstimateAprConfigName = "EstimateApr";
const string cEstimateMayConfigName = "EstimateMay";
const string cEstimateJunConfigName = "EstimateJun";
const string cEstimateJulConfigName = "EstimateJul";
const string cEstimateAugConfigName = "EstimateAug";
const string cEstimateSepConfigName = "EstimateSep";
const string cEstimateOctConfigName = "EstimateOct";
const string cEstimateNovConfigName = "EstimateNov";
const string cEstimateDecConfigName = "EstimateDec";
const int cDisplayLedPin = 1;
const int cDisplayButton1Pin = 0;
const int cDisplayButton2Pin = 3;
const int cDisplayButton3Pin = 4;
const int cDisplayButton4Pin = 2;
const int cDisplayOnTime = 60;
Tft* gTftInstance;
}
Tft::Tft(const EnergyMonitorConfig& config) :
myMonthEstimates(12),
myFramebufferDevice(-1),
myScreenSize(-1),
myScreenSurfaceMap(NULL),
myScreenSurface(NULL),
myDisplayButtonInitialized(false),
myDisplayOn(false),
myDisplayEndTime(0),
myCurrentDisplayBuffer(0)
{
gTftInstance = this;
myMonthEstimates[0] = atoi(config.getValue(cEstimateJanConfigName).c_str());
myMonthEstimates[1] = atoi(config.getValue(cEstimateFebConfigName).c_str());
myMonthEstimates[2] = atoi(config.getValue(cEstimateMarConfigName).c_str());
myMonthEstimates[3] = atoi(config.getValue(cEstimateAprConfigName).c_str());
myMonthEstimates[4] = atoi(config.getValue(cEstimateMayConfigName).c_str());
myMonthEstimates[5] = atoi(config.getValue(cEstimateJunConfigName).c_str());
myMonthEstimates[6] = atoi(config.getValue(cEstimateJulConfigName).c_str());
myMonthEstimates[7] = atoi(config.getValue(cEstimateAugConfigName).c_str());
myMonthEstimates[8] = atoi(config.getValue(cEstimateSepConfigName).c_str());
myMonthEstimates[9] = atoi(config.getValue(cEstimateOctConfigName).c_str());
myMonthEstimates[10] = atoi(config.getValue(cEstimateNovConfigName).c_str());
myMonthEstimates[11] = atoi(config.getValue(cEstimateDecConfigName).c_str());
for (int i = 0; i < NR_SCREEN_BUFFERS; ++i)
{
myBufferSurface[i] = NULL;
myScreenBufferMap[i] = NULL;
}
if (!initFrameBufferFile(config))
{
wiringPiSetup();
initFrameBuffer();
initDisplayButtons();
}
else
{
cout << "Emulating display" << endl;
}
}
Tft::~Tft()
{
cleanupDisplayButtons();
cleanupFrameBuffer();
gTftInstance = NULL;
}
void Tft::displayEnergyData(const EnergyData& energyData, const OmnikGetStats& solarData, PvOutput& pvOutput)
{
// Button 1 display
cairo_t* display1 = cairo_create(myBufferSurface[0]);
clear(display1);
displayEnergyTotals(display1, 10, 10, energyData, solarData);
displayCurrentValues(display1, 10, 150, energyData, solarData);
cairo_destroy(display1);
// Button 2 display
cairo_t* display2 = cairo_create(myBufferSurface[1]);
clear(display2);
displayEnergySummary(display2, 10, 10, energyData, solarData, pvOutput);
cairo_destroy(display2);
// Button 3 display
cairo_t* display3 = cairo_create(myBufferSurface[2]);
clear(display3);
cairo_destroy(display3);
// Button 4 display
cairo_t* display4 = cairo_create(myBufferSurface[3]);
clear(display4);
displayStatisticValues(display4, 10, 10, energyData, solarData);
cairo_destroy(display4);
apply();
}
bool Tft::initFrameBuffer()
{
myFramebufferDevice = open("/dev/fb1", O_RDWR);
if (myFramebufferDevice == -1)
{
cout << "Error opening framebuffer devide" << endl;
return false;
}
fb_var_screeninfo variableScreenInfo;
if (ioctl(myFramebufferDevice, FBIOGET_VSCREENINFO, &variableScreenInfo))
{
cout << "Error retrieving data from framebuffer" << endl;
return false;
}
memcpy(&myOriginalScreenInfo, &variableScreenInfo, sizeof(fb_var_screeninfo));
variableScreenInfo.bits_per_pixel = 8;
if (ioctl(myFramebufferDevice, FBIOPUT_VSCREENINFO, &variableScreenInfo))
{
cout << "Error sending data to framebuffer" << endl;
return false;
}
fb_fix_screeninfo fixedScreenInfo;
if (ioctl(myFramebufferDevice, FBIOGET_FSCREENINFO, &fixedScreenInfo))
{
cout << "Error retrieving fixed data from framebuffer" << endl;
return false;
}
myScreenSize = fixedScreenInfo.smem_len;
myScreenSurfaceMap = (unsigned char*)mmap(0, myScreenSize, PROT_READ | PROT_WRITE, MAP_SHARED, myFramebufferDevice, 0);
myScreenSurface = cairo_image_surface_create_for_data(
(unsigned char*)myScreenSurfaceMap,
(cairo_format_t)CAIRO_FORMAT_RGB16_565,
variableScreenInfo.xres, variableScreenInfo.yres,
cairo_format_stride_for_width((cairo_format_t)CAIRO_FORMAT_RGB16_565, variableScreenInfo.xres));
if (cairo_surface_status(myScreenSurface) != CAIRO_STATUS_SUCCESS)
{
cout << "Error creating screen surface" << endl;
return false;
}
for (int i = 0; i < NR_SCREEN_BUFFERS; ++i)
{
myScreenBufferMap[i] = static_cast<TDisplaySurfaceMap>(malloc(myScreenSize));
myBufferSurface[i] = cairo_image_surface_create_for_data(
myScreenBufferMap[i],
static_cast<cairo_format_t>(CAIRO_FORMAT_RGB16_565),
variableScreenInfo.xres, variableScreenInfo.yres,
cairo_format_stride_for_width(static_cast<cairo_format_t>(CAIRO_FORMAT_RGB16_565), variableScreenInfo.xres));
if (cairo_surface_status(myBufferSurface[i]) != CAIRO_STATUS_SUCCESS)
{
cout << "Error creating buffer surface" << endl;
return false;
}
}
return true;
}
bool Tft::initFrameBufferFile(const EnergyMonitorConfig& config)
{
bool result = false;
for (int i = 0; i < NR_SCREEN_BUFFERS; ++i)
{
stringstream fileConfigName;
fileConfigName << cPictureOutputConfigNameBase << (i + 1);
string filename = config.getValue(fileConfigName.str());
if (!filename.empty())
{
myBufferSurface[i] = cairo_svg_surface_create(filename.c_str(), 320, 240);
if (cairo_surface_status(myBufferSurface[i]) != CAIRO_STATUS_SUCCESS)
{
cout << "Error creating screen surface" << endl;
return false;
}
else
{
result = true;
}
}
}
return result;
}
void Tft::initDisplayButtons()
{
pinMode(cDisplayLedPin, OUTPUT);
displayPower(false);
pinMode(cDisplayButton1Pin, INPUT);
pinMode(cDisplayButton2Pin, INPUT);
pinMode(cDisplayButton3Pin, INPUT);
pinMode(cDisplayButton4Pin, INPUT);
pullUpDnControl(cDisplayButton1Pin, PUD_UP);
pullUpDnControl(cDisplayButton2Pin, PUD_UP);
pullUpDnControl(cDisplayButton3Pin, PUD_UP);
pullUpDnControl(cDisplayButton4Pin, PUD_UP);
wiringPiISR(cDisplayButton1Pin, INT_EDGE_FALLING, &button1Pressed);
wiringPiISR(cDisplayButton2Pin, INT_EDGE_FALLING, &button2Pressed);
wiringPiISR(cDisplayButton3Pin, INT_EDGE_FALLING, &button3Pressed);
wiringPiISR(cDisplayButton4Pin, INT_EDGE_FALLING, &button4Pressed);
signal(SIGALRM, alarmHandler);
myDisplayButtonInitialized = true;
}
void Tft::cleanupFrameBuffer()
{
if (myScreenSurface && cairo_surface_status(myScreenSurface) == CAIRO_STATUS_SUCCESS)
{
cairo_surface_destroy(myScreenSurface);
}
for (int i = 0; i < NR_SCREEN_BUFFERS; ++i)
{
if (myBufferSurface[i] && cairo_surface_status(myBufferSurface[i]) == CAIRO_STATUS_SUCCESS)
{
cairo_surface_destroy(myBufferSurface[i]);
}
if (myScreenBufferMap[i])
{
free(myScreenBufferMap[i]);
}
}
if (myScreenSurfaceMap)
{
munmap(myScreenSurfaceMap, myScreenSize);
}
if (myFramebufferDevice != -1)
{
ioctl(myFramebufferDevice, FBIOPUT_VSCREENINFO, &myOriginalScreenInfo);
close(myFramebufferDevice);
}
}
void Tft::cleanupDisplayButtons()
{
if (myDisplayButtonInitialized)
{
alarm(0);
signal(SIGALRM, SIG_DFL);
displayPower(false);
}
}
void Tft::displayEnergyTotals(cairo_t* surface, int x, int y, const EnergyData& energyData, const OmnikGetStats& solarData)
{
DisplayTable table;
// Header texts
table.setText(2, 0, 0, "Peak");
table.setText(3, 0, 0, "Off-peak");
// Row texts total
table.setText(0, 1, 0, "Total");
table.setText(1, 1, 0, "Import");
table.setText(1, 1, 1, "Export");
table.setText(1, 1, 2, "Solar");
// Row texts today
table.setText(0, 2, 0, "Today");
table.setText(1, 2, 0, "Import");
table.setText(1, 2, 1, "Export");
table.setText(1, 2, 2, "Net");
table.setText(1, 2, 3, "Solar");
table.setText(1, 2, 4, "Usage");
// Write the total numbers
table.setText(2, 1, 0, getKwhText(energyData.getTotalImportPeak()), true);
table.setText(2, 1, 1, getKwhText(energyData.getTotalExportPeak()), true);
table.setText(2, 1, 2, "");
table.setText(3, 1, 0, getKwhText(energyData.getTotalImportOffPeak()), true);
table.setText(3, 1, 1, getKwhText(energyData.getTotalExportOffPeak()), true);
table.setText(3, 1, 2, getKwhText(solarData.getGeneratedTotal()), true);
double peakUsage = energyData.getTodayNetPeak() + (energyData.isPeakDay() ? solarData.getGeneratedToday() : .0);
double offPeakUsage = energyData.getTodayNetOffPeak() + (energyData.isPeakDay() ? .0 : solarData.getGeneratedToday());
// Write the today numbers
table.setText(2, 2, 0, getKwhText(energyData.getTodayImportPeak()), true);
table.setText(2, 2, 1, getKwhText(energyData.getTodayExportPeak()), true);
table.setText(2, 2, 2, getKwhText(energyData.getTodayNetPeak()), true);
table.setText(2, 2, 3, energyData.isPeakDay() ? getKwhText(solarData.getGeneratedToday()) : "", true);
table.setText(2, 2, 4, getKwhText(peakUsage), true);
table.setText(3, 2, 0, getKwhText(energyData.getTodayImportOffPeak()), true);
table.setText(3, 2, 1, getKwhText(energyData.getTodayExportOffPeak()), true);
table.setText(3, 2, 2, getKwhText(energyData.getTodayNetOffPeak()), true);
table.setText(3, 2, 3, energyData.isPeakDay() ? "" : getKwhText(solarData.getGeneratedToday()), true);
table.setText(3, 2, 4, getKwhText(offPeakUsage), true);
table.drawTable(surface, x, y);
}
void Tft::displayCurrentValues(cairo_t* surface, int x, int y, const EnergyData& energyData, const OmnikGetStats& solarData)
{
DisplayTable table;
// Header texts
table.setText(0, 0, 0, energyData.isPeak() ? "Peak" : "Off-peak");
table.setText(1, 0, 0, "L1");
table.setText(2, 0, 0, "L2");
table.setText(3, 0, 0, "L3");
table.setText(4, 0, 0, "Total");
// Row texts
table.setText(0, 1, 0, "Usage");
table.setText(0, 2, 0, "Generation");
table.setText(0, 3, 0, "Net");
// Usage texts
table.setText(1, 1, 0, getWText(energyData.getL1Import()), true);
table.setText(2, 1, 0, getWText(energyData.getL2Import()), true);
table.setText(3, 1, 0, getWText(energyData.getL3Import()), true);
table.setText(4, 1, 0, getWText(static_cast<int>(solarData.getPower() + 1000. * energyData.getNet())), true);
// Solar text
table.setText(1, 2, 0, getWText(energyData.getL1Export()), true);
table.setText(2, 2, 0, getWText(energyData.getL2Export()), true);
table.setText(3, 2, 0, getWText(energyData.getL3Export()), true);
table.setText(4, 2, 0, getWText(static_cast<int>(solarData.getPower())), true);
// Net text
table.setText(1, 3, 0, getWText(energyData.getL1Net()), true);
table.setText(2, 3, 0, getWText(energyData.getL2Net()), true);
table.setText(3, 3, 0, getWText(energyData.getL3Net()), true);
table.setText(4, 3, 0, getWText(static_cast<int>(1000. * energyData.getNet())), true);
table.drawTable(surface, x, y);
}
void Tft::displayEnergySummary(cairo_t* surface, int x, int y, const EnergyData& energyData, const OmnikGetStats& solarData, PvOutput& pvOutput)
{
DisplayTable table;
// Row texts current
table.setText(0, 0, 0, "Current");
table.setText(1, 0, 0, "Solar");
table.setText(1, 0, 1, "Usage");
table.setText(1, 0, 2, "Net");
// Row texts today
table.setText(0, 1, 0, "Today");
table.setText(1, 1, 0, "Solar");
table.setText(1, 1, 1, "Usage");
table.setText(1, 1, 2, "Net");
// Row texts yesterday
table.setText(0, 2, 0, "Yesterday");
table.setText(1, 2, 0, "Solar");
table.setText(1, 2, 1, "Usage");
table.setText(1, 2, 2, "Net");
// Row texts month
table.setText(0, 3, 0, "Month");
table.setText(1, 3, 0, "Solar");
table.setText(1, 3, 1, "Usage");
table.setText(1, 3, 2, "Net");
// Row texts year
table.setText(0, 4, 0, "Year");
table.setText(1, 4, 0, "Solar");
table.setText(1, 4, 1, "Usage");
table.setText(1, 4, 2, "Net");
// Current power
double consumption = solarData.getPower() + 1000. * energyData.getNet();
table.setText(2, 0, 0, getWText(static_cast<int>(solarData.getPower())), true);
table.setText(2, 0, 1, getWText(static_cast<int>(consumption)), true);
table.setText(2, 0, 2, getWText(static_cast<int>(1000. * energyData.getNet())), true);
// Today
double usage = energyData.getTodayNet() + solarData.getGeneratedToday();
table.setText(2, 1, 0, getKwhText(solarData.getGeneratedToday()), true);
table.setText(2, 1, 1, getKwhText(usage), true);
table.setText(2, 1, 2, getKwhText(energyData.getTodayNet()), true);
table.setText(3, 1, 0, getPercText(solarData.getGeneratedToday() / getTodayExtimate()) + " target", true);
if (usage > .0)
{
table.setText(3, 1, 1, getPercText(solarData.getGeneratedToday() / usage) + " usage", true);
}
// Yesterday
double generated = pvOutput.getGeneratedYesterday();
double consumed = pvOutput.getConsumedYesterday();
table.setText(2, 2, 0, getKwhText(generated), true);
table.setText(2, 2, 1, getKwhText(consumed), true);
table.setText(2, 2, 2, getKwhText(consumed - generated), true);
table.setText(3, 2, 0, getPercText(generated / getYesterdayEstimate()) + " target", true);
if (consumed > .0)
{
table.setText(3, 2, 1, getPercText(generated / consumed) + " usage", true);
}
// Month to day
generated = pvOutput.getGeneratedMonth() + solarData.getGeneratedToday();
consumed = pvOutput.getConsumedMonth() + usage;
table.setText(2, 3, 0, getKwhText(generated), true);
table.setText(2, 3, 1, getKwhText(consumed), true);
table.setText(2, 3, 2, getKwhText(consumed - generated), true);
table.setText(3, 3, 0, getPercText(generated / getMonthEstimate()) + " target", true);
if (consumed > .0)
{
table.setText(3, 3, 1, getPercText(generated / consumed) + " usage", true);
}
// Year to day
generated = pvOutput.getGeneratedYear() + solarData.getGeneratedToday();
consumed = pvOutput.getConsumedYear() + usage;
table.setText(2, 4, 0, getKwhText(generated), true);
table.setText(2, 4, 1, getKwhText(consumed), true);
table.setText(2, 4, 2, getKwhText(consumed - generated), true);
table.setText(3, 4, 0, getPercText(generated / getYearEstimate()) + " target", true);
if (consumed > .0)
{
table.setText(3, 4, 1, getPercText(generated / consumed) + " usage", true);
}
table.drawTable(surface, x, y);
}
void Tft::displayStatisticValues(cairo_t* surface, int x, int y, const EnergyData& energyData, const OmnikGetStats& solarData)
{
DisplayTable table;
// Header texts
table.setText(1, 0, 0, "L1");
table.setText(2, 0, 0, "L2");
table.setText(3, 0, 0, "L3");
table.setText(4, 0, 0, "Total");
// Row texts
table.setText(0, 1, 0, "Current");
table.setText(0, 2, 0, "Today");
table.setText(0, 3, 0, "Record");
// Current texts
table.setText(1, 1, 0, getAText(energyData.getL1Amp()), true);
table.setText(2, 1, 0, getAText(energyData.getL2Amp()), true);
table.setText(3, 1, 0, getAText(energyData.getL3Amp()), true);
table.setText(4, 1, 0, getAText(energyData.getTotalAmp()), true);
// Today text
table.setText(1, 2, 0, getAText(energyData.getL1AmpToday()), true);
table.setText(2, 2, 0, getAText(energyData.getL2AmpToday()), true);
table.setText(3, 2, 0, getAText(energyData.getL3AmpToday()), true);
table.setText(4, 2, 0, getAText(energyData.getTotalAmpToday()), true);
// Record text
table.setText(1, 3, 0, getAText(energyData.getL1AmpRecord()), true);
table.setText(2, 3, 0, getAText(energyData.getL2AmpRecord()), true);
table.setText(3, 3, 0, getAText(energyData.getL3AmpRecord()), true);
table.setText(4, 3, 0, getAText(energyData.getTotalAmpRecord()), true);
table.drawTable(surface, x, y);
}
double Tft::getTodayExtimate() const
{
time_t now = time(NULL);
tm* timeinfo = localtime(&now);
timeinfo->tm_hour = 12;
int curMonth = timeinfo->tm_mon;
timeinfo->tm_mday = 0;
++timeinfo->tm_mon;
mktime(timeinfo);
int daysPerMonth = timeinfo->tm_mday;
return static_cast<double>(myMonthEstimates[curMonth]) / static_cast<double>(daysPerMonth);
}
double Tft::getYesterdayEstimate() const
{
time_t now = time(NULL);
tm* timeinfo = localtime(&now);
timeinfo->tm_hour = 12;
--timeinfo->tm_mday;
mktime(timeinfo);
int curMonth = timeinfo->tm_mon;
timeinfo->tm_mday = 0;
++timeinfo->tm_mon;
mktime(timeinfo);
int daysPerMonth = timeinfo->tm_mday;
return static_cast<double>(myMonthEstimates[curMonth]) / static_cast<double>(daysPerMonth);
}
double Tft::getMonthEstimate() const
{
time_t now = time(NULL);
tm* timeinfo = localtime(&now);
timeinfo->tm_hour = 12;
int currentDay = timeinfo->tm_mday;
int currentMonth = timeinfo->tm_mon;
int prevMonth = (currentMonth == 0) ? 11 : currentMonth - 1;
timeinfo->tm_mday = 0;
mktime(timeinfo);
int daysPrevMonth = timeinfo->tm_mday;
timeinfo = localtime(&now);
timeinfo->tm_hour = 12;
timeinfo->tm_mday = 0;
++timeinfo->tm_mon;
mktime(timeinfo);
int daysCurMonth = timeinfo->tm_mday;
double estimateCurMonth = static_cast<double>(myMonthEstimates[currentMonth])
* static_cast<double>(currentDay)
/ static_cast<double>(daysCurMonth);
double estimatePrevMonth = static_cast<double>(myMonthEstimates[prevMonth])
* (static_cast<double>(daysPrevMonth - currentDay + 1))
/ static_cast<double>(daysPrevMonth);
return estimateCurMonth + estimatePrevMonth;
}
double Tft::getYearEstimate() const
{
double result = .0;
for (int i = 0; i < 12; ++i)
{
result += myMonthEstimates[i];
}
return result;
}
void Tft::clear(cairo_t* surface)
{
cairo_set_source_rgb(surface, 0, 0, 0);
cairo_paint(surface);
}
void Tft::apply()
{
if (myScreenSurface && myBufferSurface[myCurrentDisplayBuffer])
{
cairo_t* cr = cairo_create(myScreenSurface);
cairo_set_source_surface(cr, myBufferSurface[myCurrentDisplayBuffer], 0, 0);
cairo_paint(cr);
cairo_destroy(cr);
}
}
void Tft::displayPower(bool on)
{
digitalWrite(cDisplayLedPin, on ? HIGH : LOW);
}
void Tft::displayOn()
{
alarm(cDisplayOnTime);
if (!myDisplayOn)
{
myDisplayOn = true;
displayPower(true);
}
}
void Tft::displayOff()
{
if (myDisplayOn)
{
displayPower(false);
myDisplayOn = false;
}
}
void Tft::buttonPressed(int buttonNr)
{
myCurrentDisplayBuffer = buttonNr - 1;
apply();
displayOn();
}
void Tft::button1Pressed()
{
gTftInstance->buttonPressed(1);
}
void Tft::button2Pressed()
{
gTftInstance->buttonPressed(2);
}
void Tft::button3Pressed()
{
gTftInstance->buttonPressed(3);
}
void Tft::button4Pressed()
{
gTftInstance->buttonPressed(4);
}
void Tft::alarmHandler(int)
{
gTftInstance->displayOff();
}
string Tft::getKwhText(double kwh)
{
stringstream text;
text << setprecision(3) << fixed;
text << kwh << " kWh";
return text.str();
}
string Tft::getWText(int w)
{
stringstream text;
text << w << " W";
return text.str();
}
string Tft::getAText(int a)
{
stringstream text;
text << a << " A";
return text.str();
}
string Tft::getPercText(double fraction)
{
stringstream text;
text << static_cast<int>(fraction * 100.) << "%";
return text.str();
}