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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <eos.h>
#include <soc/i2s.h>
#include <dev/net.h>
#include <net/cell.h>
#include <eve/eve.h>
#include <eve/eve_kbd.h>
#include <eve/eve_font.h>
#include <eve/screen/window.h>
#include <eve/screen/page.h>
#include <eve/screen/form.h>
#include "app/app.h"
#include "app/status.h"
#include "phone.h"
#define ABUF_SIZE 256
#define MIC_WM 128
static uint8_t mic_arr[ABUF_SIZE];
static uint8_t spk_arr[ABUF_SIZE];
static unsigned char phone_state = 0;
static void handle_mic(unsigned char type) {
uint16_t offset, size;
unsigned char *buf;
buf = eos_cell_voice_pcm_buffer(&offset);
size = eos_i2s_mic_read(buf, MIC_WM);
eos_cell_send_buffer(buf, size, offset, 0);
}
static void handle_cell_voice(unsigned char type, unsigned char *buffer, uint16_t len) {
int rv;
switch (type) {
case EOS_CELL_MTYPE_VOICE_RING: {
rv = EOS_OK;
if (len > EOS_CELL_MAX_DIAL_STR + 1) rv = EOS_ERR_SIZE;
if (rv) {
APP_LOG(APP_LOG_ERR, "RING ERROR:%d\n", rv);
break;
}
buffer[len + 1] = '\0';
APP_LOG(APP_LOG_DEBUG, "RING:%s\n", buffer+1);
if ((phone_state == PHONE_STATE_IDLE) || (phone_state == PHONE_STATE_RING)) {
phone_state = PHONE_STATE_RING;
app_status_set_msg("RING");
}
break;
}
case EOS_CELL_MTYPE_VOICE_BUSY: {
APP_LOG(APP_LOG_DEBUG, "BUSY\n");
if (phone_state == PHONE_STATE_DIAL) {
phone_state = PHONE_STATE_IDLE;
app_status_set_msg("BUSY");
}
break;
}
case EOS_CELL_MTYPE_VOICE_MISS: {
APP_LOG(APP_LOG_DEBUG, "MISS\n");
if ((phone_state == PHONE_STATE_DIAL) || (phone_state == PHONE_STATE_RING)) {
phone_state = PHONE_STATE_IDLE;
app_status_set_msg("MISS");
}
break;
}
case EOS_CELL_MTYPE_VOICE_BEGIN: {
APP_LOG(APP_LOG_DEBUG, "CALL BEGIN\n");
if ((phone_state == PHONE_STATE_DIAL) || (phone_state == PHONE_STATE_RING)) {
phone_state = PHONE_STATE_CIP;
app_status_set_msg("CALL BEGIN");
eos_net_acquire_for_evt(EOS_EVT_I2S | EOS_I2S_ETYPE_MIC, 1);
eos_i2s_spk_init(spk_arr, ABUF_SIZE);
eos_i2s_mic_init(mic_arr, ABUF_SIZE);
eos_i2s_mic_set_wm(MIC_WM);
eos_i2s_mic_set_handler(handle_mic);
eos_i2s_mic_set_vol(4);
eos_i2s_start(8000);
}
break;
}
case EOS_CELL_MTYPE_VOICE_END: {
APP_LOG(APP_LOG_DEBUG, "CALL END\n");
if (phone_state == PHONE_STATE_CIP) {
phone_state = PHONE_STATE_IDLE;
app_status_set_msg("CALL END");
eos_net_acquire_for_evt(EOS_EVT_I2S | EOS_I2S_ETYPE_MIC, 0);
eos_i2s_stop();
}
break;
}
case EOS_CELL_MTYPE_VOICE_PCM: {
if (phone_state == PHONE_STATE_CIP) {
eos_i2s_spk_write(buffer+1, len-1);
}
break;
}
}
eos_net_free(buffer, 0);
}
static void widget_btn_draw(EVEFreeWidget *widget) {
EVEWidget *_widget = &widget->w;
EVEPage *page = _widget->page;
uint8_t tag_up, tag_dn;
eve_cmd_dl(BEGIN(EVE_POINTS));
eve_cmd_dl(POINT_SIZE((_widget->g.h / 2) * 16));
tag_up = eve_freew_tag(widget);
eve_cmd_dl(COLOR_RGB(0x0, 0x255, 0x0));
eve_cmd_dl(VERTEX2F(APP_SCREEN_W / 3, _widget->g.y + _widget->g.h / 2));
tag_dn = eve_freew_tag(widget);
eve_cmd_dl(COLOR_RGB(0x255, 0x0, 0x0));
eve_cmd_dl(VERTEX2F(APP_SCREEN_W - APP_SCREEN_W / 3, _widget->g.y + _widget->g.h / 2));
eve_cmd_dl(COLOR_RGBC(page->v.color_fg));
eve_cmd_dl(END());
}
static void call(char * num) {
int rv;
if (strlen(num)) {
phone_state = PHONE_STATE_DIAL;
app_status_set_msg("DIAL");
rv = eos_cell_voice_dial(num, NULL, 0);
if (rv) APP_LOG(APP_LOG_ERR, "DIAL ERR:%d\n", rv);
}
}
static int widget_btn_touch(EVEFreeWidget *widget, EVETouch *touch, uint16_t evt) {
if (evt & EVE_TOUCH_ETYPE_TAG_UP) {
uint8_t tagi;
int rv;
tagi = eve_widget_tag_index(widget, touch->tag_up);
if (tagi == 0) {
switch (phone_state) {
case PHONE_STATE_RING: {
app_status_set_msg("ANSWER");
rv = eos_cell_voice_answer(NULL, 0);
if (rv) APP_LOG(APP_LOG_ERR, "ANSWER ERR:%d\n", rv);
break;
}
case PHONE_STATE_IDLE: {
EVEPage *page = widget->w.page;
EVEStrWidget *strw = (EVEStrWidget *)eve_page_widget(page, 0);
call(strw->str);
break;
}
}
} else if (tagi == 1) {
if (phone_state != PHONE_STATE_IDLE) {
if (phone_state != PHONE_STATE_CIP) {
phone_state = PHONE_STATE_IDLE;
}
rv = eos_cell_voice_hangup(NULL, 0);
if (rv) APP_LOG(APP_LOG_ERR, "HANGUP ERR:%d\n", rv);
}
}
return 1;
}
return 0;
}
void phone_init(void) {
eos_cell_set_handler(EOS_CELL_MTYPE_VOICE, handle_cell_voice);
}
int phone_app(EVEWindow *window, EVEViewStack *stack) {
EVEFormSpec spec[] = {
{
.label.title = "Phone:",
.widget.type = EVE_WIDGET_TYPE_STR,
.widget.tspec.str.str_size = EOS_CELL_MAX_DIAL_STR + 1,
},
APP_SPACERW(APP_SCREEN_W, 50),
{
.widget.type = EVE_WIDGET_TYPE_FREE,
.widget.g.w = APP_SCREEN_W,
.widget.g.h = 100,
.widget.tspec.free.draw = widget_btn_draw,
.widget.tspec.free.touch = widget_btn_touch,
},
};
EVEPage *page = eve_form_create(window, stack, spec, APP_SPEC_SIZE(spec), NULL, phone_close);
if (page == NULL) {
APP_LOG(APP_LOG_ERR, "OUT OF MEMORY\n");
return EVE_ERR_NOMEM;
}
return EVE_OK;
}
int phone_uievt(EVEPage *page, uint16_t evt, void *param) {
int ret = 0;
switch (evt) {
case EVE_UIEVT_WIDGET_FOCUS_OUT: {
if (param == eve_page_widget(page, 0)) {
EVEStrWidget *strw = param;
if (phone_state == PHONE_STATE_IDLE) {
call(strw->str);
}
}
break;
}
default: {
ret = eve_form_uievt(page, evt, param);
break;
}
}
return ret;
}
void phone_close(EVEPage *page) {
eve_form_destroy(page);
}
unsigned char phone_get_state(void) {
return phone_state;
}
|