#include <Wire.h>
#include "gw_grayscale_sensor.h"

// SDA : A4
// SCL : A5

#define USE_GW_GRAY_NORMALISE 1

#define GW_GRAY_ADDR GW_GRAY_ADDR_DEF // 使用默认地址

void setup() {
  // 初始化I2C
  Wire.begin();

  // 初始化串口
  Serial.begin(115200);
}

void loop() {
  uint8_t recv_value = 0;
  uint8_t ping_rep = 0;
  uint8_t sensor1, sensor2, sensor3, sensor4, sensor5, sensor6, sensor7, sensor8;

  // 循环 直到PING成功
  while (ping_rep != GW_GRAY_PING_OK) {
    Wire.beginTransmission(GW_GRAY_ADDR);
    Wire.write(GW_GRAY_PING);
    Wire.endTransmission();

    Wire.requestFrom(GW_GRAY_ADDR, 1);
    ping_rep = Wire.read();
    delay(10);
  }

  /* ping 成功 */

#if (USE_GW_GRAY_NORMALISE)
  /* 探头数据归一化(v3.6+固件版本) */
  Wire.beginTransmission(GW_GRAY_ADDR);
  Wire.write(GW_GRAY_ANALOG_NORMALIZE);
  Wire.write(0xFF); /* 归一化所有探头数据 */
  Wire.endTransmission();
#endif

  /* 模拟数据模式, 设置完毕之后, 循环读取8个探头的模拟数据 */
  Wire.beginTransmission(GW_GRAY_ADDR);
  Wire.write(GW_GRAY_ANALOG_MODE);
  Wire.endTransmission();

  // 打印8个探头的状态
  while (1) {
    Wire.requestFrom(GW_GRAY_ADDR, 8, 1);
    
    Serial.print("打印8个探头的模拟数据:");
    for (int i = 0; i < 8; ++i) {
      recv_value = Wire.read();
      Serial.print(" ");
      Serial.print(recv_value);
    }
    Serial.print("\n");
    delay(100);
  }
}