A collection of Analog Devices drivers for the mbed platform
Embed:
(wiki syntax)
Show/hide line numbers
lut_script.py
00001 def isFloat(string): 00002 try: 00003 float(string) 00004 return True 00005 except ValueError: 00006 return False 00007 00008 with open("thermocouple.cpp","w") as out: 00009 with open("Thermocouple_core_cpp.txt","r") as inp: 00010 out.write(inp.read()) 00011 out.write("\n\n\n") 00012 pass 00013 00014 with open("thermocouple.h","w") as out: 00015 with open("Thermocouple_core_h.txt", "r") as inp: 00016 out.write(inp.read()) 00017 out.write("\n\n\n") 00018 pass 00019 00020 with open("thermocouple_lut.cpp","w") as out: 00021 with open("Thermocouple_lut_core_cpp.txt","r") as inp: 00022 out.write(inp.read()) 00023 out.write("\n\n\n") 00024 pass 00025 00026 with open('tc.txt', 'r') as myfile: 00027 initstr=myfile.read().replace('\n', ' ') 00028 00029 00030 #print(initstr.split()); 00031 splitted = initstr.split(); 00032 00033 while splitted: 00034 i=0 00035 00036 for word in splitted: 00037 if(word == 'type'): 00038 break 00039 i=i+1 00040 del splitted[:i+1] 00041 if not splitted: 00042 break 00043 tctype = splitted[0][0].lower() 00044 00045 class_name = "Thermocouple_Type_" + tctype.capitalize() 00046 lut_def_name = "TYPE_" + tctype.capitalize() + "_LUT" 00047 print(lut_def_name) 00048 00049 i=0 00050 state=0 00051 lut = [] 00052 for word in splitted: 00053 if(word=="************************************"): 00054 break 00055 if(word=="mV"): 00056 state = 1 00057 continue 00058 if(state == 1 and isFloat(word)): 00059 lut.append(word) 00060 else: 00061 state=0 00062 i=i+1 00063 print(lut) 00064 del splitted[:i] 00065 i=0 00066 00067 globaloffset = lut[0] 00068 00069 if(float(lut[0]) == float(lut[2])-10): 00070 del lut[:3] 00071 else: 00072 del lut[0] 00073 00074 j=0 00075 print lut 00076 lutfloat=[] 00077 for val in lut: 00078 b=1 00079 if(i%11==0): 00080 #print "11 " + val 00081 b=0 00082 if(i%12==0): 00083 #print "12 "+ val 00084 i=0 00085 b=0 00086 i=i+1 00087 j=j+1 00088 if(b): 00089 lutfloat.append(float(val)) 00090 00091 00092 #print "unsorted" 00093 #print lutfloat 00094 lutfloat.sort() 00095 #print "sorted" 00096 #print lutfloat 00097 i=0 00098 with open("thermocouple_lut.cpp", "a") as out: 00099 out.write("#ifdef "+lut_def_name+"\n") 00100 out.write("const int16_t " + class_name + "::lut_offset = " + str(globaloffset)+";\n") 00101 out.write("const uint16_t " + class_name + "::lut_size = " + str(len(lutfloat))+";\n") 00102 out.write("const int32_t "+class_name+"::lut["+str(len(lutfloat))+"] = {\n") 00103 for val in lutfloat: 00104 out.write(str(int(val*1000)).rjust(8)+ ", ") 00105 i=i+1 00106 if(i==10): 00107 out.write("\n") 00108 i=0 00109 out.write("};\n") 00110 out.write("#endif\n\n") 00111 i=0 00112 00113 ranger = [] 00114 values = [] 00115 exp=[] 00116 ranger_index = -1 #HACK HACK HACK 00117 00118 while(splitted[0] != '************************************'): 00119 for word in splitted: 00120 if(word=='range:'): 00121 ranger.append([]) 00122 values.append([]) 00123 ranger_index=ranger_index+1 00124 break 00125 if(word=='exponential:'): 00126 exp.append(splitted[i+3]) 00127 exp.append(splitted[i+6]) 00128 exp.append(splitted[i+9]) 00129 i=i+10 00130 print exp 00131 break 00132 i=i+1 00133 if(word=='exponential:'): 00134 print("exponential") 00135 del splitted[:i + 1] 00136 i=0 00137 break 00138 del splitted[:i+1] 00139 print splitted 00140 i=0 00141 00142 ranger[ranger_index].append(splitted[0]) 00143 ranger[ranger_index].append(splitted[1]) 00144 nr_of_coef = int(splitted[2])+1 00145 del splitted[:3] 00146 for i in range(nr_of_coef): 00147 values[ranger_index].append(splitted[i]) 00148 del splitted[:nr_of_coef] 00149 print ranger 00150 print values 00151 i=0 00152 00153 with open("Thermocouple.h", "a") as out: 00154 poly_size = str(len(ranger)) 00155 00156 out.write("class " + class_name + " : public Thermocouple\n{") 00157 out.write("\npublic:\n\t~" + class_name +"();") 00158 out.write("\n\tstatic const thermocouple_poly_subrange inv_poly[" + poly_size + "];\n") 00159 out.write("\tstatic const int inv_poly_size;\n\tfloat convert_inv(float temp);\n\n") 00160 00161 with open("Thermocouple.cpp", "a") as out: 00162 00163 out.write("const int " + class_name + "::inv_poly_size = " + str(len(ranger)) + ";\n") 00164 out.write("const Thermocouple::thermocouple_poly_subrange " + class_name + "::inv_poly[" + poly_size + "] =\n{\n") 00165 for ran in ranger: 00166 poly = [] 00167 power = [] 00168 for val in values[i]: 00169 a = val.split("E") 00170 poly.append(a[0]) 00171 power.append(int(a[1])) 00172 print(poly) 00173 print(power) 00174 out.write("\t{") 00175 for r in ran: 00176 out.write(r.rjust(11)) 00177 out.write("// characteristic curve for temp range between " + ran[0] + " and " + ran[1] + "\n\t{") 00178 for p in poly: 00179 out.write(str(p).rjust(11) + ",") 00180 out.write("},\n") 00181 out.write("\t{") 00182 for p in power: 00183 # print(p) 00184 out.write(str(p).rjust(11) + ",") 00185 out.write("},\n\t") 00186 out.write(str(len(poly))) 00187 out.write(" }") 00188 00189 poly = [] 00190 power = [] 00191 i = i + 1 00192 if i != len(ranger): 00193 out.write(",") 00194 out.write("\n") 00195 out.write("};\n") 00196 00197 out.write( 00198 "\nfloat " + class_name + "::convert_inv(float temp)\n{\n\treturn Thermocouple::convert(temp, inv_poly, inv_poly_size);\n}\n") 00199 out.write( 00200 "\nfloat " + class_name + "::lookup_inv(float temp)\n{""\n#ifdef " + lut_def_name + "\n\tif((temp+lut_offset)>lut_size)\n\t\treturn lut[lut_size-1];\n\telse\n\t\treturn lut[(uint16_t)temp+lut_offset];\n#else\n\t/* NOT IMPLEMENTED */\n\treturn 0;\n#endif\n}\n") 00201 00202 i=0 00203 00204 for word in splitted: 00205 if(word == 'Voltage' and splitted[i-1] != "Temperature"): 00206 break 00207 i=i+1 00208 00209 del splitted[:i+1] 00210 i=0 00211 #print(splitted) 00212 ranger = [] 00213 values = [] 00214 for word in splitted: 00215 00216 if(word == 'Range:'): 00217 break 00218 ranger.append([word]) 00219 #print(i) 00220 i = i + 1 00221 00222 del splitted[:i+1] 00223 for j in range(i): 00224 ranger[j].append(splitted[j]) 00225 values.append([]) 00226 del splitted[:i] 00227 #print(values) 00228 00229 i=0 00230 for word in splitted: 00231 if(word == "Error"): 00232 break 00233 values[i%len(ranger)].append(word) 00234 i=i+1 00235 00236 00237 poly=[] 00238 power = [] 00239 i=0 00240 00241 with open("Thermocouple.h","a") as out: 00242 poly_size = str(len(ranger)) 00243 00244 out.write("\tstatic const thermocouple_poly_subrange poly["+poly_size+"];\n") 00245 out.write("\tstatic const int poly_size;\n\tfloat convert(float voltage);\n") 00246 out.write("#ifdef "+lut_def_name+"\n") 00247 out.write("\tstatic const int32_t lut[];\n\tstatic const int16_t lut_offset;\n\tstatic const uint16_t lut_size;\n\tfloat lookup(float voltage);\n\tfloat lookup_inv(float temp);\n#endif\n};\n\n\n") 00248 00249 00250 with open("Thermocouple.cpp","a") as out: 00251 00252 out.write("const int "+ class_name + "::poly_size = "+str(len(ranger))+";\n") 00253 out.write("const Thermocouple::thermocouple_poly_subrange "+ class_name+ "::poly["+poly_size+"] =\n{\n") 00254 for ran in ranger: 00255 for val in values[i]: 00256 a=val.split("E") 00257 poly.append(a[0]) 00258 power.append(int(a[1])) 00259 print(poly) 00260 print(power) 00261 out.write("\t{") 00262 for r in ran: 00263 out.write(r.rjust(11) +", ") 00264 out.write("// characteristic curve for mV range between " + ran[0] +" and "+ ran[1]+ "\n\t{") 00265 for p in poly: 00266 out.write(str(p).rjust(11) + ",") 00267 out.write("},\n") 00268 out.write("\t{") 00269 for p in power: 00270 # print(p) 00271 out.write(str(p).rjust(11) + ",") 00272 out.write("},\n\t") 00273 out.write(str(len(poly))) 00274 out.write(" }") 00275 00276 poly = [] 00277 power = [] 00278 i=i+1 00279 if i!=len(ranger): 00280 out.write(",") 00281 out.write("\n") 00282 out.write("};\n") 00283 out.write("\n" + class_name + "::~"+class_name+"()\n{\n\n}\n") 00284 out.write("\nfloat "+ class_name +"::convert(float voltage)\n{\n\treturn Thermocouple::convert(voltage, poly, poly_size);\n}\n") 00285 out.write("\nfloat "+class_name+"::lookup(float voltage)\n{""\n#ifdef "+lut_def_name+"\n\treturn Thermocouple::lookup(lut, voltage, lut_size, lut_offset);\n#else\n\t/* NOT IMPLEMENTED */\n\treturn 0;\n#endif\n}\n") 00286 00287 with open("Thermocouple.h", "a") as out: 00288 out.write("\n#endif\n") 00289 00290 00291 00292 00293 00294 #print(ranger) 00295 #print(values) 00296 00297 00298 00299 00300
Generated on Tue Jul 12 2022 17:59:52 by 1.7.2