]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_pin.c
7 #include "print-utils.h"
8 #include "getopt-min.h"
15 static const int namestr
= PIN_MAX
;
16 static char *pin_names
[PIN_MAX
+1];
17 static uint_least8_t pin_names_width
;
19 static void pinnames_get(void)
21 static const FLASH
char delim1
[] = {":= "};
22 static const FLASH
char delim2
[] = {", "};
27 if (pin_names
[namestr
] != NULL
)
28 free(pin_names
[namestr
]);
29 memset(pin_names
, 0, sizeof(pin_names
));
32 if ((lp
= getenv(PSTR(ENV_PINALIAS
))) != NULL
) {
33 pin_names
[namestr
] = strdup(lp
);
34 ptr
= strtok_P(pin_names
[namestr
], delim1
);
36 if (((i
= strtoul(ptr
, &lp
, 10)) < PIN_MAX
) &&
38 (ptr
= strtok_P(NULL
, delim2
)) != NULL
) {
40 ptr
= strtok_P(NULL
, delim1
);
44 for (i
= 0; i
< PIN_MAX
; i
++)
45 if (strlen(pin_names
[i
]) > pin_names_width
)
46 pin_names_width
= strlen(pin_names
[i
]);
51 static size_t xstrlen(char *s
)
59 static const FLASH
char * const FLASH pinconf_str
[] = {
67 static const FLASH
char * const FLASH pinlevel_str
[] = {
73 static int print_pin(int pin
, int multi
)
76 const FLASH
char *levelp
;
79 pinconf
= pin_config_get(pin
);
80 if (pinconf
== OUTPUT_TIMER
) {
81 div
= pin_clockdiv_get(pin
);
82 levelp
= pinlevel_str
[2];
84 levelp
= pinlevel_str
[pin_read(pin
)];
87 printf_P(PSTR("%3d "), pin
);
88 if (pin_names_width
) {
89 printf_P(PSTR("%s "), pin_names
[pin
]);
90 print_blanks(pin_names_width
- xstrlen(pin_names
[pin
]));
92 my_puts_P(pinconf_str
[pinconf
]);
93 print_blanks(7 - strlen_P(pinconf_str
[pinconf
]));
95 print_blanks(5 - strlen_P(levelp
));
96 if (pinconf
== OUTPUT_TIMER
)
97 printf_P(PSTR("%8ld %8ld"),
100 printf_P(PSTR("%d: \"%s\", "), pin
, pin_names
[pin
] ? pin_names
[pin
] : 0);
101 my_puts_P(pinconf_str
[pinconf
]);
102 printf_P(PSTR(", "));
105 if (pinconf
== OUTPUT_TIMER
)
106 printf_P(PSTR("divide by %ld (%ldHz)"),
109 printf_P(PSTR("\n"));
114 static int_fast8_t pinarg_insert(int pin
, uint_fast8_t count
, uint_fast8_t pinarg
[])
118 if (pin
< 0 || pin
>= PIN_MAX
)
121 for (pos
= 0; pos
< count
; pos
++) {
122 if (pin
== pinarg
[pos
])
124 if (pin
< pinarg
[pos
])
127 for (uint_fast8_t i
= count
-1; i
== pos
; i
--)
128 pinarg
[i
+1] = pinarg
[i
];
134 static uint_fast8_t pinarg_get(char * arg
, uint_fast8_t pinarg
[])
139 uint_fast8_t count
= 0;
142 pin1
= strtoul(arg
, &endp
, 10);
143 if (endp
!= arg
&& *endp
== '-') {
145 uint_fast8_t pin2
= (int) strtoul(arg
, &endp
, 10);
147 for (; pin1
< pin2
; pin1
++)
148 if ((rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0)
156 if ((*endp
== ',' || *endp
== '\0') &&
157 (rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0) {
171 command_ret_t
do_pin(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
173 char printheader
= 1;
174 uint_fast8_t pinarg
[PIN_MAX
];
175 uint_fast8_t pinargc
;
177 (void) cmdtp
; (void) flag
;
183 while ((opt
= getopt(argc
, argv
, PSTR("s"))) != -1) {
189 return CMD_RET_USAGE
;
193 /* remaining arguments */
199 /* print cofig of all pins */
200 for (pinargc
= 0; pinargc
< PIN_MAX
; pinargc
++)
201 pinarg
[pinargc
] = pinargc
;
204 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
206 return CMD_RET_USAGE
;
212 /* no more args, print config */
214 print_pin(pinarg
[0], 0);
217 if (pin_names_width
> 0) {
218 if ( strlen("Name") > pin_names_width
)
219 pin_names_width
= strlen("Name");
220 char s
[pin_names_width
+1];
221 memset(s
, ' ', pin_names_width
);
222 s
[pin_names_width
] = '\0';
223 strncpy_P(s
, PSTR("Name"), 4);
224 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s
);
225 memset(s
, '-', pin_names_width
);
226 printf_P(PSTR("----%s-----------------------------------\n"), s
);
228 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
229 "--------------------------------------\n"));
231 for (uint_fast8_t i
= 0; i
< pinargc
; i
++)
232 print_pin(pinarg
[i
], 1);
234 return CMD_RET_SUCCESS
;
237 /* arguments must be in pairs: pins conf */
239 return CMD_RET_USAGE
;
243 pinmode_t mode
= NONE
;
245 unsigned long value
= 0;
248 switch (toupper(argv
[optind
][0])) {
263 value
= strtoul(argv
[optind
], &endp
, 10);
272 if (*endp
&& strcmp_P(endp
, PSTR("Hz")) == 0) {
278 printf_P(PSTR("invalid parameter: '%s'\n"), argv
[optind
]);
279 return CMD_RET_USAGE
;
283 printf_P(PSTR("invalid value: %lu \n"));
284 return CMD_RET_USAGE
;
288 if (value
> F_CPU
/ 2) {
289 printf_P(PSTR("Max frequency is: %luHz\n"), F_CPU
/2);
290 return CMD_RET_USAGE
;
299 return CMD_RET_USAGE
;
301 for (uint_fast8_t i
= 0; i
< pinargc
; i
++) {
304 pin_write(pinarg
[i
], level
);
308 pin_config(pinarg
[i
], mode
);
311 if (pin_clockdiv_set(pinarg
[i
], value
) < 0) {
312 printf_P(PSTR("Setting pin %d to %lu failed.\n"),
322 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
326 return CMD_RET_SUCCESS
;