-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.cpp
128 lines (100 loc) · 3.46 KB
/
main.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
#include <Arduino.h>
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
// Clockface
#include <Clockface.h>
// Commons
#include <WiFiController.h>
#include <CWDateTime.h>
#include <CWPreferences.h>
#include <CWWebServer.h>
#include <StatusController.h>
#define MIN_BRIGHT_DISPLAY_ON 4
#define MIN_BRIGHT_DISPLAY_OFF 0
#define ESP32_LED_BUILTIN 2
MatrixPanel_I2S_DMA *dma_display = nullptr;
Clockface *clockface;
WiFiController wifi;
CWDateTime cwDateTime;
bool autoBrightEnabled;
long autoBrightMillis = 0;
uint8_t currentBrightSlot = -1;
void displaySetup(bool swapBlueGreen, uint8_t displayBright, uint8_t displayRotation)
{
HUB75_I2S_CFG mxconfig(64, 64, 1);
if (swapBlueGreen)
{
// Swap Blue and Green pins because the panel is RBG instead of RGB.
mxconfig.gpio.b1 = 26;
mxconfig.gpio.b2 = 12;
mxconfig.gpio.g1 = 27;
mxconfig.gpio.g2 = 13;
}
mxconfig.gpio.e = 18;
mxconfig.clkphase = false;
// Display Setup
dma_display = new MatrixPanel_I2S_DMA(mxconfig);
dma_display->begin();
dma_display->setBrightness8(displayBright);
dma_display->clearScreen();
dma_display->setRotation(displayRotation);
}
void automaticBrightControl()
{
if (autoBrightEnabled) {
if (millis() - autoBrightMillis > 3000)
{
int16_t currentValue = analogRead(ClockwiseParams::getInstance()->ldrPin);
uint16_t ldrMin = ClockwiseParams::getInstance()->autoBrightMin;
uint16_t ldrMax = ClockwiseParams::getInstance()->autoBrightMax;
const uint8_t minBright = (currentValue < ldrMin ? MIN_BRIGHT_DISPLAY_OFF : MIN_BRIGHT_DISPLAY_ON);
uint8_t maxBright = ClockwiseParams::getInstance()->displayBright;
uint8_t slots = 10; //10 slots
uint8_t mapLDR = map(currentValue > ldrMax ? ldrMax : currentValue, ldrMin, ldrMax, 1, slots);
uint8_t mapBright = map(mapLDR, 1, slots, minBright, maxBright);
// Serial.printf("LDR: %d, mapLDR: %d, Bright: %d\n", currentValue, mapLDR, mapBright);
if(abs(currentBrightSlot - mapLDR ) >= 2 || mapBright == 0){
dma_display->setBrightness8(mapBright);
currentBrightSlot=mapLDR;
// Serial.printf("setBrightness: %d , Update currentBrightSlot to %d\n", mapBright, mapLDR);
}
autoBrightMillis = millis();
}
}
}
void setup()
{
Serial.begin(115200);
pinMode(ESP32_LED_BUILTIN, OUTPUT);
StatusController::getInstance()->blink_led(5, 100);
ClockwiseParams::getInstance()->load();
pinMode(ClockwiseParams::getInstance()->ldrPin, INPUT);
displaySetup(ClockwiseParams::getInstance()->swapBlueGreen, ClockwiseParams::getInstance()->displayBright, ClockwiseParams::getInstance()->displayRotation);
clockface = new Clockface(dma_display);
autoBrightEnabled = (ClockwiseParams::getInstance()->autoBrightMax > 0);
StatusController::getInstance()->clockwiseLogo();
delay(1000);
StatusController::getInstance()->wifiConnecting();
if (wifi.begin())
{
StatusController::getInstance()->ntpConnecting();
cwDateTime.begin(ClockwiseParams::getInstance()->timeZone.c_str(),
ClockwiseParams::getInstance()->use24hFormat,
ClockwiseParams::getInstance()->ntpServer.c_str(),
ClockwiseParams::getInstance()->manualPosix.c_str());
clockface->setup(&cwDateTime);
}
}
void loop()
{
wifi.handleImprovWiFi();
if (wifi.isConnected())
{
ClockwiseWebServer::getInstance()->handleHttpRequest();
ezt::events();
}
if (wifi.connectionSucessfulOnce)
{
clockface->update();
}
automaticBrightControl();
}