From 052cfbc11de1fc6d2fc0d6723a6ec2643770ff6d Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 18 May 2019 18:24:17 +0200 Subject: [PATCH] Save 1k code space by using TaylorSeries log function Save 1k code space by using TaylorSeries log function --- sonoff/support.ino | 18 ++++++++++++++++++ sonoff/xsns_02_analog.ino | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sonoff/support.ino b/sonoff/support.ino index c10c81eb0..7c0bbb1a2 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -680,6 +680,24 @@ float FastPrecisePowf(const float x, const float y) return (float)FastPrecisePow(x, y); } +double TaylorLog(double x) +{ + // https://stackoverflow.com/questions/46879166/finding-the-natural-logarithm-of-a-number-using-taylor-series-in-c + + double z = (x + 1) / (x - 1); // We start from power -1, to make sure we get the right power in each iteration; + double step = ((x - 1) * (x - 1)) / ((x + 1) * (x + 1)); // Store step to not have to calculate it each time + double totalValue = 0; + double powe = 1; + double y; + for (int count = 0; count < 10; count++) { + z *= step; + y = (1 / powe) * z; + totalValue = totalValue + y; + powe = powe + 2; + } + return 2 * totalValue; +} + uint32_t SqrtInt(uint32_t num) { if (num <= 1) { diff --git a/sonoff/xsns_02_analog.ino b/sonoff/xsns_02_analog.ino index e93377165..85560e3d3 100644 --- a/sonoff/xsns_02_analog.ino +++ b/sonoff/xsns_02_analog.ino @@ -100,7 +100,7 @@ void AdcEverySecond(void) int adc = AdcRead(2); // Steinhart-Hart equation for thermistor as temperature sensor double Rt = (adc * ANALOG_NTC_BRIDGE_RESISTANCE) / (1024.0 * ANALOG_V33 - (double)adc); - double T = ANALOG_NTC_B_COEFFICIENT / (ANALOG_NTC_B_COEFFICIENT / ANALOG_T0 + log(Rt / ANALOG_NTC_RESISTANCE)); + double T = ANALOG_NTC_B_COEFFICIENT / (ANALOG_NTC_B_COEFFICIENT / ANALOG_T0 + TaylorLog(Rt / ANALOG_NTC_RESISTANCE)); adc_temp = ConvertTemp(TO_CELSIUS(T)); } }