-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_cmds.c
More file actions
258 lines (237 loc) · 6.29 KB
/
shell_cmds.c
File metadata and controls
258 lines (237 loc) · 6.29 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Step10V - STEP/DIR to analog amplifier CNC regulator
// Copyright (C) 2018-2019 Charles-Henri Mousset - ch.mousset@gmail.com
// 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, version 3 of the License
//
// 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, see <https://www.gnu.org/licenses/>.
#include "ch.h"
#include "hal.h"
#include "ch_test.h"
#include "chprintf.h"
#include "shell.h"
#include "shell_cmds.h"
#include "sigf.h"
#include <stdlib.h>
#include "cfg.h"
#include <string.h>
#include "flash.h"
char ABOUT[] = "Step10V Copyright (C) 2018-2019 Charles-Henri Mousset\r\n\
https://github.com/chmousset/Step10V\r\n\
This program comes with ABSOLUTELY NO WARRANTY;\r\n\
This is free software, and you are welcome to redistribute it under certain\r\n\
conditions.\r\n\
Published under the GNU General Public License, version 3 of the License\r\n\
You should have received a copy of the GNU General Public License along with\r\n\
this program. If not, see <https://www.gnu.org/licenses/>.\r\n";
cfg_t cfg;
extern bool update_pid_params;
float stof(const char* s){
float rez = 0, fact = 1;
if (*s == '-'){
s++;
fact = -1;
};
for (int point_seen = 0; *s; s++){
if (*s == '.'){
point_seen = 1;
continue;
};
int d = *s - '0';
if (d >= 0 && d <= 9){
if (point_seen) fact /= 10.0f;
rez = rez * 10.0f + (float)d;
};
};
return rez * fact;
};
void about(BaseSequentialStream *chp, int argc, char *argv[])
{
(void)argc; (void)argv;
chprintf(chp, ABOUT);
}
void dac(BaseSequentialStream *chp, int argc, char *argv[])
{
uint16_t chan, val;
if(argc != 2)
{
chprintf(chp, "dac <chan> <value>\r\n");
return;
}
chan = atoi(argv[0]);
val = atoi(argv[1]);
if(chan > 1 || val > 4095)
{
chprintf(chp, "chan 0/1 ; val 0-4095\r\n");
return;
}
dac_lld_put_channel(&DACD1, chan, val);
}
void enable(BaseSequentialStream *chp, int argc, char *argv[])
{
(void) argv;
(void) argc;
(void) chp;
loop_enable = 1;
chprintf(chp, "Ctrl loop enabled\r\n");
}
void disable(BaseSequentialStream *chp, int argc, char *argv[])
{
(void) argv;
(void) argc;
(void) chp;
loop_enable = 0;
chprintf(chp, "Ctrl loop disabled\r\n");
}
void cfg_term_disp_int(int *val, int min, int max, int idx, BaseSequentialStream *chp)
{
(void)min; (void)max; (void) idx;
chprintf(chp, "%s = %d\r\n", cfg_names[idx], *val);
}
void cfg_term_disp_uint32_t(uint32_t *val, uint32_t min, uint32_t max, int idx, BaseSequentialStream *chp)
{
(void)min; (void)max; (void) idx;
chprintf(chp, "%s = %u\r\n", cfg_names[idx], *val);
}
void cfg_term_disp_bool(bool *val, bool min, bool max, int idx, BaseSequentialStream *chp)
{
(void)min; (void)max; (void) idx;
chprintf(chp, "%s = %s\r\n", cfg_names[idx], *val ? "True" : "False");
}
void cfg_term_disp_float(float *val, float min, float max, int idx, BaseSequentialStream *chp)
{
(void)min; (void)max; (void) idx;
chprintf(chp, "%s = %f\r\n", cfg_names[idx], *val);
}
void cfg_term_disp_notfound(BaseSequentialStream *chp)
{
chprintf(chp, "config variable not found!\r\n");
}
void cfg_term_save_int(int *val, int min, int max, int idx, BaseSequentialStream *chp, char *value)
{
int v = atoi(value);
if( (v > max) || (v < min) )
{
chprintf(chp, "Out of range [%d,%d]!\r\n", min, max);
return;
}
*val = v;
chprintf(chp, "%s = %d\r\n", cfg_names[idx], *val);
}
void cfg_term_save_uint32_t(uint32_t *val, uint32_t min, uint32_t max, int idx, BaseSequentialStream *chp, char *value)
{
uint32_t v = atoi(value);
if( (v > max) || (v < min) )
{
chprintf(chp, "Out of range [%d,%d]!\r\n", min, max);
return;
}
*val = v;
chprintf(chp, "%s = %u\r\n", cfg_names[idx], *val);
}
void cfg_term_save_bool(bool *val, bool min, bool max, int idx, BaseSequentialStream *chp, char *value)
{
(void)min; (void)max;
bool b = false;
if(atoi(value) == 1)
b = true;
else if(strcmp("true", value) == 0)
b = true;
else if(strcmp("True", value) == 0)
b = true;
else if(strcmp("TRUE", value) == 0)
b = true;
*val = b;
chprintf(chp, "%s = %s\r\n", cfg_names[idx], *val ? "True" : "False");
}
void cfg_term_save_float(float *val, float min, float max, int idx, BaseSequentialStream *chp, char *value)
{
float v = atof(value);
if( (v > max) || (v < min) )
{
chprintf(chp, "Out of range [%f,%f]!\r\n", min, max);
return;
}
*val = v;
chprintf(chp, "%s = %f\r\n", cfg_names[idx], *val);
}
void cfg_term_save_notfound(BaseSequentialStream *chp, char *value)
{
(void)value;
chprintf(chp, "config variable not found!\r\n");
}
void shl_cfg(BaseSequentialStream *chp, int argc, char *argv[])
{
int i;
if(argc == 0)
{
for(i=0; i<CFG_CNT; i++)
{
chprintf(chp, "%d : ", cfg_ids[i]);
CFGH_ID_TERM_DISP((&cfg), cfg_ids[i], chp);
chprintf(chp, " `- %s\r\n", cfg_descriptions[i]);
}
}
if(argc == 1)
{
i = atoi(argv[0]);
if(i > 0)
{
CFGH_ID_TERM_DISP((&cfg), i, chp);
}
else
{
if(strcmp(argv[0], "save") == 0)
chprintf(chp, "saving cfg... %s\r\n", save_settings(&cfg) ? "OK" : "Failed");
else if(strcmp(argv[0], "load") == 0)
chprintf(chp, "loading cfg... %s\r\n", load_settings(&cfg) ? "OK" : "Failed");
else if(strcmp(argv[0], "default") == 0)
{
chprintf(chp, "Setting cfg to defaults\r\n");
cfg_setdefaults(&cfg);
}
else
CFGH_NAME_TERM_DISP((&cfg), argv[0], chp);
}
}
if(argc == 2)
{
i = atoi(argv[0]);
if(i > 0)
{
CFGH_ID_TERM_SAVE((&cfg), i, chp, argv[1]);
}
else
{
CFGH_NAME_TERM_SAVE((&cfg), argv[0], chp, argv[1]);
}
update_pid_params = true;
}
}
static const ShellCommand commands[] = {
{"about", about},
{"dac", dac},
{"enable", enable},
{"disable", disable},
{"cfg", shl_cfg},
{NULL, NULL}
};
char histbuff[128] = "";
static const ShellConfig shell_cfg = {
(BaseSequentialStream *)&SD2,
commands,
histbuff,
sizeof(histbuff),
.sc_completion = NULL
};
static THD_WORKING_AREA(waShell, 2048);
void start_shell(void)
{
chThdCreateStatic(waShell, sizeof(waShell), NORMALPRIO, shellThread, (void *)&shell_cfg);
}