Skip to content

Instantly share code, notes, and snippets.

@RobAxt
Last active October 30, 2024 16:39
Show Gist options
  • Save RobAxt/35f4d38e2a3a9c2c628f1644b7b82534 to your computer and use it in GitHub Desktop.
Save RobAxt/35f4d38e2a3a9c2c628f1644b7b82534 to your computer and use it in GitHub Desktop.
Active Object: private data handler
#include "ao.h"
static const uint8_t QUEUE_LENGTH_= 3;
static const uint8_t QUEUE_ITEM_SIZE_ = sizeof(ao_event_t);
struct ao_s
{
QueueHandle_t queue_h;
ao_event_handler_t event_h;
ao_privData_t privData_h;
};
static ao_t get_ao_intance(void); // ToDo: AO not responsable, do it somewhere else
static void task_(void *argument);
ao_t ao_init(ao_event_handler_t event_handler, ao_privData_t privData_handler)
{
ao_t ao = get_ao_intance();
while (NULL == ao)
{
// error
}
ao->privData_h = privData_handler;
ao->queue_h = xQueueCreate(QUEUE_LENGTH_, QUEUE_ITEM_SIZE_);
while(NULL == ao->queue_h)
{
// error
}
vQueueAddToRegistry(ao->queue_h, "Queue Handle");
ao->event_h = event_handler;
while(NULL == ao->event_h)
{
// error
}
BaseType_t status;
status = xTaskCreate(task_, "task_ao_", 128, (void* const)ao, tskIDLE_PRIORITY, NULL);
while (pdPASS != status)
{
// error
}
return ao;
}
// ......
static void task_(void *argument)
{
ao_t ao = (ao_t)argument;
while (true)
{
ao_event_t msg;
if (pdPASS == xQueueReceive(ao->queue_h, &msg, portMAX_DELAY))
{
ao->event_h(msg, ao->privData_h);
}
}
}
#ifndef INC_AO_H_
#define INC_AO_H_
typedef void* ao_event_t;
typedef void* ao_privData_t;
typedef void (*ao_event_handler_t)(ao_event_t, ao_privData_t);
typedef struct ao_s * ao_t;
ao_t ao_init(ao_event_handler_t event_handler, ao_privData_t privData_handler);
bool ao_send(ao_t ao, ao_event_t event);
#endif /* INC_AO_H_ */
#include "ao.h"
#include "ao_led.h"
// ......
typedef struct
{
ao_t base;
ao_led_color color;
} ao_led_s;
static void event_handler(ao_event_t event, ao_privData_t privData);
ao_led_t ao_led_init()
{
ao_led_t ao_led_h= allocate(sizeof(struct ao_led_s));
ao_led_h->base = ao_init(event_handler, (ao_privData_t) &(ao_led_h->color));
return ao_led_h;
}
bool ao_led_send(ao_led_t ao_led_h, ao_led_event_t event)
{
return ao_send(ao_led_h->base, (ao_event_t)event);
}
void ao_led_color(ao_led_t ao_led_h, ao_led_color color)
{
ao_led_h->color = color; // ToDo: defensive programming
}
static void event_handler(ao_event_t event, ao_privData_t privData)
{
ao_led_event_t event_ = (ao_led_event_t) event;
ao_led_color *color = (ao_led_color *) privData;
switch (event_)
{
// ......
}
}
#ifndef INC_AO_LED_H_
#define INC_AO_LED_H_
typedef enum
{
AO_LED_MESSAGE_RED_ON,
AO_LED_MESSAGE_RED_OFF,
AO_LED_MESSAGE_RED_BLINK,
AO_LED_MESSAGE_GREEN_ON,
AO_LED_MESSAGE_GREEN_OFF,
AO_LED_MESSAGE_GREEN_BLINK,
AO_LED_MESSAGE_BLUE_ON,
AO_LED_MESSAGE_BLUE_OFF,
AO_LED_MESSAGE_BLUE_BLINK,
AO_LED_MESSAGE__N
} ao_led_event_t;
typedef enum
{
AO_LED_COLOR_RED,
AO_LED_COLOR_GREEN,
AO_LED_COLOR_BLUE,
} ao_led_color;
typedef struct ao_led_s * ao_led_t
ao_led_t ao_led_init();
bool ao_led_send(ao_led_t ao_led_h, ao_led_event_t event);
void ao_led_color(ao_led_t ao_led_h, ao_led_color color);
#endif /* INC_AO_LED_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment