int16_t temp_get(temp_unit_t unit) { int16_t res; int16_t temp; /* Return if temp sensor driver not initialized */ if (temp_initialized == false) { return EOF; } /* Power up sensor */ TEMP_PORT |= (1 << TEMP_BIT_PWR); /* Init ADC and measure */ adc_init(ADC_CHAN_ADC4, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((res = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } #if MEASURE_ADC2 /* Measure external voltage supply, routed to ADC2 through a 470K/100K divider*/ /* AVCC is 3.3 volts if using external supply, else Vbat which will be lower */ /* Convert result to millivolts assuming AVCC is 3.3 volts, on battery it will be lower! */ adc_init(ADC_CHAN_ADC2, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((ADC2_reading = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } ADC2_reading = (ADC2_reading*((470+100)*3300UL))/(100*1024UL); #endif adc_deinit(); /* Re-init the adc for buttons. */ key_init(); /* Power down sensor */ TEMP_PORT &= ~(1 << TEMP_BIT_PWR); /* Get corresponding temperature from table */ if (unit == TEMP_UNIT_CELCIUS) { temp = find_temp(res, temp_table_celcius, sizeof(temp_table_celcius)/sizeof(int)) + TEMP_ZERO_OFFSET_CELCIUS; } else /*unit == TEMP_UNIT_FAHRENHEIT*/{ temp = find_temp(res, temp_table_fahrenheit, sizeof(temp_table_fahrenheit)/sizeof(int)) + TEMP_ZERO_OFFSET_FAHRENHEIT; } return temp; }
bool remove_exit_monitor(monitor_list *tTable, const client_id *hHandle, entity_handle cClient, text_info nName) { if (!tTable) return false; exit_monitor find_temp(cClient, nName); if (tTable->exits.f_find(find_temp, &exit_monitor_list::find_by_value) == data::not_found) return false; tTable->exits.f_remove_pattern(find_temp, &exit_monitor_list::find_by_value); return true; }
bool add_exit_monitor(monitor_list *tTable, const client_id *hHandle, entity_handle cClient, text_info nName) { if (!tTable) return false; exit_monitor find_temp(cClient, nName); if (tTable->exits.f_find(find_temp, &exit_monitor_list::find_by_value) != data::not_found) return false; tTable->exits.add_element( exit_monitor_list::new_element((entity_handle) hHandle, exit_monitor(cClient, nName)) ); return true; }
static bool get_exit_current_clients(exit_monitor_list &mMonitor, const client_list &cClients, entity_handle cClient, text_info nName, exit_send_list &sSend) { if (!mMonitor.size() || !cClients.size()) return false; exit_monitor_list monitor_copy = mMonitor; exit_monitor find_temp(cClient, nName); monitor_copy.f_keep_pattern(find_temp, &exit_compare); mMonitor.f_remove_pattern(find_temp, &exit_compare); if (!monitor_copy.size()) return false; sSend.f_copy_from(monitor_copy, &convert_exit_to_exit); for (unsigned int I = 0; I < sSend.size(); I++) if (cClients.f_find((entity_handle) sSend[I].key(), &find_client_id) == data::not_found) sSend.remove_single(I--); return sSend.size(); }
static float do_heat_cal(void) { float hot = 0; float cold = 0; float heat = 0; float speed_avr = 0; static float speed_avr_pre = 0; float max_tolerance = 0; int i = 0; int count = 0; for(i=0; i<TEMP_DATA_BUF_LEN; i++) { /* cal temp */ temp_data[i].hot = temp_cal(temp_data[i].data.hot); temp_data[i].cold = temp_cal(temp_data[i].data.cold); #if 1 temp_print(&temp_data[i]); #endif } {/* cal speed */ for(i=0; i<TOF_DATA_BUF_LEN; i++) { tof_data[i].speed = tof_cal(&tof_data[i].data); if((ABS(tof_data[i].speed) < 1.5) && (tof_data[i].speed > 0)){ speed_avr += tof_data[i].speed; count++; } } speed_avr = (count!=0)? (speed_avr/count): speed_avr_pre; } max_tolerance = 0.3 * speed_avr; max_tolerance = ABS(max_tolerance); for(i=0; i<TOF_DATA_BUF_LEN; i++) { /* remove wrong speed */ if(ABS(speed_avr - tof_data[i].speed) > max_tolerance) { tof_data[i].speed = 0; } } {/* cal average speed again */ count = 0; speed_avr = 0; for(i=0; i<TOF_DATA_BUF_LEN; i++) { if(tof_data[i].speed != 0) { speed_avr += tof_data[i].speed; count++; } } speed_avr = (count!=0)? (speed_avr/count): speed_avr_pre; speed_avr_pre = speed_avr; } for(i=0; i<TOF_DATA_BUF_LEN; i++) { /* replace wrong speed */ if(tof_data[i].speed == 0) { tof_data[i].speed = speed_avr; } } for(i=0; i<TOF_DATA_BUF_LEN; i++) { /* correct small value */ if((speed_avr < 0.15)&&(speed_avr > 0.065)) { tof_data[i].speed -= 0.06; } else if(speed_avr < 0.065) { tof_data[i].speed = 0; } #if 1 tof_print(&tof_data[i]); #endif } if((speed_avr < 0.15)&&(speed_avr > 0.1)) { speed_avr -= 0.05; } if((speed_avr < 0.1)&&(speed_avr > 0.06)) { speed_avr -= 0.03; } else if(speed_avr < 0.06) { speed_avr = 0; } #if 1 temp_print_epd(&temp_data[0]); tof_print_epd(speed_avr); #endif for(i=0; i<TOF_DATA_BUF_LEN-1; i++) { find_temp(tof_data[i].time, &hot, &cold); heat += heat_cal((tof_data[i].speed+tof_data[i+1].speed)/2, ticks_to_ms(tof_data[i+1].time-tof_data[i].time), hot, cold); } return heat; //kJ }