tcp_cmd
split common code for colour conversion into a separate function
This commit is contained in:
@@ -46,6 +46,38 @@ static char* shift_args(int* argc, char*** argv) {
|
|||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function converts a string to a color
|
||||||
|
* @param argc The number of arguments
|
||||||
|
* @param argv The arguments
|
||||||
|
* @param color The color to write to
|
||||||
|
* @return Returns 0 if successful, 1 if not
|
||||||
|
*/
|
||||||
|
static uint32_t str_to_color(int argc, char** argv, uint32_t* color) {
|
||||||
|
if (argc == 1) {
|
||||||
|
*color = (uint32_t)strtoul(argv[0], NULL, 16);
|
||||||
|
*color ^= 0xff000000;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (argc == 3) {
|
||||||
|
*color = 0xff000000;
|
||||||
|
*color |= (uint32_t)strtoul(argv[0], NULL, 10) << 16;
|
||||||
|
*color |= (uint32_t)strtoul(argv[1], NULL, 10) << 8;
|
||||||
|
*color |= (uint32_t)strtoul(argv[2], NULL, 10);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (argc == 4) {
|
||||||
|
*color = 0xff000000;
|
||||||
|
*color ^= (uint32_t)strtoul(argv[0], NULL, 10) << 24;
|
||||||
|
*color |= (uint32_t)strtoul(argv[1], NULL, 10) << 16;
|
||||||
|
*color |= (uint32_t)strtoul(argv[2], NULL, 10) << 8;
|
||||||
|
*color |= (uint32_t)strtoul(argv[3], NULL, 10);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This function is a wrapper for tcp_write and tcp_output
|
* @brief This function is a wrapper for tcp_write and tcp_output
|
||||||
* @param pcb The tcp_pcb struct to write to
|
* @param pcb The tcp_pcb struct to write to
|
||||||
@@ -130,27 +162,8 @@ static void tcp_cmd_text(struct tcp_pcb* pcb, int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void tcp_cmd_bg_color(struct tcp_pcb* pcb, int argc, char** argv) {
|
static void tcp_cmd_bg_color(struct tcp_pcb* pcb, int argc, char** argv) {
|
||||||
if (argc == 1) {
|
if (!str_to_color(argc, argv, &color_bg)) {
|
||||||
LOG_INFO(TAG, "Setting background color to %s", argv[0]);
|
LOG_INFO(TAG, "Setting background color to %08lX", color_bg);
|
||||||
color_bg = (uint32_t)strtoul(argv[0], NULL, 16);
|
|
||||||
color_bg ^= 0xff000000;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (argc == 3) {
|
|
||||||
LOG_INFO(TAG, "Setting background color to %s %s %s", argv[0], argv[1], argv[2]);
|
|
||||||
color_bg = 0xff000000;
|
|
||||||
color_bg |= (uint32_t)strtoul(argv[0], NULL, 10) << 16;
|
|
||||||
color_bg |= (uint32_t)strtoul(argv[1], NULL, 10) << 8;
|
|
||||||
color_bg |= (uint32_t)strtoul(argv[2], NULL, 10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (argc == 4) {
|
|
||||||
LOG_INFO(TAG, "Setting background color to %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
|
|
||||||
color_bg = 0xff000000;
|
|
||||||
color_bg ^= (uint32_t)strtoul(argv[0], NULL, 10) << 24;
|
|
||||||
color_bg |= (uint32_t)strtoul(argv[1], NULL, 10) << 16;
|
|
||||||
color_bg |= (uint32_t)strtoul(argv[2], NULL, 10) << 8;
|
|
||||||
color_bg |= (uint32_t)strtoul(argv[3], NULL, 10);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG_WARN(TAG, "Bad usage of bgcolor");
|
LOG_WARN(TAG, "Bad usage of bgcolor");
|
||||||
@@ -161,27 +174,8 @@ static void tcp_cmd_bg_color(struct tcp_pcb* pcb, int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void tcp_cmd_color(struct tcp_pcb* pcb, int argc, char** argv) {
|
static void tcp_cmd_color(struct tcp_pcb* pcb, int argc, char** argv) {
|
||||||
if (argc == 1) {
|
if (!str_to_color(argc, argv, &color_txt)) {
|
||||||
LOG_INFO(TAG, "Setting text color to %s", argv[0]);
|
LOG_INFO(TAG, "Setting color to %08lX", color_txt);
|
||||||
color_txt = (uint32_t)strtoul(argv[0], NULL, 16);
|
|
||||||
color_txt ^= 0xff000000;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (argc == 3) {
|
|
||||||
LOG_INFO(TAG, "Setting text color to %s %s %s", argv[0], argv[1], argv[2]);
|
|
||||||
color_txt = 0xff000000;
|
|
||||||
color_txt |= (uint32_t)strtoul(argv[0], NULL, 10) << 16;
|
|
||||||
color_txt |= (uint32_t)strtoul(argv[1], NULL, 10) << 8;
|
|
||||||
color_txt |= (uint32_t)strtoul(argv[2], NULL, 10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (argc == 4) {
|
|
||||||
LOG_INFO(TAG, "Setting text color to %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
|
|
||||||
color_txt = 0xff000000;
|
|
||||||
color_txt ^= (uint32_t)strtoul(argv[0], NULL, 10) << 24;
|
|
||||||
color_txt |= (uint32_t)strtoul(argv[1], NULL, 10) << 16;
|
|
||||||
color_txt |= (uint32_t)strtoul(argv[2], NULL, 10) << 8;
|
|
||||||
color_txt |= (uint32_t)strtoul(argv[3], NULL, 10);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG_WARN(TAG, "Bad usage of color");
|
LOG_WARN(TAG, "Bad usage of color");
|
||||||
|
|||||||
Reference in New Issue
Block a user