Add ADC reading

This commit is contained in:
Renze Nicolai 2022-06-04 02:30:23 +02:00
parent 6eaa8ccd89
commit 3da15dd4ed
6 changed files with 131 additions and 12 deletions

@ -1 +1 @@
Subproject commit 2882f91df13c6155062b6c94871fdf68e1c62675 Subproject commit dce32a7f6128b8f405dc7a424595ab9619dd84d6

View file

@ -24,6 +24,7 @@ idf_component_register(
"factory_test.c" "factory_test.c"
"animation.c" "animation.c"
"button_test.c" "button_test.c"
"adc_test.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
"include" "include"
"menus" "menus"

76
main/adc_test.c Normal file
View file

@ -0,0 +1,76 @@
#include <stdio.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "ili9341.h"
#include "pax_gfx.h"
#include "rp2040.h"
#include "hardware.h"
void test_adc(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) {
bool quit = false;
RP2040* rp2040 = get_rp2040();
const pax_font_t* font = pax_get_font("saira regular");
while (!quit) {
bool error = false;
float vbat = 0;
if (rp2040_read_vbat(rp2040, &vbat) != ESP_OK) {
error = true;
}
float vusb = 0;
if (rp2040_read_vusb(rp2040, &vusb) != ESP_OK) {
error = true;
}
/*uint16_t raw_temperature = 0;
if (rp2040_read_temp(rp2040, &raw_temperature) != ESP_OK) {
error = true;
}
uint8_t charging = 0;
if (rp2040_get_charging(rp2040, &charging) != ESP_OK) {
error = true;
}*/
//const float conversion_factor = 3.3f / (1 << 12); // 12-bit ADC with 3.3v vref
//float vtemperature = raw_temperature * conversion_factor; // Inside of RP2040 chip
//float temperature = 27 - (vtemperature - 0.706) / 0.001721; // From https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__adc.html
pax_noclip(pax_buffer);
pax_background(pax_buffer, 0x325aa8);
pax_draw_text(pax_buffer, 0xFFFFFFFF, font, 18, 0, 20*0, "Analog inputs");
char buffer[64];
snprintf(buffer, sizeof(buffer), "Battery voltage %f v", vbat);
pax_draw_text(pax_buffer, 0xFFFFFFFF, font, 18, 0, 20*1, buffer);
snprintf(buffer, sizeof(buffer), "USB voltage %f v", vusb);
pax_draw_text(pax_buffer, 0xFFFFFFFF, font, 18, 0, 20*2, buffer);
/*snprintf(buffer, sizeof(buffer), "Temperature %f *c", temperature);
pax_draw_text(pax_buffer, 0xFFFFFFFF, font, 18, 0, 20*3, buffer);
snprintf(buffer, sizeof(buffer), "Charging %02X", charging);
pax_draw_text(pax_buffer, 0xFFFFFFFF, font, 18, 0, 20*4, buffer);*/
pax_draw_text(pax_buffer, 0xFFFFFFFF, font, 18, 0, 20*5, (error ? "ERROR" : ""));
ili9341_write(ili9341, pax_buffer->buf);
rp2040_input_message_t buttonMessage = {0};
if (xQueueReceive(buttonQueue, &buttonMessage, 250 / portTICK_PERIOD_MS) == pdTRUE) {
uint8_t pin = buttonMessage.input;
bool value = buttonMessage.state;
if (value) {
switch(pin) {
case RP2040_INPUT_BUTTON_HOME:
case RP2040_INPUT_BUTTON_BACK:
quit = true;
default:
break;
}
}
}
}
}

9
main/include/adc_test.h Normal file
View file

@ -0,0 +1,9 @@
#include <stdio.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include "ili9341.h"
#include "pax_gfx.h"
void test_adc(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341);

View file

@ -22,6 +22,7 @@
#include "fpga_test.h" #include "fpga_test.h"
#include "animation.h" #include "animation.h"
#include "button_test.h" #include "button_test.h"
#include "adc_test.h"
extern const uint8_t dev_png_start[] asm("_binary_dev_png_start"); extern const uint8_t dev_png_start[] asm("_binary_dev_png_start");
extern const uint8_t dev_png_end[] asm("_binary_dev_png_end"); extern const uint8_t dev_png_end[] asm("_binary_dev_png_end");
@ -34,7 +35,8 @@ typedef enum action {
ACTION_FILE_BROWSER, ACTION_FILE_BROWSER,
ACTION_FILE_BROWSER_INT, ACTION_FILE_BROWSER_INT,
ACTION_ANIMATION, ACTION_ANIMATION,
ACTION_BUTTON_TEST ACTION_BUTTON_TEST,
ACTION_ADC_TEST
} menu_dev_action_t; } menu_dev_action_t;
void render_dev_help(pax_buf_t* pax_buffer) { void render_dev_help(pax_buf_t* pax_buffer) {
@ -68,6 +70,7 @@ void menu_dev(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341)
menu_insert_item(menu, "File browser (internal)", NULL, (void*) ACTION_FILE_BROWSER_INT, -1); menu_insert_item(menu, "File browser (internal)", NULL, (void*) ACTION_FILE_BROWSER_INT, -1);
menu_insert_item(menu, "Animation", NULL, (void*) ACTION_ANIMATION, -1); menu_insert_item(menu, "Animation", NULL, (void*) ACTION_ANIMATION, -1);
menu_insert_item(menu, "Button test", NULL, (void*) ACTION_BUTTON_TEST, -1); menu_insert_item(menu, "Button test", NULL, (void*) ACTION_BUTTON_TEST, -1);
menu_insert_item(menu, "Analog inputs", NULL, (void*) ACTION_ADC_TEST, -1);
bool render = true; bool render = true;
menu_dev_action_t action = ACTION_NONE; menu_dev_action_t action = ACTION_NONE;
@ -130,6 +133,8 @@ void menu_dev(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341)
display_animation(pax_buffer, ili9341); display_animation(pax_buffer, ili9341);
} else if (action == ACTION_BUTTON_TEST) { } else if (action == ACTION_BUTTON_TEST) {
test_buttons(buttonQueue, pax_buffer, ili9341); test_buttons(buttonQueue, pax_buffer, ili9341);
} else if (action == ACTION_ADC_TEST) {
test_adc(buttonQueue, pax_buffer, ili9341);
} else if (action == ACTION_BACK) { } else if (action == ACTION_BACK) {
break; break;
} }

