Project

General

Profile

Statistics
| Revision:

root / lab4 / lab4.c @ 77

History | View | Annotate | Download (10.6 KB)

1 53 up20180655
#include <lcom/lcf.h>
2
3
#include <stdint.h>
4
#include <stdio.h>
5
6 58 up20180642
#include "mouse.h"
7
#include "kbc.h"
8 70 up20180655
#include "errors.h"
9
#include "mouse_macros.h"
10 74 up20180642
#include "timer.h"
11 53 up20180655
12
int main(int argc, char *argv[]) {
13
  // sets the language of LCF messages (can be either EN-US or PT-PT)
14
  lcf_set_language("EN-US");
15
16
  // enables to log function invocations that are being "wrapped" by LCF
17
  // [comment this out if you don't want/need/ it]
18
  lcf_trace_calls("/home/lcom/labs/lab4/trace.txt");
19
20
  // enables to save the output of printf function calls on a file
21
  // [comment this out if you don't want/need it]
22
  lcf_log_output("/home/lcom/labs/lab4/output.txt");
23
24
  // handles control over to LCF
25
  // [LCF handles command line arguments and invokes the right function]
26
  if (lcf_start(argc, argv))
27
    return 1;
28
29
  // LCF clean up tasks
30
  // [must be the last statement before return]
31
  lcf_cleanup();
32
33
  return 0;
34
}
35
36 56 up20180642
extern uint8_t packet[3];
37
extern int counter;
38 53 up20180655
39
int (mouse_test_packet)(uint32_t cnt) {
40 77 up20180642
    int ret = 0;
41 55 up20180642
    /// loop stuff
42
    int ipc_status, r;
43
    message msg;
44 74 up20180642
    /// Mouse interrupt handling
45 55 up20180642
    uint8_t mouse_irq_bit = 12;
46
    int mouse_id = 0;
47
    int mouse_irq = BIT(mouse_irq_bit);
48 77 up20180642
    if ((ret = mouse_set_data_report(true))) return ret;
49
    //if ((ret = mouse_enable_data_reporting())) return ret;
50 76 up20180655
51 55 up20180642
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
52
    /// cycle
53
    int good = 1;
54 56 up20180642
    uint32_t cnt_now = 0;
55 55 up20180642
    while (good) {
56
        /* Get a request message. */
57
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
58
            printf("driver_receive failed with %d", r);
59
            continue;
60
        }
61
        if (is_ipc_notify(ipc_status)) { /* received notification */
62
            switch (_ENDPOINT_P(msg.m_source)) {
63
                case HARDWARE: /* hardware interrupt notification */
64
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
65
                        mouse_ih();
66 56 up20180642
                        if(counter >= 3){
67
                            struct packet pp = mouse_parse_packet(packet);
68
                            mouse_print_packet(&pp);
69
                            cnt_now++;
70
                            if(cnt == cnt_now) good = 0;
71
                        }
72 55 up20180642
                    }
73
                    break;
74
                default:
75
                    break; /* no other notifications expected: do nothing */
76
            }
77
        } else { /* received standart message, not a notification */
78
            /* no standart message expected: do nothing */
79
        }
80
    }
81
    if (unsubscribe_interrupt(&mouse_id)) return 1;
82 73 up20180642
    if (mouse_set_data_report(false)) return 1;
83 55 up20180642
    return 0;
84 53 up20180655
}
85
86
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
87 70 up20180655
88
    // Mouse packets data
89
    uint8_t packet[3];
90
    int sz = 0;
91
    uint8_t data = 0;
92
    // Cycle
93
    int packetCounter = 0;
94
    int good = 1;
95
    // return value
96
    int ret;
97
98
    while (good) {
99
100
        if ((ret = mouse_read_data(&data))) return ret;
101
102
        if ((data & FIRST_BYTE_ID) || sz) {
103
            packet[sz] = data;
104
            sz++;
105
        }
106
107
        if (sz == 3) {
108
            struct packet pp = mouse_parse_packet(packet);
109
            mouse_print_packet(&pp);
110
            packetCounter++;
111
            sz = 0;
112
            if (packetCounter == cnt) good = 0;
113
        }
114
115
        tickdelay(micros_to_ticks(period*1e3));
116
    }
117
118
    // Set Stream mode
119
    if ((ret = mouse_issue_cmd(SET_STREAM_MD))) return ret;
120
    // Disable data reporting
121 73 up20180642
    if ((ret = mouse_set_data_report(false))) return ret;
122 70 up20180655
123
    uint8_t cmd_byte = minix_get_dflt_kbc_cmd_byte();
124
    if ((ret = kbc_change_cmd(cmd_byte))) return ret;
125
126
    return SUCCESS;
