src.nth.io/

summaryrefslogtreecommitdiff
path: root/main/viewport_state.c
blob: 3ae8f814ecbca17ba4ad780598921933c7effa11 (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
#include "viewport_state.h"

#include <string.h>

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

static viewport_state_t s_state;
static SemaphoreHandle_t s_mutex;

void viewport_state_init(void)
{
    memset(&s_state, 0, sizeof(s_state));
    s_state.configured      = false;
    // State is always AWAKE or ASLEEP. UNCONFIGURED is reported via the
    // `configured` flag, not as a state value. Boot is always asleep.
    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;

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