aboutsummaryrefslogtreecommitdiffstats
path: root/src/hal/components/enum.c
blob: e4313ab215cc41bd72c7465f2b5353213a32de11 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//    Copyright (C) 2023 Andy Pugh

//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
//

// Convert bit pins to enumerated ints and vice-versa

#include "rtapi.h"
#include "rtapi_slab.h"
#include "rtapi_app.h"
#include "rtapi_string.h"
#include "hal.h"

#if !defined(__KERNEL__)
#include <stdio.h>
#include <stdlib.h>
#endif

/* module information */
MODULE_AUTHOR("Andy Pugh");
MODULE_DESCRIPTION("convert enumerated types to HAL_BIT pins");
MODULE_LICENSE("GPL");

#define MAX_CHAN 256

typedef struct {
    hal_bit_t *bit;
    hal_u32_t *en; // note use index 0 differently
} enum_hal_t;

typedef struct{
    int dir;
    int num_pins;
    enum_hal_t *hal;
} enum_inst_t;

typedef struct {
    int num_insts;
    enum_inst_t *insts;
} enum_t;

static int comp_id;

static enum_t e;

static char *enums[MAX_CHAN] = {0,};
RTAPI_MP_ARRAY_STRING(enums, MAX_CHAN, "states, ; delimited");
static char *names[MAX_CHAN] = {0,};
RTAPI_MP_ARRAY_STRING(names, MAX_CHAN, "component names (optional)");

static void decode(void *inst, long period);
static void encode(void *inst, long period);

int rtapi_app_main(void){
    int i, j, v;
    int retval;
    char *token;

    if (!enums[0]) {
        rtapi_print_msg(RTAPI_MSG_ERR, "The enum_decode component requires at least"
                " one enumeration list\n");
        return -EINVAL;
    }

    // count instances
    e.num_insts = MAX_CHAN;
    for (i = 0; i < MAX_CHAN; i++){
        if (! enums[i] && ! names[i]){
            e.num_insts = i;
            rtapi_print_msg(RTAPI_MSG_ERR, "making %i instances\n", e.num_insts);
            break;
        }
        if ((! enums[i] && names[i]) || ( ! names[i] && names[0] && enums[i])){
            rtapi_print_msg(RTAPI_MSG_ERR, "Inconsistent number of names and enums\n");
           return -EINVAL;
        }
    }

    comp_id = hal_init("enum");

    if (comp_id < 0) {
        rtapi_print_msg(RTAPI_MSG_ERR, "ERROR: hal_init() failed\n");
        return -EINVAL;
    }
    // allocate memory for the base struct
    e.insts = (enum_inst_t *)rtapi_kmalloc(e.num_insts * sizeof(enum_inst_t), RTAPI_GFP_KERNEL);
    for (i = 0; i < e.num_insts; i++){
        enum_inst_t *inst = &(e.insts[i]);
        char this[HAL_NAME_LEN];

        // Count the pins
        inst->num_pins = 0;
        inst->dir = HAL_OUT; // direction of bit pin, out for decode
        for (j = strlen(enums[i]); j > 0; j--){
            if (enums[i][j] == ';'){
                if (enums[i][j-1] != ';' ) inst->num_pins++;
                // insert a string terminator
                enums[i][j] = 0;
            }
        }
        inst->hal = (enum_hal_t *)hal_malloc((inst->num_pins + 1) * sizeof(enum_hal_t));
        token = enums[i];
        switch (*token){
            case 'E':
            case 'e': // encode
                inst->dir = HAL_IN;
                break;
            case 'D':
            case 'd':
                inst->dir = HAL_OUT;
                break;
            default:
                rtapi_print_msg(RTAPI_MSG_ERR, "Each enum string must start"
                        "with either E; or D; to define the mode\n");
                goto fail0;
        }

        if (names[i]) {
            rtapi_snprintf(this, HAL_NAME_LEN, "%s", names[i]);
        } else if (inst->dir == HAL_IN) {
            rtapi_snprintf(this, HAL_NAME_LEN, "enum-encode.%02i", i);
        } else {
            rtapi_snprintf(this, HAL_NAME_LEN, "enum-decode.%02i", i);
        }

        // create single per-instance int pin in index 0
        if (inst->dir == HAL_OUT) {
            retval = hal_pin_u32_newf(HAL_IN, &(inst->hal[0].en), comp_id,
                                    "%s.input", this);
        } else {
            retval = hal_pin_u32_newf(HAL_OUT, &(inst->hal[0].en), comp_id,
                                    "%s.output", this);
        }
        v = 0;
        for (j = 1; j <= inst->num_pins; j++){ // 1-based indexing
            // skip to the next pin name
            while (*(++token) != 0){}
            //increment for skipped enumerations
            while (*(++token) == 0) v++;

            retval = hal_pin_bit_newf(inst->dir, &(inst->hal[j].bit),
                    comp_id, "%s.%s-%s",this, token,
                    (inst->dir == HAL_IN)?"in":"out");
            retval += hal_pin_u32_newf(HAL_IN, &(inst->hal[j].en),
                    comp_id, "%s.%s-val",this, token);
            *(inst->hal[j].en) = v++;

            if (retval < 0){
                rtapi_print_msg(RTAPI_MSG_ERR, "Failed to create HAL pins\n");
                goto fail0;
            }
        }
        if (inst->dir == HAL_OUT){
            hal_export_funct(this, decode, inst, 0, 0, comp_id);
        } else {
            hal_export_funct(this, encode, inst, 0, 0, comp_id);
        }
        if (retval < 0){
            rtapi_print_msg(RTAPI_MSG_ERR, "Failed to export functions\n");
            goto fail0;
        }
    }


    hal_ready(comp_id);
    return 0;

    fail0:
    rtapi_kfree(e.insts);
    hal_exit(comp_id);
    return -1;

}

static void decode(void *v_inst, long period){
    int i;
    enum_inst_t *inst = v_inst;
    for (i = 1; i <= inst->num_pins; i++){
        if (*(inst->hal[0].en) == *(inst->hal[i].en)){
           *(inst->hal[i].bit) = 1;
        } else {
           *(inst->hal[i].bit) = 0;
        }
    }
}
static void encode(void *v_inst, long period){
    int i;
    enum_inst_t *inst = v_inst;
    *(inst->hal[0].en) = 0;
    for (i = 1; i <= inst->num_pins; i++){
        if (*(inst->hal[i].bit)){
            *(inst->hal[0].en) = *(inst->hal[i].en);
        }
    }
}
bues.ch cgit interface