mch2022-talktopics/main/main.c

102 lines
3 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-07-11 07:54:18 +00:00
// This file contains a simple Hello World app which you can base you own
// native Badge apps on.
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-07-11 07:54:18 +00:00
#include <esp_log.h>
2022-06-08 14:24:28 +00:00
static const char *TAG = "mch2022-demo-app";
2022-07-11 07:54:18 +00:00
// Updates the screen with the latest buffer.
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-11 07:54:18 +00:00
ESP_LOGI(TAG, "Welcome to the template app!");
// Initialize the screen, the I2C and the SPI busses.
2022-06-08 14:24:28 +00:00
bsp_init();
2022-07-11 07:54:18 +00:00
// Initialize the RP2040 (responsible for buttons, etc).
2022-06-08 14:24:28 +00:00
bsp_rp2040_init();
2022-07-11 07:54:18 +00:00
// This queue is used to receive button presses.
2022-06-08 14:24:28 +00:00
buttonQueue = get_rp2040()->queue;
2022-07-11 07:54:18 +00:00
// Initialize graphics for the screen.
pax_buf_init(&buf, NULL, 320, 240, PAX_BUF_16_565RGB);
2022-06-05 23:07:58 +00:00
2022-07-11 07:54:18 +00:00
// Initialize NVS.
2022-06-20 21:10:24 +00:00
nvs_flash_init();
2022-07-11 07:54:18 +00:00
// Initialize WiFi. This doesn't connect to Wifi yet.
2022-06-20 21:10:24 +00:00
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*/);
2022-07-11 07:54:18 +00:00
// Greet the World in front of a random background color!
// Fill the background with the random color.
pax_background(&buf, col);
2022-07-11 07:54:18 +00:00
2022-07-02 18:55:10 +00:00
// This text is shown on screen.
2022-07-11 07:54:18 +00:00
char *text = "Hello, MCH2022!";
// Pick the font (Saira is the only one that looks nice in this size).
2022-07-03 11:30:30 +00:00
const pax_font_t *font = pax_font_saira_condensed;
2022-07-11 07:54:18 +00:00
// Determine how the text dimensions so we can display it centered on
// screen.
pax_vec1_t dims = pax_text_size(font, font->default_size, text);
2022-07-11 07:54:18 +00:00
// Draw the centered text.
pax_draw_text(
2022-07-11 07:54:18 +00:00
&buf, // Buffer to draw to.
0xff000000, // color
font, font->default_size, // Font and size to use.
2022-07-02 18:55:10 +00:00
// Position (top left corner) of the app.
(buf.width - dims.x) / 2.0,
(buf.height - dims.y) / 2.0,
2022-07-11 07:54:18 +00:00
// The text to be rendered.
text
);
2022-07-11 07:54:18 +00:00
2022-07-02 18:55:10 +00:00
// Draws the entire graphics buffer to the screen.
disp_flush();
2022-07-11 07:54:18 +00:00
// Wait for button presses and do another cycle.
2022-07-02 18:55:10 +00:00
// Structure used to receive data.
rp2040_input_message_t message;
2022-07-11 07:54:18 +00:00
// Wait forever for a button press (because of portMAX_DELAY)
xQueueReceive(buttonQueue, &message, portMAX_DELAY);
2022-07-11 07:54:18 +00:00
// Which button is currently pressed?
if (message.input == RP2040_INPUT_BUTTON_HOME && message.state) {
// If home is pressed, exit to launcher.
exit_to_launcher();
}
}
}