From 78f9f6b611b9691e1750e2f8ec693442e2bef9cf Mon Sep 17 00:00:00 2001
From: PavelSindler <sindlerpa@gmail.com>
Date: Fri, 16 Mar 2018 11:01:06 +0100
Subject: [PATCH 1/3] use defines for adc pins, temp table for PIDNA update

---
 Firmware/pins_Einsy_1_0.h |  2 +-
 Firmware/temperature.cpp  | 44 ++++++++++++++++++++++++++++++++-------
 Firmware/ultralcd.cpp     | 12 +++++------
 3 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/Firmware/pins_Einsy_1_0.h b/Firmware/pins_Einsy_1_0.h
index 8595a0a3..0267b3fc 100644
--- a/Firmware/pins_Einsy_1_0.h
+++ b/Firmware/pins_Einsy_1_0.h
@@ -73,7 +73,7 @@
 #define HEATER_2_PIN        -1
 #define TEMP_2_PIN          -1
 
-#define TEMP_AMBIENT_PIN     6 //A6
+#define TEMP_AMBIENT_PIN     5 //A5
 
 #define TEMP_PINDA_PIN       3 //A3
 
diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp
index 08e423bd..3afccf99 100644
--- a/Firmware/temperature.cpp
+++ b/Firmware/temperature.cpp
@@ -182,6 +182,7 @@ static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
 static float analog2temp(int raw, uint8_t e);
 static float analog2tempBed(int raw);
 static float analog2tempAmbient(int raw);
+static float analog2tempPINDA(int raw);
 static void updateTemperaturesFromRawValues();
 
 enum TempRunawayStates
@@ -922,6 +923,35 @@ static float analog2tempBed(int raw) {
   #endif
 }
 
