1
6
7
13
14
15
16
17
18
19
20
21
22
23
24
25
30
31
32
33
34
35
36
37
38
41
42
43
44
45
46
47
48
49
50
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* ... */
#include <stdio.h>
#include <pico/stdlib.h>
#include <power_status.h>
#include "hardware/adc.h"
#include "pico/float.h"
5 includes
#if CYW43_USES_VSYS_PIN
#include "pico/cyw43_arch.h"
#endif
int main() {
stdio_init_all();
adc_init();
adc_set_temp_sensor_enabled(true);
#if CYW43_USES_VSYS_PIN
if (cyw43_arch_init()) {
printf("failed to initialise\n");
return 1;
}if (cyw43_arch_init()) { ... }
/* ... */ #endif
bool old_battery_status = false;
bool battery_status = true;
float old_voltage = -1;
char *power_str = "UNKNOWN";
while(true) {
if (power_source(&battery_status) == PICO_OK) {
power_str = battery_status ? "BATTERY" : "POWERED";
}if (power_source(&battery_status) == PICO_OK) { ... }
float voltage = 0;
int voltage_return = power_voltage(&voltage);
voltage = floorf(voltage * 100) / 100;
if (old_battery_status != battery_status || old_voltage != voltage) {
char percent_buf[10] = {0};
if (battery_status && voltage_return == PICO_OK) {
const float min_battery_volts = 3.0f;
const float max_battery_volts = 4.2f;
int percent_val = (int) (((voltage - min_battery_volts) / (max_battery_volts - min_battery_volts)) * 100);
snprintf(percent_buf, sizeof(percent_buf), " (%d%%)", percent_val);
}if (battery_status && voltage_return == PICO_OK) { ... }
adc_select_input(4);
const float conversionFactor = 3.3f / (1 << 12);
float adc = (float)adc_read() * conversionFactor;
float tempC = 27.0f - (adc - 0.706f) / 0.001721f;
printf("Power %s, %.2fV%s, temp %.1f DegC\n", power_str, voltage, percent_buf, tempC);
old_battery_status = battery_status;
old_voltage = voltage;
}if (old_battery_status != battery_status || old_voltage != voltage) { ... }
sleep_ms(1000);
}while (true) { ... }
#if CYW43_USES_VSYS_PIN
cyw43_arch_deinit();
#endif
return 0;
}{ ... }