diff --git a/examples/osd/arduino-ledstringlight/Makefile b/examples/osd/arduino-ledstringlight/Makefile new file mode 100644 index 000000000..0e6730ee3 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/Makefile @@ -0,0 +1,51 @@ +# Set this to the name of your sketch (without extension .pde) +SKETCH=sketch +EXE=arduino-example + +all: $(EXE) + +CONTIKI=../../.. + +# Contiki IPv6 configuration +CONTIKI_WITH_IPV6 = 1 + +CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\" + +PROJECT_SOURCEFILES += resource_led_pwm.c ${SKETCH}.cpp + +# variable for Makefile.include +ifneq ($(TARGET), minimal-net) +CFLAGS += -DUIP_CONF_IPV6_RPL=1 +else +# minimal-net does not support RPL under Linux and is mostly used to test CoAP only +${info INFO: compiling without RPL} +CFLAGS += -DUIP_CONF_IPV6_RPL=0 +CFLAGS += -DHARD_CODED_ADDRESS=\"fdfd::10\" +${info INFO: compiling with large buffers} +CFLAGS += -DUIP_CONF_BUFFER_SIZE=2048 +CFLAGS += -DREST_MAX_CHUNK_SIZE=1024 +CFLAGS += -DCOAP_MAX_HEADER_SIZE=640 +endif + +# linker optimizations +SMALL=1 + +# REST Engine shall use Erbium CoAP implementation +APPS += er-coap +APPS += rest-engine +APPS += time json arduino json-resource + +include $(CONTIKI)/Makefile.include +include $(CONTIKI)/apps/arduino/Makefile.include + +$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c + (cd $(CONTIKI)/tools && $(MAKE) tunslip6) + +connect-router: $(CONTIKI)/tools/tunslip6 + sudo $(CONTIKI)/tools/tunslip6 aaaa::1/64 + +connect-router-cooja: $(CONTIKI)/tools/tunslip6 + sudo $(CONTIKI)/tools/tunslip6 -a 127.0.0.1 aaaa::1/64 + +connect-minimal: + sudo ip address add fdfd::1/64 dev tap0 diff --git a/examples/osd/arduino-ledstringlight/README.md b/examples/osd/arduino-ledstringlight/README.md new file mode 100644 index 000000000..e1490ed05 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/README.md @@ -0,0 +1,11 @@ +Arduino compatibility example +============================= + +This example shows that it is now possible to re-use arduino sketches in +Contiki. This example documents the necessary magic. Arduino specifies +two routines, `setup` and `loop`. Before `setup` is called, the +framework initializes hardware. In original Arduino, all this is done in +a `main` function (in C). For contiki we define a process that does the +same. + +See the documentation file in apps/contiki-compat/README.md diff --git a/examples/osd/arduino-ledstringlight/arduino-example.c b/examples/osd/arduino-ledstringlight/arduino-example.c new file mode 100644 index 000000000..ea74dd8b8 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/arduino-example.c @@ -0,0 +1,2 @@ +#include +AUTOSTART_PROCESSES(&arduino_sketch); diff --git a/examples/osd/arduino-ledstringlight/flash.sh b/examples/osd/arduino-ledstringlight/flash.sh new file mode 100755 index 000000000..e82962073 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/flash.sh @@ -0,0 +1,2 @@ +#!/bin/bash +make TARGET=osd-merkur-128 flash diff --git a/examples/osd/arduino-ledstringlight/led_pwm.h b/examples/osd/arduino-ledstringlight/led_pwm.h new file mode 100644 index 000000000..a51939af5 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/led_pwm.h @@ -0,0 +1,34 @@ +/** + * \defgroup Arduino LED PWM example + * + * Resource definition for Arduino LED PWM module + * + * @{ + */ + +/** + * \file + * Resource definitions for the Arduino LED PWM module + * + * \author + * Ralf Schlatterbeck + */ + +#ifndef led_pwm_h +#define led_pwm_h +#include "contiki.h" +#include "contiki-net.h" +#include "er-coap.h" + +extern uint8_t pwm; +extern uint8_t period_100ms; +extern uint16_t analog2_voltage; +extern uint16_t analog5_voltage; + +extern resource_t res_led_pwm; +extern resource_t res_led_period; +extern resource_t res_analog2_voltage; +extern resource_t res_analog5_voltage; + +#endif // led_pwm_h +/** @} */ diff --git a/examples/osd/arduino-ledstringlight/project-conf.h b/examples/osd/arduino-ledstringlight/project-conf.h new file mode 100644 index 000000000..574e15250 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/project-conf.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2010, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + */ + +#ifndef PROJECT_RPL_WEB_CONF_H_ +#define PROJECT_RPL_WEB_CONF_H_ + +#define PLATFORM_HAS_LEDS 1 +//#define PLATFORM_HAS_BUTTON 1 +#define PLATFORM_HAS_BATTERY 1 + +#define SICSLOWPAN_CONF_FRAG 1 + +/* For Debug: Dont allow MCU sleeping between channel checks */ +#undef RDC_CONF_MCU_SLEEP +#define RDC_CONF_MCU_SLEEP 0 + +/* Disabling RDC for demo purposes. Core updates often require more memory. */ +/* For projects, optimize memory and enable RDC again. */ +// #undef NETSTACK_CONF_RDC +//#define NETSTACK_CONF_RDC nullrdc_driver + +/* Increase rpl-border-router IP-buffer when using more than 64. */ +#undef REST_MAX_CHUNK_SIZE +#define REST_MAX_CHUNK_SIZE 64 + +/* Estimate your header size, especially when using Proxy-Uri. */ +/* +#undef COAP_MAX_HEADER_SIZE +#define COAP_MAX_HEADER_SIZE 70 +*/ + +/* The IP buffer size must fit all other hops, in particular the border router. */ + +#undef UIP_CONF_BUFFER_SIZE +#define UIP_CONF_BUFFER_SIZE 256 + + +/* Multiplies with chunk size, be aware of memory constraints. */ +#undef COAP_MAX_OPEN_TRANSACTIONS +#define COAP_MAX_OPEN_TRANSACTIONS 4 + +/* Must be <= open transaction number, default is COAP_MAX_OPEN_TRANSACTIONS-1. */ +/* +#undef COAP_MAX_OBSERVERS +#define COAP_MAX_OBSERVERS 2 +*/ + +/* Filtering .well-known/core per query can be disabled to save space. */ +/* +#undef COAP_LINK_FORMAT_FILTERING +#define COAP_LINK_FORMAT_FILTERING 0 +*/ + +/* Save some memory for the sky platform. */ +/* +#undef NBR_TABLE_CONF_MAX_NEIGHBORS +#define NBR_TABLE_CONF_MAX_NEIGHBORS 10 +#undef UIP_CONF_MAX_ROUTES +#define UIP_CONF_MAX_ROUTES 10 +*/ + +/* Reduce 802.15.4 frame queue to save RAM. */ +/* +#undef QUEUEBUF_CONF_NUM +#define QUEUEBUF_CONF_NUM 4 +*/ + +/* +#undef SICSLOWPAN_CONF_FRAG +#define SICSLOWPAN_CONF_FRAG 1 +*/ + +#endif /* PROJECT_RPL_WEB_CONF_H_ */ diff --git a/examples/osd/arduino-ledstringlight/resource_led_pwm.c b/examples/osd/arduino-ledstringlight/resource_led_pwm.c new file mode 100644 index 000000000..03205aa09 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/resource_led_pwm.c @@ -0,0 +1,134 @@ +/** + * \file + * Resource for Arduino PWM + * \author + * Ralf Schlatterbeck + * + * \brief get/put pwm and period for LED pin + * + */ + +#include +#include +#include +#include "contiki.h" +#include "jsonparse.h" +#include "er-coap.h" +#include "generic_resource.h" +#include "led_pwm.h" + +int pwm_from_string + (const char *name, const char *uri, const char *query, const char *s) +{ + uint32_t tmp = strtoul (s, NULL, 10); + if (tmp > 255) { + tmp = 255; + } + pwm = tmp; + return 0; +} + +size_t +pwm_to_string + ( const char *name + , const char *uri + , const char *query + , char *buf + , size_t bufsize + ) +{ + return snprintf (buf, bufsize, "%d", pwm); +} + +GENERIC_RESOURCE \ + ( led_pwm + , LED PWM + , duty-cycle + , 0 + , pwm_from_string + , pwm_to_string + ); + +int period_from_string + (const char *name, const char *uri, const char *query, 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; + return 0; +} + +size_t +period_to_string + ( const char *name + , const char *uri + , const char *query + , char *buf + , size_t bufsize + ) +{ + return snprintf (buf, bufsize, "%d", period_100ms * 100); +} + +GENERIC_RESOURCE \ + ( led_period + , LED Period + , ms + , 0 + , period_from_string + , period_to_string + ); + +size_t +analog2_v + ( const char *name + , const char *uri + , const char *query + , char *buf + , size_t bufsize + ) +{ + return snprintf + (buf, bufsize, "%d.%03d", analog2_voltage / 1000, analog2_voltage % 1000); +} + +GENERIC_RESOURCE \ + ( analog2_voltage + , Analog 2 voltage + , V + , 0 + , NULL + , analog2_v + ); + +size_t +analog5_v + ( const char *name + , const char *uri + , const char *query + , char *buf + , size_t bufsize + ) +{ + return snprintf + (buf, bufsize, "%d.%03d", analog5_voltage / 1000, analog5_voltage % 1000); +} + +GENERIC_RESOURCE \ + ( analog5_voltage + , Analog 5 voltage + , V + , 0 + , NULL + , analog5_v + ); + +/* + * VI settings, see coding style + * ex:ts=8:et:sw=2 + */ diff --git a/examples/osd/arduino-ledstringlight/run.sh b/examples/osd/arduino-ledstringlight/run.sh new file mode 100755 index 000000000..5d5cbbbb4 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# For the ages-old bootloader (before 2014) you want to use +# BOOTLOADER_GET_MAC=0x0001f3a0 as parameter to make below. +make clean TARGET=osd-merkur-128 +make TARGET=osd-merkur-128 diff --git a/examples/osd/arduino-ledstringlight/sketch.pde b/examples/osd/arduino-ledstringlight/sketch.pde new file mode 100644 index 000000000..51a6af859 --- /dev/null +++ b/examples/osd/arduino-ledstringlight/sketch.pde @@ -0,0 +1,47 @@ +/* + * Sample arduino sketch using contiki features. + * We turn the LED on and off and allow setting the interval and the + * brightness of the LED via coap. + * Unfortunately sleeping for long times in loop() isn't currently + * possible, something turns off the CPU (including PWM outputs) if a + * Proto-Thread is taking too long. We need to find out how to sleep in + * a Contiki-compatible way. + * Note that for a normal arduino sketch you won't have to include any + * of the contiki-specific files here, the sketch should just work. + */ + +extern "C" { +#include +#include "led_pwm.h" +#define LED_PIN 4 +#define LED_STRING 3 + +uint8_t pwm = 128; +uint8_t period_100ms = 1; /* 1/10 second (period_100ms * 100ms) */ +uint16_t analog2_voltage = 0; +uint16_t analog5_voltage = 0; +} + +void setup (void) +{ + arduino_pwm_timer_init (); + rest_init_engine (); + rest_activate_resource (&res_led_pwm, (char *)"led/pwm"); + rest_activate_resource (&res_led_period, (char *)"led/period"); + rest_activate_resource (&res_analog2_voltage, (char *)"analog/2"); + rest_activate_resource (&res_analog5_voltage, (char *)"analog/5"); +} + +void loop (void) +{ + /* Use 255 - pwm, LED on merkur-board is wired to +3.3V */ + analogWrite (LED_PIN, 255 - pwm); + analogWrite (LED_STRING, pwm); + analog2_voltage = analogRead (A2) * 1600L / 1023L; + analog5_voltage = analogRead (A5) * 1600L / 1023L; + printf ("clock : %lu\nmillis: %lu\n", clock_time (), millis ()); + delay (period_100ms * 100); + analogWrite (LED_PIN, 255); /* OFF: LED on merkur-board is wired to +3.3V */ + analogWrite (LED_STRING, 0); + delay (period_100ms * 100); +}