Embedded C++ Class for LED Control - Embedded-c-cpp Typing CST Test
Loading…
Embedded C++ Class for LED Control — Embedded-c-cpp Code
A simple Embedded C++ class encapsulating GPIO operations for LED control on an ARM Cortex-M MCU.
#include "stm32f4xx.h"
class LED {
public:
LED(GPIO_TypeDef* port, uint16_t pin) : port(port), pin(pin)
{
port->MODER |= (1U << (pin * 2)); // Set as output
}
void on() { port->ODR |= (1U << pin); }
void off() { port->ODR &= ~(1U << pin); }
void toggle() { port->ODR ^= (1U << pin); }
private:
GPIO_TypeDef* port;
uint16_t pin;
};
int main()
{
LED led(GPIOB, 0);
while(1)
{
led.toggle();
for(volatile int i=0; i<1000000; ++i);
}
}Embedded-c-cpp Language Guide
Embedded C/C++ refers to using the C or C++ programming languages for programming embedded systems. These are resource-constrained devices like microcontrollers, IoT devices, automotive controllers, and real-time systems where direct hardware control and performance are critical.
Primary Use Cases
- ▸Microcontroller firmware
- ▸Real-time operating systems (RTOS) tasks
- ▸IoT devices and sensors
- ▸Automotive ECU programming
- ▸Industrial automation and robotics
Notable Features
- ▸Direct memory and register access
- ▸Low-level I/O control and peripheral interfacing
- ▸Deterministic and high-performance execution
- ▸Supports modular and object-oriented designs
- ▸Rich ecosystem of compilers, toolchains, and RTOS libraries
Origin & Creator
C was created by Dennis Ritchie at Bell Labs in the 1970s, and C++ was developed by Bjarne Stroustrup in the 1980s. The embedded variant evolved as developers adapted these languages for low-level, resource-constrained systems.
Industrial Note
Embedded C/C++ dominates industries like automotive (ISO 26262), aerospace (DO-178C), industrial automation, and consumer electronics where memory, speed, and reliability are critical.