Monday, 28 May 2012

Writing to expansion pins on Telos B

#include "contiki.h"
#include "httpd-simple.h"
#include "net/rpl/rpl.h"
#include "dev/leds.h"
#include <stdio.h>
#include <io.h>

#define P23_OUT() P2DIR |= BV(3)
#define P23_IN() P2DIR &= ~BV(3)
#define P23_SEL() P2SEL &= ~BV(3)
#define P23_IS_1  (P2OUT & BV(3))
#define P23_WAIT_FOR_1() do{}while (!P23_IS_1)
#define P23_IS_0  (P2OUT & ~BV(3))
#define P23_WAIT_FOR_0() do{}while (!P23_IS_0)
#define P23_1() P2OUT |= BV(3)
#define P23_0() P2OUT &= ~BV(3)


static uint8_t toggle;
PROCESS(web_sense_process, "Sense Web Demo");
PROCESS(webserver_nogui_process, "Web server");
PROCESS_THREAD(webserver_nogui_process, ev, data)
{
  PROCESS_BEGIN();
  httpd_init();
  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
    httpd_appcall(data);
  }
  PROCESS_END();
}
AUTOSTART_PROCESSES(&web_sense_process,&webserver_nogui_process);
/*---------------------------------------------------------------------------*/
static const char *TOP = "<html><head><title>Contiki Web Sense</title></head><body>\n";
static const char *BOTTOM = "</body></html>\n";
/*---------------------------------------------------------------------------*/
/* Only one single request at time */
static char buf[256];
static int blen;
#define ADD(...) do {                                                   \
    blen += snprintf(&buf[blen], sizeof(buf) - blen, __VA_ARGS__);      \
  } while(0)
static
PT_THREAD(send_values(struct httpd_state *s))
{
  PSOCK_BEGIN(&s->sout);
 // SEND_STRING(&s->sout, TOP);
  if(strncmp(s->filename, "/index", 6) == 0 ||
     s->filename[1] == '\0') {
  } else if(s->filename[1] == '0') {
    /* Turn off leds */ 
    toggle=0;
    //leds_off(LEDS_ALL);
   //  SEND_STRING(&s->sout, "Turned off leds!");
  } else if(s->filename[1] == '1') {
    /* Turn on leds */ 
    toggle=1;
    //leds_on(LEDS_ALL);
   // SEND_STRING(&s->sout, "Turned on leds!");
  }
 
  //SEND_STRING(&s->sout, BOTTOM);
  PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
httpd_simple_script_t
httpd_simple_get_script(const char *name)
{
  return send_values;
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(web_sense_process, ev, data)
{
  static struct etimer timer;
  PROCESS_BEGIN();
  etimer_set(&timer, CLOCK_SECOND * 2);
  P23_OUT();
  P23_SEL();
 

  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
    etimer_reset(&timer);
    if(toggle==0)
    {  
          P23_1();  
          rpl_repair_dag(rpl_get_dag(RPL_ANY_INSTANCE));     
    }
    else
    {
          P23_0();   
          rpl_repair_dag(rpl_get_dag(RPL_ANY_INSTANCE));
    }
  }
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

No comments:

Post a Comment