Refactor GENERIC_RESOURCE macro
Now callback functions get the URI of the request, this allows to use a single resource for multiple different URIs. The is_json flag is now gone for the to-string function, instead the macro has an is_str flag. If set this automagically produces quotes around the string for json output. Now from-string functions can return an error-code, 0 for success, -1 for error.
This commit is contained in:
parent
c7daa7c45d
commit
c6165a3bcf
9 changed files with 106 additions and 68 deletions
|
@ -46,27 +46,25 @@ static uint8_t name_to_offset (const char * name)
|
|||
}
|
||||
|
||||
static size_t
|
||||
color_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize)
|
||||
color_to_string (const char *name, const char *uri, char *buf, size_t bsize)
|
||||
{
|
||||
char *fmt = "%d";
|
||||
if (is_json) {
|
||||
fmt = "\"%d\"";
|
||||
}
|
||||
return snprintf (buf, bsize, fmt, color_rgb [name_to_offset (name)]);
|
||||
return snprintf (buf, bsize, "%d", color_rgb [name_to_offset (name)]);
|
||||
}
|
||||
|
||||
void color_from_string (const char *name, const char *s)
|
||||
int color_from_string (const char *name, const char *uri, const char *s)
|
||||
{
|
||||
color_rgb [name_to_offset (name)] = atoi (s);
|
||||
Driver.begin();
|
||||
Driver.SetColor(color_rgb [0], color_rgb [1], color_rgb [2]);
|
||||
Driver.end();
|
||||
return 0;
|
||||
}
|
||||
|
||||
GENERIC_RESOURCE
|
||||
( red
|
||||
, RED_LED
|
||||
, s
|
||||
, 1
|
||||
, color_from_string
|
||||
, color_to_string
|
||||
);
|
||||
|
@ -75,6 +73,7 @@ GENERIC_RESOURCE
|
|||
( green
|
||||
, GREEN_LED
|
||||
, s
|
||||
, 1
|
||||
, color_from_string
|
||||
, color_to_string
|
||||
);
|
||||
|
@ -83,6 +82,7 @@ GENERIC_RESOURCE
|
|||
( blue
|
||||
, BLUE_LED
|
||||
, s
|
||||
, 1
|
||||
, color_from_string
|
||||
, color_to_string
|
||||
);
|
||||
|
@ -136,4 +136,3 @@ void loop (void)
|
|||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue