Hello again…
Looking for some guidance with a little project that I am working on here just to see if I can get the voltage to display on the TTGO T-Display board referenced earlier in this thread. I have added the two libraries (TFT-eSPI and Button2) that this board requires when working with the Arduino IDE into the /led00/src/dependencies folder.
I then added my code to the usermod.cpp file as best I could (I also included declarations for the functions that are needed - I think). (I am not very experienced with coding, so I likely did a lot wrong here).
The code I placed in the usermod.cpp file is basically a modified version of this, with a bunch of stuff stripped out to ONLY show the voltage:
This is the stripped down code I am using:
#include “wled.h”
#include <TFT_eSPI.h>
#include <SPI.h>
#include "WiFi.h"
#include <Wire.h>
#include <Button2.h>
#include "esp_adc_cal.h"
#ifndef TFT_DISPOFF
#define TFT_DISPOFF 0x28
#endif
#ifndef TFT_SLPIN
#define TFT_SLPIN 0x10
#endif
#define TFT_MOSI 19
#define TFT_SCLK 18
#define TFT_CS 5
#define TFT_DC 16
#define TFT_RST 23
#define TFT_BL 4 // Display backlight control pin
#define ADC_EN 14
#define ADC_PIN 34
#define BUTTON_1 35
#define BUTTON_2 0
void espDelay(int ms);
void showVoltage();
TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library
//Button2 btn1(BUTTON_1);
//Button2 btn2(BUTTON_2);
char buff[512];
int vref = 1100;
int btnCick = false;
const int freq = 5000;
const int ledChannel = 3;
const int resolution = 8;
const int BLDutyCycle = 32; //PWM duty cycle for display backlight
//! Long time delay, it is recommended to use shallow sleep, which can effectively reduce the current consumption
void espDelay(int ms)
{
esp_sleep_enable_timer_wakeup(ms * 1000);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH,ESP_PD_OPTION_ON);
esp_light_sleep_start();
}
void showVoltage()
{
static uint64_t timeStamp = 0;
if (millis() - timeStamp > 1000) {
timeStamp = millis();
uint16_t v = analogRead(ADC_PIN);
float battery_voltage = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
String voltage = "Voltage :" + String(battery_voltage) + "V";
Serial.println(voltage);
tft.setTextSize(2);
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.drawString(voltage, tft.width() / 2, tft.height() / 2 );
espDelay(3000); //testing current draw during shallow sleep
}
}
/*
* This file allows you to add own functionality to WLED more easily
* See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality
* EEPROM bytes 2750+ are reserved for your custom use case. (if you extend #define EEPSIZE in const.h)
* bytes 2400+ are currently ununsed, but might be used for future wled features
*/
//Use userVar0 and userVar1 (API calls &U0=,&U1=, uint16_t)
//gets called once at boot. Do all initialization that doesn't depend on network here
void userSetup()
{
Serial.begin(115200);
Serial.println("Start");
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
tft.setCursor(0, 0);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(2);
//ledcSetup(ledChannel, freq, resolution);
//ledcAttachPin(TFT_BL, ledChannel);
if (TFT_BL > 0) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
pinMode(TFT_BL, OUTPUT); // Set backlight pin to output mode
//ledcSetup(ledChannel, freq, resolution);
//ledcAttachPin(TFT_BL, ledChannel);
digitalWrite(TFT_BL, HIGH); // Turn backlight on. TFT_BACKLIGHT_ON has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
//ledcWrite(ledChannel, BLDutyCycle);
}
//tft.setSwapBytes(true);
//tft.pushImage(0, 0, 240, 135, ttgo);
//espDelay(30000); //testing current draw during shallow sleep
tft.setRotation(1);
// button_init();
esp_adc_cal_characteristics_t adc_chars;
esp_adc_cal_value_t val_type = esp_adc_cal_characterize((adc_unit_t)ADC_UNIT_1, (adc_atten_t)ADC1_CHANNEL_6, (adc_bits_width_t)ADC_WIDTH_BIT_12, 1100, &adc_chars);
//Check type of calibration value used to characterize ADC
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
Serial.printf("eFuse Vref:%u mV", adc_chars.vref);
Serial.println(" ");
vref = adc_chars.vref;
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
Serial.printf("Two Point --> coeff_a:%umV coeff_b:%umV\n", adc_chars.coeff_a, adc_chars.coeff_b);
Serial.println(" ");
} else {
Serial.println("Default Vref: 1100mV");
}
}
//gets called every time WiFi is (re-)connected. Initialize own network interfaces here
void userConnected()
{
}
//loop. You can use "if (WLED_CONNECTED)" to check for successful connection
void userLoop()
{
showVoltage();
}
When I attempt to build, I get a LOT of errors - here is a snippet of them:
wled00\src\dependencies\TFT_eSPI-master\Fonts\Font72x53rle.c:118:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before
‘const’
PROGMEM const unsigned char chr_f72_34[] =
^
wled00\src\dependencies\TFT_eSPI-master\Fonts\Font16.c:118:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘const’
PROGMEM const unsigned char chr_f16_2D[16] = // 1 unsigned char per row
^
wled00\src\dependencies\TFT_eSPI-master\Extensions\Button.cpp:74:72: error: ‘_yd’ was not declared in this scope
_gfx->drawString(long_name, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd);
^
wled00\src\dependencies\TFT_eSPI-master\Extensions\Smooth_font.cpp:149:6: error: ‘TFT_eSPI’ has not been declared
void TFT_eSPI::loadMetrics(void)
^
*** [.pio\build\esp32dev\src\src\dependencies\TFT_eSPI-master\Processors\TFT_eSPI_ESP32.c.o] Error 1
wled00\src\dependencies\TFT_eSPI-master\Fonts\Font64rle.c:164:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘const’
PROGMEM const unsigned char chr_f64_37[] =
^
wled00\src\dependencies\TFT_eSPI-master\Fonts\Font32rle.c:181:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘const’
PROGMEM const unsigned char chr_f32_31[] =
^
wled00\src\dependencies\TFT_eSPI-master\Fonts\Font16.c:124:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘const’
PROGMEM const unsigned char chr_f16_2E[16] = // 1 unsigned char per row
^
wled00\src\dependencies\TFT_eSPI-master\Fonts\Font7srle.c:182:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘const’
PROGMEM const unsigned char chr_f7s_37[] =
^
wled00\src\dependencies\TFT_eSPI-master\Extensions\Touch.cpp:98:1: error: ‘uint16_t’ does not name a type
uint16_t TFT_eSPI::getTouchRawZ(void){
^
wled00\src\dependencies\TFT_eSPI-master\Extensions\Button.cpp:75:22: error: ‘tempdatum’ was not declared in this scope
_gfx->setTextDatum(tempdatum);
^
wled00\src\dependencies\TFT_eSPI-master\Extensions\Touch.cpp:118:1: error: ‘uint8_t’ does not name a type
uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
^
wled00\src\dependencies\TFT_eSPI-master\Fonts\Font16.c:130:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘const’
PROGMEM const unsigned char chr_f16_2F[16] = // 1 unsigned char per row
^