add mcu_sleep routines

This commit is contained in:
harald42 2016-04-12 10:34:40 +02:00
parent 743245e230
commit 9a3c6adf8d
9 changed files with 71 additions and 37 deletions

View file

@ -54,6 +54,34 @@
#include "adc.h" #include "adc.h"
#include "hw-arduino.h" #include "hw-arduino.h"
extern volatile uint8_t mcusleepcycle;
volatile uint8_t mcusleepcycleval;
/*-------------- enabled sleep mode ----------------------------------------*/
void
mcu_sleep_init(void)
{
mcusleepcycleval=mcusleepcycle;
}
void
mcu_sleep_on(void)
{
mcusleepcycle= mcusleepcycleval;
}
/*--------------- disable sleep mode ---------------------------------------*/
void
mcu_sleep_off(void)
{
mcusleepcycle=0;
}
/*---------------- set duty cycle value ------------------------------------*/
void
mcu_sleep_set(uint8_t value)
{
mcusleepcycleval= value;
mcusleepcycle = mcusleepcycleval;
}
PROCESS(arduino_sketch, "Arduino Sketch Wrapper"); PROCESS(arduino_sketch, "Arduino Sketch Wrapper");
#ifndef LOOP_INTERVAL #ifndef LOOP_INTERVAL
@ -65,8 +93,8 @@ PROCESS_THREAD(arduino_sketch, ev, data)
static struct etimer loop_periodic_timer; static struct etimer loop_periodic_timer;
PROCESS_BEGIN(); PROCESS_BEGIN();
adc_init (); adc_init ();
mcu_sleep_init ();
setup (); setup ();
/* Define application-specific events here. */ /* Define application-specific events here. */
etimer_set(&loop_periodic_timer, LOOP_INTERVAL); etimer_set(&loop_periodic_timer, LOOP_INTERVAL);

View file

@ -51,6 +51,13 @@
#include "contiki.h" #include "contiki.h"
/*--------------- enable sleep mode ---------------------------------------*/
void mcu_sleep_on(void);
/*--------------- disable sleep mode ---------------------------------------*/
void mcu_sleep_off(void);
/*---------------- set sleep value ------------------------------------*/
void mcu_sleep_set(uint8_t value);
extern void loop (void); extern void loop (void);
extern void setup (void); extern void setup (void);
extern void arduino_init (void); extern void arduino_init (void);

View file

