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 u8_t count = 0;
86 #define MAX_CMD_LEN 20
88 u8_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;
218 extern void clock_adjust_ticks(uint16_t howmany);
223 if (saved_howlong)
break;
225 if (serial_char_received)
break;
230 #if AVR_CONF_USE32KCRYSTAL
234 TCCR2A = savedTCCR2A;
235 TCCR2B = savedTCCR2B;
240 TIMSK2 &= ~(1 << OCIE2A);
245 #if !AVR_CONF_USE32KCRYSTAL
249 ISR(TIMER2_COMPA_vect)
262 raven_gui_loop(process_event_t ev, process_data_t data)
264 uint8_t i,activeconnections,radio_state;
268 printf_P(PSTR(
"Buffer [%d]="),serialcount);
271 printf_P(PSTR(
" %d"),dbuf[i]);
275 if(ev == tcpip_icmp6_event)
switch(*((uint8_t *)data)) {
287 send_frame(REPORT_PING_BEEP, 0, 0);
291 send_frame(REPORT_PING, 1, &seqno);
298 PRINTF(
"\nCommand %d length %d done %d",cmd.cmd,cmd.len,cmd.done);
304 seqno = cmd.frame[0];
310 web_set_temp((
char *)cmd.frame);
316 web_set_voltage((
char *)cmd.frame);
321 if (cmd.frame[0]==0) {
330 if((uip_conns[i].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) activeconnections++;
332 if (activeconnections) {
333 PRINTF(
"\nWaiting for %d connections",activeconnections);
338 radio_state = NETSTACK_RADIO.off();
339 PRINTF (
"\nsleep %d radio state %d...",cmd.frame[0],radio_state);
342 PRINTF(
"\nSleeping...");
343 micro_sleep(cmd.frame[0]);
345 radio_state = NETSTACK_RADIO.on();
346 if (radio_state > 0) {
349 PRINTF(
"Radio wake error %d\n",radio_state);
354 send_frame(REPORT_WAKE, 0, 0);
374 int raven_lcd_serial_input(
unsigned char ch)
378 if (ch) serial_char_received++;
380 if (serialcount<25) dbuf[serialcount]=ch;
409 if (cmd.ndx >= (MAX_CMD_LEN+3)) {
416 if (cmd.ndx >= cmd.len+3){
434 cmd.frame[cmd.ndx - 3] = ch;
445 raven_lcd_show_text(
char *text) {
446 uint8_t textlen=strlen(text)+1;
447 if (textlen > MAX_CMD_LEN) textlen=MAX_CMD_LEN;
448 send_frame(REPORT_TEXT_MSG, textlen, (uint8_t *) text);
453 lcd_show_servername(
void) {
456 extern uint8_t eemem_server_name[16];
458 char buf[
sizeof(eemem_server_name)+1];
459 eeprom_read_block (buf,eemem_server_name,
sizeof(eemem_server_name));
460 buf[
sizeof(eemem_server_name)]=0;
461 raven_lcd_show_text(buf);
465 PROCESS(raven_lcd_process,
"Raven LCD interface process");
472 lcd_show_servername();
476 if(icmp6_new(
NULL) == 0) {
481 raven_gui_loop(ev, data);