osd-contiki/examples/osd/arduino-sketch/resource_led_pwm.c
Ralf Schlatterbeck 4643c5d02d Factor resources, fix time
Now there is a generic resource that can generate and parse
application/json as well as text/plain. It can be re-used, only the
from_string and to_string routines have to be written and the resource
properly set up. A new resource format is specified, see
GENERIC_RESOURCE in, e.g., examples/osd/pwm-example. This is now used in
all my examples, namely pwm-example, arduino-sketch, wallclock-time.

There was an off by one error for the month in time formatting (in
gmtime and localtime). And the leap-year computation was broken. Both
fixed now, so we get a correct date. For localtime we are still 2 hours
off because daylight saving isn't implemented yet.

Also renamed gmtime to utc.
2014-06-27 22:25:51 +02:00

76 lines
1.4 KiB
C

/**
* \file
* Resource for Arduino PWM
* \author
* Ralf Schlatterbeck <rsc@runtux.com>
*
* \brief get/put pwm and period for LED pin
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "contiki.h"
#include "jsonparse.h"
/* Only coap 13 for now */
#include "er-coap-13.h"
#include "generic_resource.h"
#include "led_pwm.h"
void pwm_from_string (const char *name, const char *s)
{
uint32_t tmp = strtoul (s, NULL, 10);
if (tmp > 255) {
tmp = 255;
}
pwm = tmp;
}
size_t
pwm_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
{
return snprintf (buf, bufsize, "%d", pwm);
}
GENERIC_RESOURCE \
( led_pwm, METHOD_GET | METHOD_PUT
, "led/pwm"
, LED PWM
, duty-cycle
, pwm_from_string
, pwm_to_string
);
void period_from_string (const char *name, const char *s)
{
uint32_t tmp = (strtoul (s, NULL, 10) + 50) / 100;
if (tmp > 10) {
tmp = 10;
}
if (tmp == 0) {
tmp = 1;
}
period_100ms = tmp;
}
size_t
period_to_string (const char *name, uint8_t is_json, char *buf, size_t bufsize)
{
return snprintf (buf, bufsize, "%d", period_100ms * 100);
}
GENERIC_RESOURCE \
( led_period, METHOD_GET | METHOD_PUT
, "led/period"
, LED Period
, ms
, period_from_string
, period_to_string
);
/*
* VI settings, see coding style
* ex:ts=8:et:sw=2
*/