From cef42a45dab3f2399e175799c6be8af924544499 Mon Sep 17 00:00:00 2001
From: Sebastian Fernandez <sebfer@fing.edu.uy>
Date: Thu, 30 Apr 2020 00:09:18 -0300
Subject: [PATCH] herramienta para inicializar valores en EEPROM

---
 .../un_meter_setup_arduino.ino                | 244 ++++++++++++++++++
 1 file changed, 244 insertions(+)
 create mode 100644 sw/prototipo_0/uv_meter_setup_arduino/un_meter_setup_arduino/un_meter_setup_arduino.ino

diff --git a/sw/prototipo_0/uv_meter_setup_arduino/un_meter_setup_arduino/un_meter_setup_arduino.ino b/sw/prototipo_0/uv_meter_setup_arduino/un_meter_setup_arduino/un_meter_setup_arduino.ino
new file mode 100644
index 0000000..2435730
--- /dev/null
+++ b/sw/prototipo_0/uv_meter_setup_arduino/un_meter_setup_arduino/un_meter_setup_arduino.ino
@@ -0,0 +1,244 @@
+/* Herramientas para configurar valores en EEPROm y calibrar
+ * Sebastian Fernandez
+ * sebfer@fing.edu.uy 
+ * 
+ * * Comandos
+ *   leer valores eeprom       : r
+ *   guardar potencia forzada  : p,potencia_raw_en_mW 
+ *   guardar potencia medida   : s 
+ *   horas de encendido        : h,decenas_minutos    
+ *   calibracion ganancia      : g,ganancia
+ *   calibracion offset        : o,offset
+ *   
+ * 
+ * 
+ * Plataformas:
+ *  - ESP32 de ESPRESSIF
+ *    En IDE seleccionar ESP32 Dev Module, CPU Freq, 240MHz
+ * 
+ *  * v0.1
+ *  
+ *  * LedControl library
+ * Instalarla y editar 
+ * #include <avr/pgmspace.h>
+ * https://github.com/wayoda/LedControl
+ * 
+ * 
+  */
+
+#include <EEPROM.h>
+#include "LedControl.h"     // 8 digit 7 segment. MAX  
+
+/****************************************
+ * Platform parameters
+ ****************************************/
+const int tic_period = 100; //ms
+
+// Baud rate for debug serial
+#define SERIAL_DEBUG_BAUD    115200
+char incomingByte = 0;   // for incoming serial data
+char inData[80];
+int indexData = 0;
+String readString;
+
+
+// 7 segment
+LedControl lc=LedControl(12,27,14,1);
+// pin 12 is connected to the DIN pin
+// pin 27 is connected to the CLK pin
+// pin 14 is connected to the CS pin
+// 1 as we are only using 1 MAX7219
+char digitos_disp1[4] = {};
+char digitos_disp2[4] = {};
+
+// alarma
+const int alarm_led_pin = 26;
+const int alarm_buz_pin = 25;
+int alarm_state = LOW;
+
+// load position sensor
+const int back_position_pin = 32;
+int back_position = LOW; // HIGH when present 
+const int front_position_pin = 33;
+int front_position = LOW; // HIGH when present
+
+// EEPROM
+// define the number of byte needed
+#define ADD_POWER_RAW    0 // address 0 and 1 store radiation power (4 bytes)
+#define ADD_ON_TIME      4 // address 1 and 2 store TIME on decens of minutes (4 bytes)
+#define ADD_CAL_G        8 // address 3 and 4 store Calibration parameter 1 (4 bytes)
+#define ADD_CAL_O       12 // address 5 and 6 store Calibration parameter 1 (4 bytes)
+#define EEPROM_SIZE     20 //
+
+// UV metering 
+float radiation_power_raw = 1.0;
+float radiation_power     = 1.0;
+float calibration_gain    = 1.0;
+float calibration_offset  = 1.0;
+
+float fdec_minutes_on      = 0;
+int   idec_minutes_on      = 0;
+
+float scaleRadiationPower(float radiation_raw)
+{
+  return radiation_raw * calibration_gain + calibration_offset;
+}
+
+
+void setup() {
+// Initialize serial for debugging
+  Serial.begin(SERIAL_DEBUG_BAUD);
+
+// the zero refers to the MAX7219 number, it is zero for 1 chip
+  lc.shutdown(0,false);// turn off power saving, enables display
+  lc.setIntensity(0,10);// sets brightness (0~15 possible values)
+  lc.clearDisplay(0);// clear screen
+
+// initialize alarm pins
+pinMode(alarm_led_pin, OUTPUT);
+ledcSetup(0,1E5,12);
+ledcAttachPin(alarm_buz_pin,0);
+
+// init sensor pins
+pinMode(back_position_pin, INPUT);
+pinMode(front_position_pin, INPUT);
+
+// Initialize EEPROM
+  EEPROM.begin(EEPROM_SIZE);
+  
+
+}
+
+void loop() {
+  delay(tic_period);
+
+if (Serial.available() > 0)
+  {
+    indexData = 0;
+    readString = "";
+
+    while (Serial.available() > 0)  {
+      incomingByte = Serial.read();  //gets one byte from serial buffer
+      inData[indexData] = incomingByte;
+      indexData++;
+    }
+    Serial.print("index data máximo: ");
+    Serial.println(indexData);
+
+    if (inData[0] == 'r') //r
+    {
+      Serial.println("===== comando recibido: leer eeprom ===== ");
+      // formato mensaje: r
+      Serial.print("Potencia guardada: ");
+      EEPROM.get(ADD_POWER_RAW, radiation_power_raw);
+      radiation_power = scaleRadiationPower(radiation_power_raw);
+      Serial.println(radiation_power);
+      
+      Serial.print("Horas encendido: ");
+      EEPROM.get(ADD_ON_TIME, fdec_minutes_on);
+      Serial.println(fdec_minutes_on/6);
+
+      Serial.print("Coeficiente ganancia: ");
+      EEPROM.get(ADD_CAL_G, calibration_gain);
+      Serial.println(calibration_gain);
+
+      Serial.print("Coeficiente offset: ");
+      EEPROM.get(ADD_CAL_O, calibration_offset);
+      Serial.println(calibration_offset);
+    }
+
+    if (inData[0] == 'h') //h
+    {
+      Serial.println("===== comando recibido: setear horas encendido ===== ");
+      // formato mensaje: h,decenas_minutos
+      int decenas_minutos_on = 1;
+      bool recepcion_ok = false;
+      for (int j = 2; j < indexData; j = j + 1) {
+        readString += inData[j];
+      }
+      if (readString) {
+        decenas_minutos_on = round(readString.toInt());
+        Serial.print("Decenas de minutos a grabar en EEPROM: ");
+        Serial.println(decenas_minutos_on);
+        fdec_minutes_on= (float)decenas_minutos_on;
+        EEPROM.put(ADD_ON_TIME, fdec_minutes_on);
+        EEPROM.commit();
+        recepcion_ok = true;
+      }
+      if (not recepcion_ok)
+      {
+        Serial.println("Comando incompleto");
+      }
+    }
+
+    if (inData[0] == 'p') //f
+    {
+      Serial.println("===== comando recibido: setear potencia manualmente ===== ");
+      // formato mensaje: p,potencia_en_mW 
+      float potencia_raw_en_mW = 1;
+      bool recepcion_ok = false;
+      for (int j = 2; j < indexData; j = j + 1) {
+        //Serial.println(inData[j]);
+        readString += inData[j];
+      }
+      if (readString) {
+        potencia_raw_en_mW = (float)readString.toInt();
+        Serial.print("Potencia raw en mW a grabar en EEPROM: ");
+        Serial.println(potencia_raw_en_mW);
+        EEPROM.put(ADD_POWER_RAW, potencia_raw_en_mW);
+        EEPROM.commit();
+        recepcion_ok = true;
+      }
+      if (not recepcion_ok)
+      {
+        Serial.println("Comando incompleto");
+      }
+    }
+
+    if (inData[0] == 'g') //g
+    {
+      Serial.println("===== comando recibido: ganancia ===== ");
+      // formato mensaje: g,ganancia
+      int ganancia = 1;
+      bool recepcion_ok = false;
+      for (int j = 2; j < indexData; j = j + 1) {
+        readString += inData[j];
+      }
+      if (readString) {
+        ganancia = round(readString.toInt());
+        Serial.print("Ganancia a grabar en EEPROM: ");
+        Serial.println(ganancia);
+        EEPROM.put(ADD_CAL_G, (float)ganancia);
+        EEPROM.commit();
+        recepcion_ok = true;
+      }
+      if (not recepcion_ok)
+      {
+        Serial.println("Comando incompleto");
+      }
+    }
+
+    if (inData[0] == 'o') //o
+    {
+      Serial.println("===== comando recibido: offset ===== ");
+      // formato mensaje: o,offset
+      int offset = 1;
+      bool recepcion_ok = false;
+      for (int j = 2; j < indexData; j = j + 1) {
+        readString += inData[j];
+      }
+      if (readString) {
+        offset = round(readString.toInt());
+        Serial.print("Offset a grabar en EEPROM: ");
+        Serial.println(offset);
+        EEPROM.put(ADD_CAL_O, (float)offset);
+        EEPROM.commit();
+        recepcion_ok = true;
+      }
+      if (not recepcion_ok)
+      {
+        Serial.println("Comando incompleto");
+      }
+    }   
+  }
+}
-- 
GitLab