hardware/Rmodule/main.c
2013-12-11 13:34:53 +07:00

78 lines
1.5 KiB
C
Executable File

// Ïðîøèâêà äëÿ óäàëåííîãî ðàäèîìîäóëÿ
#include <avr/io.h>
#include <stdio.h>
#include <string.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include "nrf24l01/nrf24l01.h"
#include "onewire/onewire.h"
typedef struct t_data{
uint8_t from;
uint8_t to;
char cmd[5];
uint8_t value[25];
}t_data;
uint8_t* address_at_eeprom_location = (uint8_t*)0;
uint8_t getid(void){
uint8_t result = 0;
if ( eeprom_read_byte(address_at_eeprom_location) == 0xdf ){
result = eeprom_read_byte(address_at_eeprom_location+1);
}else{
result =0x00;
}
return result;
}
void getListDevices(t_data data){
uint8_t i;
uint8_t id[OW_ROMCODE_SIZE];
uint8_t diff;
ow_reset();
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++ ){
data.value[i] = id[i];
}
data.value[OW_ROMCODE_SIZE] = 0x00;
}
if (nrf24l01_write((uint8_t*) &data)){
}
}
}
uint8_t mac[NRF24L01_ADDRSIZE] = {0xF0, 0xF0, 0xF0, 0xF0, 0xF0};
int main(void){
uint8_t id = getid();
nrf24l01_init();
sei();
nrf24l01_setrxaddr(0, mac);
mac[4] = 0xF0 & ( 0x0F & id);
nrf24l01_settxaddr(mac);
t_data data;
while(1){
if(nrf24l01_readready(&id)){
nrf24l01_read((uint8_t*) &data);
if (data.to==id && data.from==0x00){ // åñëè ýòî íàì è îò ñåðâåðà
if (strcmp(data.cmd, "ld")==0){ // êîìàíäà îïðîñà øèíû
data.from = id;
data.to = 0x00;
getListDevices(data);
}
}
}
}
}