View file

@ -17,6 +17,8 @@
#include "settings.h" #include "settings.h"
#include "dev.h" #include "dev.h"
#include "bootscreen.h" #include "bootscreen.h"
#include "hardware.h"
#include "math.h"
extern const uint8_t home_png_start[] asm("_binary_home_png_start"); extern const uint8_t home_png_start[] asm("_binary_home_png_start");
extern const uint8_t home_png_end[] asm("_binary_home_png_end"); extern const uint8_t home_png_end[] asm("_binary_home_png_end");
@ -41,17 +43,13 @@ typedef enum action {
ACTION_SETTINGS ACTION_SETTINGS
} menu_start_action_t; } menu_start_action_t;
void render_start_help(pax_buf_t* pax_buffer, const char* version) { void render_start_help(pax_buf_t* pax_buffer, const char* text) {
const pax_font_t *font = pax_get_font("saira regular"); const pax_font_t *font = pax_get_font("saira regular");
pax_background(pax_buffer, 0xFFFFFF); pax_background(pax_buffer, 0xFFFFFF);
pax_noclip(pax_buffer); pax_noclip(pax_buffer);
pax_draw_text(pax_buffer, 0xFF491d88, font, 18, 5, 240 - 18, "[A] accept"); pax_draw_text(pax_buffer, 0xFF491d88, font, 18, 5, 240 - 18, "[A] accept");
pax_vec1_t version_size = pax_text_size(font, 18, text);
char version_text[64]; pax_draw_text(pax_buffer, 0xFF491d88, font, 18, 320 - 5 - version_size.x, 240 - 18, text);
snprintf(version_text, sizeof(version_text), "v%s", version);
pax_vec1_t version_size = pax_text_size(font, 18, version_text);
pax_draw_text(pax_buffer, 0xFF491d88, font, 18, 320 - 5 - version_size.x, 240 - 18, version_text);
} }
void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, const char* version) { void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341, const char* version) {
@ -88,12 +86,21 @@ void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili934
bool render = true; bool render = true;
menu_start_action_t action = ACTION_NONE; menu_start_action_t action = ACTION_NONE;
uint8_t analogReadTimer = 0;
float battery_voltage = 0;
float usb_voltage = 0;
//uint8_t rp2040_usb = 0;
render_start_help(pax_buffer, version); // Calculated:
uint8_t battery_percent = 0;
bool battery_charging = false;
RP2040* rp2040 = get_rp2040();
while (1) { while (1) {
rp2040_input_message_t buttonMessage = {0}; rp2040_input_message_t buttonMessage = {0};
if (xQueueReceive(buttonQueue, &buttonMessage, 16 / portTICK_PERIOD_MS) == pdTRUE) { if (xQueueReceive(buttonQueue, &buttonMessage, 100 / portTICK_PERIOD_MS) == pdTRUE) {
uint8_t pin = buttonMessage.input; uint8_t pin = buttonMessage.input;
bool value = buttonMessage.state; bool value = buttonMessage.state;
switch(pin) { switch(pin) {
@ -122,7 +129,29 @@ void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili934
} }
} }
if (analogReadTimer > 0) {
analogReadTimer--;
} else {
analogReadTimer = 10; // No need to update these values really quickly
if (rp2040_read_vbat(rp2040, &battery_voltage) != ESP_OK) {
battery_voltage = 0;
}
if (rp2040_read_vusb(rp2040, &usb_voltage) != ESP_OK) {
usb_voltage = 0;
}
battery_percent = ((battery_voltage - 3.7) * 100) / (4.1 - 3.7);
if (battery_percent > 100) battery_percent = 100;
battery_charging = (usb_voltage > 4.0) && (battery_percent < 100);
render = true;
}
if (render) { if (render) {
char textBuffer[64];
snprintf(textBuffer, sizeof(textBuffer), "B%1.1fv U%1.1fv %03u%%%c v%s", battery_voltage, usb_voltage, battery_percent, battery_charging ? '+' : ' ', version);
render_start_help(pax_buffer, textBuffer);
menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF491d88); menu_render(pax_buffer, menu, 0, 0, 320, 220, 0xFF491d88);
ili9341_write(ili9341, pax_buffer->buf); ili9341_write(ili9341, pax_buffer->buf);
render = false; render = false;
@ -141,7 +170,6 @@ void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili934
} }
action = ACTION_NONE; action = ACTION_NONE;
render = true; render = true;
render_start_help(pax_buffer, version);
} }
} }