mirror of https://github.com/nirenjan/libx52.git
Use X-macros for x52cli commands
parent
7dcd3049ec
commit
b1139806f5
|
@ -15,21 +15,6 @@
|
||||||
|
|
||||||
#include "libx52.h"
|
#include "libx52.h"
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
X52_CTL_CMD_BRIGHTNESS,
|
|
||||||
X52_CTL_CMD_MFD_TEXT,
|
|
||||||
X52_CTL_CMD_LED_STATE,
|
|
||||||
X52_CTL_CMD_BLINK,
|
|
||||||
X52_CTL_CMD_SHIFT,
|
|
||||||
X52_CTL_CMD_CLOCK,
|
|
||||||
X52_CTL_CMD_OFFSET,
|
|
||||||
X52_CTL_CMD_TIME,
|
|
||||||
X52_CTL_CMD_DATE,
|
|
||||||
X52_CTL_CMD_RAW,
|
|
||||||
|
|
||||||
X52_CTL_CMD_MAX
|
|
||||||
} x52_ctl_command;
|
|
||||||
|
|
||||||
struct string_map {
|
struct string_map {
|
||||||
char *key;
|
char *key;
|
||||||
union {
|
union {
|
||||||
|
@ -160,21 +145,6 @@ DEFINE_MAP(time_format) = {
|
||||||
MAP_TERMINATOR
|
MAP_TERMINATOR
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Map for commands */
|
|
||||||
DEFINE_MAP(command) = {
|
|
||||||
MAP_INT( "led", X52_CTL_CMD_LED_STATE ),
|
|
||||||
MAP_INT( "bri", X52_CTL_CMD_BRIGHTNESS ),
|
|
||||||
MAP_INT( "mfd", X52_CTL_CMD_MFD_TEXT ),
|
|
||||||
MAP_INT( "blink", X52_CTL_CMD_BLINK ),
|
|
||||||
MAP_INT( "shift", X52_CTL_CMD_SHIFT ),
|
|
||||||
MAP_INT( "clock", X52_CTL_CMD_CLOCK ),
|
|
||||||
MAP_INT( "offset", X52_CTL_CMD_OFFSET ),
|
|
||||||
MAP_INT( "time", X52_CTL_CMD_TIME ),
|
|
||||||
MAP_INT( "date", X52_CTL_CMD_DATE ),
|
|
||||||
MAP_INT( "raw", X52_CTL_CMD_RAW ),
|
|
||||||
MAP_TERMINATOR
|
|
||||||
};
|
|
||||||
|
|
||||||
static int update_led(libx52_device *x52, void *args[])
|
static int update_led(libx52_device *x52, void *args[])
|
||||||
{
|
{
|
||||||
return libx52_set_led_state(x52,
|
return libx52_set_led_state(x52,
|
||||||
|
@ -276,7 +246,7 @@ static int update_date(libx52_device *x52, void *args[])
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_raw(libx52_device *x52, void *args[])
|
static int update_raw(libx52_device *x52, void *args[])
|
||||||
{
|
{
|
||||||
uint16_t wIndex = (uint16_t)strtoul(args[0], NULL, 0);
|
uint16_t wIndex = (uint16_t)strtoul(args[0], NULL, 0);
|
||||||
uint16_t wValue = (uint16_t)strtoul(args[1], NULL, 0);
|
uint16_t wValue = (uint16_t)strtoul(args[1], NULL, 0);
|
||||||
|
@ -284,97 +254,62 @@ static int write_raw(libx52_device *x52, void *args[])
|
||||||
return libx52_vendor_command(x52, wIndex, wValue);
|
return libx52_vendor_command(x52, wIndex, wValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct command_handler handlers[X52_CTL_CMD_MAX] = {
|
/* Commands for CLI */
|
||||||
[X52_CTL_CMD_LED_STATE] = {
|
#define COMMANDS \
|
||||||
update_led,
|
X(led, LED_STATE, "<led-id> <state>", 2, \
|
||||||
2,
|
MAP(led_id), MAP(led_state)) \
|
||||||
{
|
X(bri, BRIGHTNESS, "{mfd | led} <brightness level>", 2, \
|
||||||
MAP(led_id),
|
MAP(brightness_targets), NULL) \
|
||||||
MAP(led_state),
|
X(mfd, MFD_TEXT, "<line> <text in quotes>", 2, \
|
||||||
},
|
NULL, NULL) \
|
||||||
"led <led-id> <state>"
|
X(blink, BLINK, "{ on | off }", 1, \
|
||||||
},
|
MAP(on_off)) \
|
||||||
[X52_CTL_CMD_BRIGHTNESS] = {
|
X(shift, SHIFT, "{ on | off }", 1, \
|
||||||
update_bri,
|
MAP(on_off)) \
|
||||||
2,
|
X(clock, CLOCK, \
|
||||||
{
|
"{local | gmt} {12hr | 24hr} {ddmmyy | mmddyy | yymmdd}", \
|
||||||
MAP(brightness_targets),
|
3, MAP(clock0_timezone), MAP(time_format), MAP(date_format)) \
|
||||||
NULL,
|
X(offset, OFFSET, \
|
||||||
},
|
"{2 | 3} <offset from clock 1 in minutes> {12hr | 24hr}", \
|
||||||
"bri {mfd | led} <brightness level>"
|
3, MAP(clocks), NULL, MAP(time_format)) \
|
||||||
},
|
X(time, TIME, \
|
||||||
[X52_CTL_CMD_MFD_TEXT] = {
|
"<hour> <minute> {12hr | 24hr}", 3, \
|
||||||
update_mfd,
|
NULL, NULL, MAP(time_format)) \
|
||||||
2,
|
X(date, DATE, \
|
||||||
{
|
"<dd> <mm> <yy> {ddmmyy | mmddyy | yymmdd}", 4, \
|
||||||
NULL,
|
NULL, NULL, NULL, MAP(date_format)) \
|
||||||
NULL,
|
X(raw, RAW, "<wIndex> <wValue>", 2, NULL, NULL)
|
||||||
},
|
|
||||||
"mfd <line> <text in quotes>"
|
/* Enums for command identification */
|
||||||
},
|
#define X(cmd, en, help, args, ...) X52_CTL_CMD_ ## en,
|
||||||
[X52_CTL_CMD_BLINK] = {
|
enum {
|
||||||
update_blink,
|
COMMANDS
|
||||||
1,
|
|
||||||
{ MAP(on_off) },
|
X52_CTL_CMD_MAX
|
||||||
"blink { on | off }"
|
|
||||||
},
|
|
||||||
[X52_CTL_CMD_SHIFT] = {
|
|
||||||
update_shift,
|
|
||||||
1,
|
|
||||||
{ MAP(on_off) },
|
|
||||||
"shift { on | off }"
|
|
||||||
},
|
|
||||||
[X52_CTL_CMD_CLOCK] = {
|
|
||||||
update_clock,
|
|
||||||
3,
|
|
||||||
{
|
|
||||||
MAP(clock0_timezone),
|
|
||||||
MAP(time_format),
|
|
||||||
MAP(date_format),
|
|
||||||
},
|
|
||||||
"clock {local | gmt} {12hr | 24hr} {ddmmyy | mmddyy | yymmdd}"
|
|
||||||
},
|
|
||||||
[X52_CTL_CMD_OFFSET] = {
|
|
||||||
update_offset,
|
|
||||||
3,
|
|
||||||
{
|
|
||||||
MAP(clocks),
|
|
||||||
NULL,
|
|
||||||
MAP(time_format)
|
|
||||||
},
|
|
||||||
"offset {2 | 3} <offset from clock 1 in minutes> {12hr | 24hr}"
|
|
||||||
},
|
|
||||||
[X52_CTL_CMD_TIME] = {
|
|
||||||
update_time,
|
|
||||||
3,
|
|
||||||
{
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
MAP(time_format)
|
|
||||||
},
|
|
||||||
"time <hour> <minute> {12hr | 24hr}"
|
|
||||||
},
|
|
||||||
[X52_CTL_CMD_DATE] = {
|
|
||||||
update_date,
|
|
||||||
4,
|
|
||||||
{
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
MAP(date_format)
|
|
||||||
},
|
|
||||||
"date <dd> <mm> <yy> {ddmmyy | mmddyy | yymmdd}"
|
|
||||||
},
|
|
||||||
[X52_CTL_CMD_RAW] = {
|
|
||||||
write_raw,
|
|
||||||
2,
|
|
||||||
{
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
},
|
|
||||||
"raw <wIndex> <wValue>"
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
/* Map for commands */
|
||||||
|
#define X(cmd, en, help, args, ...) MAP_INT( #cmd, X52_CTL_CMD_ ## en),
|
||||||
|
DEFINE_MAP(command) = {
|
||||||
|
COMMANDS
|
||||||
|
|
||||||
|
MAP_TERMINATOR
|
||||||
|
};
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
/* Command handlers */
|
||||||
|
#define X(cmd, en, help, args, ...) \
|
||||||
|
[X52_CTL_CMD_ ## en] = { \
|
||||||
|
update_ ## cmd, \
|
||||||
|
args, \
|
||||||
|
{ __VA_ARGS__ }, \
|
||||||
|
#cmd " " help \
|
||||||
|
},
|
||||||
|
const struct command_handler handlers[X52_CTL_CMD_MAX] = {
|
||||||
|
COMMANDS
|
||||||
|
};
|
||||||
|
#undef X
|
||||||
|
|
||||||
static void do_help(const struct command_handler *cmd)
|
static void do_help(const struct command_handler *cmd)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue