#include "Avalanche_Beacon.h"


void start_meas( void ){
  if( W1_IN & 1<< W1_PIN ){
    w1_command( CONVERT_T, NULL );
    W1_OUT |= 1<< W1_PIN;
    W1_DDR |= 1<< W1_PIN;			// parasite power on

  }else{
  //  uputsnl( "Short Circuit !" );
  }
}


uint read_meas( void )
{
  uchar id[8], diff;
  uchar s[30];
  uchar i;
  uint temp=0;

  for( diff = SEARCH_FIRST; diff != LAST_DEVICE; ){
    diff = w1_rom_search( diff, id );

    if( diff == PRESENCE_ERR ){
   //   uputsnl( "No sensor found" );
   	LcdGotoXYFont(1,6);
   	LcdFStr(FONT_1X,(unsigned char*)PSTR("No Sns. found"));
   	LcdUpdate();
      break;
    }
    if( diff == DATA_ERR ){
     // uputsnl( "Bus Error" );
	 LcdGotoXYFont(2,6);
	 LcdFStr(FONT_1X,(unsigned char*)PSTR("Bus Error"));
	 LcdUpdate();
      break;
    }
    if( id[0] == 0x28 || id[0] == 0x10 ){	// temperature sensor
  //    uputs( "ID: " );
      for( i = 0; i < 8; i++ ){
	//sprintf( s, "%02X ", id[i] );
	//uputs( s );
      }
      w1_byte_wr( READ );			// read command
      temp = w1_byte_rd();			// low byte
      temp |= (uint)w1_byte_rd() << 8;		// high byte
      if( id[0] == 0x10 )			// 9 -> 12 bit
        temp <<= 3;
     // sprintf( s, "  T: %04X = ", temp );	// hex value
      //uputs( s );
      //sprintf( s, "%4d.%01døC", temp >> 4, (temp << 12) / 6553 ); // 0.1øC
      //uputsnl( s );
	  
    }
  }
  //uputsnl( "" );
  return temp;
}
