calibration_screen.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * calibration_screen.c
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #include "calibration_screen.h"
  8. #include "settings_screen.h"
  9. #include "oled.h"
  10. #include "gui.h"
  11. typedef enum {cal_250, cal_350, cal_450, cal_suceed, cal_failed, cal_needsAdjust}state_t;
  12. static uint32_t temp;
  13. static bool error;
  14. static uint32_t errorTimer;
  15. static uint32_t screenTimer;
  16. static uint16_t adjustSetPoint;
  17. static uint32_t lastUpdateTick;
  18. static int16_t lastTipTemp;
  19. static uint16_t backupTemp;
  20. static bool backupTempUnit;
  21. static uint16_t backupCal250;
  22. static uint16_t backupCal350;
  23. static uint16_t backupCal450;
  24. const static uint16_t state_temps[3] = {250, 350, 450};
  25. static char* state_tempstr[3] = {"250C", "350C", "450C"};
  26. static uint16_t measured_temps[3];
  27. static uint16_t adcAtTemp[3];
  28. static state_t current_state = cal_250;
  29. static uint8_t tempReady;
  30. static int32_t measuredTemp;
  31. static uint16_t adcCal[3];
  32. static uint8_t processCalibration();
  33. static tipData * Currtip;
  34. screen_t Screen_edit_calibration;
  35. screen_t Screen_edit_calibration_start;
  36. screen_t Screen_edit_calibration_adjust;
  37. screen_t Screen_edit_calibration_input;
  38. static widget_t comboWidget_Cal;
  39. static comboBox_widget_t comboBox_Cal;
  40. static comboBox_item_t Cal_Combo_Start;
  41. static comboBox_item_t Cal_Combo_Adjust;
  42. static comboBox_item_t Cal_Combo_Exit;
  43. static widget_t Widget_Cal_Start_Cancel;
  44. static button_widget_t button_Cal_Start_Cancel;
  45. static widget_t comboWidget_Cal_Adjust;
  46. static comboBox_widget_t comboBox_Cal_Adjust;
  47. static comboBox_item_t Cal_Combo_Adjust_Target;
  48. static comboBox_item_t Cal_Combo_Adjust_Setpoint;
  49. static comboBox_item_t Cal_Combo_Adjust_Save;
  50. static comboBox_item_t Cal_Combo_Adjust_Cancel;
  51. static widget_t Widget_Cal_Adjust_Select;
  52. static editable_widget_t editable_Cal_Adjust_Select;
  53. static widget_t Widget_Cal_Adjust_Setpoint;
  54. static editable_widget_t editable_Cal_Adjust_Setpoint;
  55. static widget_t Widget_Cal_Input_Measured;
  56. static editable_widget_t editable_Cal_Input_Measured;
  57. static widget_t Widget_Cal_Input_Cancel;
  58. static button_widget_t button_Cal_Input_Cancel;
  59. static widget_t Widget_Cal_Input_OK;
  60. static button_widget_t button_Cal_Input_OK;
  61. static void tempReached(uint16_t temp) {
  62. if(temp == state_temps[(int)current_state])
  63. tempReady = 1;
  64. }
  65. static setTemperatureReachedCallback tempReachedCallback = &tempReached;
  66. static void *getMeasuredTemp() {
  67. return &measuredTemp;
  68. }
  69. static void setMeasuredTemp(int32_t *val) {
  70. measuredTemp = *val;
  71. }
  72. //******************************************************
  73. static void *getCalStep() {
  74. temp = current_state;
  75. return &temp;
  76. }
  77. static void setCalStep(int32_t *val) {
  78. current_state = *val;
  79. adjustSetPoint = adcAtTemp[current_state];
  80. setDebugTemp(adjustSetPoint);
  81. }
  82. static void *getAdjustSetpoint() {
  83. temp = adjustSetPoint;
  84. return &temp;
  85. }
  86. static void setAdjustSetpoint(int32_t *val) {
  87. adjustSetPoint = *val;
  88. adcAtTemp[current_state] = *val;
  89. setDebugTemp(*val);
  90. }
  91. static int cal_adjust_SaveAction(widget_t* w) {
  92. systemSettings.Profile.Cal250_default = adcAtTemp[cal_250];
  93. systemSettings.Profile.Cal350_default = adcAtTemp[cal_350];
  94. systemSettings.Profile.Cal450_default = adcAtTemp[cal_450];
  95. systemSettings.Profile.CalNTC = readColdJunctionSensorTemp_x10(mode_Celsius) / 10;
  96. return screen_edit_calibration;
  97. }
  98. static int cal_adjust_CancelAction(widget_t* w) {
  99. return screen_edit_calibration;
  100. }
  101. //***************************************************
  102. static void setCalState(state_t s) {
  103. current_state = s;
  104. if(current_state < cal_suceed) {
  105. setCurrentMode(mode_run);
  106. setUserTemperature(state_temps[(int)s]);
  107. measuredTemp = state_temps[(int)s];
  108. }
  109. else if(current_state == cal_suceed) {
  110. if(processCalibration()){
  111. backupCal250 = adcCal[cal_250];
  112. backupCal350 = adcCal[cal_350];
  113. backupCal450 = adcCal[cal_450];
  114. }
  115. else{
  116. current_state = cal_failed;
  117. }
  118. }
  119. }
  120. static int cancelAction(widget_t* w) {
  121. return screen_edit_calibration;
  122. }
  123. static int okAction(widget_t *w) {
  124. tempReady = 0;
  125. // Abort if the measured temp is >50ºC than requested
  126. if( measuredTemp > (state_temps[current_state]+50)){
  127. setCalState(cal_needsAdjust);
  128. }
  129. else{
  130. measured_temps[current_state] = measuredTemp - (readColdJunctionSensorTemp_x10(mode_Celsius) / 10);
  131. adcAtTemp[(int)current_state] = TIP.last_avg;
  132. setCalState(++current_state);
  133. }
  134. return screen_edit_calibration_start;
  135. }
  136. //-------------------------------------------------------------------------------------------------------------------------------
  137. // Calibration screen functions
  138. //-------------------------------------------------------------------------------------------------------------------------------
  139. static void Cal_onEnter(screen_t *scr) {
  140. screenTimer = HAL_GetTick();
  141. errorTimer=0;
  142. error=0;
  143. if(scr == &Screen_settingsmenu) {
  144. comboResetIndex(&comboWidget_Cal);
  145. }
  146. }
  147. static void Cal_draw(screen_t *scr){
  148. if(error){
  149. if(errorTimer==0){
  150. errorTimer=HAL_GetTick();
  151. FillBuffer(BLACK,fill_dma);
  152. scr->refresh=screenRefresh_alreadyErased;
  153. putStrAligned("ERROR DETECTED!", 10, align_center);
  154. putStrAligned("Aborting...", 25, align_center);
  155. }
  156. }
  157. else{
  158. default_screenDraw(scr);
  159. }
  160. }
  161. static int Cal_ProcessInput(struct screen_t *scr, RE_Rotation_t input, RE_State_t *s) {
  162. if(GetIronError()){
  163. error=1;
  164. }
  165. if(error){
  166. if((errorTimer==0) || ((HAL_GetTick()-errorTimer)<2000) ){
  167. return -1;
  168. }
  169. else{
  170. return screen_settingsmenu;
  171. }
  172. }
  173. else{
  174. if(input!=Rotate_Nothing){
  175. screenTimer=HAL_GetTick();
  176. }
  177. if(input==LongClick || (HAL_GetTick()-screenTimer)>15000){
  178. return screen_main;
  179. }
  180. }
  181. return default_screenProcessInput(scr, input, s);
  182. }
  183. //-------------------------------------------------------------------------------------------------------------------------------
  184. // Calibration start screen functions
  185. //-------------------------------------------------------------------------------------------------------------------------------
  186. static void Cal_Start_onEnter(screen_t *scr) {
  187. if(scr != &Screen_edit_calibration_input) {
  188. Currtip = getCurrentTip();
  189. Iron.calibrating=1;
  190. tempReady = 0;
  191. setCurrentMode(mode_run);
  192. backupTemp = getUserTemperature();
  193. backupTempUnit=systemSettings.settings.tempUnit;
  194. setSystemTempUnit(mode_Celsius);
  195. backupCal250 = Currtip->calADC_At_250;
  196. backupCal350 = Currtip->calADC_At_350;
  197. backupCal450 = Currtip->calADC_At_450;
  198. Currtip->calADC_At_250 = systemSettings.Profile.Cal250_default;
  199. Currtip->calADC_At_350 = systemSettings.Profile.Cal350_default;
  200. Currtip->calADC_At_450 = systemSettings.Profile.Cal450_default;
  201. setCalState(cal_250);
  202. }
  203. }
  204. static int Cal_Start_ProcessInput(struct screen_t *scr, RE_Rotation_t input, RE_State_t *s) {
  205. if(GetIronError()){
  206. return screen_edit_calibration;
  207. }
  208. if(tempReady){
  209. return screen_edit_calibration_input;
  210. }
  211. return default_screenProcessInput(scr, input, s);
  212. }
  213. static void Cal_Start_OnExit(screen_t *scr) {
  214. if((scr != &Screen_edit_calibration_start) && (scr != &Screen_edit_calibration_input)) {
  215. setSystemTempUnit(backupTempUnit);
  216. tempReady = 0;
  217. current_state = cal_250;
  218. setUserTemperature(backupTemp);
  219. setCurrentMode(mode_run);
  220. Iron.calibrating=0;
  221. Currtip->calADC_At_250 = backupCal250;
  222. Currtip->calADC_At_350 = backupCal350;
  223. Currtip->calADC_At_450 = backupCal450;
  224. }
  225. }
  226. static void Cal_Start_draw(screen_t *scr){
  227. if((HAL_GetTick()-lastUpdateTick)>200){ // Refresh every 200mS
  228. lastUpdateTick=HAL_GetTick();
  229. scr->refresh=screenRefresh_eraseNow;
  230. default_screenDraw(scr);
  231. u8g2_SetDrawColor(&u8g2, WHITE);
  232. char waitstr[6];
  233. lastTipTemp = readTipTemperatureCompensated(stored_reading,read_Avg);
  234. switch((int)current_state){
  235. case cal_250:
  236. case cal_350:
  237. case cal_450:
  238. u8g2_DrawStr(&u8g2, 10, 15, "CAL STEP:"); // Draw current cal state
  239. u8g2_DrawStr(&u8g2, 90, 15, state_tempstr[(int)current_state]);
  240. u8g2_DrawStr(&u8g2, 10, 30, "WAIT..."); // Draw current temp
  241. sprintf(waitstr, "%3u\260C",lastTipTemp);
  242. u8g2_DrawStr(&u8g2, 90, 30, waitstr);
  243. break;
  244. case cal_suceed:
  245. {
  246. char str[20];
  247. for(uint8_t x=0;x<3;x++){
  248. sprintf(str, "ADC %s: %u", state_tempstr[x], adcCal[x]);
  249. u8g2_DrawStr(&u8g2, 6, (x*14), str);
  250. }
  251. u8g2_DrawStr(&u8g2, 0, 50, "SUCCEED!");
  252. break;
  253. }
  254. case cal_failed:
  255. putStrAligned("FAILED!", 15, align_center);
  256. break;
  257. case cal_needsAdjust:
  258. putStrAligned("DELTA TOO HIGH!", 0, align_center);
  259. putStrAligned("Adjust manually", 15, align_center);
  260. putStrAligned("and try again", 30, align_center);
  261. break;
  262. }
  263. }
  264. }
  265. //-------------------------------------------------------------------------------------------------------------------------------
  266. // Calibration input screen functions
  267. //-------------------------------------------------------------------------------------------------------------------------------
  268. static void Cal_Input_init(screen_t *scr) {
  269. default_init(scr);
  270. u8g2_SetFont(&u8g2,default_font );
  271. u8g2_SetDrawColor(&u8g2, WHITE);
  272. u8g2_DrawStr(&u8g2,0,19,"MEASURED:");//12
  273. }
  274. static void Cal_Input_OnExit(screen_t *scr) {
  275. button_Cal_Input_OK.selectable.previous_state=widget_idle;;
  276. button_Cal_Input_OK.selectable.state=widget_idle;
  277. button_Cal_Input_Cancel.selectable.previous_state=widget_idle;
  278. button_Cal_Input_Cancel.selectable.state=widget_idle;
  279. editable_Cal_Input_Measured.selectable.previous_state=widget_selected;
  280. editable_Cal_Input_Measured.selectable.state=widget_edit;
  281. Screen_edit_calibration_input.current_widget=&Widget_Cal_Input_Measured;
  282. }
  283. static int Cal_Input_ProcessInput(struct screen_t *scr, RE_Rotation_t input, RE_State_t *s) {
  284. if(GetIronError()){
  285. return screen_edit_calibration;
  286. }
  287. return default_screenProcessInput(scr, input, s);
  288. }
  289. //-------------------------------------------------------------------------------------------------------------------------------
  290. // Calibration adjust screen functions
  291. //-------------------------------------------------------------------------------------------------------------------------------
  292. static void Cal_Adjust_init(screen_t *scr) {
  293. comboResetIndex(&comboWidget_Cal_Adjust);
  294. adcAtTemp[cal_250] = systemSettings.Profile.Cal250_default;
  295. adcAtTemp[cal_350] = systemSettings.Profile.Cal350_default;
  296. adcAtTemp[cal_450] = systemSettings.Profile.Cal450_default;
  297. Cal_Combo_Adjust_Save.enabled=0;
  298. current_state = cal_250;
  299. adjustSetPoint = adcAtTemp[cal_250];
  300. setDebugTemp(adjustSetPoint);
  301. setDebugMode(debug_On);
  302. setCurrentMode(mode_run);
  303. }
  304. static void Cal_Adjust_OnExit(screen_t *scr) {
  305. setDebugMode(debug_Off);
  306. setCurrentMode(mode_run);
  307. }
  308. static int Cal_Adjust_ProcessInput(struct screen_t *scr, RE_Rotation_t input, RE_State_t *s) {
  309. if(GetIronError()){
  310. return screen_edit_calibration;
  311. }
  312. // Only enable save option is valid and different data is entered
  313. if( (adcAtTemp[cal_250] && adcAtTemp[cal_350] && adcAtTemp[cal_450]) &&
  314. ((adcAtTemp[cal_250] != systemSettings.Profile.Cal250_default)||
  315. (adcAtTemp[cal_350] != systemSettings.Profile.Cal350_default)||
  316. (adcAtTemp[cal_450] != systemSettings.Profile.Cal450_default)) &&
  317. ((adcAtTemp[cal_250] < adcAtTemp[cal_350]) && (adcAtTemp[cal_350] < adcAtTemp[cal_450]))){
  318. Cal_Combo_Adjust_Save.enabled=1;
  319. }
  320. else{
  321. Cal_Combo_Adjust_Save.enabled=0;
  322. }
  323. return default_screenProcessInput(scr, input, s);
  324. }
  325. //-------------------------------------------------------------------------------------------------------------------------------
  326. // Calibration screens setup
  327. //-------------------------------------------------------------------------------------------------------------------------------
  328. void calibration_screen_setup(screen_t *scr) {
  329. widget_t* w;
  330. displayOnly_widget_t *dis;
  331. editable_widget_t* edit;
  332. screen_t* sc;
  333. //########################################## CALIBRATION SCREEN ##########################################
  334. //
  335. screen_setDefaults(scr);
  336. scr->onEnter = &Cal_onEnter;
  337. scr->processInput = &Cal_ProcessInput;
  338. scr->draw = &Cal_draw;
  339. addSetTemperatureReachedCallback(tempReachedCallback);
  340. w = &comboWidget_Cal;
  341. w->content = &comboBox_Cal;
  342. screen_addWidget(w, scr);
  343. widgetDefaultsInit(w, widget_combo, &comboBox_Cal);
  344. comboAddScreen(&Cal_Combo_Start, w, "START", screen_edit_calibration_start);
  345. comboAddScreen(&Cal_Combo_Adjust, w, "ADJUST", screen_edit_calibration_adjust);
  346. comboAddScreen(&Cal_Combo_Exit, w, "BACK", screen_settingsmenu);
  347. w->posY=10;
  348. //########################################## CALIBRATION START SCREEN ##########################################
  349. //
  350. sc = &Screen_edit_calibration_start;
  351. oled_addScreen(sc, screen_edit_calibration_start);
  352. screen_setDefaults(sc);
  353. sc->processInput = &Cal_Start_ProcessInput;
  354. sc->draw = &Cal_Start_draw;
  355. sc->onExit = &Cal_Start_OnExit;
  356. sc->onEnter = &Cal_Start_onEnter;
  357. w = &Widget_Cal_Start_Cancel;
  358. screen_addWidget(w,sc);
  359. widgetDefaultsInit(w, widget_button, &button_Cal_Start_Cancel);
  360. button_Cal_Start_Cancel.displayString="BACK";
  361. w->posX = 86;
  362. w->posY = 48;
  363. w->width = 42;
  364. ((button_widget_t*)w->content)->selectable.tab = 0;
  365. ((button_widget_t*)w->content)->action = &cancelAction;
  366. //########################################## CALIBRATION ADJUST SCREEN ##########################################
  367. //
  368. sc = &Screen_edit_calibration_adjust;
  369. oled_addScreen(sc, screen_edit_calibration_adjust);
  370. screen_setDefaults(sc);
  371. sc->init = &Cal_Adjust_init;
  372. sc->processInput = &Cal_Adjust_ProcessInput;
  373. sc->onExit = &Cal_Adjust_OnExit;
  374. w = &Widget_Cal_Adjust_Select;
  375. widgetDefaultsInit(w, widget_multi_option, &editable_Cal_Adjust_Select);
  376. dis=extractDisplayPartFromWidget(w);
  377. edit=extractEditablePartFromWidget(w);
  378. dis->getData = &getCalStep;
  379. edit->big_step = 1;
  380. edit->step = 1;
  381. edit->setData = (void (*)(void *))&setCalStep;
  382. edit->max_value = cal_450;
  383. edit->min_value = cal_250 ;
  384. edit->options = state_tempstr;
  385. edit->numberOfOptions = 3;
  386. w->posX = 20;
  387. w->posY = 20;
  388. w->width = 40;
  389. w = &Widget_Cal_Adjust_Setpoint;
  390. widgetDefaultsInit(w, widget_editable, &editable_Cal_Adjust_Setpoint);
  391. dis=extractDisplayPartFromWidget(w);
  392. edit=extractEditablePartFromWidget(w);
  393. dis->reservedChars=4;
  394. dis->getData = &getAdjustSetpoint;
  395. edit->big_step = 100;
  396. edit->step = 50;
  397. edit->setData = (void (*)(void *))&setAdjustSetpoint;
  398. edit->max_value = 4000;
  399. edit->min_value = 0;
  400. w->posX = 80;
  401. w->posY = 20;
  402. w->width = 40;
  403. w = &comboWidget_Cal_Adjust;
  404. screen_addWidget(w, sc);
  405. widgetDefaultsInit(w, widget_combo, &comboBox_Cal_Adjust);
  406. comboAddMultiOption(&Cal_Combo_Adjust_Target, w, "Cal. Step", &editable_Cal_Adjust_Select);
  407. comboAddEditable(&Cal_Combo_Adjust_Setpoint, w, "ADC Value", &editable_Cal_Adjust_Setpoint);
  408. comboAddAction(&Cal_Combo_Adjust_Save, w, "SAVE", &cal_adjust_SaveAction);
  409. comboAddAction(&Cal_Combo_Adjust_Cancel, w, "CANCEL", &cal_adjust_CancelAction);
  410. //########################################## CALIBRATION INPUT SCREEN ##########################################
  411. //
  412. sc = &Screen_edit_calibration_input;
  413. oled_addScreen(sc, screen_edit_calibration_input);
  414. screen_setDefaults(sc);
  415. sc->init = &Cal_Input_init;
  416. sc->onExit = &Cal_Input_OnExit;;
  417. sc->processInput = &Cal_Input_ProcessInput;
  418. w=&Widget_Cal_Input_Measured;
  419. widgetDefaultsInit(w, widget_editable, &editable_Cal_Input_Measured);
  420. screen_addWidget(w,sc);
  421. dis=extractDisplayPartFromWidget(w);
  422. edit=extractEditablePartFromWidget(w);
  423. dis->reservedChars = 5;
  424. dis->endString = "\260C";
  425. w->posX = 86;
  426. w->posY = 17;
  427. w->width = 42;
  428. dis->getData = &getMeasuredTemp;
  429. edit->setData = (void (*)(void *)) &setMeasuredTemp;
  430. edit->selectable.tab = 0;
  431. w=&Widget_Cal_Input_OK;
  432. screen_addWidget(w,sc);
  433. widgetDefaultsInit(w, widget_button, &button_Cal_Input_OK);
  434. button_Cal_Input_OK.displayString="SAVE";
  435. ((button_widget_t*)w->content)->selectable.tab = 1;
  436. ((button_widget_t*)w->content)->action = &okAction;
  437. w->posX = 86;
  438. w->posY = 48;
  439. w->width = 42;
  440. w=&Widget_Cal_Input_Cancel;
  441. screen_addWidget(w,sc);
  442. widgetDefaultsInit(w, widget_button, &button_Cal_Input_Cancel);
  443. button_Cal_Input_Cancel.displayString="CANCEL";
  444. ((button_widget_t*)w->content)->selectable.tab = 2;
  445. ((button_widget_t*)w->content)->action = &cancelAction;
  446. w->posX = 0;
  447. w->posY = 48;
  448. w->width = 56;
  449. }
  450. static uint8_t processCalibration() {
  451. uint16_t delta = (state_temps[cal_350] - state_temps[cal_250])/2;
  452. uint16_t ambient = readColdJunctionSensorTemp_x10(mode_Celsius) / 10;
  453. // Ensure measured temps are valid (250<350<450)
  454. if ( (measured_temps[cal_250] >= measured_temps[cal_350]) || (measured_temps[cal_350] >= measured_temps[cal_450]) ){
  455. return 0;
  456. }
  457. // Ensure adc values are valid (250<350<450)
  458. if ( (adcAtTemp[cal_250] >=adcAtTemp[cal_350]) || (adcAtTemp[cal_350] >= adcAtTemp[cal_450]) ){
  459. return 0;
  460. }
  461. if ((measured_temps[cal_250] + delta) < measured_temps[cal_350]){
  462. adcCal[cal_350] = map(state_temps[cal_350], measured_temps[cal_250], measured_temps[cal_350], adcAtTemp[cal_250], adcAtTemp[cal_350]);
  463. }
  464. else{
  465. adcCal[cal_350] = map(state_temps[cal_350], ambient, measured_temps[cal_350], 0, adcAtTemp[cal_350]);
  466. }
  467. adcCal[cal_350] += map(state_temps[cal_350], measured_temps[cal_250], measured_temps[cal_450], adcAtTemp[cal_250], adcAtTemp[cal_450]) + 1;
  468. adcCal[cal_350] >>= 1;
  469. if((measured_temps[cal_250] + delta) < measured_temps[cal_350]){
  470. adcCal[cal_250] = map(state_temps[cal_250], measured_temps[cal_250], state_temps[cal_350], adcAtTemp[cal_250], adcCal[cal_350]);
  471. }
  472. else{
  473. adcCal[cal_250] = map(state_temps[cal_250], ambient, state_temps[cal_350], 0, adcCal[cal_350]);
  474. }
  475. if ((measured_temps[cal_350] + delta) < measured_temps[cal_450]){
  476. adcCal[cal_450] = map(state_temps[cal_450], state_temps[cal_350], measured_temps[cal_450], adcCal[cal_350], adcAtTemp[cal_450]);
  477. }
  478. else{
  479. adcCal[cal_450] = map(state_temps[cal_450], state_temps[cal_250], measured_temps[cal_450], adcCal[cal_350], adcAtTemp[cal_450]);
  480. }
  481. if (((adcCal[cal_250] + delta) > adcCal[cal_350]) || ((adcCal[cal_350] + delta) > adcCal[cal_450])) {
  482. adcCal[cal_250] = map(state_temps[cal_250], measured_temps[cal_250], measured_temps[cal_450], adcAtTemp[cal_250], adcAtTemp[cal_450]);
  483. adcCal[cal_350] = map(state_temps[cal_350], measured_temps[cal_250], measured_temps[cal_450], adcAtTemp[cal_250], adcAtTemp[cal_450]);
  484. adcCal[cal_450] = map(state_temps[cal_450], measured_temps[cal_250], measured_temps[cal_450], adcAtTemp[cal_250], adcAtTemp[cal_450]);
  485. }
  486. if(adcCal[cal_250]>4090 || adcCal[cal_350]>4090 || adcCal[cal_450]>4090){ // Check that values don't exceed ADC range
  487. return 0;
  488. }
  489. return 1;
  490. }