]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_pin.c
5 #include <avr/pgmspace.h>
8 #include "print-utils.h"
9 #include "getopt-min.h"
16 static const int namestr
= PIN_MAX
;
17 static char *pin_names
[PIN_MAX
+1];
18 static uint_least8_t pin_names_width
;
20 static void pinnames_get(void)
22 static const FLASH
char delim1
[] = {":= "};
23 static const FLASH
char delim2
[] = {", "};
28 if (pin_names
[namestr
] != NULL
)
29 free(pin_names
[namestr
]);
30 memset(pin_names
, 0, sizeof(pin_names
));
33 if ((lp
= getenv(PSTR(ENV_PINALIAS
))) != NULL
) {
34 pin_names
[namestr
] = strdup(lp
);
35 ptr
= strtok_P(pin_names
[namestr
], delim1
);
37 if (((i
= strtoul(ptr
, &lp
, 10)) < PIN_MAX
) &&
39 (ptr
= strtok_P(NULL
, delim2
)) != NULL
) {
41 ptr
= strtok_P(NULL
, delim1
);
45 for (i
= 0; i
< PIN_MAX
; i
++)
46 if (strlen(pin_names
[i
]) > pin_names_width
)
47 pin_names_width
= strlen(pin_names
[i
]);
52 static size_t xstrlen(char *s
)
60 static const FLASH
char * const FLASH pinconf_str
[] = {
68 static const FLASH
char * const FLASH pinlevel_str
[] = {
74 static int print_pin(int pin
, int multi
)
77 const FLASH
char *levelp
;
80 pinconf
= pin_config_get(pin
);
81 if (pinconf
== OUTPUT_TIMER
) {
82 div
= pin_clockdiv_get(pin
);
83 levelp
= pinlevel_str
[2];
85 levelp
= pinlevel_str
[pin_read(pin
)];
88 printf_P(PSTR("%3d "), pin
);
89 if (pin_names_width
) {
90 printf_P(PSTR("%s "), pin_names
[pin
]);
91 print_blanks(pin_names_width
- xstrlen(pin_names
[pin
]));
93 my_puts_P(pinconf_str
[pinconf
]);
94 print_blanks(7 - strlen_P(pinconf_str
[pinconf
]));
96 print_blanks(5 - strlen_P(levelp
));
97 if (pinconf
== OUTPUT_TIMER
)
98 printf_P(PSTR("%8ld %8ld"),
101 printf_P(PSTR("%d: \"%s\", "), pin
, pin_names
[pin
] ? pin_names
[pin
] : 0);
102 my_puts_P(pinconf_str
[pinconf
]);
103 printf_P(PSTR(", "));
106 if (pinconf
== OUTPUT_TIMER
)
107 printf_P(PSTR("divide by %ld (%ldHz)"),
110 printf_P(PSTR("\n"));
115 static int_fast8_t pinarg_insert(int pin
, uint_fast8_t count
, uint_fast8_t pinarg
[])
119 if (pin
< 0 || pin
>= PIN_MAX
)
122 for (pos
= 0; pos
< count
; pos
++) {
123 if (pin
== pinarg
[pos
])
125 if (pin
< pinarg
[pos
])
128 for (uint_fast8_t i
= count
-1; i
== pos
; i
--)
129 pinarg
[i
+1] = pinarg
[i
];
135 static uint_fast8_t pinarg_get(char * arg
, uint_fast8_t pinarg
[])
140 uint_fast8_t count
= 0;
143 pin1
= strtoul(arg
, &endp
, 10);
144 if (endp
!= arg
&& *endp
== '-') {
146 uint_fast8_t pin2
= (int) strtoul(arg
, &endp
, 10);
148 for (; pin1
< pin2
; pin1
++)
149 if ((rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0)
157 if ((*endp
== ',' || *endp
== '\0') &&
158 (rc
= pinarg_insert(pin1
, count
, pinarg
)) >= 0) {
172 command_ret_t
do_pin(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
174 char printheader
= 1;
175 uint_fast8_t pinarg
[PIN_MAX
];
176 uint_fast8_t pinargc
;
178 (void) cmdtp
; (void) flag
;
184 while ((opt
= getopt(argc
, argv
, PSTR("s"))) != -1) {
190 return CMD_RET_USAGE
;
194 /* remaining arguments */
200 /* print cofig of all pins */
201 for (pinargc
= 0; pinargc
< PIN_MAX
; pinargc
++)
202 pinarg
[pinargc
] = pinargc
;
205 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
207 return CMD_RET_USAGE
;
213 /* no more args, print config */
215 print_pin(pinarg
[0], 0);
218 if (pin_names_width
> 0) {
219 if ( strlen("Name") > pin_names_width
)
220 pin_names_width
= strlen("Name");
221 char s
[pin_names_width
+1];
222 memset(s
, ' ', pin_names_width
);
223 s
[pin_names_width
] = '\0';
224 strncpy_P(s
, PSTR("Name"), 4);
225 printf_P(PSTR("Pin %s Config Level Divider Frequency/Hz\n"),s
);
226 memset(s
, '-', pin_names_width
);
227 printf_P(PSTR("----%s-----------------------------------\n"), s
);
229 printf_P(PSTR("Pin Config Level Divider Frequency/Hz\n"
230 "--------------------------------------\n"));
232 for (uint_fast8_t i
= 0; i
< pinargc
; i
++)
233 print_pin(pinarg
[i
], 1);
235 return CMD_RET_SUCCESS
;
238 /* arguments must be in pairs: pins conf */
240 return CMD_RET_USAGE
;
244 pinmode_t mode
= NONE
;
246 unsigned long value
= 0;
249 switch (toupper(argv
[optind
][0])) {
264 value
= strtoul(argv
[optind
], &endp
, 10);
273 if (*endp
&& strcmp_P(endp
, PSTR("Hz")) == 0) {
279 printf_P(PSTR("invalid parameter: '%s'\n"), argv
[optind
]);
280 return CMD_RET_USAGE
;
284 printf_P(PSTR("invalid value: %lu \n"));
285 return CMD_RET_USAGE
;
289 if (value
> F_CPU
/ 2) {
290 printf_P(PSTR("Max frequency is: %luHz\n"), F_CPU
/2);
291 return CMD_RET_USAGE
;
300 return CMD_RET_USAGE
;
302 for (uint_fast8_t i
= 0; i
< pinargc
; i
++) {
305 pin_write(pinarg
[i
], level
);
309 pin_config(pinarg
[i
], mode
);
312 if (pin_clockdiv_set(pinarg
[i
], value
) < 0) {
313 printf_P(PSTR("Setting pin %d to %lu failed.\n"),
323 pinargc
= pinarg_get(argv
[optind
++], pinarg
);
327 return CMD_RET_SUCCESS
;