#include "project.h" #define BUFFER_SIZE 32 #define TIMEOUT 500 #define SLEEPY 550 #define BACKOFF 200 static char modem_buf[BUFFER_SIZE]; static unsigned modem_ptr=0; static char *modem_cmd; static int timeout; static int sleepy=SLEEPY; void modem_send(char *buf) { modem_cmd=buf; if (sleepy>=SLEEPY) { printf("modem is sleepy\r\n"); usart2_queue('\r'); sleepy=0; timeout=BACKOFF; return; } sleepy=0; printf("(re)send\r\n"); timeout=TIMEOUT; usart2_queue('\r'); usart2_tx(modem_cmd,strlen(modem_cmd)); usart2_queue('\r'); } void modem_line() { printf("Modem said: %s\r\n",modem_buf); sleepy=0; if (!strncmp(modem_buf,"RING",4)) ring(4000); if (!strncmp(modem_buf,"OK",2)) { modem_cmd=NULL; timeout=0; } } void modem_byte(uint8_t b) { if (b=='\n') return; if (b=='\r') { if (modem_ptr) modem_line(); modem_ptr=0; modem_buf[modem_ptr]=0; return; } if (modem_ptr>=(sizeof(modem_buf)-1)) return; modem_buf[modem_ptr]=b; modem_ptr++; modem_buf[modem_ptr]=0; } void answer_call(void) { printf("Answering call\r\n"); modem_send("ATA"); } void terminate_call(void) { printf("Terminating any call\r\n"); modem_send("ATH0"); } void modem_tick(void) { if (sleepy