hardware/Rmodule/main.c
2013-11-18 21:27:01 +07:00

84 lines
1.5 KiB
C
Executable File

// Ïðîøèâêà äëÿ óäàëåííîãî ðàäèîìîäóëÿ
#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "uart/uart.h"
#include "uart/uart_addon.h"
#include "onewire/onewire.h"
#include "res/strings.h"
#define UART_BAUD_RATE 19200
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;
}
void getListDevices(void){
uint8_t i;
uint8_t id[OW_ROMCODE_SIZE];
uint8_t diff, nSensors;
uart_puts_p(ScanStr);
ow_reset();
nSensors = 0;
diff = OW_SEARCH_FIRST;
while ( diff != OW_LAST_DEVICE) {
diff = ow_rom_search( diff, &id[0] );
if ( diff != OW_PRESENCE_ERR && diff != OW_DATA_ERR) {
for ( i=0; i < OW_ROMCODE_SIZE; i++ ){
uart_puthex_byte(id[i]);
uart_putc(' ');
}
uart_puts(CRLF);
}
nSensors++;
}
uart_put_int(nSensors);
}
int main(void){
uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
#ifndef OW_ONE_BUS
ow_set_bus(&PIND,&PORTD,&DDRD,PD6);
#endif
sei();
char cmd[5];
unsigned int len_line=0;
while(1){
uart_puts_p(CmdPrompt);
len_line=get_uart_line(&cmd[0],sizeof(cmd));
if (len_line>0){
if (strcmp_P(cmd, CmdLD)==0){
getListDevices();
}
if (strcmp_P(cmd, CmdHelp)==0){
uart_puts_p(HelpTitle);
uart_puts_p(HelpItem1);
uart_puts_p(HelpItem2);
}
}
}
}