Fix servo example
This commit is contained in:
parent
3ba9009f50
commit
0980393d22
2 changed files with 34 additions and 84 deletions
|
@ -31,40 +31,9 @@ endif
|
||||||
# linker optimizations
|
# linker optimizations
|
||||||
SMALL=1
|
SMALL=1
|
||||||
|
|
||||||
# REST framework, requires WITH_COAP
|
|
||||||
ifeq ($(WITH_COAP), 13)
|
|
||||||
${info INFO: compiling with CoAP-13}
|
|
||||||
CFLAGS += -DWITH_COAP=13
|
|
||||||
CFLAGS += -DREST=coap_rest_implementation
|
|
||||||
CFLAGS += -DUIP_CONF_TCP=0
|
|
||||||
APPS += er-coap-13
|
|
||||||
else ifeq ($(WITH_COAP), 12)
|
|
||||||
${info INFO: compiling with CoAP-12}
|
|
||||||
CFLAGS += -DWITH_COAP=12
|
|
||||||
CFLAGS += -DREST=coap_rest_implementation
|
|
||||||
CFLAGS += -DUIP_CONF_TCP=0
|
|
||||||
APPS += er-coap-12
|
|
||||||
else ifeq ($(WITH_COAP), 7)
|
|
||||||
${info INFO: compiling with CoAP-08}
|
|
||||||
CFLAGS += -DWITH_COAP=7
|
|
||||||
CFLAGS += -DREST=coap_rest_implementation
|
|
||||||
CFLAGS += -DUIP_CONF_TCP=0
|
|
||||||
APPS += er-coap-07
|
|
||||||
else ifeq ($(WITH_COAP), 3)
|
|
||||||
${info INFO: compiling with CoAP-03}
|
|
||||||
CFLAGS += -DWITH_COAP=3
|
|
||||||
CFLAGS += -DREST=coap_rest_implementation
|
|
||||||
CFLAGS += -DUIP_CONF_TCP=0
|
|
||||||
APPS += er-coap-03
|
|
||||||
else
|
|
||||||
${info INFO: compiling with HTTP}
|
|
||||||
CFLAGS += -DWITH_HTTP
|
|
||||||
CFLAGS += -DREST=http_rest_implementation
|
|
||||||
CFLAGS += -DUIP_CONF_TCP=1
|
|
||||||
APPS += er-http-engine
|
|
||||||
endif
|
|
||||||
|
|
||||||
APPS += erbium
|
APPS += er-coap
|
||||||
|
APPS += rest-engine
|
||||||
|
|
||||||
# optional rules to get assembly
|
# optional rules to get assembly
|
||||||
#CUSTOM_RULE_C_TO_OBJECTDIR_O = 1
|
#CUSTOM_RULE_C_TO_OBJECTDIR_O = 1
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "contiki.h"
|
#include "contiki.h"
|
||||||
#include "contiki-net.h"
|
#include "contiki-net.h"
|
||||||
|
#include "rest-engine.h"
|
||||||
|
|
||||||
|
|
||||||
/* Define which resources to include to meet memory constraints. */
|
/* Define which resources to include to meet memory constraints. */
|
||||||
|
@ -52,8 +53,6 @@
|
||||||
#define REST_RES_TOGGLE 0
|
#define REST_RES_TOGGLE 0
|
||||||
#define REST_RES_BATTERY 1
|
#define REST_RES_BATTERY 1
|
||||||
|
|
||||||
#include "erbium.h"
|
|
||||||
|
|
||||||
#if defined (PLATFORM_HAS_BUTTON)
|
#if defined (PLATFORM_HAS_BUTTON)
|
||||||
#include "dev/button-sensor.h"
|
#include "dev/button-sensor.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -74,19 +73,6 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* For CoAP-specific example: not required for normal RESTful Web service. */
|
|
||||||
#if WITH_COAP == 3
|
|
||||||
#include "er-coap-03.h"
|
|
||||||
#elif WITH_COAP == 7
|
|
||||||
#include "er-coap-07.h"
|
|
||||||
#elif WITH_COAP == 12
|
|
||||||
#include "er-coap-12.h"
|
|
||||||
#elif WITH_COAP == 13
|
|
||||||
#include "er-coap-13.h"
|
|
||||||
#else
|
|
||||||
#warning "Erbium example without CoAP-specifc functionality"
|
|
||||||
#endif /* CoAP-specific example */
|
|
||||||
|
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
#define PRINTF(...) printf(__VA_ARGS__)
|
#define PRINTF(...) printf(__VA_ARGS__)
|
||||||
|
@ -101,12 +87,6 @@
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#if REST_RES_INFO
|
#if REST_RES_INFO
|
||||||
/*
|
|
||||||
* Resources are defined by the RESOURCE macro.
|
|
||||||
* Signature: resource name, the RESTful methods it handles, and its URI path (omitting the leading slash).
|
|
||||||
*/
|
|
||||||
RESOURCE(info, METHOD_GET, "info", "title=\"Info\";rt=\"text\"");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A handler function named [resource name]_handler must be implemented for each RESOURCE.
|
* A handler function named [resource name]_handler must be implemented for each RESOURCE.
|
||||||
* A buffer for the response payload is provided through the buffer pointer. Simple resources can ignore
|
* A buffer for the response payload is provided through the buffer pointer. Simple resources can ignore
|
||||||
|
@ -114,7 +94,7 @@ RESOURCE(info, METHOD_GET, "info", "title=\"Info\";rt=\"text\"");
|
||||||
* If a smaller block size is requested for CoAP, the REST framework automatically splits the data.
|
* If a smaller block size is requested for CoAP, the REST framework automatically splits the data.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
info_get_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
{
|
{
|
||||||
char message[100];
|
char message[100];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
@ -132,11 +112,17 @@ info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_
|
||||||
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
||||||
REST.set_response_payload(response, buffer, length);
|
REST.set_response_payload(response, buffer, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Resources are defined by the RESOURCE macro.
|
||||||
|
* Signature: resource name, the RESTful methods it handles, and its URI path (omitting the leading slash).
|
||||||
|
*/
|
||||||
|
RESOURCE(res_info, "title=\"Info\";rt=\"text\"", info_get_handler, NULL, NULL, NULL);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined (PLATFORM_HAS_SERVO)
|
#if defined (PLATFORM_HAS_SERVO)
|
||||||
/*A simple actuator example. read the servo status*/
|
/*A simple actuator example. read the servo status*/
|
||||||
RESOURCE(servo, METHOD_GET | METHOD_PUT , "actuators/servo", "title=\"Servo\";rt=\"servo\"");
|
|
||||||
void
|
void
|
||||||
servo_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
servo_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
{
|
{
|
||||||
|
@ -187,10 +173,10 @@ servo_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
|
||||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RESOURCE(res_servo, "title=\"Servo\";rt=\"servo\"", servo_handler, servo_handler, NULL, NULL );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined (PLATFORM_HAS_T4_SERVO)
|
#if defined (PLATFORM_HAS_T4_SERVO)
|
||||||
RESOURCE(t4_servo, METHOD_GET | METHOD_PUT , "actuators/t4_servo", "title=\"Timer4Servo\";rt=\"t4_servo\"");
|
|
||||||
void
|
void
|
||||||
t4_servo_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
t4_servo_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
{
|
{
|
||||||
|
@ -266,6 +252,7 @@ t4_servo_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
|
||||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RESOURCE(res_t4_servo,"title=\"Timer4Servo\";rt=\"t4_servo\"", t4_servo_handler, te4_servo_handler, NULL, NULL );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -274,8 +261,6 @@ t4_servo_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
#if REST_RES_LEDS
|
#if REST_RES_LEDS
|
||||||
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
|
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
|
||||||
RESOURCE(leds, METHOD_POST | METHOD_PUT , "actuators/leds", "title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"");
|
|
||||||
|
|
||||||
void
|
void
|
||||||
leds_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
leds_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
{
|
{
|
||||||
|
@ -319,17 +304,18 @@ leds_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_
|
||||||
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
REST.set_response_status(response, REST.status.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RESOURCE(res_leds, "title=\"LEDs: ?color=r|g|b, POST/PUT mode=on|off\";rt=\"Control\"", NULL, leds_handler, leds_handler, NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
#if REST_RES_TOGGLE
|
#if REST_RES_TOGGLE
|
||||||
/* A simple actuator example. Toggles the red led */
|
/* A simple actuator example. Toggles the red led */
|
||||||
RESOURCE(toggle, METHOD_GET | METHOD_PUT | METHOD_POST, "actuators/toggle", "title=\"Red LED\";rt=\"Control\"");
|
|
||||||
void
|
void
|
||||||
toggle_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
toggle_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
{
|
{
|
||||||
leds_toggle(LEDS_RED);
|
leds_toggle(LEDS_RED);
|
||||||
}
|
}
|
||||||
|
RESOURCE(res_toggle, "title=\"Red LED\";rt=\"Control\"", toggle_handler, toggle_handler, toggle_handler, NULL);
|
||||||
#endif
|
#endif
|
||||||
#endif /* PLATFORM_HAS_LEDS */
|
#endif /* PLATFORM_HAS_LEDS */
|
||||||
|
|
||||||
|
@ -338,7 +324,6 @@ toggle_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
#if REST_RES_TEMPERATURE && defined (PLATFORM_HAS_TEMPERATURE)
|
#if REST_RES_TEMPERATURE && defined (PLATFORM_HAS_TEMPERATURE)
|
||||||
/* A simple getter example. Returns the reading from light sensor with a simple etag */
|
/* A simple getter example. Returns the reading from light sensor with a simple etag */
|
||||||
RESOURCE(temperature, METHOD_GET, "sensors/cputemp", "title=\"Temperature status\";rt=\"temperature-c\"");
|
|
||||||
void
|
void
|
||||||
temperature_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
temperature_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
{
|
{
|
||||||
|
@ -368,41 +353,37 @@ temperature_handler(void* request, void* response, uint8_t *buffer, uint16_t pre
|
||||||
REST.set_response_payload(response, msg, strlen(msg));
|
REST.set_response_payload(response, msg, strlen(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RESOURCE(res_temperature, "title=\"Temperature status\";rt=\"temperature-c\"", temperature_handler, NULL, NULL, NULL);
|
||||||
#endif /* PLATFORM_HAS_TEMPERATURE */
|
#endif /* PLATFORM_HAS_TEMPERATURE */
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
#if REST_RES_BATTERY && defined (PLATFORM_HAS_BATTERY)
|
#if REST_RES_BATTERY && defined (PLATFORM_HAS_BATTERY)
|
||||||
/* A simple getter example. Returns the reading from light sensor with a simple etag */
|
/* A simple getter example. Returns the reading from light sensor with a simple etag */
|
||||||
RESOURCE(battery, METHOD_GET, "sensors/battery", "title=\"Battery status\";rt=\"battery-mV\"");
|
|
||||||
void
|
void
|
||||||
battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
|
||||||
{
|
{
|
||||||
int battery = battery_sensor.value(0);
|
int battery = battery_sensor.value(0);
|
||||||
|
|
||||||
const uint16_t *accept = NULL;
|
unsigned int accept = -1;
|
||||||
int num = REST.get_header_accept(request, &accept);
|
REST.get_header_accept(request, &accept);
|
||||||
|
|
||||||
if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN))
|
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
|
||||||
{
|
|
||||||
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
|
||||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", battery);
|
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d.%02d", battery/1000, battery % 1000);
|
||||||
|
|
||||||
REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer));
|
|
||||||
}
|
|
||||||
else if (num && (accept[0]==REST.type.APPLICATION_JSON))
|
|
||||||
{
|
|
||||||
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
|
||||||
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d}", battery);
|
|
||||||
|
|
||||||
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||||
}
|
} else if(accept == REST.type.APPLICATION_JSON) {
|
||||||
else
|
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
|
||||||
{
|
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'battery':%d.%02d}", battery/1000, battery % 1000);
|
||||||
|
|
||||||
|
REST.set_response_payload(response, buffer, strlen((char *)buffer));
|
||||||
|
} else {
|
||||||
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
|
||||||
const char *msg = "Supporting content-types text/plain and application/json";
|
const char *msg = "Supporting content-types text/plain and application/json";
|
||||||
REST.set_response_payload(response, msg, strlen(msg));
|
REST.set_response_payload(response, msg, strlen(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RESOURCE(res_battery, "title=\"Battery status\";rt=\"battery-mV\"", battery_handler, NULL, NULL, NULL);
|
||||||
#endif /* PLATFORM_HAS_BATTERY */
|
#endif /* PLATFORM_HAS_BATTERY */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
@ -448,31 +429,31 @@ PROCESS_THREAD(rest_server_example, ev, data)
|
||||||
|
|
||||||
/* Activate the application-specific resources. */
|
/* Activate the application-specific resources. */
|
||||||
#if REST_RES_INFO
|
#if REST_RES_INFO
|
||||||
rest_activate_resource(&resource_info);
|
rest_activate_resource(&res_info, "info");
|
||||||
#endif
|
#endif
|
||||||
#if defined (PLATFORM_HAS_LEDS)
|
#if defined (PLATFORM_HAS_LEDS)
|
||||||
#if REST_RES_LEDS
|
#if REST_RES_LEDS
|
||||||
rest_activate_resource(&resource_leds);
|
rest_activate_resource(&res_leds, "actuators/leds");
|
||||||
#endif
|
#endif
|
||||||
#if REST_RES_TOGGLE
|
#if REST_RES_TOGGLE
|
||||||
rest_activate_resource(&resource_toggle);
|
rest_activate_resource(&res_toggle, "actuators/toggle");
|
||||||
#endif
|
#endif
|
||||||
#endif /* PLATFORM_HAS_LEDS */
|
#endif /* PLATFORM_HAS_LEDS */
|
||||||
#if defined (PLATFORM_HAS_TEMPERATURE) && REST_RES_TEMPERATURE
|
#if defined (PLATFORM_HAS_TEMPERATURE) && REST_RES_TEMPERATURE
|
||||||
SENSORS_ACTIVATE(temperature_sensor);
|
SENSORS_ACTIVATE(temperature_sensor);
|
||||||
rest_activate_resource(&resource_temperature);
|
rest_activate_resource(&res_temperature, "sensors/cputemp");
|
||||||
#endif
|
#endif
|
||||||
#if defined (PLATFORM_HAS_BATTERY) && REST_RES_BATTERY
|
#if defined (PLATFORM_HAS_BATTERY) && REST_RES_BATTERY
|
||||||
SENSORS_ACTIVATE(battery_sensor);
|
SENSORS_ACTIVATE(battery_sensor);
|
||||||
rest_activate_resource(&resource_battery);
|
rest_activate_resource(&res_battery, "sensors/battery");
|
||||||
#endif
|
#endif
|
||||||
#if defined (PLATFORM_HAS_SERVO) && REST_RES_SERVO
|
#if defined (PLATFORM_HAS_SERVO) && REST_RES_SERVO
|
||||||
SENSORS_ACTIVATE(servo_sensor);
|
SENSORS_ACTIVATE(servo_sensor);
|
||||||
rest_activate_resource(&res_servo);
|
rest_activate_resource(&res_servo, "actuators/servo");
|
||||||
#endif
|
#endif
|
||||||
#if defined (PLATFORM_HAS_T4_SERVO) && REST_RES_T4_SERVO
|
#if defined (PLATFORM_HAS_T4_SERVO) && REST_RES_T4_SERVO
|
||||||
SENSORS_ACTIVATE(t4_servo_sensor);
|
SENSORS_ACTIVATE(t4_servo_sensor);
|
||||||
rest_activate_resource(&resource_t4_servo);
|
rest_activate_resource(&res_t4_servo, "actuators/t4_servo");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Define application-specific events here. */
|
/* Define application-specific events here. */
|
||||||
|
|
Loading…
Add table
Reference in a new issue