summaryrefslogtreecommitdiff
path: root/fw/fe310/test/phone.c
blob: 2832fc4e58a18314cb1d4302a7945c8f890e41c8 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <eos.h>
#include <event.h>
#include <spi.h>
#include <i2s.h>
#include <net.h>
#include <cell.h>

#include <unicode.h>

#include <eve/eve.h>
#include <eve/eve_kbd.h>

#include <eve/screen/screen.h>
#include <eve/screen/window.h>
#include <eve/screen/view.h>
#include <eve/screen/page.h>
#include <eve/screen/form.h>

#include <eve/widget/widgets.h>

#include <app/root.h>

#include "status.h"
#include "phone.h"

extern EVEFont *_app_font_default;

#define ABUF_SIZE       512
#define MIC_WM          128

static uint8_t mic_arr[ABUF_SIZE];
static uint8_t spk_arr[ABUF_SIZE];

#define VOICE_STATE_IDLE    0
#define VOICE_STATE_DIAL    1
#define VOICE_STATE_RING    2
#define VOICE_STATE_CIP     3

static unsigned char voice_state = 0;

static void handle_mic(unsigned char type) {
    uint16_t size;
    unsigned char *buf = eos_net_alloc();

    buf[0] = EOS_CELL_MTYPE_VOICE_PCM;
    size = eos_i2s_mic_read(buf+1, MIC_WM);
    eos_net_send(EOS_NET_MTYPE_CELL, buf, size+1, 0);
}

static void cell_voice_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
    char msg[128];

    printf("VOICE: %d\n", type);
    msg[0] = 0;
    switch (type) {
        case EOS_CELL_MTYPE_VOICE_RING:
            voice_state = VOICE_STATE_RING;
            sprintf(msg, "RING:%s", buffer+1);
            break;

        case EOS_CELL_MTYPE_VOICE_MISS:
            voice_state = VOICE_STATE_IDLE;
            break;

        case EOS_CELL_MTYPE_VOICE_BEGIN:
            voice_state = VOICE_STATE_CIP;
            eos_i2s_start(8000, EOS_I2S_FMT_PCM16);
            break;

        case EOS_CELL_MTYPE_VOICE_END:
            voice_state = VOICE_STATE_IDLE;
            eos_i2s_stop();
            break;

        case EOS_CELL_MTYPE_VOICE_PCM:
            if (voice_state == VOICE_STATE_CIP) {
                eos_i2s_spk_write(buffer+1, len-1);
            }
            break;
    }
    app_status_msg_set(msg, 1);
    eos_net_free(buffer, 0);
}

void app_phone(EVEWindow *window, EVEViewStack *stack) {
    char *title = "Phone:";
    uint16_t w = eve_font_str_w(_app_font_default, title) + 10;
    APPWidgetSpec spec[] = {
        {
            .label.g.w = w,
            .label.font = _app_font_default,
            .label.title = title,

            .widget.type = EVE_WIDGET_TYPE_STR,
            .widget.g.w = APP_SCREEN_W - w,
            .widget.spec.str.font = _app_font_default,
            .widget.spec.str.str_size = 128,
        },
    };

    EVEForm *form = app_form_create(window, stack, spec, 1, app_phone_action, NULL);
}

void app_phone_action(EVEForm *form) {
    char msg[128];
    EVEStrWidget *w = (EVEStrWidget *)eve_form_widget(form, 0);
    unsigned char *buf = eos_net_alloc();

    buf[0] = EOS_CELL_MTYPE_VOICE | EOS_CELL_MTYPE_VOICE_DIAL;
    strcpy(buf + 1, w->str);
    eos_net_send(EOS_NET_MTYPE_CELL, buf, 1 + strlen(w->str), 0);

    voice_state = VOICE_STATE_DIAL;
    sprintf(msg, "DIAL:%s", w->str);
    app_status_msg_set(msg, 0);
}

void app_phone_init(void) {
    eos_cell_set_handler(EOS_CELL_MTYPE_VOICE, cell_voice_handler);

    eos_i2s_mic_init(mic_arr, ABUF_SIZE);
    eos_i2s_mic_set_wm(MIC_WM);
    eos_i2s_mic_set_handler(handle_mic);
    eos_i2s_spk_init(spk_arr, ABUF_SIZE);
    eos_net_acquire_for_evt(EOS_EVT_I2S | EOS_I2S_ETYPE_MIC, 1);
}

unsigned char app_phone_state_get(void) {
    return voice_state;
}