const int LED_PIN = 13;
const int SENSOR_PIN = A0;
typedef bool (*ConditionPtr)();
// ==========================================================
// 1. TEMPLATE DECORATOR IMPLEMENTATIONS (I/O-Agnostic)
// ==========================================================
// A. TEMPLATE FOR FUNCTIONS THAT RETURN A VALUE (R != void)
template <typename R, typename... Args>
class ConditionalDecorator {
private:
R (*_func)(Args...);
ConditionPtr _check;
const char* _name;
public:
ConditionalDecorator(const char* func_name, R (*func)(Args...), ConditionPtr check)
: _name(func_name), _func(func), _check(check) {}
R operator()(Args... args) const {
if (!_check()) {
return R{};
}
Serial.print("Executing: ");
Serial.println(_name);
unsigned long start_time = millis();
R result = _func(args...);
unsigned long end_time = millis();
Serial.print("Finished ");
Serial.print(_name);
Serial.print(" in ");
Serial.print(end_time - start_time);
Serial.println(" ms.");
return result;
}
};
// B. TEMPLATE SPECIALIZATION FOR VOID FUNCTIONS (R == void)
template <typename... Args>
class ConditionalDecorator<void, Args...> {
private:
void (*_func)(Args...);
ConditionPtr _check;
const char* _name;
public:
ConditionalDecorator(const char* func_name, void (*func)(Args...), ConditionPtr check)
: _name(func_name), _func(func), _check(check) {}
void operator()(Args... args) const {
if (!_check()) {
return;
}
Serial.print("Executing: ");
Serial.println(_name);
unsigned long start_time = millis();
_func(args...);
unsigned long end_time = millis();
Serial.print("Finished ");
Serial.print(_name);
Serial.print(" in ");
Serial.print(end_time - start_time);
Serial.println(" ms.");
}
};
// ==========================================================
// 2. THE SEPARATE CONDITION CHECK FUNCTIONS (Now I/O-aware)
// ==========================================================
bool is_system_ready() {
static bool ready = false;
if (millis() > 5000) {
ready = true;
}
if (!ready) {
Serial.println("System NOT ready. Condition FAILED.");
}
return ready;
}
bool is_temp_safe() {
bool safe = analogRead(A1) < 800;
if (!safe) {
Serial.println("Temperature unsafe! Condition FAILED.");
}
return safe;
}
bool is_sensor_connected() {
bool safe = digitalRead(12);
if (!safe) {
Serial.println("temp sensor disconnected");
}
return safe;
}
// ==========================================================
// 3. CORE LOGIC FUNCTIONS
// ==========================================================
void power_on_motor() {
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
int calculate_checksum(int data) {
return data * 0xDEAF;
}
float temp(uint8_t pin){
float value = random(0,1000);
return value;
}
// ==========================================================
// 4. DECORATOR APPLICATION
// ==========================================================
ConditionalDecorator<void> decorated_motor_start("power_on_motor", power_on_motor, is_system_ready);
ConditionalDecorator<int, int> decorated_checksum("calculate_checksum", calculate_checksum, is_temp_safe);
ConditionalDecorator<float, uint8_t> decorated_temp("temp", temp, is_sensor_connected);
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(A1, INPUT);
pinMode(12,INPUT_PULLUP);
Serial.begin(9600);
while (!Serial);
Serial.println("\n--- Final I/O-Agnostic Decorator Demo ---");
}
void loop() {
Serial.println("--- LOOP START ---");
decorated_motor_start();
Serial.println("----------------------------------------");
int data = 10;
int result = decorated_checksum(data);
if (result != 0) {
Serial.print("Checksum Result: ");
Serial.println(result);
}
float t=decorated_temp(A0);
Serial.println(t);
Serial.println("========================================");
delay(1000);
}