Simple LED Blinker for an Embedded System
This challenge focuses on writing a minimal embedded application in Rust that controls an LED. It simulates the core aspects of embedded development: interacting with hardware (represented abstractly), managing timing, and ensuring reliable operation within resource constraints. Successfully completing this challenge demonstrates a foundational understanding of Rust's capabilities in the embedded space.
Problem Description
You are tasked with creating a Rust program that simulates blinking an LED connected to an embedded system. Since we don't have actual hardware, you'll use a simplified abstraction. The program should repeatedly toggle the state of an LED (on/off) with a defined delay. The LED state is represented by a boolean variable. The delay is simulated using a simple std::thread::sleep call.
What needs to be achieved:
- Define an
Ledstruct to represent the LED and its state. - Implement a function
blink_ledthat takes anLedand a delay duration (in milliseconds) as input. - Inside
blink_led, repeatedly toggle the LED's state and sleep for the specified delay. - The program should run indefinitely (or until manually terminated).
Key Requirements:
- The
Ledstruct should have a field to store its state (boolean). - The
blink_ledfunction should accept theLedand delay as arguments. - The LED state should be toggled (inverted) in each iteration of the loop.
- The delay should be implemented using
std::thread::sleep. - The program should compile and run without errors.
Expected Behavior:
The program should continuously print the current state of the LED to the console (e.g., "LED: on" or "LED: off") after each toggle. The output should alternate between "LED: on" and "LED: off" at the specified delay interval.
Edge Cases to Consider:
- What happens if the delay is set to 0? (While not strictly an error, it's good to consider the implications).
- How can you ensure the program continues to run indefinitely? (Use an infinite loop).
Examples
Example 1:
Input: Led { state: false }, 500 (milliseconds)
Output:
LED: on
LED: off
LED: on
LED: off
... (continues indefinitely)
Explanation: The LED starts off. The program toggles it to on, sleeps for 500ms, toggles it to off, sleeps for 500ms, and repeats.
Example 2:
Input: Led { state: true }, 1000 (milliseconds)
Output:
LED: off
LED: on
LED: off
LED: on
... (continues indefinitely)
Explanation: The LED starts on. The program toggles it to off, sleeps for 1000ms, toggles it to on, sleeps for 1000ms, and repeats.
Constraints
- The delay duration must be a positive integer (greater than 0).
- The program must use
std::thread::sleepfor the delay. - The program should compile and run without panicking.
- The program should be reasonably efficient (avoid unnecessary allocations or computations).
Notes
- You don't need to worry about actual hardware interaction. The
Ledstruct and theblink_ledfunction are purely for simulation. - Consider using a
loopto create the infinite blinking behavior. - The
std::thread::sleepfunction takes aDurationas input. You can create aDurationfrom a number of milliseconds usingstd::time::Duration::from_millis(milliseconds). - Printing the LED state to the console is a simple way to verify that the program is working correctly. You can use
println!()for this. - This is a simplified example. Real embedded development involves much more complex hardware interaction and timing considerations.