@ -17,12 +17,10 @@
extern "C" { extern "C" {
#include "arduino-process.h"
#include "rest-engine.h" #include "rest-engine.h"
#include "sketch.h" #include "sketch.h"
extern volatile uint8_t mcusleepcycle; // default 16
// Data wire is plugged into port 2 on the Arduino // Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3 #define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 9 #define TEMPERATURE_PRECISION 9
@ -130,7 +128,7 @@ void setup (void)
// LOOP_INTERVAL (20 * CLOCK_SECOND) // LOOP_INTERVAL (20 * CLOCK_SECOND)
void loop (void) void loop (void)
{ {
mcusleepcycle=0; // dont sleep mcu_sleep_off();
// call sensors.requestTemperatures() to issue a global temperature // call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus // request to all devices on the bus
// printf("Requesting temperatures..."); // printf("Requesting temperatures...");
@ -156,5 +154,5 @@ void loop (void)
printf("Temp: %s",htu21d_temp_s); printf("Temp: %s",htu21d_temp_s);
printf("\t\tHum: %s\n",htu21d_hum_s); printf("\t\tHum: %s\n",htu21d_hum_s);
mcusleepcycle=32; // sleep, wakeup every 32 cycles mcu_sleep_on();
} }

View file

@ -15,7 +15,7 @@
extern "C" { extern "C" {
#include "arduino-process.h"
#include "rest-engine.h" #include "rest-engine.h"
#include "sketch.h" #include "sketch.h"
@ -161,7 +161,7 @@ void setup (void)
// LOOP_INTERVAL (10 * CLOCK_SECOND) // LOOP_INTERVAL (10 * CLOCK_SECOND)
void loop (void) void loop (void)
{ {
mcusleepcycle=0; // dont sleep mcu_sleep_off();
// call sensors.requestTemperatures() to issue a global temperature // call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus // request to all devices on the bus
printf("Requesting temperatures..."); printf("Requesting temperatures...");
@ -171,7 +171,7 @@ void loop (void)
// print the device information // print the device information
printData(insideThermometer,0); printData(insideThermometer,0);
printData(outsideThermometer,1); printData(outsideThermometer,1);
mcusleepcycle=32; // sleep, wakeup every 32 cycles mcu_sleep_on();
// debug only // debug only
} }

View file

@ -11,11 +11,11 @@
*/ */
extern "C" { extern "C" {
#include "arduino-process.h"
#include "sketch.h" #include "sketch.h"
#include "rest-engine.h" #include "rest-engine.h"
#include "net/netstack.h" #include "net/netstack.h"
extern volatile uint8_t mcusleepcycle; // default 16
extern resource_t res_battery, res_distance; extern resource_t res_battery, res_distance;
#define LED_PIN 4 /* LED Pin */ #define LED_PIN 4 /* LED Pin */
@ -39,8 +39,8 @@ void setup (void)
void loop (void) void loop (void)
{ {
mcusleepcycle=0; // dont sleep mcu_sleep_off();
mcusleepcycle=16; // sleep, wakeup every 32 cycles mcu_sleep_on();
} }

View file

@ -11,6 +11,7 @@
*/ */
extern "C" { extern "C" {
#include "arduino-process.h"
#include "rest-engine.h" #include "rest-engine.h"
extern resource_t res_door, res_battery; extern resource_t res_door, res_battery;
@ -29,9 +30,12 @@ void setup (void)
rest_init_engine (); rest_init_engine ();
rest_activate_resource (&res_door, "s/door"); rest_activate_resource (&res_door, "s/door");
rest_activate_resource (&res_battery, "s/battery"); rest_activate_resource (&res_battery, "s/battery");
// NETSTACK_MAC.off(1);
} }
void loop (void) void loop (void)
{ {
mcu_sleep_off();
mcu_sleep_on();
} }

View file

@ -14,12 +14,9 @@
#include "Adafruit_HTU21DF.h" #include "Adafruit_HTU21DF.h"
extern "C" { extern "C" {
#include "arduino-process.h"
#include "rest-engine.h" #include "rest-engine.h"
extern volatile uint8_t mcusleepcycle; // default 16
Adafruit_HTU21DF htu = Adafruit_HTU21DF(); Adafruit_HTU21DF htu = Adafruit_HTU21DF();
extern resource_t res_htu21dtemp, res_htu21dhum, res_battery; extern resource_t res_htu21dtemp, res_htu21dhum, res_battery;
@ -51,19 +48,19 @@ void setup (void)
// LOOP_INTERVAL (10 * CLOCK_SECOND) // LOOP_INTERVAL (10 * CLOCK_SECOND)
void loop (void) void loop (void)
{ {
mcusleepcycle=0; // dont sleep mcu_sleep_off();
htu21d_temp = htu.readTemperature(); htu21d_temp = htu.readTemperature();
htu21d_hum = htu.readHumidity(); htu21d_hum = htu.readHumidity();
mcusleepcycle=32; // sleep, wakeup every 32 cycles mcu_sleep_on();
dtostrf(htu21d_temp , 6, 2, htu21d_temp_s ); dtostrf(htu21d_temp , 6, 2, htu21d_temp_s );
dtostrf(htu21d_hum , 6, 2, htu21d_hum_s ); tostrf(htu21d_hum , 6, 2, htu21d_hum_s );
// remove space // remove space
if(htu21d_temp_s[0]==' '){ if(htu21d_temp_s[0]==' '){
memcpy (htu21d_temp_s,htu21d_temp_s+1,strlen(htu21d_temp_s)+1); memcpy (htu21d_temp_s,htu21d_temp_s+1,strlen(htu21d_temp_s)+1);
} }
if(htu21d_hum_s[0]==' '){ if(htu21d_hum_s[0]==' '){
memcpy (htu21d_hum_s,htu21d_hum_s+1,strlen(htu21d_hum_s)+1); memcpy (htu21d_hum_s,htu21d_hum_s+1,strlen(htu21d_hum_s)+1);
} }
// debug only // debug only
// printf("Temp: %s",htu21d_temp_s); // printf("Temp: %s",htu21d_temp_s);

View file

@ -11,11 +11,10 @@
*/ */
extern "C" { extern "C" {
#include "arduino-process.h"
#include "rest-engine.h" #include "rest-engine.h"
#include "net/netstack.h" #include "net/netstack.h"
extern volatile uint8_t mcusleepcycle; // default 16
extern resource_t res_leds, res_battery, res_cputemp; extern resource_t res_leds, res_battery, res_cputemp;
uint8_t door_pin = 3; uint8_t door_pin = 3;
uint8_t door_status = 0; uint8_t door_status = 0;
@ -39,7 +38,7 @@ void setup (void)
void loop (void) void loop (void)
{ {
mcusleepcycle=0; mcu_sleep_off();
mcusleepcycle=16; mcu_sleep_on();
} }

View file

@ -11,6 +11,7 @@
*/ */
extern "C" { extern "C" {
#include "arduino-process.h"
#include "rest-engine.h" #include "rest-engine.h"
extern volatile uint8_t mcusleepcycle; // default 16 extern volatile uint8_t mcusleepcycle; // default 16
@ -37,12 +38,12 @@ void setup (void)
void loop (void) void loop (void)
{ {
mcusleepcycle=0; // dont sleep mcu_sleep_off();
moisture_voltage = analogRead(moisture_pin); moisture_voltage = analogRead(moisture_pin);
if(moisture_voltage < 800){ if(moisture_voltage < 800){
digitalWrite(BUZZER_PIN, LOW); digitalWrite(BUZZER_PIN, LOW);
}else{ }else{
digitalWrite(BUZZER_PIN, HIGH); digitalWrite(BUZZER_PIN, HIGH);
} }
mcusleepcycle=32; // sleep, wakeup every 32 cycles mcu_sleep_on();
} }