// I2C Master Example
// Matthew Domenech
// 4/18/18

// This example code is in the public domain.
//According to PIC conventions, PIC slave address should be equal to
//the (slave address used by Arduino x 2) or (slave address used by Arduino x 2 + 1).
//That is, when the slave address indicated in the arduino program is 10, both 20 and 21 addresses in PIC slave can be used to communicate.
//0 is write and 1 is read

#include <Wire.h>
int CorrectAddr = 0x05;
int WrongAddr =  0x06;
char write_data1 = 1;
char write_data2 = 2;
long read_data;
char selector;
int data_array[8];
int i = 0;

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Wire.setClock(10000);
  Serial.begin(9600);  // start serial for output
  Serial.println("Type 'r' to read and 'w' write. ");
}

void loop() {
  if (Serial.available()) {
    selector = Serial.read();
    Serial.println(selector);
    if (selector == 'r') {
      Serial.println("Begin Reading");
      Wire.requestFrom(CorrectAddr, 2);    // request x bytes from slave device #0
      //delay(1);
      Serial.println("Request Sent");
      i = 0;
      while (Wire.available()) { // slave may send less than requested
        read_data = Wire.read(); // receive a byte as character
        data_array[i] = read_data;
        Serial.println(read_data);         // print the dummy data
        i=i+1;
        
      }
      Serial.println("END READ"); 
      Wire.endTransmission(); //JANKY FIX.  EXTRA STOP SO READ>WRITE WORKS.
      
    }
    if (selector == 'w') {
      Serial.println("RIGHT WRITE");
      
      Wire.beginTransmission(CorrectAddr); // begin transmit to device with address #0
      delay(2);
      Wire.write(write_data1); //Send the # 5 to PIC32. Will be stored in int dummy
      //Wire.write(dummy_data); //Send another transmission
      Serial.println(Wire.endTransmission());    // stop transmitting

      Serial.println("___END WRITE");
      Serial.println("*******************************");
    }
    if (selector == 'R'){
      Serial.println("Begin Reading from Wrong Address");
      Wire.requestFrom(WrongAddr, 2);    // request x bytes from slave device #0
      delay(1);
      Serial.println("Request Sent");
      i = 0;
      while (Wire.available()) { // slave may send less than requested
        read_data = Wire.read(); // receive a byte as character
        data_array[i] = read_data;
        Serial.println(read_data);         // print the dummy data
        i=i+1;
        Serial.println("END READ from Wrong Address"); 
      }
      Wire.endTransmission(); //JANKY FIX.  EXTRA STOP SO READ>WRITE WORKS.
    }
    if (selector == 'W') {
      Serial.println("WRONG WRITE");
      
      Wire.beginTransmission(WrongAddr); // begin transmit to device with address #0
      delay(2);
      Wire.write(write_data2); //Send the # 5 to PIC32. Will be stored in int dummy
      //Wire.write(dummy_data); //Send another transmission
      Serial.println(Wire.endTransmission());    // stop transmitting

      Serial.println("___END WRONG WRITE");
      Serial.println("*******************************");
    }
  }
}
