When you’re building applications for the Raspberry Pi Pico 2 W, it’s easy to start with a simple loop and a blinking LED. But as your projects grow, you quickly realize you need structure — predictable startup, safe shutdown, logging, and reusable hardware abstractions. The RPI Pico Application Life Cycle Demo shows exactly how to build that structure using your rpi_app_framework.
To test this app on your RPI Pico see this post.
What the LEDLifeCycleDemo Demonstrates
LEDLifeCycleDemo shows how to:
- Use the RPIApp lifecycle
- Initialize hardware in
setup() - Run a cooperative main loop in
run() - Shut down cleanly in
stop() - Use a device manager — specifically LEDSimple — with integrated logging
A minimal version of the app looks like this:
python
from rpi_app_framework import RPIApp, LEDSimple
import time
class LEDLifeCycleDemo(RPIApp):
def setup(self):
self.led = LEDSimple("status_led", log_func=self.log)
self.log("LED initialized")
def run(self):
while self.running:
self.led.toggle()
time.sleep(0.5)
def stop(self):
self.led.off()
super().stop()
The rpi_app_framework in a Nutshell
The framework is built around two core abstractions.
1. RPIApp — the application lifecycle
From your uploaded rpi_app.txt:
“Provides common functionality like logging and application lifecycle management.”
A typical RPIApp subclass follows this pattern:
python
class MyApp(RPIApp):
def setup(self):
self.log("Setup complete")
def run(self):
while self.running:
self.log("Running...")
time.sleep(1)
def stop(self):
self.log("Stopping...")
super().stop()
The start() method (inherited from RPIApp) orchestrates everything:
python
app = MyApp()
app.start()
2. DeviceManager — the hardware abstraction
From device_manager.txt:
“Abstract base class for device managers. Provides common logging functionality and device naming…”
A typical device manager looks like this:
python
class MyDevice(DeviceManager):
def __init__(self, name, log_func=None):
super().__init__(name, log_func)
self.log("Device initialized")
def do_something(self):
self.log("Action performed")
There is no universal cleanup method — each device manager implements cleanup only if needed.
LEDSimple: The Hardware Behind the Demo
The LEDLifeCycleDemo uses the LEDSimple device manager, which wraps the Pico’s onboard LED (or a GPIO LED on full Raspberry Pi).
From led_simple.txt:
“Simple LED manager… Provides basic on/off and toggle functionality.”
Example usage:
python
self.led = LEDSimple("status_led", log_func=self.log)
self.led.on()
time.sleep(1)
self.led.off()
time.sleep(1)
self.led.toggle()
Cleanup is simple — just turn the LED off:
python
self.led.off()
How LEDLifeCycleDemo Works
Here’s the conceptual flow of the demo.
1. App starts → RPIApp.start() runs
The inherited start() method handles:
python
self.running = True
self.setup()
self.run()
self.stop()
2. setup(): Initialize the LED
python
def setup(self):
self.led = LEDSimple("status_led", log_func=self.log)
self.log("LED ready")
3. run(): Blink in a cooperative loop
python
def run(self):
while self.running:
self.led.toggle()
time.sleep(0.5)
4. stop(): Clean shutdown
python
def stop(self):
self.log("Stopping LED demo...")
self.led.off()
super().stop()
No deinit() is required or available.
Why This Lifecycle Matters
Most MicroPython examples online look like this:
python
while True:
led.toggle()
time.sleep(0.5)
This works — until you try to:
- Add WiFi
- Add a web server
- Add motors or servos
- Add logging
- Stop the program cleanly from Thonny
- Handle KeyboardInterrupt safely
Your framework solves all of this.
How This Demo Fits Into Larger Projects
Once you understand LEDLifeCycleDemo, you can extend the same lifecycle to:
- Add Wi‑Fi support
- Add a Microdot web server
- Add motors or servos
- Add background tasks
- Add power monitoring
Final Thoughts
The RPI Pico Application Life Cycle Demo is intentionally simple — but it teaches the most important lesson:
Good embedded software starts with a good lifecycle.
By using RPIApp and DeviceManager, you get structure, safety, and clarity from the very first line of code. And because LEDSimple is a real device manager, the demo shows exactly how your future hardware modules will plug into the same architecture.
Full LEDLifeCycleDemo Application
python
from rpi_app_framework import RPIApp, LEDSimple
import time
class LEDLifeCycleDemo(RPIApp):
"""
Simple demonstration of the rpi_app_framework lifecycle.
Blinks an LED using a DeviceManager and shuts down cleanly.
"""
def setup(self):
# Initialize LED device manager
self.led = LEDSimple("status_led", log_func=self.log)
self.log("LED initialized")
def run(self):
# Main loop — cooperative, checks self.running
self.log("Starting LED blink loop")
while self.running:
self.led.toggle()
time.sleep(0.5)
def stop(self):
# Clean shutdown (no deinit() exists)
self.log("Stopping LED demo...")
self.led.off()
super().stop()
# Entry point
if __name__ == "__main__":
app = LEDLifeCycleDemo("LEDLifeCycleDemo")
app.start()