
/*
  Libraries used:
  GrumpyOldPizza/ArduinoCore-stm32l0
*/

#include <EEPROM.h>
#include <LoRaWAN.h>

#define LED_RED 13

typedef struct {
  int32_t channel1Offset;
  float channel1Factor;
  int32_t channel2Offset;
  float channel2Factor;
  int32_t channel3Offset;
  float channel3Factor;
  float temperatureOffset;
  float temperatureCoefficient;
  boolean sendWeight;
  boolean temperatureCorrection;
} eeprom;

char devEui[32];

// adjust configuration parameters
const char *appEui = "0000000000000000";
const char *appKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
eeprom configuration = { 412621, 3157.0, -96749, 3098.0, 488797, 1720.9, 0.0, 0.0, true, false };

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

  // led
  pinMode(LED_RED, OUTPUT);
  digitalWrite(LED_RED, LOW);

  LoRaWAN.begin(EU868);
  LoRaWAN.getDevEui(devEui, 32);
  LoRaWAN.setAppEui(appEui);
  LoRaWAN.setAppKey(appKey);

  Serial.print("devEUI: ");
  Serial.println(devEui);
  Serial.print("appEui: ");
  Serial.println(appEui);
  Serial.print("appKey: ");
  Serial.println(appKey);

  Serial.print("eeprom length: ");
  Serial.println(EEPROM.length());

  // write and read again
  EEPROM.put(0, configuration);
  EEPROM.get(0, configuration);

  Serial.print("Channel1 Scale Offset: ");
  Serial.println(configuration.channel1Offset);
  Serial.print("Channel1 Scale Factor: ");
  Serial.println(configuration.channel1Factor, 1);
  Serial.print("Channel2 Scale Offset: ");
  Serial.println(configuration.channel2Offset);
  Serial.print("Channel2 Scale Factor: ");
  Serial.println(configuration.channel2Factor, 1);
  Serial.print("Channel3 Scale Offset: ");
  Serial.println(configuration.channel3Offset);
  Serial.print("Channel3 Scale Factor: ");
  Serial.println(configuration.channel3Factor, 1);
  Serial.print("Temperature Offset: ");
  Serial.println(configuration.temperatureOffset);
  Serial.print("Temperature Coefficient: ");
  Serial.println(configuration.temperatureCoefficient);
}

void loop( void ) {
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_RED, LOW);
  delay(500);
}