127 53 up20180655
}
128
129
int (mouse_test_async)(uint8_t idle_time) {
130 74 up20180642
    /// loop stuff
131
    int ipc_status, r;
132
    message msg;
133
    /// Timer interrupt handling
134
    const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
135
    uint8_t timer_irq_bit = 0;
136
    int timer_id = 0;
137
    int timer_irq = BIT(timer_irq_bit);
138
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1;
139
140
    no_interrupts = 0;
141
    int time = 0;
142
    /// Mouse interrupt handling
143
    uint8_t mouse_irq_bit = 12;
144
    int mouse_id = 0;
145
    int mouse_irq = BIT(mouse_irq_bit);
146 76 up20180655
    if (mouse_set_data_report(true)) return 1;
147
148 74 up20180642
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
149
    /// cycle
150
    int good = 1;
151
    while (good) {
152
        /* Get a request message. */
153
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
154
            printf("driver_receive failed with %d", r);
155
            continue;
156
        }
157
        if (is_ipc_notify(ipc_status)) { /* received notification */
158
            switch (_ENDPOINT_P(msg.m_source)) {
159
                case HARDWARE: /* hardware interrupt notification */
160
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
161
                        timer_int_handler();
162
                        if (no_interrupts%frequency == 0) time++;
163
                        if(time >= idle_time) good = 0;
164
                    }
165
                    if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt
166
                        mouse_ih();
167
                        if(counter >= 3){
168
                            struct packet pp = mouse_parse_packet(packet);
169
                            mouse_print_packet(&pp);
170
                            time = 0;
171
                            no_interrupts = 0;
172
                        }
173
                    }
174
                    break;
175
                default:
176
                    break; /* no other notifications expected: do nothing */
177
            }
178
        } else { /* received standart message, not a notification */
179
            /* no standart message expected: do nothing */
180
        }
181
    }
182
183
    if (unsubscribe_interrupt(&mouse_id)) return 1;
184
    if (mouse_set_data_report(false)) return 1;
185
186
    if (unsubscribe_interrupt(&timer_id)) return 1;
187
188
    return 0;
189 53 up20180655
}
190
191 77 up20180642
enum state{
192
    STATE_NEUTRAL,
193
    STATE_FIRST,
194
    STATE_MID,
195
    STATE_SECOND
196
};
197
198 54 up20180642
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
199 77 up20180642
    int ret = 0;
200
    /// loop stuff
201
    int ipc_status, r;
202
    message msg;
203
    /// Mouse interrupt handling
204
    uint8_t mouse_irq_bit = 12;
205
    int mouse_id = 0;
206
    int mouse_irq = BIT(mouse_irq_bit);
207
    //if ((ret = mouse_set_data_report(true))) return ret;
208
    if ((ret = mouse_enable_data_reporting())) return ret;
209
210
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
211
    /// cycle
212
    int good = 1;
213
    //int st = STATE_NEUTRAL;
214
    //int16_t dx, dy;
215
    while (good) {
216
        /* Get a request message. */
217
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
218
            printf("driver_receive failed with %d", r);
219
            continue;
220
        }
221
        if (is_ipc_notify(ipc_status)) { /* received notification */
222
            switch (_ENDPOINT_P(msg.m_source)) {
223
                case HARDWARE: /* hardware interrupt notification */
224
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
225
                        mouse_ih();
226
                        if(counter >= 3){
227
                            struct packet pp = mouse_parse_packet(packet);
228
                            mouse_print_packet(&pp);/*
229
                            switch(st){
230
                                case STATE_NEUTRAL:
231
                                    if(pp.lb == 1 && pp.mb == 0 && pp.rb == 0){
232
                                        st = STATE_FIRST;
233
                                        dx = 0;
234
                                        dy = 0;
235
                                    }
236
                                    break;
237
                                case STATE_FIRST:
238
                                    if(pp.lb == 1){
239
                                        if(pp.delta_x >= -tolerance && pp.delta_y >= -tolerance){
240
                                            dx += pp.delta_x;
241
                                            dy += pp.delta_y;
242
                                        }else{
243
                                            st = STATE_NEUTRAL;
244
                                        }
245
                                    }else{
246
                                        if(abs(dx) < abs(dy) && dx > 0 && dy > 0){
247
                                            st = STATE_MID;
248
                                        }else{
249
                                            st = STATE_NEUTRAL;
250
                                        }
251
                                    }
252
                                    break;
253
                                case STATE_MID:
254
                                    if(pp.lb == 0 && pp.mb == 0 && pp.rb == 1){
255
                                        st = STATE_SECOND;
256
                                        dx = 0;
257
                                        dy = 0;
258
                                    }else{
259
                                        st = STATE_NEUTRAL;
260
                                    }
261
                                    break;
262
                                case STATE_SECOND:
263
                                    if(pp.rb == 1){
264
                                        if(pp.delta_x >= -tolerance && pp.delta_y <= tolerance){
265
                                            dx += pp.delta_x;
266
                                            dy += pp.delta_y;
267
                                        }else{
268
                                            st = STATE_NEUTRAL;
269
                                        }
270
                                    }else{
271
                                        if(abs(dx) < abs(dy) && dx > 0 && dy < 0){
272
                                            st = STATE_NEUTRAL;
273
                                            good = false;
274
                                        }else{
275
                                            st = STATE_NEUTRAL;
276
                                        }
277
                                    }
278
                                    break;
279
                                default:
280
                                    return OTHER_ERROR;
281
                                    break;
282
                            }*/
283
                        }
284
                    }
285
                    break;
286
                default:
287
                    break; /* no other notifications expected: do nothing */
288
            }
289
        } else { /* received standart message, not a notification */
290
            /* no standart message expected: do nothing */
291
        }
292
    }
293
    if (unsubscribe_interrupt(&mouse_id)) return 1;
294
    if (mouse_set_data_report(false)) return 1;
295
296
    return 0;
297 53 up20180655
}