Clean up onboard_temperature example slightly, and let i tbuild when there is no default LED

This commit is contained in:
graham sanderson 2022-05-17 19:43:30 -05:00
parent 6a747b3b71
commit 2a11e9f8f2

View File

@ -20,8 +20,8 @@ float read_onboard_temperature(const char unit) {
/* 12-bit conversion, assume max value == ADC_VREF == 3.3 V */ /* 12-bit conversion, assume max value == ADC_VREF == 3.3 V */
const float conversionFactor = 3.3f / (1 << 12); const float conversionFactor = 3.3f / (1 << 12);
float adc = adc_read() * conversionFactor; float adc = (float)adc_read() * conversionFactor;
float tempC = 27.0 - (adc - 0.706) / 0.001721; float tempC = 27.0f - (adc - 0.706f) / 0.001721f;
if (unit == 'C') { if (unit == 'C') {
return tempC; return tempC;
@ -29,13 +29,15 @@ float read_onboard_temperature(const char unit) {
return tempC * 9 / 5 + 32; return tempC * 9 / 5 + 32;
} }
return -1.0; return -1.0f;
} }
int main() { int main() {
stdio_init_all(); stdio_init_all();
#ifdef PICO_DEFAULT_LED_PIN
gpio_init(PICO_DEFAULT_LED_PIN); gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
#endif
/* Initialize hardware AD converter, enable onboard temperature sensor and /* Initialize hardware AD converter, enable onboard temperature sensor and
* select its channel (do this once for efficiency, but beware that this * select its channel (do this once for efficiency, but beware that this
@ -48,10 +50,12 @@ int main() {
float temperature = read_onboard_temperature(TEMPERATURE_UNITS); float temperature = read_onboard_temperature(TEMPERATURE_UNITS);
printf("Onboard temperature = %.02f %c\n", temperature, TEMPERATURE_UNITS); printf("Onboard temperature = %.02f %c\n", temperature, TEMPERATURE_UNITS);
#ifdef PICO_DEFAULT_LED_PIN
gpio_put(PICO_DEFAULT_LED_PIN, 1); gpio_put(PICO_DEFAULT_LED_PIN, 1);
sleep_ms(10); sleep_ms(10);
gpio_put(PICO_DEFAULT_LED_PIN, 0); gpio_put(PICO_DEFAULT_LED_PIN, 0);
#endif
sleep_ms(990); sleep_ms(990);
} }