I need to initialize several buttons and LEDs connected to my Arduino. To store data related to an individual button or LED, I decided to define my own data type using two different structs. One for a button and another one for a LED.
As I need to configure some GPIOs and other peripherals after instantiating each button/LED, I implemented a small factory function to get a clean interface and combine the required steps for instantiating and initialization.
Defining my own button_t type works as expected. The compiler does not throw any errors. However, I tried to implement the same approach for led_t type upon the compiler states the following error:
example:25:1: error: 'led_t' does not name a type
led_t ledFactory(uint8_t pinNumber)
^~~~~
exit status 1 'led_t' does not name a type
My example code looks like this:
#include <Arduino.h>
#include <stdint.h>
struct button_t
{
const uint8_t pin;
uint8_t currentState;
uint8_t lastState;
uint32_t lastDebounceTime;
};
button_t buttonFactory(uint8_t pinNumber)
{
button_t btn = {pinNumber, 0, 0, 0};
// setting some MCU registers here
return btn;
};
struct led_t
{
const uint8_t pin;
const uint8_t state;
};
led_t ledFactory(uint8_t pinNumber)
{
led_t led = {pinNumber, 0};
// setting some MCU registers here
return led;
};
void setup()
{
}
void loop()
{
}