
/*
   17.2.2024 JS
*/

#include "Wire.h"
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"

static struct {
  int32_t channel1Offset;
  float channel1Factor;
  int32_t channel2Offset;
  float channel2Factor;
  int32_t channel3Offset;
  float channel3Factor;
  float temperatureOffset;
  float temperatureCoefficient;
  boolean sendWeight;
  boolean temperatureCorrection;
} configuration = { 210500, 3157.0, 86800, 3098.0, 484839, 1720.9, 0, 0, true, true };

NAU7802 Scale;

void setup() {
  // uart
  Serial.begin(9600);
  while (!Serial) delay(100);

  // i2c
  Wire.begin();

  // scale
  bool ok = true;
  Scale.begin(Wire, false);
  ok &= Scale.reset();
  ok &= Scale.powerUp();
  ok &= Scale.setLDO(NAU7802_LDO_3V0);
  ok &= Scale.setGain(NAU7802_GAIN_128);
  ok &= Scale.setSampleRate(NAU7802_SPS_20);
  uint8_t adc = Scale.getRegister(NAU7802_ADC);
  ok &= Scale.setRegister(NAU7802_ADC, adc | 0x30);
  ok &= Scale.clearBit(NAU7802_PGA_LDOMODE, NAU7802_PGA);
  ok &= Scale.powerDown();
  if (!ok) Serial.println("Error initialising NAU8702");
  delay(1000);
}

void loop() {
  bool ok = true;
  int32_t v1, v2, t;
  float w1, w2, weight, temperature, mcp9808;

  // power
  ok &= Scale.powerUp();
  if (!ok) Serial.println("Error Power Up");
  
  // channel 1
  ok &= Scale.setChannel(NAU7802_CHANNEL_1);
  ok &= Scale.setGain(NAU7802_GAIN_128);
  delay(250);
  ok &= Scale.calibrateAFE(NAU7802_CALMOD_INTERNAL);
  if (!ok) Serial.println("Error Channel 1");
  Scale.getAverage(3); // dummy read
  v1 = Scale.getAverage(5);
  w1 = (v1 - configuration.channel1Offset) / configuration.channel1Factor;

  // channel 2
  ok &= Scale.setChannel(NAU7802_CHANNEL_2);
  ok &= Scale.setGain(NAU7802_GAIN_128);
  delay(250);
  ok &= Scale.calibrateAFE(NAU7802_CALMOD_INTERNAL);
  if (!ok) Serial.println("Error Channel 2");
  Scale.getAverage(3); // dummy read
  v2 = Scale.getAverage(5);
  w2 = (v2 - configuration.channel2Offset) / configuration.channel2Factor;
  weight = w1 + w2;

  // temperature sensor
  ok &= Scale.setBit(1, NAU7802_I2C_CONTROL);
  ok &= Scale.setGain(NAU7802_GAIN_2);
  delay(250);
  ok &= Scale.calibrateAFE(NAU7802_CALMOD_INTERNAL);
  Scale.getAverage(3); // dummy read
  t = Scale.getAverage(5);
  ok &= Scale.clearBit(1, NAU7802_I2C_CONTROL);
  if (!ok) Serial.println("Error Temperature Sensor");
  temperature = (t - configuration.channel3Offset) / configuration.channel3Factor;

  // power
  ok &= Scale.powerDown();
  if (!ok) Serial.println("Error Power Down");  
  delay(1000);
  
  if (ok) {
    Serial.print("Conf: ");
    Serial.print(configuration.channel1Offset);
    Serial.print(" ");
    Serial.print(configuration.channel1Factor, 1);
    Serial.print(" ");
    Serial.print(configuration.channel2Offset);
    Serial.print(" ");
    Serial.print(configuration.channel2Factor, 1);
    Serial.print(" ");

    Serial.print("ADC: ");
    Serial.print(v1);
    Serial.print(" ");
    Serial.print(v2);
    Serial.print(" ");
    Serial.print(t);
    Serial.print(" ");

    Serial.print("Weight: ");
    Serial.print(w1);
    Serial.print(" ");
    Serial.print(w2);
    Serial.print(" ");
    Serial.print(weight);
    Serial.print(" ");

    Serial.print("Temperature: ");
    Serial.print(temperature, 1);

    Serial.println();
  }

  if (Serial.available()) {
    byte in = Serial.read();
    switch (in) {
      // channel 1
      case 'q':
        configuration.channel1Offset -= 100;
        break;

      case 'w':
        configuration.channel1Offset += 100;
        break;

      case 'a':
        configuration.channel1Factor -= 10;
        break;

      case 's':
        configuration.channel1Factor += 10;
        break;

      // channel 2
      case 'o':
        configuration.channel2Offset -= 100;
        break;

      case 'p':
        configuration.channel2Offset += 100;
        break;

      case 'k':
        configuration.channel2Factor -= 10;
        break;

      case 'l':
        configuration.channel2Factor += 10;
        break;

      default:
        break;
    }
  }
}
