src.nth.io/

summaryrefslogtreecommitdiff
path: root/main/viewport_state.c
blob: a67745a7ee62e4ad035793b1e8231baa3067b7a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "viewport_state.h"

#include <stdio.h>
#include <string.h>

#include "esp_mac.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"

static viewport_state_t s_state;
static SemaphoreHandle_t s_mutex;

// Populate viewport_name with the full base MAC, colons stripped
// (e.g. e8:f6:0a:e0:90:94 → "e8f60ae09094"). Stable across reboots,
// globally unique, and 12 alphanumeric chars — fits comfortably under
// the 32-char mDNS hostname limit with the "viewport-" prefix.
// POST /config can override it with a friendlier name.
static void seed_mac_and_name(char *mac_str, size_t mac_cap,
                              char *name,    size_t name_cap)
{
    uint8_t mac[6] = {0};
    esp_read_mac(mac, ESP_MAC_BASE);
    snprintf(mac_str, mac_cap, "%02x:%02x:%02x:%02x:%02x:%02x",
             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    snprintf(name, name_cap, "%02x%02x%02x%02x%02x%02x",
             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

void viewport_state_init(void)
{
    memset(&s_state, 0, sizeof(s_state));
    s_state.configured      = false;
    s_state.state           = VIEWPORT_STATE_ASLEEP;
    s_state.brightness      = 80;
    s_state.idle_timeout_ms = 60000;
    s_state.orientation     = VIEWPORT_ORIENTATION_PORTRAIT;
    s_state.boot_us         = (uint64_t)esp_timer_get_time();
    s_state.last_frame_us   = -1;
    seed_mac_and_name(s_state.mac_str,       sizeof(s_state.mac_str),
                      s_state.viewport_name, sizeof(s_state.viewport_name));

    s_mutex = xSemaphoreCreateMutex();
}

viewport_state_t *viewport_state_get(void)
{
    return &s_state;
}

void viewport_state_lock(void)
{
    xSemaphoreTake(s_mutex, portMAX_DELAY);
}

void viewport_state_unlock(void)
{
    xSemaphoreGive(s_mutex);
}

const char *viewport_state_resolution_str(void)
{
    return (s_state.orientation == VIEWPORT_ORIENTATION_PORTRAIT)
               ? "480x800"
               : "800x480";
}

void viewport_state_effective_dims(uint16_t *w, uint16_t *h)
{
    viewport_state_lock();
    viewport_orientation_t o = s_state.orientation;
    viewport_state_unlock();
    if (o == VIEWPORT_ORIENTATION_PORTRAIT) { *w = 480; *h = 800; }
    else                                    { *w = 800; *h = 480; }
}