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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 
/**
 * @file Ems_pin.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2023-04-05
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include <string.h>
#include "dev_pin.h"
#include "ems_assert.h"
 
#ifdef __cplusplus
extern "C" {
#endif
 
EMS_TAG("EMS_PIN")
 
static ems_ops_t _obj_ops =
{
    .open = NULL,
    .close = NULL,
    .read = NULL,
    .write = NULL,
};
 
/**
  * @brief  EMS pin initialization.
  * @param  me      this pointer
  * @param  name    pin's name.
  * @param  mode    pin's mode.
  * @retval None
  */
void ems_pin_register(ems_pin_t * const me,
                        const char *name,
                        const ems_pin_ops_t *ops,
                        void *user_data)
{
    ems_assert(me != NULL);
    ems_assert(name != NULL);
    ems_assert(ops != NULL);
 
    ems_obj_attr_t attr =
    {
        .user_data = user_data,
        .standlone = true,
        .type = EMS_OBJ_PIN,
    };
 
    ems_register(&me->super, name, &_obj_ops, &attr);
 
    me->ops = ops;
    me->ops->init(me);
    me->status = me->ops->get_status(me);
}
 
/**
 * @brief 
 * 
 * @param me 
 * @param mode 
 */
void ems_pin_set_mode(ems_object_t * const me, uint8_t mode)
{
    ems_assert(me != NULL);
 
    ems_pin_t *pin = (ems_pin_t *)me;
    if (pin->mode != mode)
    {
        pin->ops->set_mode(pin, mode);
        pin->mode = mode;
    }
}
 
 
/**
 * @brief 
 * 
 * @param me 
 * @return true 
 * @return false 
 */
bool ems_pin_get_status(ems_object_t * const me)
{
    ems_assert(me != NULL);
    ems_pin_t *pin = (ems_pin_t *)me;
 
    pin->status = pin->ops->get_status(pin);
 
    return pin->status;
}
 
/**
 * @brief 
 * 
 * @param me 
 * @param status 
 */
void ems_pin_set_status(ems_object_t * const me, bool status)
{
    ems_assert(me != NULL);
 
    ems_pin_t *pin = (ems_pin_t *)me;
    ems_assert(pin->mode == PIN_MODE_OUTPUT || pin->mode == PIN_MODE_OUTPUT_OD);
    
    if (status != pin->status)
    {
        pin->ops->set_status(pin, status);
        ems_pin_get_status(me);
        ems_assert(pin->status == status);
    }
}
 
#ifdef __cplusplus
}
#endif
 
/* ----------------------------- end of file -------------------------------- */