2014-06-26 11:00:01 +02:00
|
|
|
/**
|
|
|
|
* \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"
|
2015-01-21 15:09:31 +01:00
|
|
|
#include "er-coap.h"
|
2014-06-27 22:25:51 +02:00
|
|
|
#include "generic_resource.h"
|
2014-06-26 11:00:01 +02:00
|
|
|
#include "led_pwm.h"
|
|
|
|
|
2016-02-26 07:54:05 +01:00
|
|
|
int pwm_from_string (const char *name, const char *uri, const char *s)
|
2014-06-27 22:25:51 +02:00
|
|
|
{
|
|
|
|
uint32_t tmp = strtoul (s, NULL, 10);
|
|
|
|
if (tmp > 255) {
|
|
|
|
tmp = 255;
|
|
|
|
}
|
|
|
|
pwm = tmp;
|
2016-02-26 07:54:05 +01:00
|
|
|
return 0;
|
2014-06-27 22:25:51 +02:00
|
|
|
}
|
2014-06-26 11:00:01 +02:00
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
size_t
|
2016-02-26 07:54:05 +01:00
|
|
|
pwm_to_string (const char *name, const char *uri, char *buf, size_t bufsize)
|
2014-06-27 22:25:51 +02:00
|
|
|
{
|
|
|
|
return snprintf (buf, bufsize, "%d", pwm);
|
|
|
|
}
|
2014-06-26 11:00:01 +02:00
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
GENERIC_RESOURCE \
|
2015-01-21 15:09:31 +01:00
|
|
|
( led_pwm
|
2014-06-27 22:25:51 +02:00
|
|
|
, LED PWM
|
|
|
|
, duty-cycle
|
2016-02-26 07:54:05 +01:00
|
|
|
, 0
|
2014-06-27 22:25:51 +02:00
|
|
|
, pwm_from_string
|
|
|
|
, pwm_to_string
|
2014-06-26 11:00:01 +02:00
|
|
|
);
|
|
|
|
|
2016-02-26 07:54:05 +01:00
|
|
|
int period_from_string (const char *name, const char *uri, const char *s)
|
2014-06-26 11:00:01 +02:00
|
|
|
{
|
2014-06-27 22:25:51 +02:00
|
|
|
uint32_t tmp = (strtoul (s, NULL, 10) + 50) / 100;
|
|
|
|
if (tmp > 10) {
|
|
|
|
tmp = 10;
|
2014-06-26 11:00:01 +02:00
|
|
|
}
|
2014-06-27 22:25:51 +02:00
|
|
|
if (tmp == 0) {
|
|
|
|
tmp = 1;
|
|
|
|
}
|
|
|
|
period_100ms = tmp;
|
2016-02-26 07:54:05 +01:00
|
|
|
return 0;
|
2014-06-27 22:25:51 +02:00
|
|
|
}
|
2014-06-26 11:00:01 +02:00
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
size_t
|
2016-02-26 07:54:05 +01:00
|
|
|
period_to_string (const char *name, const char *uri, char *buf, size_t bufsize)
|
2014-06-27 22:25:51 +02:00
|
|
|
{
|
|
|
|
return snprintf (buf, bufsize, "%d", period_100ms * 100);
|
2014-06-26 11:00:01 +02:00
|
|
|
}
|
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
GENERIC_RESOURCE \
|
2015-01-21 15:09:31 +01:00
|
|
|
( led_period
|
2014-06-27 22:25:51 +02:00
|
|
|
, LED Period
|
|
|
|
, ms
|
2016-02-26 07:54:05 +01:00
|
|
|
, 0
|
2014-06-27 22:25:51 +02:00
|
|
|
, period_from_string
|
|
|
|
, period_to_string
|
2014-06-26 11:00:01 +02:00
|
|
|
);
|
|
|
|
|
2014-06-29 17:26:15 +02:00
|
|
|
size_t
|
2016-02-26 07:54:05 +01:00
|
|
|
analog2_v (const char *name, const char *uri, char *buf, size_t bufsize)
|
2014-06-29 17:26:15 +02:00
|
|
|
{
|
|
|
|
return snprintf
|
2014-06-30 10:24:49 +02:00
|
|
|
(buf, bufsize, "%d.%03d", analog2_voltage / 1000, analog2_voltage % 1000);
|
2014-06-29 17:26:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GENERIC_RESOURCE \
|
2015-01-21 15:09:31 +01:00
|
|
|
( analog2_voltage
|
2014-06-30 10:24:49 +02:00
|
|
|
, Analog 2 voltage
|
2014-06-29 17:26:15 +02:00
|
|
|
, V
|
2016-02-26 07:54:05 +01:00
|
|
|
, 0
|
2014-06-29 17:26:15 +02:00
|
|
|
, NULL
|
2014-06-30 10:24:49 +02:00
|
|
|
, analog2_v
|
2014-06-29 17:26:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
size_t
|
2016-02-26 07:54:05 +01:00
|
|
|
analog5_v (const char *name, const char *uri, char *buf, size_t bufsize)
|
2014-06-29 17:26:15 +02:00
|
|
|
{
|
|
|
|
return snprintf
|
|
|
|
(buf, bufsize, "%d.%03d", analog5_voltage / 1000, analog5_voltage % 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
GENERIC_RESOURCE \
|
2015-01-21 15:09:31 +01:00
|
|
|
( analog5_voltage
|
2014-06-29 17:26:15 +02:00
|
|
|
, Analog 5 voltage
|
|
|
|
, V
|
2016-02-26 07:54:05 +01:00
|
|
|
, 0
|
2014-06-29 17:26:15 +02:00
|
|
|
, NULL
|
|
|
|
, analog5_v
|
|
|
|
);
|
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
/*
|
|
|
|
* VI settings, see coding style
|
|
|
|
* ex:ts=8:et:sw=2
|
|
|
|
*/
|