#include <M5Unified.h>
#include <Wire.h>
#include <M5_ADS1100.h>
#define UNIT_ADC_SDA_PIN 2
#define UNIT_ADC_SCL_PIN 1
namespace {
constexpr uint8_t ADC_ADDR = ADS1100_DEFAULT_ADDRESS;
constexpr bool V11 = true;
constexpr float RATIO = V11 ? 6.10f : 4.00f;
constexpr int W = 320, H = 240, M = 16, CY = 45, CH = 128, CW = W - 2 * M;
constexpr uint16_t BG = 0x0861, CARD = 0x10C4, TRACK = 0x3186;
constexpr uint8_t ZERO_N = 16, ZERO_SKIP = 2;
ADS1100 adc;
M5Canvas canvas(&M5.Display);
int32_t zeroOffset = 0;
bool calibrateZero() {
int32_t sum = 0;
int16_t sample = 0;
for (uint8_t i = 0; i < ZERO_N; ++i) {
if (!adc.measureDifferential(sample)) {
return false;
}
if (i >= ZERO_SKIP) {
sum += sample;
}
}
zeroOffset = sum / (ZERO_N - ZERO_SKIP);
return true;
}
void drawText(const char* text, int x, int y, textdatum_t datum,
uint16_t color, uint16_t background) {
canvas.setTextDatum(datum);
canvas.setTextColor(color, background);
canvas.drawString(text, x, y);
}
void drawScreen(bool ok, int16_t raw, float voltage) {
const uint16_t accent = !ok || voltage < -0.1f || voltage > 12.5f
? TFT_RED
: voltage > 11.5f ? TFT_ORANGE : TFT_CYAN;
char value[24];
char detail[40];
if (ok) {
snprintf(value, sizeof(value), "%.3f V", voltage);
} else {
snprintf(value, sizeof(value), "ADC NOT FOUND");
}
if (ok) {
snprintf(detail, sizeof(detail), "RAW %d", raw);
} else {
snprintf(detail, sizeof(detail), "CHECK PORT.A / I2C");
}
canvas.fillSprite(BG);
canvas.fillRect(0, 0, W, 3, accent);
canvas.fillCircle(M + 3, 24, 3, accent);
canvas.setFont(&fonts::FreeSansBold9pt7b);
canvas.setTextSize(1);
drawText("UNIT ADC", M + 12, 24, middle_left, TFT_WHITE, BG);
drawText(V11 ? "ADS1110" : "ADS1100", W - M, 24, middle_right, TFT_LIGHTGREY, BG);
canvas.fillRoundRect(M, CY, CW, CH, 12, CARD);
canvas.drawRoundRect(M, CY, CW, CH, 12, accent);
if (ok) {
canvas.setFont(&fonts::FreeSansBold24pt7b);
} else {
canvas.setFont(&fonts::FreeSansBold12pt7b);
}
drawText(value, W / 2, CY + 61, middle_center, ok ? TFT_WHITE : TFT_RED, CARD);
canvas.setFont(&fonts::FreeSansBold9pt7b);
drawText(detail, W / 2, CY + CH - 12, middle_center, TFT_LIGHTGREY, CARD);
drawText("0 V", M, 192, middle_left, TFT_LIGHTGREY, BG);
drawText("12 V", W - M, 192, middle_right, TFT_LIGHTGREY, BG);
canvas.fillRoundRect(M, 200, CW, 10, 5, TRACK);
if (ok && voltage > 0) {
const int fill = constrain(static_cast<int>(voltage / 12.0f * CW + 0.5f), 0, CW);
canvas.fillRoundRect(M, 200, fill, 10, min(5, fill / 2), accent);
}
canvas.pushSprite(0, 0);
}
}
void setup() {
M5.begin();
Serial.begin(115200);
canvas.setColorDepth(16);
canvas.createSprite(W, H);
adc.getAddr_ADS1100(ADC_ADDR);
adc.setChip(V11 ? ADS_CHIP_ADS1110 : ADS_CHIP_ADS1100);
adc.setGain(GAIN_ONE);
adc.setMode(MODE_CONTIN);
adc.setRate(V11 ? ADS1110_RATE_15 : RATE_8);
adc.setOSMode(OSMODE_SINGLE);
Wire.end();
const bool connected = adc.begin(UNIT_ADC_SDA_PIN, UNIT_ADC_SCL_PIN, 100000) &&
adc.isConnected();
const bool ok = connected && calibrateZero();
if (ok) {
Serial.printf("ZERO=%ld\n", static_cast<long>(zeroOffset));
} else {
Serial.println("ADC init/zero calibration failed");
}
drawScreen(ok, 0, 0.0f);
}
void loop() {
M5.update();
int16_t raw = 0;
if (!adc.measureDifferential(raw)) {
drawScreen(false, 0, 0.0f);
delay(500);
return;
}
const int16_t adjusted = static_cast<int16_t>(
constrain(static_cast<int32_t>(raw) - zeroOffset, -32768, 32767));
const float voltage =
adjusted / 32768.0f * adc.getNominalReferenceVoltage() * RATIO;
drawScreen(true, adjusted, voltage);
Serial.printf("VIN=%.3f V RAW=%d\n", voltage, adjusted);
delay(500);
}