/* --------------------------------------------------
 * I2C Doctor - Sensor Simulator Test: Write
 * --------------------------------------------------
 * Connect at least GND, SDA & SCL of your device
 * to the I2C Doctor and navigate in the menu to
 * the Sensor Simulator (within More Tools).
 * 
 * Select an Addr (e.g. 0x42) and click on START.
 * 
 * Update the SENSOR_ADDR define below accordingly
 * and upload this code to your device.
 * 
 * You should now see that the WRITES counter increases
 * every 200ms (DELAY_IN_MS) and the most recent
 * bytes (LAST RX) cycle from 0x00 to 0xFF.
 *
 * In case of any problems, use the DIAGNOSE function
 * of your I2C Doctor :)
 * --------------------------------------------------
 */

#include <Wire.h>

#define SENSOR_ADDR 0x42  // Match what you configured on the I2C Doctor (Avoid 0x00)
#define DELAY_IN_MS 200

int i = 0;

void setup() {
  Serial.begin(115200);
  Wire.begin();
}

void loop() {
  /* Write a single byte to the (simulated) sensor */
  Wire.beginTransmission(SENSOR_ADDR);
  Wire.write(i); // 0 ... 255
  byte err = Wire.endTransmission();

  /* Print TX status */
  Serial.print("TX: 0x");
  if (i < 16) Serial.print("0"); // Nice hex formatting
  Serial.print(i, HEX);
  Serial.print(" -> ");
  if (err == 0) {
    Serial.println("OK");
  } else {
    Serial.print("ERROR (");
    Serial.print(err);
    Serial.println(")");
  }

  /* Increment variable and wait a few ms */
  i++;
  if (i > 0xFF) { i = 0; }
  delay(DELAY_IN_MS);
}
