summaryrefslogtreecommitdiff
path: root/fw/fe310/test/modem.c
blob: ec7448260e9f0e7e96b7ac84a7608549eae6f1dc (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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

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

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

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

#include <eve/widget/widgets.h>

#include <app/app_root.h>
#include <app/app_form.h>

#include "modem.h"

typedef struct {
    uint32_t mem;
    EVEViewStack *stack;
    eos_evt_handler_t cell_dev_handler;
    EVEText text;
} VParam;

static void key_down(void *p, int c) {
    EVEView *view = p;
    EVEText *text = &((VParam *)view->param)->text;
    unsigned char *buf;
    int i = 2;

    if (c == 0x11) {
        app_modem_close(view);
        return;
    }
    buf = eos_net_alloc();
    buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_DATA;
    if (c == '\n') {
        buf[1] = '\r';
        buf[2] = '\n';
        i++;
    } else {
        buf[1] = c;
    }

    eos_net_send(EOS_NET_MTYPE_CELL, buf, i, 0);
    eve_text_scroll0(text);
}

static void handle_uart(unsigned char type) {
    int i = 0;
    int c = 0;
    unsigned char *buf = NULL;

    c = eos_uart_getc(0);
    if (c == EOS_ERR_EMPTY) return;

    buf = eos_net_alloc();
    buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_DATA;
    buf[1] = c;
    i = 2;
    while ((c = eos_uart_getc(0)) != EOS_ERR_EMPTY) {
        buf[i] = c;
        i++;
        if (i == EOS_NET_SIZE_BUF) break;
    }
    eos_net_send(EOS_NET_MTYPE_CELL, buf, i, 0);
    eos_uart_rxwm_set(0);
}

static void handle_cell_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
    EVEWindow *root = app_root();
    EVEWindow *window = eve_window_search(root, "main");
    VParam *param = window->view->param;

    if (type == EOS_CELL_MTYPE_UART_DATA) {
        EVEText *text = &param->text;
        int i;

        eve_spi_start();
        for (i=1; i<len; i++) {
            if (buffer[i] != '\r') eve_text_putc(text, buffer[i]);
        }
        if (text->dirty) {
            text->dirty = 0;
            eve_window_root_draw(root);
        }
        eve_spi_stop();
        eos_net_free(buffer, 0);
    } else {
        param->cell_dev_handler(type, buffer, len);
    }
}

static uint8_t modem_draw(EVEView *view, uint8_t tag0) {
    VParam *param = view->param;
    EVEText *text = &param->text;

    tag0 = eve_view_clear(view, tag0);
    return eve_text_draw(text, tag0);
}

static int modem_touch(EVEView *view, EVETouch *touch, uint16_t evt, uint8_t tag0) {
    VParam *param = view->param;
    EVEText *text = &param->text;

    return eve_text_touch(text, touch, evt, tag0);
}

void app_modem(EVEWindow *window, EVEViewStack *stack) {
    unsigned char *buf;
    EVEWindowRoot *root = (EVEWindowRoot *)window->root;
    EVEKbd *kbd = eve_window_kbd(window);
    EVERect g = {0, 60, 480, 512};
    EVEView *view;
    VParam *param;

    view = eve_malloc(sizeof(EVEView));
    param = eve_malloc(sizeof(VParam));
    param->mem = root->mem_next;
    param->stack = stack;
    param->cell_dev_handler = eos_cell_get_handler(EOS_CELL_MTYPE_DEV);
    eve_text_init(&param->text, &g, 30, 16, 200, root->mem_next, &root->mem_next);
    eve_view_init(view, window, modem_draw, modem_touch, NULL, param);

    eve_kbd_set_handler(kbd, key_down, view);
    eve_window_kbd_attach(window);

    eos_uart_set_handler(EOS_UART_ETYPE_RX, handle_uart);
    eos_cell_set_handler(EOS_CELL_MTYPE_DEV, handle_cell_msg);
    eos_net_acquire_for_evt(EOS_EVT_UART | EOS_UART_ETYPE_RX, 1);

    buf = eos_net_alloc();
    buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_UART_TAKE;
    eos_net_send(EOS_NET_MTYPE_CELL, buf, 1, 0);
    eos_uart_rxwm_set(0);
}

void app_modem_close(EVEView *view) {
    unsigned char *buf = eos_net_alloc();
    VParam *param = view->param;
    EVEWindow *window = view->window;
    EVEWindowRoot *root = (EVEWindowRoot *)window->root;
    EVEKbd *kbd = eve_window_kbd(window);
    EVEViewStack *stack = param->stack;

    buf[0] = EOS_CELL_MTYPE_DEV | EOS_CELL_MTYPE_RESET;
    eos_net_send(EOS_NET_MTYPE_CELL, buf, 1, 0);
    eos_uart_rxwm_clear();
    eos_uart_set_handler(EOS_UART_ETYPE_RX, NULL);
    eos_cell_set_handler(EOS_CELL_MTYPE_DEV, param->cell_dev_handler);
    eos_net_acquire_for_evt(EOS_EVT_UART | EOS_UART_ETYPE_RX, 0);

    root->mem_next = param->mem;
    eve_window_kbd_detach(window);
    eve_kbd_set_handler(kbd, NULL, NULL);

    eve_free(param);
    eve_free(view);
    eve_view_destroy(window, stack);
}