summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/net/pwr.c
blob: 89f1817215670841ba563c66d19b76d4a2477229 (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
#include <stdlib.h>
#include <stdint.h>

#include "eos.h"
#include "event.h"
#include "dev/net.h"

#include "soc/pwr.h"
#include "soc/spi.h"
#include "dev/spi.h"
#include "dev/lcd.h"
#include "eve/eve.h"

#include "pwr.h"

static eos_evt_handler_t evt_handler[EOS_PWR_MAX_MTYPE];
static unsigned char power_btn_down;

static void pwr_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
    unsigned char mtype;

    if ((buffer == NULL) || (len < 1)) {
        eos_net_bad_handler(type, buffer, len);
        return;
    }

    mtype = buffer[0];
    if ((mtype < EOS_PWR_MAX_MTYPE) && evt_handler[mtype]) {
        evt_handler[mtype](mtype, buffer, len);
    } else {
        eos_net_bad_handler(type, buffer, len);
    }
}

static void pwr_handle_btn(unsigned char type, unsigned char *buffer, uint16_t len) {
    int rv;
    unsigned char level = buffer[1];

    eos_net_free(buffer, 0);
    if (!level) {
        power_btn_down = 1;
        return;
    }
    if (!power_btn_down) return;

    rv = eos_lcd_sleep();

    rv = eos_spi_select(EOS_SPI_DEV_EVE);
    if (!rv) {
        eve_pwr_sleep();
        eos_spi_deselect();
    }

    rv = eos_pwr_sleep();
}

void eos_pwr_net_init(void) {
    int i;

    for (i=0; i<EOS_PWR_MAX_MTYPE; i++) {
        evt_handler[i] = NULL;
    }
    eos_net_set_handler(EOS_NET_MTYPE_POWER, pwr_handle_msg);
    eos_pwr_set_handler(EOS_PWR_MTYPE_BUTTON, pwr_handle_btn);
}

void eos_pwr_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
    if (mtype < EOS_PWR_MAX_MTYPE) evt_handler[mtype] = handler;
}

eos_evt_handler_t eos_pwr_get_handler(unsigned char mtype) {
    if (mtype < EOS_PWR_MAX_MTYPE) return evt_handler[mtype];
    return NULL;
}