Iāll admit - Hobbiest, just getting started with making my own apps/drivers (which I find fun).
Today I decided to look into this āLibrary Codeā: I donāt find much on it so I thought I would add a comment to help other hobbiest in their adventure. I have a couple of Apps and one Driver that use a function to calculate dew point. Today I learned that I can have that function in one location instead of in each location I need it. This would have been fantastic during the development of that function so I wouldnāt need to change it in multiple locations as I found errors.
Code that was in 3 different locations:
def dewpt(temp,humid) {
double lnPws
double Pws
double Pw
double lnPw
C1=-10214.165
C2=-4.8932428
C3=-0.005376579
C4= 0.000000192024
C5= 0.000000000355758
C6= -0.0000000000000903447
C7=4.1635019
C8=-10440.397
C9=-11.29465
C10=-0.027022355
C11=1.28904e-5
C12=-2.47807e-9
C13=6.5459673
ln=0.4342944819
Tr=459.67+temp
if (temp < 32) {
lnPws=C1/Tr + C2 + C3*Tr + C4*Tr**2 + C5*Tr**3 + C6*Tr**4 + C7*Math.log(Tr)
} else {
lnPws= C8/Tr + C9 + C10*Tr + C11*Tr**2 + C12*Tr**3 + Math.log(Tr)*C13
}
Pws= Math.exp(lnPws)
Pw=humid*Pws/100
lnPw=Math.log(Pw)
if (temp < 32) {
Td=(90.12+26.412*lnPw+0.8927*lnPw**2).round(2)
} else {
Td=(100.45+33.193*lnPw+2.319*lnPw*lnPw+0.17074*lnPw**3+1.2063*Pw**0.1984).round(2)
}
return Td
}
As a Library Code I moved this code from each of those locations to a library with this added prior to the code:
/******************************************************************************\
* Dew Point Calculation Funcion *
*
* to use call the function with #include ERA6515.dewpoint
* #include [namespace.name]
*
* then in the app / driver use it by:
* double dpTemperature = dewpt(temperature,relativeHumidity)
*
* temperture and relativeHumidity are float or larger real numbers
* the function returns a double real number
*
*
* 2023 No Rights Reserved
* Kokopelli Home Automation (eric albright)
*
******************************************************************************\
*/
library (
author: "Kokopelli Home Automation",
category: "function",
description: "Dew point calculation",
name: "dewpoint",
namespace: "ERA6515",
documentationLink: "n/a"
)
AND ADDED this code to the Apps and Driver that was using it:
#include ERA6515.dewpoint
If I made mistakes in it I would like to know so I can keep improving.