/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdint.h>
#include "pico/stdlib.h"
// Enum with uint8_t to save RAM (1 byte)
typedef enum : uint8_t {
LED_OFF = 0,
LED_ON = 1
} LedState;
typedef struct StatusIndicator StatusIndicator;
struct StatusIndicator {
uint8_t gpio_pin;
uint16_t interval_ms; // Heartbeat interval variable stored inside the struct
void (*init)(StatusIndicator *this);
void (*update)(StatusIndicator *this);
};
void status_indicator_init(StatusIndicator *this) {
gpio_init(this->gpio_pin);
gpio_set_dir(this->gpio_pin, GPIO_OUT);
}
void status_indicator_update(StatusIndicator *this) {
gpio_put(this->gpio_pin, LED_ON);
sleep_ms(this->interval_ms);
gpio_put(this->gpio_pin, LED_OFF);
sleep_ms(this->interval_ms);
}
StatusIndicator create_status_indicator(uint8_t pin, uint16_t interval_ms) {
StatusIndicator indicator;
indicator.gpio_pin = pin;
indicator.interval_ms = interval_ms;
indicator.init = status_indicator_init;
indicator.update = status_indicator_update;
return indicator;
}
int main(void) {
// Pass 100 ms interval directly to the struct variable during instantiation
const uint16_t INTERVAL = 100;
StatusIndicator heartbeat = create_status_indicator(PICO_DEFAULT_LED_PIN, INTERVAL);
heartbeat.init(&heartbeat);
while (true) {
heartbeat.update(&heartbeat);
}
return 0;
}