mch2022-template-app/main/main.c

85 lines
2.8 KiB
C
Raw Normal View History

/*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
2022-05-30 21:56:23 +00:00
2022-06-08 14:24:28 +00:00
#include "main.h"
2022-05-16 21:07:30 +00:00
static pax_buf_t buf;
2022-06-08 14:24:28 +00:00
xQueueHandle buttonQueue;
2022-06-08 14:24:28 +00:00
static const char *TAG = "mch2022-demo-app";
2022-07-02 18:55:10 +00:00
// Updates the screen with the last drawing.
2022-06-08 14:24:28 +00:00
void disp_flush() {
ili9341_write(get_ili9341(), buf.buf);
2022-06-06 02:01:49 +00:00
}
2022-07-02 18:55:10 +00:00
// Exits the app, returning to the launcher.
void exit_to_launcher() {
REG_WRITE(RTC_CNTL_STORE0_REG, 0);
esp_restart();
}
2022-06-08 14:24:28 +00:00
void app_main() {
2022-07-02 18:55:10 +00:00
// Init the screen, the I2C and the SPI busses.
2022-06-08 14:24:28 +00:00
bsp_init();
2022-07-02 18:55:10 +00:00
// Init the RP2040 (responsible for buttons among other important things).
2022-06-08 14:24:28 +00:00
bsp_rp2040_init();
2022-07-02 18:55:10 +00:00
// This queue is used to await button presses.
2022-06-08 14:24:28 +00:00
buttonQueue = get_rp2040()->queue;
2022-07-02 18:55:10 +00:00
// Init graphics for the screen.
pax_buf_init(&buf, NULL, 320, 240, PAX_BUF_16_565RGB);
2022-06-05 23:07:58 +00:00
2022-06-20 21:10:24 +00:00
// Init NVS.
nvs_flash_init();
// Init (but not connect to) WiFi.
wifi_init();
while (1) {
// Pick a random background color.
int hue = esp_random() & 255;
pax_col_t col = pax_col_hsv(hue, 255 /*saturation*/, 255 /*brighness*/);
// Show some random color hello world.
2022-07-02 18:55:10 +00:00
// Draw the background with aforementioned random color.
pax_background(&buf, col);
2022-07-02 18:55:10 +00:00
// This text is shown on screen.
char *text = "Hello, World!";
2022-07-02 18:55:10 +00:00
// Pick the font to draw in (this is the only one that looks nice this big).
2022-07-03 11:30:30 +00:00
const pax_font_t *font = pax_font_saira_condensed;
2022-07-02 18:55:10 +00:00
// Determine how large the text is so it can be centered.
pax_vec1_t dims = pax_text_size(font, font->default_size, text);
2022-07-02 18:55:10 +00:00
// Use info to draw the centered text.
pax_draw_text(
2022-07-02 18:55:10 +00:00
// Buffer to draw to.
&buf, 0xff000000,
// Font and size to use.
font, font->default_size,
// Position (top left corner) of the app.
(buf.width - dims.x) / 2.0,
(buf.height - dims.y) / 2.0,
2022-07-02 18:55:10 +00:00
// The text to be shown.
text
);
2022-07-02 18:55:10 +00:00
// Draws the entire graphics buffer to the screen.
disp_flush();
// Await any button press and do another cycle.
2022-07-02 18:55:10 +00:00
// Structure used to receive data.
rp2040_input_message_t message;
2022-07-02 18:55:10 +00:00
// Await forever (because of portMAX_DELAY), a button press.
xQueueReceive(buttonQueue, &message, portMAX_DELAY);
2022-07-02 18:55:10 +00:00
// Is the home button currently pressed?
if (message.input == RP2040_INPUT_BUTTON_HOME && message.state) {
// If home is pressed, exit to launcher.
exit_to_launcher();
}
}
}