#include <TinyDebug.h>
#include <TinyWireM.h>
#include <OneWire.h>


#define ONE_WIRE_BUS 3

OneWire oneWire(ONE_WIRE_BUS);

uint8_t addr_sensor_x[8] = { 0x28, 0xB8, 0x3D, 0x95, 0xF0, 0x01, 0x3C, 0xE4 };
uint8_t addr_sensor_z[8] = { 0x28, 0xB4, 0x76, 0x95, 0xF0, 0x01, 0x3C, 0x00 };
uint8_t addr_sensor_y1[8] = { 0x28, 0x8C, 0xB6, 0x95, 0xF0, 0x01, 0x3C, 0x6E };
uint8_t addr_sensor_y2[8] = { 0x28, 0x07, 0xD0, 0x95, 0xF0, 0x01, 0x3C, 0xF1 };

void setup() {
  pinMode( PB0, OUTPUT);      // for a start pulse and stop pulse
  pinMode( PB1, OUTPUT);      // for a pin toggle serial output
  digitalWrite( PB1, HIGH);   // A serial signal is idle high

  delay( 100);

  Debug.begin();

  digitalWrite( PB0, HIGH);   
  delayMicroseconds(10);
  digitalWrite( PB0, LOW);

  //oneWire.reset();  Search already resets

  // digitalWrite( PB0, HIGH);   
  // delayMicroseconds(10);
  // digitalWrite( PB0, LOW);

  doSearch1Wire();

  //Debug.println(F("1Wire::done"));

  // digitalWrite( PB0, HIGH);   
  // delayMicroseconds(10);
  // digitalWrite( PB0, LOW);

  // oneWire.reset();

  digitalWrite( PB0, HIGH);   
  delayMicroseconds(10);
  digitalWrite( PB0, LOW);

  // oneWire.select(addr_sensor_x);

  //oneWire.reset();
  //oneWire.skip();
  //oneWire.select(addr_sensor_x);
  //oneWire.write(0x44, 1);

  // digitalWrite( PB0, HIGH);   
  // delayMicroseconds(10);
  // digitalWrite( PB0, LOW);

}

void loop() {
  // digitalWrite( PB0, HIGH);   
  // delay( 1);
  // digitalWrite( PB0, LOW);

  // doSearch1Wire();

  // //Debug.println(F("1Wire::done"));

  // digitalWrite( PB0, HIGH);   
  // delay( 1);
  // digitalWrite( PB0, LOW);

  delay(2000);

  // digitalWrite( ONE_WIRE_BUS, HIGH);   
  // digitalWrite( ONE_WIRE_BUS, LOW);
  // delay(1000);

}

static void doSearch1Wire() {

  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  
  //Debug.println("Searching for 1-wire devices.");
  
  while( oneWire.search(addr) ) 
  {
    // TODO: Test with more then one 1-wire device.
    digitalWrite( PB0, HIGH);   
    digitalWrite( PB0, LOW);
  
    Debug.print("Found 1-Wire device with address: ");
  
    for( i = 0; i < 8; i++) 
    {
      Debug.print("0x");
      
      if (addr[i] < 16) 
      {
        Debug.print('0');
      }
      
      Debug.print(addr[i], HEX);
      
      if (i < 7) 
      {
        Debug.print(", ");
      }
      else
      {
        Debug.println();
      }
    }
    
    if ( OneWire::crc8( addr, 7) != addr[7]) 
    {
        Debug.println("Error: CRC is invalid.");
        //return;
    }
  }
  
  Debug.println("No more devices found.");
  
  digitalWrite( PB0, HIGH);   
  digitalWrite( PB0, LOW);

  oneWire.reset_search();

  digitalWrite( PB0, HIGH);   
  digitalWrite( PB0, LOW);

  return;
}

/*
static void doSearch1Wire() {
  Debug.println(F("-- doSearch1Wire --"));
  
  uint8_t addr[8];
  
  while(oneWire.search(addr)) {
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        // CRC is not valid!
        return;
    }

    for (uint8_t i = 0; i < 8; i++) {
      if (addr[i] < 0x10) Debug.print("0");
      Debug.print(addr[i], HEX);

    }
  }

}
*/
ATTINY8520PU
DS18B20Breakout
D0D1D2D3D4D5D6D7GNDLOGIC