hardware/Smodule/main.c

124 lines
2.6 KiB
C
Raw Normal View History

2013-12-09 21:14:43 +07:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "uart/uart.h"
#include "uart/uart_addon.h"
#include "res/strings.h"
#include "nrf24l01/nrf24.h"
#define UART_BAUD_RATE 9600
unsigned char get_uart_char(void){
unsigned int res;
do{
res = uart_getc();
} while(res & UART_NO_DATA);
return (unsigned char) res;
}
static int get_uart_line(char *pbuf, int len_ln){
int i = 0;
unsigned char c;
len_ln=len_ln-1;
while((c=get_uart_char()) != 0x0D && i<len_ln){
uart_putc(c);
pbuf[i] = c;
i=i+1;
}
pbuf[i] = '\0';
return i;
}
typedef struct data_arr{
uint8_t buffer[32];
}data_array;
uint8_t rx_mac[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
uint8_t tx_mac[5] = {0xD7,0xD7,0xD7,0xD7,0xD7};
uint8_t temp;
int main(void){
uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
/* initializes hardware pins */
nrf24_init();
/* RF channel: #2 , payload length: 4 */
nrf24_config(2,32);
/* Set the module's own address */
nrf24_rx_address(rx_mac);
/* Set the transmit address */
nrf24_tx_address(tx_mac);
sei();
data_array buff;
char cmd[5];
uint8_t i;
unsigned int len_line=0;
uart_puts_p(CmdPrompt);
while(1){
if (!(UCSRA & (1 << RXC))){
len_line=get_uart_line(&cmd[0],sizeof(cmd));
if (len_line>0){
if (strcmp(cmd, CmdLD)==0){
/* Automatically goes to TX mode */
nrf24_send((uint8_t*) &cmd);
/* Wait for transmission to end */
while(nrf24_isSending());
/* Make analysis on last tranmission attempt */
temp = nrf24_lastMessageStatus();
if(temp == NRF24_TRANSMISSON_OK){
uart_puts_p(TransOK);
}else if(temp == NRF24_MESSAGE_LOST){
uart_puts_p(TransLost);
}
/* Retranmission count indicates the tranmission quality */
temp = nrf24_retransmissionCount();
uart_puts_p(ReTransCnt);
uart_put_int(temp);
uart_puts_p(CmdPrompt);
/* Optionally, go back to RX mode ... */
nrf24_powerUpRx();
/* Or you might want to power down after TX */
// nrf24_powerDown();
/* Wait a little ... */
_delay_ms(10);
}
if (strcmp(cmd, CmdHelp)==0){
uart_puts_p(HelpTitle);
uart_puts_p(HelpItem1);
uart_puts_p(HelpItem2);
uart_puts_p(CmdPrompt);
}
}
}
if(nrf24_dataReady()){
nrf24_getData((uint8_t *) &buff);
for ( i=0; i < sizeof(buff.buffer); i++ ){
uart_puthex_byte(buff.buffer[i]);
uart_putc(' ');
}
uart_puts(CRLF);
}
}
}