ethan
2023-04-08 f5e277c240b9211a1f047b88788f34f3dd5a97c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * @file Ems_object.h
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2023-04-05
 * 
 * @copyright Copyright (c) 2023
 * 
 */
 
#ifndef Ems_OBJECT_H
#define Ems_OBJECT_H
 
/* includes ----------------------------------------------------------------- */
#include <stdbool.h>
#include <stdint.h>
#include "Ems_def.h"
 
#ifdef __cplusplus
extern "C" {
#endif
 
/* public define ------------------------------------------------------------ */
enum ems_obect_type
{
    EMS_OBJ_PIN = 0,
    EMS_OBJ_PWM,
    EMS_OBJ_ADC,
    EMS_OBJ_DAC,
    EMS_OBJ_UART,
    EMS_OBJ_SPI,
    EMS_OBJ_I2C,
    EMS_OBJ_CAN,
    EMS_OBJ_MAX
};
 
/* public typedef ----------------------------------------------------------- */
typedef struct ems_obj_attr
{
    void *user_data;
    uint8_t type;
    bool standlone;
} ems_obj_attr_t;
 
typedef struct ems_object
{
    struct ems_object *next;
    const char *name;
    const struct ems_ops *ops;
    uint16_t count_open;
    ems_obj_attr_t attr;
} ems_object_t;
 
typedef struct ems_ops
{
    ems_err_t (* open)(ems_object_t * const me);
    ems_err_t (* close)(ems_object_t * const me);
    int32_t (* read)(ems_object_t * const me, void *buffer, uint32_t size);
    int32_t (* write)(ems_object_t * const me, const void *buffer, uint32_t size);
} ems_ops_t;
 
 
/* For io-level driver. */
void ems_register(ems_object_t * const me, const char *name,
                    const ems_ops_t *ops,
                    ems_obj_attr_t *attr);
 
/* For high-level code. */
ems_object_t *ems_find(const char *name);
ems_err_t ems_open(ems_object_t * const me);
ems_err_t ems_close(ems_object_t * const me);
int32_t ems_read(ems_object_t * const me, void *buffer, uint32_t size);
int32_t ems_write(ems_object_t * const me, const void *buffer, uint32_t size);
 
#ifdef __cplusplus
}
#endif
 
#endif  /* EMS_OBJECT_H */