Skip to content

Commit fe72ef4

Browse files
committed
add pwm backlight
1 parent cf8932b commit fe72ef4

2 files changed

Lines changed: 57 additions & 11 deletions

File tree

platformio.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ build_flags =
2525
-D TFT_CS=15
2626
-D TFT_DC=22
2727
-D TFT_RST=4
28-
-D TFT_BL=32
29-
-D TFT_BACKLIGHT_ON=HIGH
3028
-D LOAD_GLCD=1
3129
-D LOAD_FONT2
3230
-D LOAD_FONT4

src/ESP32_DevkitC_3.5_SPI.ino

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ TFT_eSprite spr = TFT_eSprite(&display);
3838
#define RXD 16
3939
#define TXD 17
4040

41+
// Backlight control
42+
#define BACKLIGHT_PIN 32
43+
#define BACKLIGHT_CHANNEL 0
44+
#define BACKLIGHT_FREQ 5000
45+
#define BACKLIGHT_RESOLUTION 8
46+
#define BACKLIGHT_BRIGHTNESS 100 // 0-255 (0 = off, 255 = max brightness)
47+
4148
#define COMM_CAN 0
4249
#define COMM_SERIAL 1
4350

@@ -55,11 +62,11 @@ bool clientConnected = true;
5562
uint8_t iat = 0, clt = 0;
5663
uint8_t refreshRate = 0;
5764
unsigned int rpm = 0, lastRpm, vss = 0;
58-
int mapData, tps, adv, fp;
65+
int mapData, tps, adv, fp, triggerError = 0;
5966
float bat = 0.0, afrConv = 0.0;
6067
bool syncStatus, fan, ase, wue, rev, launch, airCon, dfco;
6168

62-
int lastIat = -1, lastClt = -1, lastTps = -1, lastAdv = -1, lastMapData = -1, lastFp = -1;
69+
int lastIat = -1, lastClt = -1, lastTps = -1, lastAdv = -1, lastMapData = -1, lastFp = -1, lastTriggerError = -1;
6370
float lastBat = -1, lastAfrConv = -1;
6471
unsigned int lastRefreshRate = -1;
6572
bool first_run = true;
@@ -68,6 +75,30 @@ uint32_t startupTime;
6875
uint32_t lazyUpdateTime;
6976
uint16_t spr_width = 0;
7077

78+
// Function to set backlight brightness (0-255)
79+
void setBacklightBrightness(uint8_t brightness) {
80+
ledcWrite(BACKLIGHT_CHANNEL, brightness);
81+
}
82+
83+
// Function to adjust brightness based on time or RPM
84+
void adjustBacklightAutomatically() {
85+
static uint32_t lastBrightnessCheck = 0;
86+
87+
if (millis() - lastBrightnessCheck > 1000) { // Check every second
88+
uint8_t newBrightness = BACKLIGHT_BRIGHTNESS;
89+
90+
// Reduce brightness when engine is running (RPM > 500)
91+
if (rpm > 500) {
92+
newBrightness = 80; // Dimmer when driving
93+
} else {
94+
newBrightness = 150; // Brighter when idle/parked
95+
}
96+
97+
setBacklightBrightness(newBrightness);
98+
lastBrightnessCheck = millis();
99+
}
100+
}
101+
71102
void canTask(void *pvParameters)
72103
{
73104
while (1)
@@ -80,6 +111,12 @@ void canTask(void *pvParameters)
80111
void setup()
81112
{
82113
EEPROM.begin(EEPROM_SIZE);
114+
115+
// Setup backlight PWM
116+
ledcSetup(BACKLIGHT_CHANNEL, BACKLIGHT_FREQ, BACKLIGHT_RESOLUTION);
117+
ledcAttachPin(BACKLIGHT_PIN, BACKLIGHT_CHANNEL);
118+
ledcWrite(BACKLIGHT_CHANNEL, BACKLIGHT_BRIGHTNESS); // Set brightness
119+
83120
display.init();
84121
display.setRotation(3);
85122
drawSplashScreenWithImage();
@@ -98,6 +135,7 @@ void setup()
98135
CAN0.watchFor(0x361); // Fuel Pressure
99136
CAN0.watchFor(0x362); // Ignition Angle (Leading)
100137
CAN0.watchFor(0x368); // AFR 01
138+
CAN0.watchFor(0x369); // Trigger System Error Count
101139
CAN0.watchFor(0x370); // VSS
102140
CAN0.watchFor(0x372); // Voltage
103141
CAN0.watchFor(0x3E0); // CLT, IAT
@@ -174,6 +212,9 @@ void loop()
174212
handleSerialCommunication();
175213
}
176214

215+
// Automatic backlight adjustment
216+
adjustBacklightAutomatically();
217+
177218
// static uint32_t lastDraw = 0;
178219
// if (millis() - lastDraw > 25)
179220
// { // Update every 100ms
@@ -269,6 +310,12 @@ void handleCANCommunication()
269310
afrConv = lambda * 14.7;
270311
break;
271312
}
313+
case 0x369:
314+
{ // Trigger System Error Count
315+
uint16_t trigger_raw = (can_message.data.byte[0] << 8) | can_message.data.byte[1]; // Byte 0-1
316+
triggerError = trigger_raw; // Raw value, no conversion
317+
break;
318+
}
272319
case 0x370:
273320
{ // VSS
274321
uint16_t vss_raw = (can_message.data.byte[0] << 8) | can_message.data.byte[1]; // Byte 0-1
@@ -294,6 +341,7 @@ void handleCANCommunication()
294341
}
295342
case 0x3E4:
296343
{ // Indicator
344+
dfco = (can_message.data.byte[1] << 8) | can_message.data.byte[4]; // Byte 1-4
297345
launch = (can_message.data.byte[2] << 8) | can_message.data.byte[6]; // Byte 2-6
298346
airCon = (can_message.data.byte[3] << 8) | can_message.data.byte[4]; // Byte 3-4
299347
fan = (can_message.data.byte[3] << 8) | can_message.data.byte[0]; // Byte 3-0
@@ -515,7 +563,7 @@ void drawSplashScreenWithImage()
515563
display.drawString("MAZDUINO Display", centerX, centerY);
516564
display.loadFont(AA_FONT_SMALL);
517565
display.drawString("Firmware version: " + String(version), centerX, centerY + 50);
518-
display.drawString("https://mazduino.kerja.dev", centerX, 300);
566+
display.drawString("www.mazduino.com", centerX, 300);
519567
// display.drawString("Powered by "+ String(ESP.getChipModel())+ " Rev"+ String(ESP.getChipRevision()), centerX, 300);
520568

521569
delay(5000);
@@ -524,8 +572,8 @@ void drawSplashScreenWithImage()
524572
void itemDraw(bool setup)
525573
{
526574
const char *labels[] = {"AFR", "TPS", "ADV", "MAP"};
527-
int values[] = {afrConv, tps, adv, mapData};
528-
int lastValues[] = {lastAfrConv, lastTps, lastAdv, lastMapData};
575+
float values[] = {afrConv, tps, adv, mapData};
576+
float lastValues[] = {lastAfrConv, lastTps, lastAdv, lastMapData};
529577
int positions[][2] = {{5, 190}, {360, 190}, {120, 190}, {360, 10}};
530578
uint16_t colors[] = {(afrConv < 13.0) ? TFT_ORANGE : ((afrConv > 14.7) ? TFT_RED : TFT_GREEN), TFT_WHITE, TFT_RED, TFT_WHITE};
531579

@@ -537,9 +585,9 @@ void itemDraw(bool setup)
537585

538586
if ((millis() - lazyUpdateTime > 1000) || setup)
539587
{
540-
const char *labelsLazy[4] = {"IAT", "Coolant", "Voltage", (EEPROM.read(0) == 1) ? "FPS" : "FP"};
541-
int valuesLazy[4] = {iat, clt, static_cast<float>(bat), (EEPROM.read(0) == 1) ? refreshRate : fp};
542-
int lastValuesLazy[4] = {lastIat, lastClt, static_cast<float>(lastBat), (EEPROM.read(0) == 1) ? lastRefreshRate : lastFp};
588+
const char *labelsLazy[4] = {"IAT", "Coolant", "Voltage", (EEPROM.read(0) == 1) ? "Trigger" : "FP"};
589+
float valuesLazy[4] = {iat, clt, bat, (EEPROM.read(0) == 1) ? triggerError : fp};
590+
float lastValuesLazy[4] = {lastIat, lastClt, lastBat, (EEPROM.read(0) == 1) ? lastTriggerError : lastFp};
543591

544592
int positionsLazy[][2] = {{5, 10}, {5, 100}, {360, 100}, {240, 190}};
545593
uint16_t colorsLazy[] = {TFT_WHITE, (clt > 95) ? TFT_RED : TFT_WHITE,
@@ -585,7 +633,7 @@ void startUpDisplay()
585633
}
586634
}
587635

588-
void drawDataBox(int x, int y, const char *label, const float value, uint16_t labelColor, const int valueToCompare, const int decimal, bool setup)
636+
void drawDataBox(int x, int y, const char *label, const float value, uint16_t labelColor, const float valueToCompare, const int decimal, bool setup)
589637
{
590638
const int BOX_WIDTH = 100; // Reduced width to fit screen
591639
const int BOX_HEIGHT = 80; // Adjusted height

0 commit comments

Comments
 (0)