hardware/Smodule/main.c

105 lines
2.0 KiB
C
Executable File

// Ïðîøèâêà äëÿ ñåðâåðíîãî ðàäèîìîäóëÿ
#include <avr/io.h>
#include <stdio.h>
#include <string.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "uart/uart.h"
#include "uart/uart_addon.h"
#include "res/strings.h"
#include "nrf24l01/nrf24l01.h"
#define UART_BAUD_RATE 9600
typedef struct t_data{
uint8_t from;
uint8_t to;
char cmd[5];
uint8_t value[25];
}t_data;
unsigned char get_uart_char(void){
unsigned int res;
do{
res = uart_getc();
} while(res & UART_NO_DATA);
return (unsigned char) res;
}
static uint8_t get_uart_line(char *pbuf, uint8_t len_ln){
uint8_t 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;
}
uint8_t mac[NRF24L01_ADDRSIZE] = {0xF0, 0xF0, 0xF0, 0xF0, 0xF0};
int main(void){
t_data data;
uint8_t i;
uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
nrf24l01_init();
sei();
nrf24l01_settxaddr(mac);
for (i=1; i<6; i++){
mac[4] = 0xF0 & ( 0x0F & i);
nrf24l01_setrxaddr(i, mac);
}
uart_puts_p(CmdPrompt);
while(1){
if (!(UCSRA & (1 << RXC))){
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);
if(i == 1){
uart_puts_p(TransOK);
}else{
uart_puts_p(TransLost);
}
uart_puts_p(CmdPrompt);
}
if (strcmp(data.cmd, CmdHelp)==0){
uart_puts_p(HelpTitle);
uart_puts_p(HelpItem1);
uart_puts_p(HelpItem2);
uart_puts_p(CmdPrompt);
}
}
}
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]);
uart_putc(' ');
}
uart_puts(CRLF);
}
}
}