hardware/Smodule/main.c

105 lines
2.0 KiB
C
Raw Normal View History

2013-12-11 13:34:53 +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>
2013-12-09 21:14:43 +07:00
#include <avr/io.h>
#include <stdio.h>
2013-12-11 13:34:53 +07:00
#include <string.h>
2013-12-09 21:14:43 +07:00
#include <avr/interrupt.h>
#include <util/delay.h>
#include "uart/uart.h"
#include "uart/uart_addon.h"
#include "res/strings.h"
2013-12-11 13:34:53 +07:00
#include "nrf24l01/nrf24l01.h"
2013-12-09 21:14:43 +07:00
#define UART_BAUD_RATE 9600
2013-12-11 13:34:53 +07:00
typedef struct t_data{
uint8_t from;
uint8_t to;
char cmd[5];
uint8_t value[25];
}t_data;
2013-12-09 21:14:43 +07:00
unsigned char get_uart_char(void){
unsigned int res;
do{
res = uart_getc();
} while(res & UART_NO_DATA);
return (unsigned char) res;
}
2013-12-11 13:34:53 +07:00
static uint8_t get_uart_line(char *pbuf, uint8_t len_ln){
uint8_t i = 0;
2013-12-09 21:14:43 +07:00
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;
}
2013-12-11 13:34:53 +07:00
uint8_t mac[NRF24L01_ADDRSIZE] = {0xF0, 0xF0, 0xF0, 0xF0, 0xF0};
2013-12-09 21:14:43 +07:00
int main(void){
2013-12-11 13:34:53 +07:00
t_data data;
uint8_t i;
2013-12-09 21:14:43 +07:00
uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
2013-12-11 13:34:53 +07:00
nrf24l01_init();
2013-12-09 21:14:43 +07:00
sei();
2013-12-11 13:34:53 +07:00
nrf24l01_settxaddr(mac);
for (i=1; i<6; i++){
mac[4] = 0xF0 & ( 0x0F & i);
nrf24l01_setrxaddr(i, mac);
}
2013-12-09 21:14:43 +07:00
uart_puts_p(CmdPrompt);
while(1){
if (!(UCSRA & (1 << RXC))){
2013-12-11 13:34:53 +07:00
i=get_uart_line(&data.cmd[0],sizeof(data.cmd));
if (i>0){
data.from = 0;
data.to = 0xFF;
if (strcmp(data.cmd, CmdLD)==0){
i = nrf24l01_write((uint8_t*) &data);
2013-12-09 21:14:43 +07:00
2013-12-11 13:34:53 +07:00
if(i == 1){
2013-12-09 21:14:43 +07:00
uart_puts_p(TransOK);
2013-12-11 13:34:53 +07:00
}else{
2013-12-09 21:14:43 +07:00
uart_puts_p(TransLost);
}
uart_puts_p(CmdPrompt);
}
2013-12-11 13:34:53 +07:00
if (strcmp(data.cmd, CmdHelp)==0){
2013-12-09 21:14:43 +07:00
uart_puts_p(HelpTitle);
uart_puts_p(HelpItem1);
uart_puts_p(HelpItem2);
uart_puts_p(CmdPrompt);
}
}
}
2013-12-11 13:34:53 +07:00
if(nrf24l01_readready(0)){
nrf24l01_read((uint8_t*) &data);
uart_puts(data.cmd);
uart_putc('-');
uart_puthex_byte(data.from);
uart_putc('>');
uart_puthex_byte(data.to);
uart_putc(':');
for ( i=0; i < sizeof(data.value); i++ ){
uart_puthex_byte(data.value[i]);
2013-12-09 21:14:43 +07:00
uart_putc(' ');
}
uart_puts(CRLF);
}
}
}