# #############################################################################
# ** Proyecto : 3_3 Herencia Torre Leds
# ** Plataforma : ESP32 / WROOM
# ** Herramienta : https://www.wokwi.com
# ** : Thonny aplicacion de escritorio descargar en www.thonny.org
# ** Compilador : wokwi Simulador online
# ** Version : 1.0
# ** Fecha/Hora : 19-10-2025, 6:45 pm,
# **
# ** Torre de Leds utilizando concepto de Herencia
# ** y tuplas, listas ciclo for
# **
# ** Versión : 1
# ** Revisión : A
# ** Release : 0
# ** Bugs & Fixes :
# ** Date : 19/10/2025
# **
# ** By : Jorge Anzaldo
# ** contact : [email protected]
# ** twitter x : @janzaldob
# #############################################################################
# ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# : Librerias / Bibliotecas / Modulos | :
# ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
from machine import Pin #https://docs.micropython.org/en/latest/library/machine.Pin.html
import time #https://docs.micropython.org/en/latest/library/time.html
import sys
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# | Definición y Desarrollo de Clase |
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# CLASE PADRE (BASE)
class Led:
# Clase base que representa un LED
def __init__(self, pin, color="blanco"):
self.color = color
self.estado = False # False = apagado, True = prendido
self.led = Pin(pin,Pin.OUT)
print(f"LED {color} creado")
def prender(self):
self.estado = True
self.led.on()
print(f" LED {self.color} PRENDIDO")
def apagar(self):
self.estado = False
self.led.off()
print(f" LED {self.color} APAGADO")
def parpadear(self):
print(f" LED {self.color} parpadeando...")
# Prender y apagar rápidamente
self.prender()
time.sleep(0.2)
self.apagar()
def __str__(self):
#Método especial que muestra información del LED
if self.estado:
return f"LED {self.color} -> PRENDIDO"
else:
return f"LED {self.color} -> APAGADO"
def __del__(self):
#Destructor - se ejecuta cuando se elimina el LED
print(f" LED {self.color} eliminado")
# SUBCLASE / CLASE HIJA (HEREDA DE Led)
class TorreLed(Led):
#Clase subclase / hija que representa una torre con múltiples LEDs
#HEREDA de la clase Led
def __init__(self):
# Constructor de la torre LED
# Llamar al constructor del padre
super().__init__(pin=1, color="Torre Completa")
# Crear 3 LEDs individuales dentro de la torre
self.leds = [
Led(13,"rojo"),
Led(12,"verde"),
Led(14,"azul"),
Led(27,"azul"),
Led(26,"azul"),
Led(25,"azul"),
Led(33,"azul"),
Led(32,"azul")
]
print(" Torre LED creada con 3 LEDs")
def prender(self):
# Prender TODOS los LEDs (sobrescribe el método del padre)
print(" PRENDIENDO TODA LA TORRE:")
for led in self.leds:
led.prender()
def apagar(self):
#Apagar TODOS los LEDs (sobrescribe el método del padre)
print(" APAGANDO TODA LA TORRE:")
for led in self.leds:
led.apagar()
def prender_izq_der(self):
print(" PRENDIENDO DE IZQUIERDA A DERECHA:")
for led in self.leds:
led.prender()
time.sleep(0.2)
def prender_der_izq(self):
print(" PRENDIENDO DE DERECHA A IZQUIERDA:")
for led in reversed(self.leds):
led.prender()
time.sleep(0.2)
def mostrar_estado(self):
# Muestra el estado de todos los LEDs
print("\n ESTADO DE LA TORRE:")
for led in self.leds:
print(f" - {led}")
# +-------------------------------------------------------------------------------
# | V A R I A B L E S / O B J E T O S - G L O B A L E S |
# +-------------------------------------------------------------------------------
# Crear una instancia/Objeto de la subclase TorreLeds
led = Led(2,"Rojo")
torre = TorreLed()
# ===============================================================================
# || ||
# || B L O Q U E / F U N C I O N P R I N C I P A L ||
# || ||
# ===============================================================================
if __name__ == "__main__":
while True:
led.prender()
time.sleep(1)
led.apagar()
time.sleep(1)
torre.prender()
time.sleep(1)
torre.apagar()
time.sleep(1)
torre.prender_izq_der()
torre.apagar()
torre.prender_der_izq()
torre.apagar()
# ********************************************************************************
#
#
#
# R E F E R E N C I A S / C O M E N T A R I O S
#
# *********************************************************************************