Merge pull request #58 from badgeteam/renze/adc

Add ADC reading
This commit is contained in:
Renze Nicolai 2022-06-04 16:20:37 +02:00 committed by GitHub
commit f16963c176
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 166 additions and 23 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;
}
}
}
}
}

View file

@ -22,6 +22,19 @@ void test_buttons(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9
bool btn_start = false; bool btn_start = false;
bool btn_accept = false; bool btn_accept = false;
bool btn_back = false; bool btn_back = false;
bool btn_joy_down_green = false;
bool btn_joy_up_green = false;
bool btn_joy_left_green = false;
bool btn_joy_right_green = false;
bool btn_joy_press_green = false;
bool btn_home_green = false;
bool btn_menu_green = false;
bool btn_select_green = false;
bool btn_start_green = false;
bool btn_accept_green = false;
bool btn_back_green = false;
while (!quit) { while (!quit) {
rp2040_input_message_t buttonMessage = {0}; rp2040_input_message_t buttonMessage = {0};
@ -32,36 +45,47 @@ void test_buttons(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9
switch(pin) { switch(pin) {
case RP2040_INPUT_JOYSTICK_DOWN: case RP2040_INPUT_JOYSTICK_DOWN:
btn_joy_down = value; btn_joy_down = value;
if (value) btn_joy_down_green = true;
break; break;
case RP2040_INPUT_JOYSTICK_UP: case RP2040_INPUT_JOYSTICK_UP:
btn_joy_up = value; btn_joy_up = value;
if (value) btn_joy_up_green = true;
break; break;
case RP2040_INPUT_JOYSTICK_LEFT: case RP2040_INPUT_JOYSTICK_LEFT:
btn_joy_left = value; btn_joy_left = value;
if (value) btn_joy_left_green = true;
break; break;
case RP2040_INPUT_JOYSTICK_RIGHT: case RP2040_INPUT_JOYSTICK_RIGHT:
btn_joy_right = value; btn_joy_right = value;
if (value) btn_joy_right_green = true;
break; break;
case RP2040_INPUT_JOYSTICK_PRESS: case RP2040_INPUT_JOYSTICK_PRESS:
btn_joy_press = value; btn_joy_press = value;
if (value) btn_joy_press_green = true;
break; break;
case RP2040_INPUT_BUTTON_HOME: case RP2040_INPUT_BUTTON_HOME:
btn_home = value; btn_home = value;
if (value) btn_home_green = true;
break; break;
case RP2040_INPUT_BUTTON_MENU: case RP2040_INPUT_BUTTON_MENU:
btn_menu = value; btn_menu = value;
if (value) btn_menu_green = true;
break; break;
case RP2040_INPUT_BUTTON_SELECT: case RP2040_INPUT_BUTTON_SELECT:
btn_select = value; btn_select = value;
if (value) btn_select_green = true;
break; break;
case RP2040_INPUT_BUTTON_START: case RP2040_INPUT_BUTTON_START:
btn_start = value; btn_start = value;
if (value) btn_start_green = true;
break; break;
case RP2040_INPUT_BUTTON_ACCEPT: case RP2040_INPUT_BUTTON_ACCEPT:
btn_accept = value; btn_accept = value;
if (value) btn_accept_green = true;
break; break;
case RP2040_INPUT_BUTTON_BACK: case RP2040_INPUT_BUTTON_BACK:
btn_back = value; btn_back = value;
if (value) btn_back_green = true;
default: default:
break; break;
} }
@ -73,27 +97,27 @@ void test_buttons(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, "Press HOME + START to exit"); pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, "Press HOME + START to exit");
char buffer[64]; char buffer[64];
snprintf(buffer, sizeof(buffer), "JOY DOWN %s", btn_joy_down ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "JOY DOWN %s", btn_joy_down ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*1, buffer); pax_draw_text(pax_buffer, btn_joy_down_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*1, buffer);
snprintf(buffer, sizeof(buffer), "JOY UP %s", btn_joy_up ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "JOY UP %s", btn_joy_up ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*2, buffer); pax_draw_text(pax_buffer, btn_joy_up_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*2, buffer);
snprintf(buffer, sizeof(buffer), "JOY LEFT %s", btn_joy_left ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "JOY LEFT %s", btn_joy_left ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*3, buffer); pax_draw_text(pax_buffer, btn_joy_right_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*3, buffer);
snprintf(buffer, sizeof(buffer), "JOY RIGHT %s", btn_joy_right ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "JOY RIGHT %s", btn_joy_right ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*4, buffer); pax_draw_text(pax_buffer, btn_joy_left_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*4, buffer);
snprintf(buffer, sizeof(buffer), "JOY PRESS %s", btn_joy_press ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "JOY PRESS %s", btn_joy_press ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*5, buffer); pax_draw_text(pax_buffer, btn_joy_press_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*5, buffer);
snprintf(buffer, sizeof(buffer), "BTN HOME %s", btn_home ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "BTN HOME %s", btn_home ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*6, buffer); pax_draw_text(pax_buffer, btn_home_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*6, buffer);
snprintf(buffer, sizeof(buffer), "BTN MENU %s", btn_menu ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "BTN MENU %s", btn_menu ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*7, buffer); pax_draw_text(pax_buffer, btn_menu_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*7, buffer);
snprintf(buffer, sizeof(buffer), "BTN SELECT %s", btn_select ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "BTN SELECT %s", btn_select ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*8, buffer); pax_draw_text(pax_buffer, btn_select_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*8, buffer);
snprintf(buffer, sizeof(buffer), "BTN START %s", btn_start ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "BTN START %s", btn_start ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*9, buffer); pax_draw_text(pax_buffer, btn_start_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*9, buffer);
snprintf(buffer, sizeof(buffer), "BTN A %s", btn_accept ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "BTN A %s", btn_accept ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*10, buffer); pax_draw_text(pax_buffer, btn_accept_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*10, buffer);
snprintf(buffer, sizeof(buffer), "BTN B %s", btn_back ? "PRESSED" : "released"); snprintf(buffer, sizeof(buffer), "BTN B %s", btn_back ? "PRESSED" : "released");
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*11, buffer); pax_draw_text(pax_buffer, btn_back_green ? 0xFF00FF00 : 0xFFFFFFFF, NULL, 18, 0, 20*11, buffer);
ili9341_write(ili9341, pax_buffer->buf); ili9341_write(ili9341, pax_buffer->buf);
render = false; render = false;
} }

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);
} }
} }

Binary file not shown.