MVP: Basic working clock and alarm

This commit is contained in:
ookami125 2025-05-27 02:40:50 +00:00
commit b6ac19a54d
18 changed files with 22909 additions and 0 deletions

12
.devcontainer/Dockerfile Normal file
View file

@ -0,0 +1,12 @@
ARG DOCKER_TAG=release-v5.4
FROM espressif/idf:${DOCKER_TAG}
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
RUN apt-get update -y && apt-get install udev -y
RUN echo "source /opt/esp/idf/export.sh > /dev/null 2>&1" >> ~/.bashrc
ENTRYPOINT [ "/opt/esp/entrypoint-adf.sh" ]
CMD ["/bin/bash", "-c"]

View file

@ -0,0 +1,21 @@
{
"name": "ESP-IDF QEMU",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",
"idf.espIdfPath": "/opt/esp/idf",
"idf.toolsPath": "/opt/esp",
"idf.gitPath": "/usr/bin/git"
},
"extensions": [
"espressif.esp-idf-extension",
"espressif.esp-idf-web"
]
}
},
"runArgs": ["--privileged"]
}

24
.gitignore vendored Normal file
View file

@ -0,0 +1,24 @@
.vscode/
.pytest_cache/
__pycache__/
# esp-idf built binaries
build/
build_*_*/
sdkconfig
sdkconfig.old
# idf-ci build run output
build_summary_*.xml
app_info_*.txt
size_info_*.txt
# pytest-embedded log folder
pytest_embedded_log/
# idf-component-manager output
dependencies.lock
managed_components/*
!managed_components/.gitkeep

6
.gitmodules vendored Normal file
View file

@ -0,0 +1,6 @@
[submodule "components/esp32-wifi-manager"]
path = components/esp32-wifi-manager
url = https://github.com/ookami125/esp32-wifi-manager.git
[submodule "components/BearSSL"]
path = components/BearSSL
url = https://www.bearssl.org/git/BearSSL

9
CMakeLists.txt Executable file
View file

@ -0,0 +1,9 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(rust-alarm)

9
Readme.md Normal file
View file

@ -0,0 +1,9 @@
GND -> GND
13 -> CS
12 -> CLK
11 -> DIN
9 -> DC
8 -> RST
7 -> BUSY
3V3 -> PWR
3V3 -> VCC

0
components/.gitkeep Normal file
View file

@ -0,0 +1 @@
Subproject commit f233f86f4064d3dc9d9bee3ca40e04f5e6871317

21
dependencies.lock Normal file
View file

@ -0,0 +1,21 @@
dependencies:
espressif/mdns:
component_hash: 3ec0af5f6bce310512e90f482388d21cc7c0e99668172d2f895356165fc6f7c5
dependencies:
- name: idf
require: private
version: '>=5.0'
source:
registry_url: https://components.espressif.com/
type: service
version: 1.8.2
idf:
source:
type: idf
version: 5.4.1
direct_dependencies:
- espressif/mdns
- idf
manifest_hash: f376e1854ef8b392e85a6ea8d0f831fb27d00c798371cf7349c0ef18e925f107
target: esp32s3
version: 2.0.0

2
main/CMakeLists.txt Executable file
View file

@ -0,0 +1,2 @@
idf_component_register(SRCS "main.c" "epd7in5.c"
INCLUDE_DIRS ".")

82
main/Kconfig.projbuild Executable file
View file

@ -0,0 +1,82 @@
menu "Example Configuration"
menu "SoftAP Configuration"
comment "SoftAP Configuration"
config ESP_WIFI_AP_SSID
string "WiFi AP SSID"
default "myssid"
help
SSID (network name) of the AP for the example to connect to.
config ESP_WIFI_AP_PASSWORD
string "WiFi AP Password"
default "mypassword"
help
WiFi password of the AP for the example to use.
config ESP_WIFI_AP_CHANNEL
int "WiFi AP Channel"
range 1 14
default 1
help
WiFi channel (network channel) of the AP for the example to use.
config ESP_MAX_STA_CONN_AP
int "Maximal STA connections"
default 4
help
Max number of the STA connects to AP.
endmenu
menu "STA Configuration"
comment "STA Configuration"
config ESP_WIFI_REMOTE_AP_SSID
string "WiFi Remote AP SSID"
default "otherapssid"
help
SSID (network name) for the example's sta to connect to.
config ESP_WIFI_REMOTE_AP_PASSWORD
string "WiFi Remote AP Password"
default "otherappassword"
help
WiFi password for the example to use.
config ESP_MAXIMUM_STA_RETRY
int "Maximum retry"
default 5
help
Set the maximum retry value to prevent the station from continuously
attempting to reconnect to the Access Point (AP) when the AP doesn't exist.
choice ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD
prompt "WiFi Scan auth mode threshold"
default ESP_WIFI_AUTH_WPA2_PSK
help
The weakest authmode to accept in the scan mode.
This value defaults to ESP_WIFI_AUTH_WPA2_PSK in case password is present
and ESP_WIFI_AUTH_OPEN is used. Please select ESP_WIFI_AUTH_WEP / ESP_WIFI_AUTH_WPA_PSK
in case AP is operating in WEP / WPA mode.
config ESP_WIFI_AUTH_OPEN
bool "OPEN"
config ESP_WIFI_AUTH_WEP
bool "WEP"
config ESP_WIFI_AUTH_WPA_PSK
bool "WPA PSK"
config ESP_WIFI_AUTH_WPA2_PSK
bool "WPA2 PSK"
config ESP_WIFI_AUTH_WPA_WPA2_PSK
bool "WPA/WPA2 PSK"
config ESP_WIFI_AUTH_WPA3_PSK
bool "WPA3 PSK"
config ESP_WIFI_AUTH_WPA2_WPA3_PSK
bool "WPA2/WPA3 PSK"
config ESP_WIFI_AUTH_WAPI_PSK
bool "WAPI PSK"
endchoice
endmenu
endmenu

1791
main/OverpassMono_Bitmaps.h Normal file

File diff suppressed because it is too large Load diff

20163
main/audio.h Normal file

File diff suppressed because it is too large Load diff

219
main/epd7in5.c Normal file
View file

@ -0,0 +1,219 @@
// epd7in5_v3.c
#include "epd7in5.h"
#include "driver/gpio.h"
#define TIMEOUT_MS 1000
static esp_err_t gpio_setup(int pin, gpio_mode_t mode)
{
gpio_config_t io_conf = {
.pin_bit_mask = 1ULL << pin,
.mode = mode,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
return gpio_config(&io_conf);
}
#define ESP_RETURN_ON_ERROR(x, y, z) x
esp_err_t epd7in5_v3_create(epd7in5_v3_t *epd, spi_host_device_t host)
{
esp_err_t ret;
epd->pin_rst = EPD_RST_PIN;
epd->pin_dc = EPD_DC_PIN;
epd->pin_cs = EPD_CS_PIN;
epd->pin_busy = EPD_BUSY_PIN;
epd->width = EPD_WIDTH;
epd->height = EPD_HEIGHT;
epd->spi_host = host;
// 1) configure GPIOs
ESP_RETURN_ON_ERROR(gpio_setup(epd->pin_rst, GPIO_MODE_OUTPUT), "rst gpio", ret);
ESP_RETURN_ON_ERROR(gpio_setup(epd->pin_dc, GPIO_MODE_OUTPUT), "dc gpio", ret);
ESP_RETURN_ON_ERROR(gpio_setup(epd->pin_cs, GPIO_MODE_OUTPUT), "cs gpio", ret);
ESP_RETURN_ON_ERROR(gpio_setup(epd->pin_busy,GPIO_MODE_INPUT), "busy gpio", ret);
// 2) initialize SPI bus if needed, then add device
spi_bus_config_t buscfg = {
.mosi_io_num = 11/* your MOSI pin */,
.miso_io_num = 13/* your MISO pin */,
.sclk_io_num = 12/* your SCLK pin */,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = epd->width * epd->height / 8 + 16
};
ESP_RETURN_ON_ERROR(spi_bus_initialize(epd->spi_host, &buscfg, SPI_DMA_CH_AUTO), "bus init", ret);
return ESP_OK;
}
esp_err_t epd7in5_v3_init(epd7in5_v3_t *epd)
{
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 20 * 1000 * 1000, // 2 MHz
.mode = 0, // SPI mode 0
.spics_io_num = epd->pin_cs,
.queue_size = 1,
.flags = SPI_DEVICE_HALFDUPLEX // use .flags |= SPI_DEVICE_3WIRE if necessary
};
ESP_RETURN_ON_ERROR(spi_bus_add_device(epd->spi_host, &devcfg, &epd->spi), "add device", ret);
// 3) reset panel
gpio_set_level(epd->pin_rst, 1);
vTaskDelay(pdMS_TO_TICKS(20));
gpio_set_level(epd->pin_rst, 0);
vTaskDelay(pdMS_TO_TICKS(4));
gpio_set_level(epd->pin_rst, 1);
vTaskDelay(pdMS_TO_TICKS(20));
// 4) initial sequence (matches epd7in5_v3::Init)
epd7in5_v3_send_command(epd, CMD_BOOSTER_SOFT_START);
epd7in5_v3_send_data(epd, 0x17);
epd7in5_v3_send_data(epd, 0x17);
epd7in5_v3_send_data(epd, 0x28);
epd7in5_v3_send_data(epd, 0x17);
epd7in5_v3_send_command(epd, CMD_POWER_SETTING);
epd7in5_v3_send_data(epd, 0x07);
epd7in5_v3_send_data(epd, 0x07);
epd7in5_v3_send_data(epd, 0x28);
epd7in5_v3_send_data(epd, 0x17);
epd7in5_v3_send_command(epd, CMD_POWER_ON);
vTaskDelay(pdMS_TO_TICKS(100));
epd7in5_v3_busy_wait(epd);
epd7in5_v3_send_command(epd, CMD_PANEL_SETTING);
epd7in5_v3_send_data(epd, 0x1F);
epd7in5_v3_send_command(epd, CMD_RESOLUTION_SETTING);
epd7in5_v3_send_data(epd, 0x03);
epd7in5_v3_send_data(epd, 0x20);
epd7in5_v3_send_data(epd, 0x01);
epd7in5_v3_send_data(epd, 0xE0);
epd7in5_v3_send_command(epd, CMD_DUAL_SPI);
epd7in5_v3_send_data(epd, 0x00);
epd7in5_v3_send_command(epd, CMD_VCOM_AND_DATA_INTERVAL_SETTING);
epd7in5_v3_send_data(epd, 0x10);
epd7in5_v3_send_data(epd, 0x07);
epd7in5_v3_send_command(epd, CMD_TCON_SETTING);
epd7in5_v3_send_data(epd, 0x22);
return ESP_OK;
}
esp_err_t epd7in5_v3_send_command(epd7in5_v3_t *epd, uint8_t cmd)
{
esp_err_t ret;
spi_transaction_t t = {
.flags = SPI_TRANS_USE_TXDATA,
.tx_data = { cmd, 0, 0, 0 },
.length = 8,
};
gpio_set_level(epd->pin_dc, 0);
ret = spi_device_polling_transmit(epd->spi, &t);
return ret;
}
esp_err_t epd7in5_v3_send_data(epd7in5_v3_t *epd, uint8_t data)
{
esp_err_t ret;
spi_transaction_t t = {
.flags = SPI_TRANS_USE_TXDATA,
.tx_data = { data, 0, 0, 0 },
.length = 8,
};
gpio_set_level(epd->pin_dc, 1);
ret = spi_device_polling_transmit(epd->spi, &t);
return ret;
}
esp_err_t epd7in5_v3_send_data_block(epd7in5_v3_t *epd, const uint8_t *data, size_t len)
{
esp_err_t ret;
spi_transaction_t t = {
.tx_buffer = data,
.length = len * 8,
};
gpio_set_level(epd->pin_dc, 1);
ret = spi_device_polling_transmit(epd->spi, &t);
return ret;
}
void epd7in5_v3_busy_wait(epd7in5_v3_t *epd)
{
// send dummy 0x71 until BUSY goes high
do {
epd7in5_v3_send_command(epd, 0x71);
vTaskDelay(pdMS_TO_TICKS(10));
} while (gpio_get_level(epd->pin_busy) == 0);
vTaskDelay(pdMS_TO_TICKS(20));
}
esp_err_t epd7in5_v3_display(epd7in5_v3_t *epd, const uint8_t *image)
{
size_t width_bytes = (epd->width + 7) / 8;
size_t img_size = width_bytes * epd->height;
epd7in5_v3_send_command(epd, CMD_DISPLAY_START_TRANSMISSION_1);
// white buffer:
for (size_t i = 0; i < img_size; i++) {
epd7in5_v3_send_data(epd, 0xFF);
}
epd7in5_v3_send_command(epd, CMD_DISPLAY_START_TRANSMISSION_2);
for (size_t i = 0; i < img_size; i += width_bytes) {
epd7in5_v3_send_data_block(epd, &image[i], width_bytes);
}
epd7in5_v3_send_command(epd, CMD_DISPLAY_REFRESH);
vTaskDelay(pdMS_TO_TICKS(100));
epd7in5_v3_busy_wait(epd);
return ESP_OK;
}
esp_err_t epd7in5_v3_clear(epd7in5_v3_t *epd)
{
size_t len = (epd->width + 7) / 8 * epd->height;
// Stage 1: paint white
epd7in5_v3_send_command(epd, CMD_DISPLAY_START_TRANSMISSION_1);
for (size_t i = 0; i < len; i++) {
epd7in5_v3_send_data(epd, 0xFF);
}
// Stage 2: paint black
epd7in5_v3_send_command(epd, CMD_DISPLAY_START_TRANSMISSION_2);
for (size_t i = 0; i < len; i++) {
epd7in5_v3_send_data(epd, 0x00);
}
// Refresh
epd7in5_v3_send_command(epd, CMD_DISPLAY_REFRESH);
vTaskDelay(pdMS_TO_TICKS(100));
epd7in5_v3_busy_wait(epd);
return ESP_OK;
}
esp_err_t epd7in5_v3_sleep(epd7in5_v3_t *epd)
{
epd7in5_v3_send_command(epd, 0x50);
epd7in5_v3_send_data(epd, 0xF7);
epd7in5_v3_send_command(epd, CMD_POWER_OFF);
epd7in5_v3_busy_wait(epd);
epd7in5_v3_send_command(epd, 0x07);
epd7in5_v3_send_data(epd, 0xA5);
vTaskDelay(pdMS_TO_TICKS(2000));
spi_bus_remove_device(epd->spi);
gpio_set_level(epd->pin_rst, 0);
gpio_set_level(epd->pin_dc, 0);
return ESP_OK;
}

