#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define ANCHO_PANTALLA 128
#define ALTO_PANTALLA 64
Adafruit_SSD1306 display(ANCHO_PANTALLA, ALTO_PANTALLA, &Wire, -1);
#define port1 14
#define port2 15
#define port3 13
// Cambia este número para probar diferentes compuertas
// 0=NOT, 1=AND, 2=OR, 3=NAND, 4=NOR, 5=XOR
int tipoCompuerta = 1;
// ==========================================================
// IMÁGENES DE LAS COMPUERTAS (Mapas de bits 16x16)
// ==========================================================
const unsigned char bmp_NOT [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x06, 0x00, 0x07, 0x00, 0x07, 0x80, 0x07, 0xc0, 0xff, 0xe0,
0xff, 0xe0, 0x07, 0xc0, 0x07, 0x80, 0x07, 0x00, 0x06, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bmp_AND [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x0f, 0x80, 0x0f, 0xc0, 0x0f, 0xe0, 0xff, 0xf0, 0xff, 0xf0,
0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xe0, 0x0f, 0xc0, 0x0f, 0x80, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bmp_OR [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x07, 0x00, 0x07, 0x80, 0x07, 0xc0, 0xff, 0xe0, 0xff, 0xf0,
0xff, 0xe0, 0x07, 0xc0, 0x07, 0x80, 0x07, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bmp_NAND [] PROGMEM = {
0x00, 0x00, 0x0f, 0x00, 0x0f, 0x80, 0x0f, 0xc0, 0x0f, 0xe0, 0xff, 0xf0, 0xff, 0xf8, 0xff, 0xf8,
0xff, 0xf8, 0xff, 0xf8, 0xff, 0xf0, 0x0f, 0xe0, 0x0f, 0xc0, 0x0f, 0x80, 0x0f, 0x00, 0x00, 0x00
};
const unsigned char bmp_NOR [] PROGMEM = {
0x00, 0x00, 0x06, 0x00, 0x07, 0x00, 0x07, 0x80, 0x07, 0xc0, 0xff, 0xe0, 0xff, 0xf8, 0xff, 0xfc,
0xff, 0xf8, 0xff, 0xe0, 0x07, 0xc0, 0x07, 0x80, 0x07, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bmp_XOR [] PROGMEM = {
0x00, 0x00, 0x0d, 0x00, 0x0f, 0x00, 0x0f, 0x80, 0x0f, 0xc0, 0xff, 0xe0, 0xff, 0xf0, 0xff, 0xf0,
0xff, 0xe0, 0x0f, 0xc0, 0x0f, 0x80, 0x0f, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Arreglo para acceder fácilmente a la imagen correcta
const unsigned char* const imagenes_compuertas[] PROGMEM = {
bmp_NOT, bmp_AND, bmp_OR, bmp_NAND, bmp_NOR, bmp_XOR
};
void setup() {
Serial.begin(115200);
pinMode(port1, INPUT_PULLDOWN);
pinMode(port2, INPUT_PULLDOWN);
pinMode(port3, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Error de OLED"));
for(;;);
}
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
int puerto1 = digitalRead(port1);
int puerto2 = digitalRead(port2);
bool resultado = false;
String nombreCompuerta = "";
// Evaluar lógica
switch(tipoCompuerta) {
case 0: resultado = !puerto1; nombreCompuerta = "NOT"; break;
case 1: resultado = (puerto1 && puerto2); nombreCompuerta = "AND"; break;
case 2: resultado = (puerto1 || puerto2); nombreCompuerta = "OR"; break;
case 3: resultado = !(puerto1 && puerto2); nombreCompuerta = "NAND"; break;
case 4: resultado = !(puerto1 || puerto2); nombreCompuerta = "NOR"; break;
case 5: resultado = (puerto1 ^ puerto2); nombreCompuerta = "XOR"; break;
}
// Encender/Apagar LED
digitalWrite(port3, resultado ? HIGH : LOW);
// --- DIBUJAR EN OLED ---
display.clearDisplay();
// 1. Textos
display.setTextSize(2);
display.setCursor(0, 0);
display.print(nombreCompuerta);
display.setTextSize(1);
display.setCursor(0, 25);
display.print("In 1: "); display.print(puerto1);
display.setCursor(0, 35);
display.print("In 2: "); display.print(puerto2);
display.setTextSize(2);
display.setCursor(0, 50);
display.print("OUT: "); display.print(resultado);
// 2. Dibujar Símbolo (Imagen)
// Lee la imagen desde la memoria PROGMEM basada en la variable tipoCompuerta
display.drawBitmap(85, 0, imagenes_compuertas[tipoCompuerta], 16, 16, WHITE);
display.display();
delay(100); // Pequeña pausa para estabilizar la simulación
}