ESP32 ILI9341 RTC y MicroSD — ESP32 simulation

Sistema embebido con ESP32, pantalla ILI9341, módulo RTC DS1307 y tarjeta MicroSD

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by panditalibre123.

Components

Sketch code

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <RTClib.h>
#include <SD.h>
#include <math.h>

// =============================================================
// 1. ESTRUCTURAS Y PROTOTIPOS
// =============================================================
struct NodoBST {
  int idVehiculo;
  int idLugar;
  uint32_t t;
  NodoBST* izq;
  NodoBST* der;
};

NodoBST* insertarBST(NodoBST* n, int v, int l, uint32_t t);
NodoBST* eliminarBST(NodoBST* n, int v);

enum EstadoLugar { LIBRE, OCUPADO, MANTENIMIENTO };

struct Lugar {
  int id;
  int fila;
  int col;
  EstadoLugar estado;
  int idVehiculo;
  uint32_t segundosSinMoto;
};

struct Movimiento {
  char tipo;
  int idVehiculo;
  int idLugar;
  uint32_t timestamp;
};

// =============================================================
// 2. PINES
// =============================================================
#define TFT_CS    5
#define TFT_DC    2
#define TFT_RST   4
#define SD_CS     15
#define TOUCH_CS  27

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
RTC_DS1307 rtc;
HardwareSerial CamSerial(2); // RX2=16, TX2=17

bool rtcDisponible = false;
bool sdDisponible  = false;

// =============================================================
// 3. ÁLGEBRA LINEAL DE CÁMARA
// =============================================================
class AlgebraLinealVision {
public:
  float H[3][3] = {
    {0.0125, -0.0021, -1.20},
    {0.0018,  0.0142, -0.85},
    {0.0001,  0.0002,  1.00}
  };
  float vecCajon[2] = {0.0, 1.0};

  void transformarPixelesAMetros(float px, float py, float &xReal, float &yReal) {
    float w = H[2][0] * px + H[2][1] * py + H[2][2];
    if (fabs(w) < 1e-6) w = 1.0;
    xReal = (H[0][0] * px + H[0][1] * py + H[0][2]) / w;
    yReal = (H[1][0] * px + H[1][1] * py + H[1][2]) / w;
  }

  float calcularNormaEuclidiana(float x1, float y1, float x2, float y2) {
    return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
  }

  float calcularAlineacion(float x1, float y1, float x2, float y2) {
    float vx = x2 - x1;
    float vy = y2 - y1;
    float normaV = sqrt(vx * vx + vy * vy);
    if (normaV < 1e-4) return 1.0;
    return (vx * vecCajon[0] + vy * vecCajon[1]) / normaV;
  }
};

AlgebraLinealVision algebra;

// =============================================================
// 4. LECTURA TOUCH XPT2046
// =============================================================
uint16_t leerCanalTouch(uint8_t cmd) {
  digitalWrite(TOUCH_CS, LOW);
  SPI.transfer(cmd);
  uint16_t v = (SPI.transfer(0x00) << 8) | SPI.transfer(0x00);
  digitalWrite(TOUCH_CS, HIGH);
  return v >> 3;
}

bool obtenerToque(int &pantallaX, int &pantallaY) {
  uint16_t rawX = leerCanalTouch(0xD0);
  uint16_t rawY = leerCanalTouch(0x90);

  if (rawX < 150 || rawX > 3900 || rawY < 150 || rawY > 3900) return false;

  pantallaX = map(rawX, 300, 3800, 0, 320);
  pantallaY = map(rawY, 300, 3800, 0, 240);
  pantallaX = constrain(pantallaX, 0, 319);
  pantallaY = constrai

More projects by panditalibre123