120
main/epd7in5.h Normal file
View file

@ -0,0 +1,120 @@
// epd7in5_v3.h
#ifndef epd7in5_v3_H
#define epd7in5_v3_H
#include <stdint.h>
#include "driver/spi_master.h"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#ifdef __cplusplus
extern "C" {
#endif
#define EPD_WIDTH 800
#define EPD_HEIGHT 480
// Default pin assignments (override in your own board header or before init)
#ifndef EPD_RST_PIN
#define EPD_RST_PIN 8
#endif
#ifndef EPD_DC_PIN
#define EPD_DC_PIN 9
#endif
#ifndef EPD_CS_PIN
#define EPD_CS_PIN 10
#endif
#ifndef EPD_BUSY_PIN
#define EPD_BUSY_PIN 7
#endif
typedef enum {
CMD_PANEL_SETTING = 0x00,
CMD_POWER_SETTING = 0x01,
CMD_POWER_OFF = 0x02,
CMD_POWER_OFF_SEQUENCE_SETTING = 0x03,
CMD_POWER_ON = 0x04,
CMD_POWER_ON_MEASURE = 0x05,
CMD_BOOSTER_SOFT_START = 0x06,
CMD_DISPLAY_START_TRANSMISSION_1 = 0x10,
CMD_DISPLAY_REFRESH = 0x12,
CMD_DISPLAY_START_TRANSMISSION_2 = 0x13,
CMD_DUAL_SPI = 0x15,
CMD_VCOM_AND_DATA_INTERVAL_SETTING = 0x50,
CMD_TCON_SETTING = 0x60,
CMD_RESOLUTION_SETTING = 0x61,
CMD_PARTIAL_WINDOW = 0x90,
CMD_PARTIAL_IN = 0x91,
CMD_PARTIAL_OUT = 0x92,
CMD_CASCADE_SETTING = 0xE0,
CMD_FORCE_TEMPERATURE = 0xE5,
} epd_cmd_t;
typedef struct {
spi_device_handle_t spi;
spi_host_device_t spi_host;
int8_t pin_rst;
int8_t pin_dc;
int8_t pin_cs;
int8_t pin_busy;
uint16_t width;
uint16_t height;
} epd7in5_v3_t;
/**
* @brief Initialize the SPI bus, GPIOs and EPD hardware.
* @param epd Pointer to an epd7in5_v3_t struct (must be allocated by caller).
* @param host SPI host to use (e.g. VSPI_HOST).
* @return ESP_OK on success, or an error code.
*/
esp_err_t epd7in5_v3_create(epd7in5_v3_t *epd, spi_host_device_t host);
/**
* @brief Initialize the EPD panel with default settings.
* @param epd Pointer to an epd7in5_v3_t struct (must be allocated by caller).
* @return ESP_OK on success, or an error code.
*/
esp_err_t epd7in5_v3_init(epd7in5_v3_t *epd);
/**
* @brief Send a blocking command byte.
*/
esp_err_t epd7in5_v3_send_command(epd7in5_v3_t *epd, uint8_t cmd);
/**
* @brief Send a blocking data byte.
*/
esp_err_t epd7in5_v3_send_data(epd7in5_v3_t *epd, uint8_t data);
/**
* @brief Send a block of data.
*/
esp_err_t epd7in5_v3_send_data_block(epd7in5_v3_t *epd, const uint8_t *data, size_t len);
/**
* @brief Wait until BUSY line goes high (not busy).
*/
void epd7in5_v3_busy_wait(epd7in5_v3_t *epd);
/**
* @brief Full-screen update of a black/white image (size = width*height/8).
*/
esp_err_t epd7in5_v3_display(epd7in5_v3_t *epd, const uint8_t *image);
/**
* @brief Clear the display (white).
*/
esp_err_t epd7in5_v3_clear(epd7in5_v3_t *epd);
/**
* @brief Put the panel into deep sleep.
*/
esp_err_t epd7in5_v3_sleep(epd7in5_v3_t *epd);
#ifdef __cplusplus
}
#endif
#endif // epd7in5_v3_H

