//以16进制格式输出整数 void print_hex(unsigned char a) { unsigned char t[17]="0123456789abcdef"; // print_str("0x"); send_usart(t[a/16]); delay_usart(); send_usart(t[a%16]); delay_usart(); }
static int iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) { struct connection_info_struct *con_info = coninfo_cls; //printf("file name =%s \n",filename); //printf("key =%s \n",key); //printf("size =%d \n",size); if (0 == strcmp (key, "open")) { str = "open "; printf("open\n"); send_usart(0xb6d6,1,1,1);//send=================================== if ((size > 0) && (size <= MAXNAMESIZE)) { char *answerstring; answerstring = malloc (MAXANSWERSIZE); if (!answerstring) return MHD_NO; snprintf (answerstring, MAXANSWERSIZE, askpage, key); con_info->answerstring = answerstring; //printf("data =%s \n",data); } else con_info->answerstring = NULL; return MHD_NO; } else if (0 == strcmp (key, "close")) { str = "close"; printf("close\n"); send_usart(0xb6d6,0,0,0);//send=================================== if ((size > 0) && (size <= MAXNAMESIZE)) { char *answerstring; answerstring = malloc (MAXANSWERSIZE); if (!answerstring) return MHD_NO; snprintf (answerstring, MAXANSWERSIZE, askpage, key); con_info->answerstring = answerstring; //printf("data =%s \n",data); } else con_info->answerstring = NULL; return MHD_NO; } return MHD_YES; }
//字符串输出函数 void print_str(const unsigned char *d) { while(*d!='\0') { send_usart(*d); d++; delay_usart(); } }
int main(void) { // Initiera funktionalitet init_spi(); USART_init(); sei(); //Enable global interrupts clearbit(PORTC, PINC0); // "Ready to Receive" till Firefly // Har autonom-toggle på PA0 static uint8_t has_pressed_auto; clearbit(DDRA, 0); // Huvudloop while(1) { static uint8_t spi_state = 0; // 0 väntar, 1 skriver static uint8_t usart_state = 0; // 0 väntar, 1 skriver uint8_t spir, has_spir = 0, usartr, has_usartr = 0; if(!has_pressed_auto) { if(!(PINA & 0x01)) { has_pressed_auto = 1; send_spi(COMM_ENABLE_PID); } } // Processa SPI if(spi_state == 0) { // Kolla om vi har fått nåt if(SPSR & (1 << SPIF)) { spir = SPDR; has_spir = 1; } // Finns det nåt att skicka? else if(spiw_r != spiw_w) { SPDR = spiw_data[spiw_r++]; PORTA ^= (1 << PORTA7); spi_state = 1; } } else if(spi_state == 1) { if(SPSR & (1 << SPIF)) { uint8_t c; spi_state = 0; c = SPDR; if(c != 0) { has_spir = 1; spir = c; } } } // Processa USART if(usart_state == 0) { // Firefly vill sända data if(UCSRA & (1 << RXC)) { usartr = UDR; has_usartr = 1; } // Vi vill sända data else if(usartw_r != usartw_w) { usart_state = 1; setbit(PORTC, PINC0); } } else if(usart_state == 1) { if(UCSRA & (1 << UDRE)) { UDR = usartw_data[usartw_r++]; usart_state = 0; clearbit(PORTC, PINC0); } } // Nu finns data i usartr om has_usartr == 1 // och i spir om has_spir == 1 if(has_usartr) { decode_remote(usartr); } if(has_spir) { send_usart(spir); } } }
// Skicka sträng till fjärrenheten void send_usart_string(uint8_t *str) { while(*str) send_usart(*str++); }