osd-contiki/examples/osd/arduino-sketch/sketch.pde
Ralf Schlatterbeck c46d6afa39 Make Arduino timer stuff work on Contiki
New discovery: Contiki also uses timer 0. With almost the same interface
as Arduino. So we now completely get rid of wiring.c (only the main
file, the other wiring_xxx stay) and implement Arduino timer, delay, etc
in terms of the corresponding Contiki routines. Verified that now delay
works as expected. The LED in examples/osd/arduino-sketch blinks!

Before this, the arduino_init routine in wiring.c destroyed the timer-0
initialization of contiki, making both, contiki timer implementation
*and* contiki timer implementation fail if the arduino_init routine was
called. Now both work.

Squashed with following bug-fix commit.
2014-11-19 13:50:58 +01:00

38 lines
1.1 KiB
Plaintext

/*
* 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 <stdio.h>
#include "led_pwm.h"
#define LED_PIN 5
uint8_t pwm = 128;
uint8_t period_100ms = 10; /* one second */
}
void setup (void)
{
rest_init_engine ();
rest_activate_resource (&resource_led_pwm);
rest_activate_resource (&resource_led_period);
}
void loop (void)
{
/* Use 255 - pwm, LED on merkur-board is wired to +3.3V */
analogWrite (LED_PIN, 255 - pwm);
printf ("clock : %u\nmillis: %lu\n", clock_time (), millis ());
delay (period_100ms * 100);
analogWrite (LED_PIN, 255); /* OFF: LED on merkur-board is wired to +3.3V */
delay (period_100ms * 100);
}