This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdln2-pin.c
71 lines (57 loc) · 1.87 KB
/
dln2-pin.c
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
// SPDX-License-Identifier: CC0-1.0
/*
* Written in 2021 by Noralf Trønnes <noralf@tronnes.org>
*
* To the extent possible under law, the author(s) have dedicated all copyright and related and
* neighboring rights to this software to the public domain worldwide. This software is
* distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along with this software.
* If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include "dln2.h"
#define LOG1 //printf
#define DLN2_PIN_MAX 32
#define DLN2_PIN_NOT_AVAILABLE 0xff
struct dln2_pin_state {
uint8_t module;
};
static struct dln2_pin_state dln2_pin_states[DLN2_PIN_MAX];
bool dln2_pin_is_requested(uint16_t pin, uint8_t module)
{
if (pin >= DLN2_PIN_MAX)
return false;
struct dln2_pin_state *state = &dln2_pin_states[pin];
return state->module == module;
}
uint16_t dln2_pin_request(uint16_t pin, uint8_t module)
{
if (pin >= DLN2_PIN_MAX)
return DLN2_RES_INVALID_PIN_NUMBER;
struct dln2_pin_state *state = &dln2_pin_states[pin];
if (state->module && state->module != module)
return DLN2_RES_PIN_IN_USE;
state->module = module;
return 0;
}
uint16_t dln2_pin_free(uint16_t pin, uint8_t module)
{
if (pin >= DLN2_PIN_MAX)
return DLN2_RES_INVALID_PIN_NUMBER;
struct dln2_pin_state *state = &dln2_pin_states[pin];
if (state->module && state->module != module)
return DLN2_RES_PIN_NOT_CONNECTED_TO_MODULE;
state->module = 0;
return 0;
}
void dln2_pin_set_available(uint32_t mask)
{
for (uint i = 0; i < DLN2_PIN_MAX; i++) {
if (!(mask & 1))
dln2_pin_states[i].module = DLN2_PIN_NOT_AVAILABLE;
mask >>= 1;
}
dln2_pin_states[30].module = DLN2_PIN_NOT_AVAILABLE;
dln2_pin_states[31].module = DLN2_PIN_NOT_AVAILABLE;
}