forked from pine64/bl_iot_sdk
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
demo.c
126 lines (118 loc) · 4.4 KB
/
demo.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
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
/*
* Copyright (c) 2020 Bouffalolab.
*
* This file is part of
* *** Bouffalolab Software Dev Kit ***
* (see www.bouffalolab.com).
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Bouffalo Lab nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <cli.h>
#include "bl_gpio.h"
static void cmd_gpio_func(char *buf, int len, int argc, char **argv)
{
int ionum = -1, inputmode = -1, pullup = -1, pulldown = -1;
if (5 != argc) {
printf("Usage: %s 24 1 1 0\r\n set GPIO24 to input with pullup\r\n",
argv[0]
);
return;
}
ionum = atoi(argv[1]);
inputmode = atoi(argv[2]);
pullup = atoi(argv[3]);
pulldown = atoi(argv[4]);
if (ionum < 0 || inputmode < 0 || pullup < 0 || pulldown < 0) {
puts("Illegal arg\r\n");
return;
}
printf("GPIO%d is set %s with %s pullup %s pulldown\r\n",
ionum,
inputmode ? "input" : "output",
pullup ? "Active" : "null",
pulldown ? "Active" : "null"
);
if (inputmode) {
bl_gpio_enable_input(ionum, pullup ? 1 : 0, pulldown ? 1 : 0);
} else {
bl_gpio_enable_output(ionum, pullup ? 1 : 0, pulldown ? 1 : 0);
}
}
static void cmd_gpio_set(char *buf, int len, int argc, char **argv)
{
int ionum = -1, val = -1;
if (3 != argc) {
printf("Usage: %s 24 1\r\n set GPIO24 output to high\r\n",
argv[0]
);
return;
}
ionum = atoi(argv[1]);
val = atoi(argv[2]);
if (ionum < 0 || val < 0) {
puts("Illegal arg\r\n");
return;
}
printf("GPIO%d is set to %s\r\n",
ionum,
val ? "high" : "lo"
);
bl_gpio_output_set(ionum, val ? 1 : 0);
}
static void cmd_gpio_get(char *buf, int len, int argc, char **argv)
{
int ionum = -1, ret;
uint8_t val;
if (2 != argc) {
printf("Usage: %s 24\r\n get GPIO24 value\r\n",
argv[0]
);
return;
}
ionum = atoi(argv[1]);
if (ionum < 0) {
puts("Illegal arg\r\n");
return;
}
ret = bl_gpio_input_get(ionum, &val);
printf("GPIO%d val is %s\r\n",
ionum,
0 == ret ? (val ? "high" : "low") : "Err"
);
}
// STATIC_CLI_CMD_ATTRIBUTE makes this(these) command(s) static
const static struct cli_command cmds_user[] STATIC_CLI_CMD_ATTRIBUTE = {
{"gpio-func", "gpio-func pinnum 0/1[0:output, 1:input] 0/1[pullup] 0/1[pulldown]", cmd_gpio_func},
{"gpio-set", "gpio-set pinnum 0/1", cmd_gpio_set},
{"gpio-get", "gpio-get pinnum", cmd_gpio_get},
};
int gpio_cli_init(void)
{
// static command(s) do NOT need to call aos_cli_register_command(s) to register.
// However, calling aos_cli_register_command(s) here is OK but is of no effect as cmds_user are included in cmds list.
// XXX NOTE: Calling this *empty* function is necessary to make cmds_user in this file to be kept in the final link.
//return aos_cli_register_commands(cmds_user, sizeof(cmds_user)/sizeof(cmds_user[0]));
return 0;
}