423
main/main.c Executable file
View file

@ -0,0 +1,423 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <esp_wifi.h>
#include <esp_netif.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_netif_sntp.h"
#include "nvs.h"
#include "epd7in5.h"
#include "wifi_manager.h"
#include "OverpassMono_Bitmaps.h"
#include "esp_tls.h"
#include "esp_log.h"
#include <string.h>
#include "driver/i2s_std.h"
static const char TAG[] = "main";
enum {
WIFI_CONNECTED,
WIFI_LOST,
ACCESS_POINT,
} WifiState;
struct {
bool radio;
} GlobalState;
typedef struct {
uint8_t event_id;
uint32_t time;
} TimerEvent;
typedef struct {
uint8_t event_id;
} AlarmEvent;
QueueHandle_t wifiQueue;
QueueHandle_t timerEinkQueue;
QueueHandle_t timerAlarmQueue;
QueueHandle_t alarmQueue;
void draw_char(uint8_t *image, int x, int y, char c) {
uint32_t char_offset = (c - '0') * 16 * 176;
for(int i=0; i<176; ++i) {
memcpy(&image[x + (y+i)*EPD_WIDTH/8], &OverpassMono_Bitmaps[i*16+char_offset], 16);
}
}
void draw_text(uint8_t *image, int x, int y, const char *text, int length)
{
//89x130
for(int i=0; i<length; ++i) {
if(text[i] < '0' || text[i] > '9') {
ESP_LOGE(TAG, "Invalid character '%c' in text: %s", text[i], text);
return;
}
draw_char(image, x + i * 17, y, text[i]);
}
}
void eink_task(void *pvParameter)
{
static uint8_t image[EPD_WIDTH * EPD_HEIGHT / 8];
memset(image, 0x00, sizeof(image));
epd7in5_v3_t epd;
epd7in5_v3_create(&epd, SPI2_HOST);
while(true) {
// Wait for time update from timerQueue
TimerEvent event;
if (xQueueReceive(timerEinkQueue, &event, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Failed to receive timer event from queue");
continue;
}
char buffer[5] = {0};
memcpy(buffer, &event.time, sizeof(event.time));
memset(image, 0x00, sizeof(image));
epd7in5_v3_init(&epd);
draw_text(image, 0, 0, buffer, 4);
epd7in5_v3_busy_wait(&epd);
epd7in5_v3_display(&epd, image);
epd7in5_v3_busy_wait(&epd);
epd7in5_v3_sleep(&epd);
vTaskDelay( pdMS_TO_TICKS(500) );
}
}
#include "audio.h"
#define SAMPLE_RATE 44100
#define TONE_HZ 853
#define TONE_2_HZ 960
#define I2S_NUM 0
#define PI 3.14159265
#define BITS_PER_SAMPLE 16
#define CHANNELS 2
#define BUFFER_SAMPLES 2048*4
static i2s_chan_handle_t tx_handle;
void radio_task(void *pvParameter)
{
bool radioActive = false;
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle, NULL));
// --- Clock configuration ---
i2s_std_clk_config_t clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(SAMPLE_RATE);
// --- Slot configuration ---
i2s_std_slot_config_t slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(
BITS_PER_SAMPLE,
I2S_SLOT_MODE_MONO
);
slot_cfg.slot_mask = I2S_STD_SLOT_LEFT | I2S_STD_SLOT_RIGHT;
// --- GPIO configuration ---
i2s_std_gpio_config_t gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = GPIO_NUM_35,
.ws = GPIO_NUM_36,
.dout = GPIO_NUM_37,
.din = I2S_GPIO_UNUSED,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false
}
};
// --- Initialize the TX channel in STD mode ---
i2s_std_config_t std_cfg = {
.clk_cfg = clk_cfg,
.slot_cfg = slot_cfg,
.gpio_cfg = gpio_cfg
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle, &std_cfg));
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle));
// --- Generate and send sine wave ---
int16_t *samples = malloc(BUFFER_SAMPLES * sizeof(int16_t));
if (!samples) {
ESP_LOGE("I2S", "Failed to allocate memory for samples");
return;
}
uint32_t offset = 0;
size_t level_up_size = sizeof(level_up) / sizeof(level_up[0]);
AlarmEvent event;
while(true) {
if(!radioActive) {
if(xQueueReceive(alarmQueue, &event, portMAX_DELAY) && event.event_id == 1) {
ESP_LOGI(TAG, "Alarm activated, starting radio");
radioActive = true;
}
} else {
if(xQueueReceive(alarmQueue, &event, 0) && event.event_id == 0) {
ESP_LOGI(TAG, "Alarm deactivated, stopping radio");
radioActive = false;
}
//Do radio shit
for (int i = 0; i < BUFFER_SAMPLES; ++i) {
int16_t sample = level_up[(i+offset) % level_up_size];
samples[i] = sample / 4;
}
size_t bytes_to_write = BUFFER_SAMPLES * sizeof(int16_t);
size_t bytes_written = 0;
ESP_ERROR_CHECK(i2s_channel_write(tx_handle, samples, bytes_to_write, &bytes_written, portMAX_DELAY));
offset += bytes_written/2;
vTaskDelay(1);
}
}
free(samples);
}
void alarm_task(void *pvParameter)
{
bool alarmActive = false;
int alarmStart = 830; // 20:55 in HHMM format
int alarmEnd = 900; // 20:56 in HHMM format
TimerEvent event;
while(true) {
if (xQueueReceive(timerAlarmQueue, &event, portMAX_DELAY) == pdTRUE) {
ESP_LOGI(TAG, "Time event received");
int time = atoi((const char *)&event.time);
if (time >= alarmStart && time < alarmEnd && !alarmActive) {
alarmActive = true;
AlarmEvent alarm_event;
alarm_event.event_id = 1; // or any other event ID you want to use
if (xQueueSend(alarmQueue, &alarm_event, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send alarm event to queue");
} else {
ESP_LOGI(TAG, "Alarm event pushed to queue");
}
} else if (time >= alarmEnd && alarmActive) {
alarmActive = false;
AlarmEvent alarm_event;
alarm_event.event_id = 0; // or any other event ID you want to use
if (xQueueSend(alarmQueue, &alarm_event, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send alarm event to queue");
} else {
ESP_LOGI(TAG, "Alarm event pushed to queue");
}
}
}
}
}
static bool timeSet = false;
void time_update_task(void *pvParameter)
{
TimerEvent event;
char lastMinute = '-';
while(!timeSet) {
// Wait for time to be set by SNTP
vTaskDelay(pdMS_TO_TICKS(1000));
}
while(true) {
// check if the last minute has changed and push to queue if so
time_t now;
struct tm timeinfo = { 0 };
time(&now);
localtime_r(&now, &timeinfo);
char buffer[5];
strftime(buffer, sizeof(buffer), "%H%M", &timeinfo);
if (buffer[3] != lastMinute) {
lastMinute = buffer[3];
event.event_id = WIFI_CONNECTED; // or any other event ID you want to use
memcpy(&event.time, buffer, sizeof(event.time));
if (xQueueSend(timerEinkQueue, &event, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send timer event to queue");
} else {
ESP_LOGI(TAG, "Pushed time update to queue: %s", buffer);
}
if (xQueueSend(timerAlarmQueue, &event, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send timer event to queue");
} else {
ESP_LOGI(TAG, "Pushed time update to queue: %s", buffer);
}
}
vTaskDelay(pdMS_TO_TICKS(500)); //Check every half second
}
}
void time_sync_notification_cb(struct timeval *tv)
{
timeSet = true;
ESP_LOGI(TAG, "Notification of a time synchronization event");
}
//RTC_DATA_ATTR static int boot_count = 0;
static void obtain_time(void* pvParameter)
{
ESP_LOGI(TAG, "Initializing SNTP");
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
config.start = false;
config.server_from_dhcp = false;
config.renew_servers_after_new_IP = false;
config.index_of_first_server = 1;
config.ip_event_to_renew = IP_EVENT_STA_GOT_IP;
config.sync_cb = time_sync_notification_cb;
esp_netif_sntp_init(&config);
setenv("TZ", "EST5EDT,M3.2.0/2,M11.1.0", 1);
tzset();
while(true) {
ESP_LOGI(TAG, "Starting SNTP");
esp_netif_sntp_start();
time_t now = 0;
struct tm timeinfo = { 0 };
int retry = 0;
const int retry_count = 15;
while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
}
time(&now);
localtime_r(&now, &timeinfo);
vTaskDelay(pdMS_TO_TICKS(60 * 60 * 1000));
}
}
void cb_connection_ok(void *pvParameter){
ip_event_got_ip_t* param = (ip_event_got_ip_t*)pvParameter;
char str_ip[16];
esp_ip4addr_ntoa(&param->ip_info.ip, str_ip, IP4ADDR_STRLEN_MAX);
ESP_LOGI(TAG, "I have a connection and my IP is %s!", str_ip);
// Notify the radio task that we have a connection
int wifi_event = WIFI_CONNECTED;
if (xQueueSend(wifiQueue, &wifi_event, portMAX_DELAY) != pdTRUE) {
ESP_LOGE(TAG, "Failed to send WIFI_CONNECTED event to queue");
} else {
ESP_LOGI(TAG, "Sent WIFI_CONNECTED event to queue");
}
}
//void audio_task(void* pvParameter);
//void http_stream_task(void *pvParameter);
//void https_insecure_task(void *pv);
void app_main()
{
esp_log_level_set("esp-tls-mbedtls", ESP_LOG_DEBUG);
esp_log_level_set("TLSv1", ESP_LOG_DEBUG);
//mbedtls_debug_set_threshold(4);
wifiQueue = xQueueCreate(2, sizeof(int));
timerEinkQueue = xQueueCreate(2, sizeof(TimerEvent));
timerAlarmQueue = xQueueCreate(2, sizeof(TimerEvent));
alarmQueue = xQueueCreate(2, sizeof(AlarmEvent));
wifi_manager_start();
wifi_manager_set_callback(WM_EVENT_STA_GOT_IP, &cb_connection_ok);
xTaskCreatePinnedToCore(&radio_task, "radio", 3072, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(&eink_task, "eink", 3072, NULL, 1, NULL, 1);
xTaskCreate(&obtain_time, "sntp", 3072, NULL, 10, NULL);
xTaskCreate(&time_update_task, "timer", 3072, NULL, 10, NULL);
//xTaskCreate(&audio_task, "timer", 3072, NULL, 10, NULL);
xTaskCreate(&alarm_task, "timer", 3072, NULL, 10, NULL);
//xTaskCreate(https_insecure_task, "https_insec", 8192, NULL, 5, NULL);
}
// HTTP test code
//
//void https_get_insecure(const char *host, const char *path)
//{
// const int port = 443;
//
// // 1) Allocate the TLS handle
// esp_tls_t *tls = esp_tls_init();
// if (!tls) {
// ESP_LOGE(TAG, "Failed to init TLS handle");
// return;
// }
//
// // Insecure TLS config: no CA, skip CN
// esp_tls_cfg_t tls_cfg = {
// .cacert_buf = NULL,
// .cacert_bytes = 0,
// .skip_common_name = true,
// // Force TLS 1.2 only:
// .tls_version = ESP_TLS_VER_TLS_1_2,
// // Advertise HTTP/1.1 via ALPN (many servers require this for HTTPS):
// .alpn_protos = (const char*[]){"http/1.1", NULL},
// };
//
// // This will send the SNI extension = host
// int ret = esp_tls_conn_new_sync(host, strlen(host), port, &tls_cfg, tls);
// if (ret != 1) {
// ESP_LOGE(TAG, "TLS handshake failed, esp_tls_conn_new_sync returned %d", ret);
// esp_tls_conn_destroy(tls);
// return;
// }
// ESP_LOGI(TAG, "TLS handshake succeeded (insecure)");
//
// // Send a minimal GET
// char req[256];
// int len = snprintf(req, sizeof(req),
// "GET %s HTTP/1.1\r\n"
// "Host: %s\r\n"
// "Connection: close\r\n\r\n",
// path, host);
// esp_tls_conn_write(tls, (const unsigned char *)req, len);
// // Read and print
// char buf[128];
// int r;
// do {
// r = esp_tls_conn_read(tls, (unsigned char*)buf, sizeof(buf)-1);
// if (r > 0) {
// buf[r] = '\0';
// printf("%s", buf);
// }
// } while (r > 0 || r == ESP_TLS_ERR_SSL_WANT_READ);
// esp_tls_conn_destroy(tls);
//}
//
//void https_insecure_task(void *pv)
//{
// // Wait for WiFi to be connected (from wifiqueue)
// int event;
// if( xQueueReceive(wifiQueue, &event, portMAX_DELAY) != pdTRUE ) {
// ESP_LOGE(TAG, "Failed to receive WIFI_CONNECTED event from queue");
// return;
// }
//
// https_get_insecure("listen.moe", "/stream");
//}

View file

6
sdkconfig.defaults Executable file
View file

@ -0,0 +1,6 @@
# This file was generated using idf.py save-defconfig. It can be edited manually.
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
#
CONFIG_LWIP_IP_FORWARD=y
CONFIG_LWIP_IPV4_NAPT=y
CONFIG_HTTPD_MAX_URI_LEN=1024