+#ifdef PINDA_THERMISTOR
+
+static float analog2tempPINDA(int raw) {
+
+	float celsius = 0;
+	byte i;
+
+	for (i = 1; i<BEDTEMPTABLE_LEN; i++)
+	{
+		if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
+		{
+			celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
+				(raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
+				(float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
+				(float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
+			break;
+		}
+	}
+
+	// Overflow: Set to last value in the table
+	if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
+
+	return celsius;
+}
+
+
+#endif //PINDA_THERMISTOR
+
+
 #ifdef AMBIENT_THERMISTOR
 static float analog2tempAmbient(int raw)
 {
@@ -955,7 +985,7 @@ static void updateTemperaturesFromRawValues()
     }
 
 #ifdef PINDA_THERMISTOR
-	current_temperature_pinda = analog2tempBed(current_temperature_raw_pinda); //thermistor for pinda is the same as for bed
+	current_temperature_pinda = analog2tempPINDA(current_temperature_raw_pinda);
 #endif
 
 #ifdef AMBIENT_THERMISTOR
@@ -1504,17 +1534,17 @@ extern "C" {
 
 void adc_ready(void) //callback from adc when sampling finished
 {
-	current_temperature_raw[0] = adc_values[0];
-	current_temperature_raw_pinda = adc_values[1];
-	current_temperature_bed_raw = adc_values[2];	
+	current_temperature_raw[0] = adc_values[TEMP_0_PIN]; //heater
+	current_temperature_raw_pinda = adc_values[TEMP_PINDA_PIN];
+	current_temperature_bed_raw = adc_values[TEMP_BED_PIN];
 #ifdef VOLT_PWR_PIN
-	current_voltage_raw_pwr = adc_values[4];
+	current_voltage_raw_pwr = adc_values[VOLT_PWR_PIN];
 #endif
 #ifdef AMBIENT_THERMISTOR
-	current_temperature_raw_ambient = adc_values[5];
+	current_temperature_raw_ambient = adc_values[TEMP_AMBIENT_PIN];
 #endif //AMBIENT_THERMISTOR
 #ifdef VOLT_BED_PIN
-	current_voltage_raw_bed = adc_values[6];
+	current_voltage_raw_bed = adc_values[VOLT_BED_PIN]; // 6->9
 #endif
 	temp_meas_ready = true;
 }
diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp
index 2fbf2a63..56fa8dd0 100644
--- a/Firmware/ultralcd.cpp
+++ b/Firmware/ultralcd.cpp
@@ -1695,7 +1695,7 @@ static void lcd_menu_temperatures()
     }
 }
 
-#ifdef defined(VOLT_BED_PIN) || defined(VOLT_BED_PIN)
+#if defined VOLT_BED_PIN || defined VOLT_PWR_PIN
 #define VOLT_DIV_R1 10000
 #define VOLT_DIV_R2 2370
 #define VOLT_DIV_FAC ((float)VOLT_DIV_R2 / (VOLT_DIV_R2 + VOLT_DIV_R1))
@@ -1703,8 +1703,8 @@ static void lcd_menu_temperatures()
 static void lcd_menu_voltages()
 {
 	float volt_pwr = VOLT_DIV_REF * ((float)current_voltage_raw_pwr / (1023 * OVERSAMPLENR)) / VOLT_DIV_FAC;
-	//float volt_bed = VOLT_DIV_REF * ((float)current_voltage_raw_bed / (1023 * OVERSAMPLENR)) / VOLT_DIV_FAC;
-	//fprintf_P(lcdout, PSTR(ESC_H(1,1)"PWR:      %d.%01dV" ESC_H(1,2)"BED:      %d.%01dV"), (int)volt_pwr, (int)(10*fabs(volt_pwr - (int)volt_pwr)), (int)volt_bed, (int)(10*fabs(volt_bed - (int)volt_bed)));
+	float volt_bed = VOLT_DIV_REF * ((float)current_voltage_raw_bed / (1023 * OVERSAMPLENR)) / VOLT_DIV_FAC;
+	fprintf_P(lcdout, PSTR(ESC_H(1,1)"PWR:      %d.%01dV" ESC_H(1,2)"BED:      %d.%01dV"), (int)volt_pwr, (int)(10*fabs(volt_pwr - (int)volt_pwr)), (int)volt_bed, (int)(10*fabs(volt_bed - (int)volt_bed)));
     fprintf_P(lcdout, PSTR( ESC_H(1,1)"PWR:      %d.%01dV"), (int)volt_pwr, (int)(10*fabs(volt_pwr - (int)volt_pwr))) ;
     if (lcd_clicked())
     {
@@ -1712,7 +1712,7 @@ static void lcd_menu_voltages()
         lcd_return_to_status();
     }
 }
-#endif //defined(VOLT_BED_PIN) || defined(VOLT_BED_PIN)
+#endif //defined VOLT_BED_PIN || defined VOLT_PWR_PIN
 
 #ifdef TMC2130
 static void lcd_menu_belt_status()
@@ -1824,9 +1824,9 @@ static void lcd_support_menu()
     
   MENU_ITEM(submenu, MSG_MENU_TEMPERATURES, lcd_menu_temperatures);
 
-#ifdef defined(VOLT_BED_PIN) || defined(VOLT_BED_PIN)
+#if defined VOLT_BED_PIN || defined VOLT_BED_PIN
   MENU_ITEM(submenu, MSG_MENU_VOLTAGES, lcd_menu_voltages);
-#endif //defined(VOLT_BED_PIN) || defined(VOLT_BED_PIN)
+#endif //defined VOLT_BED_PIN || defined VOLT_BED_PIN
 
 #ifdef DEBUG_BUILD
   MENU_ITEM(submenu, PSTR("Debug"), lcd_menu_debug);

From 2d30261976f9371ee2682e3d12c0e705840b311d Mon Sep 17 00:00:00 2001
From: PavelSindler <sindlerpa@gmail.com>
Date: Fri, 23 Mar 2018 16:49:06 +0100
Subject: [PATCH 2/3] bed voltage menu hidden

---
 Firmware/ultralcd.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp
index 56fa8dd0..a55dcb4f 100644
--- a/Firmware/ultralcd.cpp
+++ b/Firmware/ultralcd.cpp
@@ -1703,8 +1703,8 @@ static void lcd_menu_temperatures()
 static void lcd_menu_voltages()
 {
 	float volt_pwr = VOLT_DIV_REF * ((float)current_voltage_raw_pwr / (1023 * OVERSAMPLENR)) / VOLT_DIV_FAC;
-	float volt_bed = VOLT_DIV_REF * ((float)current_voltage_raw_bed / (1023 * OVERSAMPLENR)) / VOLT_DIV_FAC;
-	fprintf_P(lcdout, PSTR(ESC_H(1,1)"PWR:      %d.%01dV" ESC_H(1,2)"BED:      %d.%01dV"), (int)volt_pwr, (int)(10*fabs(volt_pwr - (int)volt_pwr)), (int)volt_bed, (int)(10*fabs(volt_bed - (int)volt_bed)));
+	//float volt_bed = VOLT_DIV_REF * ((float)current_voltage_raw_bed / (1023 * OVERSAMPLENR)) / VOLT_DIV_FAC;
+	//fprintf_P(lcdout, PSTR(ESC_H(1,1)"PWR:      %d.%01dV" ESC_H(1,2)"BED:      %d.%01dV"), (int)volt_pwr, (int)(10*fabs(volt_pwr - (int)volt_pwr)), (int)volt_bed, (int)(10*fabs(volt_bed - (int)volt_bed)));
     fprintf_P(lcdout, PSTR( ESC_H(1,1)"PWR:      %d.%01dV"), (int)volt_pwr, (int)(10*fabs(volt_pwr - (int)volt_pwr))) ;
     if (lcd_clicked())
     {

From dc32bd24fc9758ff6493c90d005ab4b0eaf5330c Mon Sep 17 00:00:00 2001
From: PavelSindler <sindlerpa@gmail.com>
Date: Fri, 23 Mar 2018 18:20:55 +0100
Subject: [PATCH 3/3] correction

---
 Firmware/ultralcd.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp
index a55dcb4f..b1a5abf9 100644
--- a/Firmware/ultralcd.cpp
+++ b/Firmware/ultralcd.cpp
@@ -1695,7 +1695,7 @@ static void lcd_menu_temperatures()
     }
 }
 
-#if defined VOLT_BED_PIN || defined VOLT_PWR_PIN
+#if defined (VOLT_BED_PIN) || defined (VOLT_PWR_PIN)
 #define VOLT_DIV_R1 10000
 #define VOLT_DIV_R2 2370
 #define VOLT_DIV_FAC ((float)VOLT_DIV_R2 / (VOLT_DIV_R2 + VOLT_DIV_R1))
@@ -1824,7 +1824,7 @@ static void lcd_support_menu()
     
   MENU_ITEM(submenu, MSG_MENU_TEMPERATURES, lcd_menu_temperatures);
 
-#if defined VOLT_BED_PIN || defined VOLT_BED_PIN
+#if defined (VOLT_BED_PIN) || defined (VOLT_BED_PIN)
   MENU_ITEM(submenu, MSG_MENU_VOLTAGES, lcd_menu_voltages);
 #endif //defined VOLT_BED_PIN || defined VOLT_BED_PIN