From d6618ade1a6a0e70d41beb6dfb44a8c894dcd808 Mon Sep 17 00:00:00 2001 From: Ralf Schlatterbeck Date: Fri, 1 Dec 2017 15:51:15 +0100 Subject: [PATCH] Add soft-blink LED example --- examples/osd/arduino-led/Makefile | 58 ++++++++++++ examples/osd/arduino-led/README.md | 7 ++ examples/osd/arduino-led/arduino-example.c | 2 + examples/osd/arduino-led/gen.py | 19 ++++ examples/osd/arduino-led/project-conf.h | 103 +++++++++++++++++++++ examples/osd/arduino-led/sketch.pde | 47 ++++++++++ 6 files changed, 236 insertions(+) create mode 100644 examples/osd/arduino-led/Makefile create mode 100644 examples/osd/arduino-led/README.md create mode 100644 examples/osd/arduino-led/arduino-example.c create mode 100644 examples/osd/arduino-led/gen.py create mode 100644 examples/osd/arduino-led/project-conf.h create mode 100644 examples/osd/arduino-led/sketch.pde diff --git a/examples/osd/arduino-led/Makefile b/examples/osd/arduino-led/Makefile new file mode 100644 index 000000000..f44210fca --- /dev/null +++ b/examples/osd/arduino-led/Makefile @@ -0,0 +1,58 @@ +# Set this to the name of your sketch (without extension .pde) +SKETCH=sketch +EXE=arduino-example + +all: $(EXE) + +sketch.cpp: led_setting.h + +CONTIKI=../../.. + +# Contiki IPv6 configuration +CONTIKI_WITH_IPV6 = 1 + +CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\" + +PROJECT_SOURCEFILES += ${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 + +led_setting.h: gen.py + python gen.py > led_setting.h + +$(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 + +CLEAN = led_setting.h sketch.cpp diff --git a/examples/osd/arduino-led/README.md b/examples/osd/arduino-led/README.md new file mode 100644 index 000000000..b6b12116a --- /dev/null +++ b/examples/osd/arduino-led/README.md @@ -0,0 +1,7 @@ +Arduino LED example +=================== + +This example does a soft-blink of the built-in LED. + +For Arduino compatibility info see the documentation file in +apps/contiki-compat/README.md diff --git a/examples/osd/arduino-led/arduino-example.c b/examples/osd/arduino-led/arduino-example.c new file mode 100644 index 000000000..ea74dd8b8 --- /dev/null +++ b/examples/osd/arduino-led/arduino-example.c @@ -0,0 +1,2 @@ +#include +AUTOSTART_PROCESSES(&arduino_sketch); diff --git a/examples/osd/arduino-led/gen.py b/examples/osd/arduino-led/gen.py new file mode 100644 index 000000000..609dbfb2e --- /dev/null +++ b/examples/osd/arduino-led/gen.py @@ -0,0 +1,19 @@ +#!/usr/bin/python + +from __future__ import division +from math import log + +mx = 256 // 8 + +print "unsigned const char led_setting [] =" + +for k in range (mx) : + if k : + print ", ", + else : + print "{ ", + print "(unsigned char)", int (round (log (k + 1) / log (mx) * 255 + .5)) - 1 + +print "};" + + diff --git a/examples/osd/arduino-led/project-conf.h b/examples/osd/arduino-led/project-conf.h new file mode 100644 index 000000000..a7584b2e7 --- /dev/null +++ b/examples/osd/arduino-led/project-conf.h @@ -0,0 +1,103 @@ +/* + * 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 +*/ + +#define LOOP_INTERVAL 2 + +#endif /* PROJECT_RPL_WEB_CONF_H_ */ diff --git a/examples/osd/arduino-led/sketch.pde b/examples/osd/arduino-led/sketch.pde new file mode 100644 index 000000000..e3281faf9 --- /dev/null +++ b/examples/osd/arduino-led/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 +#include "led_setting.h" +#define TIMER 3 +#define PIN_PWM_C 4 + +static uint16_t pwm_max = 0; +} + +void setup (void) +{ + hwtimer_ini (3, HWT_WGM_PWM_PHASE_8_BIT, HWT_CLOCK_PRESCALER_8, 0); + //hwtimer_pwm_ini (TIMER, 1000, HWT_PWM_FAST, 0); + hwtimer_pwm_enable (TIMER, HWT_CHANNEL_C); + hwtimer_set_pwm (TIMER, HWT_CHANNEL_C, 0); + pinMode (PIN_PWM_C, OUTPUT); + pwm_max = hwtimer_pwm_max_ticks (TIMER); + printf ("pwm_max: %u\n", pwm_max); +} + +void loop (void) +{ + static int direction = 1; + static unsigned const char *led_ptr = led_setting; + hwtimer_set_pwm (TIMER, HWT_CHANNEL_C, *led_ptr); + //printf ("p: %u\n", *led_ptr); + if (led_ptr >= led_setting + sizeof (led_setting) - 1 && direction > 0) { + direction = -1; + } + if (led_ptr <= led_setting && direction < 0) { + direction = 1; + } + led_ptr += direction; +}