56 #define DEBUG 0 //Making this 1 will slightly alter command timings
58 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
62 #define DEBUGSERIAL 0 //Making this 1 will significantly alter command timings
65 #include "contiki-lib.h"
66 #include "contiki-net.h"
69 #include "webserver-nogui.h"
70 #include "httpd-cgi.h"
73 #include "raven-lcd.h"
77 #include <avr/pgmspace.h>
78 #include <avr/eeprom.h>
79 #include <avr/sleep.h>
80 #include <dev/watchdog.h>
82 static uint8_t count = 0;
84 uip_ipaddr_t dest_addr;
86 #define MAX_CMD_LEN 20
88 uint8_t frame[MAX_CMD_LEN];
95 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
96 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
97 #define PING6_DATALEN 16
99 void rs232_send(uint8_t port,
unsigned char c);
107 #define PING_GOOGLE 0
117 }
else if (seqno==2) {
119 }
else if (seqno==3) {
136 count, PING6_DATALEN);
153 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
164 char serial_char_received;
177 void micro_sleep(uint8_t howlong)
179 uint8_t saved_sreg = SREG, saved_howlong = howlong;
180 #if AVR_CONF_USE32KCRYSTAL
182 uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A;
188 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
189 if (howlong==0) howlong=3;
193 TCCR2B =((1<<CS22)|(1<<CS21)|(1<<CS20));
208 while(ASSR & (1 << TCN2UB));
209 TIMSK2 |= (1 << OCIE2A);
214 serial_char_received=0;
222 if (saved_howlong)
break;
224 if (serial_char_received)
break;
229 #if AVR_CONF_USE32KCRYSTAL
233 TCCR2A = savedTCCR2A;
234 TCCR2B = savedTCCR2B;
239 TIMSK2 &= ~(1 << OCIE2A);
244 #if !AVR_CONF_USE32KCRYSTAL
248 ISR(TIMER2_COMPA_vect)
261 raven_gui_loop(process_event_t ev, process_data_t data)
263 uint8_t i,activeconnections,radio_state;
267 printf_P(PSTR(
"Buffer [%d]="),serialcount);
270 printf_P(PSTR(
" %d"),dbuf[i]);
274 if(ev == tcpip_icmp6_event)
switch(*((uint8_t *)data)) {
286 send_frame(REPORT_PING_BEEP, 0, 0);
290 send_frame(REPORT_PING, 1, &seqno);
297 PRINTF(
"\nCommand %d length %d done %d",cmd.cmd,cmd.len,cmd.done);
303 seqno = cmd.frame[0];
309 web_set_temp((
char *)cmd.frame);
315 web_set_voltage((
char *)cmd.frame);
320 if (cmd.frame[0]==0) {
329 if((uip_conns[i].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) activeconnections++;
331 if (activeconnections) {
332 PRINTF(
"\nWaiting for %d connections",activeconnections);
337 radio_state = NETSTACK_RADIO.off();
338 PRINTF (
"\nsleep %d radio state %d...",cmd.frame[0],radio_state);
341 PRINTF(
"\nSleeping...");
342 micro_sleep(cmd.frame[0]);
344 radio_state = NETSTACK_RADIO.on();
345 if (radio_state > 0) {
348 PRINTF(
"Radio wake error %d\n",radio_state);
353 send_frame(REPORT_WAKE, 0, 0);
373 int raven_lcd_serial_input(
unsigned char ch)
377 if (ch) serial_char_received++;
379 if (serialcount<25) dbuf[serialcount]=ch;
408 if (cmd.ndx >= (MAX_CMD_LEN+3)) {
415 if (cmd.ndx >= cmd.len+3){
433 cmd.frame[cmd.ndx - 3] = ch;
444 raven_lcd_show_text(
char *text) {
445 uint8_t textlen=strlen(text)+1;
446 if (textlen > MAX_CMD_LEN) textlen=MAX_CMD_LEN;
447 send_frame(REPORT_TEXT_MSG, textlen, (uint8_t *) text);
452 lcd_show_servername(
void) {
455 extern uint8_t eemem_server_name[16];
457 char buf[
sizeof(eemem_server_name)+1];
458 eeprom_read_block (buf,eemem_server_name,
sizeof(eemem_server_name));
459 buf[
sizeof(eemem_server_name)]=0;
460 raven_lcd_show_text(buf);
464 PROCESS(raven_lcd_process,
"Raven LCD interface process");
471 lcd_show_servername();
475 if(icmp6_new(
NULL) == 0) {
480 raven_gui_loop(ev, data);