-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.cpp
108 lines (81 loc) · 2.45 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
#include <Wire.h>
#include <limits.h>
#include <U8g2lib.h>
//https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
//https://github.com/olikraus/u8g2/wiki/u8g2reference
//https://github.com/olikraus/u8g2/wiki/setup_tutorial
#include <StateMachine.h>
U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R2, D1, D2);
const int sclPin = D1;
const int sdaPin = D2;
char dspBuffer[20];
bool displayEnabled = true;
long int dspPower = 0;
float dspTemp = 0.0;
int dspHumidity = 0;
int dspPressure = 0;
const char *tplPower = "%dW";
const char *tplMetter = "%dkWh";
const char *tplTemp = "%0.1fC";
const char *tplHumidity = "%d%%";
const char *tplPressure = "%dhPa";
const char *tplTime = "%02d:%02d";
void displayOff(ActionContext *ctx)
{
displayEnabled = false;
u8g2.clearBuffer();
u8g2.sendBuffer();
}
void displayOn(ActionContext *ctx)
{
displayEnabled = true;
}
void displayInit(){
u8g2.begin();
}
void displayPrintStr(const char* str){
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.drawStr(0, 20, str);
u8g2.sendBuffer();
}
void updateDisplay(ActionContext *ctx){
u8g2.clearBuffer();
if (!displayEnabled) {
u8g2.sendBuffer();
return;
}
long int power = ctx->compute->getVarInt("pow.w");
long int peakMetter = ctx->compute->getVarInt("pow.peak");
long int offPeakMetter = ctx->compute->getVarInt("pow.off_peak");
long int totalMetter = ctx->compute->getVarInt("pow.total");
bool isPeakTime = ctx->compute->getVarInt("pow.is_peak") > 0;
float temp = ctx->compute->getVarInt("bme.temp_c");
long int humidity = ctx->compute->getVarInt("bme.hum");
long int pressure = ctx->compute->getVarInt("bme.pres_hpa");
u8g2.setFont(u8g2_font_6x10_tf);
// 1st line
// indicate active day/night time counter
u8g2.drawStr(isPeakTime ? 0 : 56, 10, ">");
sprintf(dspBuffer, tplMetter, peakMetter);
u8g2.drawStr(8, 10, dspBuffer);
sprintf(dspBuffer, tplMetter, offPeakMetter);
u8g2.drawStr(64, 10, dspBuffer);
// 2nd line
sprintf(dspBuffer, tplPower, power);
u8g2.drawStr(0, 21, dspBuffer);
// sprintf(TimeBuff, tplTime, hour(), minute());
// u8g2.drawStr(70, 21, TimeBuff);
//
// if (dspStatus == STATUS_UPLOADING) {
// u8g2.drawStr(120, 21, "*");
// }
// 3rd line
sprintf(dspBuffer, tplTemp, temp);
u8g2.drawStr(0,32, dspBuffer);
sprintf(dspBuffer, tplHumidity, humidity);
u8g2.drawStr(38,32, dspBuffer);
sprintf(dspBuffer, tplPressure, pressure);
u8g2.drawStr(70,32, dspBuffer);
u8g2.sendBuffer();
}