feat(firmware): IM1281C driver

This commit is contained in:
2026-04-19 23:20:50 +08:00
parent 2d48724e37
commit 696f2735ff
4 changed files with 232 additions and 2 deletions

View File

@@ -15,6 +15,7 @@
#include "esp_system.h"
#include "config.h"
#include "IM1281C.h"
#include "pins.h"
/* LED State Enum */
@@ -95,6 +96,7 @@ Adafruit_SSD1306 display(128, 64, &Wire, -1);
SmartLed leds(LED_WS2812B, LED_COUNT, LED_PIN, 0, DoubleBuffer);
MFRC522 rfid(PIN_RC_CS, PIN_RC_RST);
IM1281C im1281c;
static bool isConnectorPlugged(unsigned int connectorId)
{
@@ -303,6 +305,42 @@ static void expireAuthWaitIfNeeded()
}
}
static void pollIm1281c()
{
static unsigned long s_last_poll_ms = 0;
const unsigned long now = millis();
if ((now - s_last_poll_ms) < 1000)
{
return;
}
s_last_poll_ms = now;
if (!im1281c.readAll())
{
Serial.printf("[IM1281C] read failed: A=%u B=%u\n", im1281c.lastAResult(), im1281c.lastBResult());
return;
}
const IM1281CAData &a = im1281c.a();
const IM1281CBData &b = im1281c.b();
Serial.printf("[IM1281C] A: U=%.4fV I=%.4fA P=%.4fW E=%.4fkWh PF=%.3f CO2=%.4fkg T=%.2fC F=%.2fHz\n",
a.voltage,
a.current,
a.power,
a.energy,
a.powerFactor,
a.co2,
a.temperature,
a.frequency);
Serial.printf("[IM1281C] B: U=%.4fV I=%.4fA P=%.4fW E=%.4fkWh PF=%.3f CO2=%.4fkg\n",
b.voltage,
b.current,
b.power,
b.energy,
b.powerFactor,
b.co2);
}
/* LED Control Functions */
void updateLED()
{
@@ -435,8 +473,8 @@ void setup()
// Initialize I2C for OLED (from schematic pin map)
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
// Initialize UART2 for IM1281C (U2)
Serial2.begin(BAUD_IM1281C, SERIAL_8N1, PIN_U2RXD, PIN_U2TXD);
// Initialize IM1281C over UART2 (default: address 1, 4800bps, 8N1)
im1281c.begin(Serial2, PIN_U2RXD, PIN_U2TXD);
// Initialize SPI bus for RC522
SPI.begin(PIN_RC_SCK, PIN_RC_MISO, PIN_RC_MOSI, PIN_RC_CS);
@@ -773,6 +811,7 @@ void loop()
stopIfUnplugged();
pollRfidCard();
expireAuthWaitIfNeeded();
pollIm1281c();
mg_mgr_poll(&mgr, 10);
mocpp_loop();