Project

General

Profile

Statistics
| Revision:

root / lab4 / lab4.c @ 85

History | View | Annotate | Download (8.18 KB)

1
#include <lcom/lcf.h>
2

    
3
#include <stdint.h>
4
#include <stdio.h>
5

    
6
#include "mouse.h"
7
#include "kbc.h"
8
#include "errors.h"
9
#include "mouse_macros.h"
10
#include "kbc_macros.h"
11
#include "timer.h"
12
#include "state_machine.h"
13

    
14
int main(int argc, char *argv[]) {
15
  // sets the language of LCF messages (can be either EN-US or PT-PT)
16
  lcf_set_language("EN-US");
17

    
18
  // enables to log function invocations that are being "wrapped" by LCF
19
  // [comment this out if you don't want/need/ it]
20
  lcf_trace_calls("/home/lcom/labs/lab4/trace.txt");
21

    
22
  // enables to save the output of printf function calls on a file
23
  // [comment this out if you don't want/need it]
24
  lcf_log_output("/home/lcom/labs/lab4/output.txt");
25

    
26
  // handles control over to LCF
27
  // [LCF handles command line arguments and invokes the right function]
28
  if (lcf_start(argc, argv))
29
    return 1;
30

    
31
  // LCF clean up tasks
32
  // [must be the last statement before return]
33
  lcf_cleanup();
34

    
35
  return 0;
36
}
37

    
38
int (mouse_test_packet)(uint32_t cnt) {
39
    int ret = 0;
40
    /// loop stuff
41
    int ipc_status, r;
42
    message msg;
43
    /// Mouse interrupt handling
44
    uint8_t mouse_irq_bit = 12;
45
    int mouse_id = 0;
46
    int mouse_irq = BIT(mouse_irq_bit);
47
    if ((ret = mouse_set_data_report(true))) return ret;
48
    //if ((ret = mouse_enable_data_reporting())) return ret;
49

    
50
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
51
    /// cycle
52
    int good = cnt != 0;
53
    uint32_t cnt_now = 0;
54
    while (good) {
55
        /* Get a request message. */
56
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
57
            printf("driver_receive failed with %d", r);
58
            continue;
59
        }
60
        if (is_ipc_notify(ipc_status)) { /* received notification */
61
            switch (_ENDPOINT_P(msg.m_source)) {
62
                case HARDWARE: /* hardware interrupt notification */
63
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
64
                        mouse_ih();
65
                        if(counter_mouse_ih >= 3){
66
                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
67
                            mouse_print_packet(&pp);
68
                            cnt_now++;
69
                            if(cnt == cnt_now) good = 0;
70
                        }
71
                    }
72
                    break;
73
                default:
74
                    break; /* no other notifications expected: do nothing */
75
            }
76
        } else { /* received standart message, not a notification */
77
            /* no standart message expected: do nothing */
78
        }
79
    }
80
    if (unsubscribe_interrupt(&mouse_id)) return 1;
81
    if (mouse_set_data_report(false)) return 1;
82
    return 0;
83
}
84

    
85
int (mouse_test_remote)(uint16_t period, uint8_t cnt) {
86
    int ret;
87
    //if ((ret = mouse_issue_cmd(SET_REMOTE_MD))) return ret;
88
    //if ((ret = mouse_set_data_report(true))) return ret;
89

    
90
    struct packet pp;
91
    while(cnt--){
92
        if(mouse_poll(&pp, period)) return 1;
93
        mouse_print_packet(&pp);
94
        tickdelay(micros_to_ticks(period*1000));
95
    }
96
    /*
97
    while (good) {
98

99
        if ((ret = mouse_read_data(&data))) return ret;
100

101
        if ((data & FIRST_BYTE_ID) || sz) {
102
            packet[sz] = data;
103
            sz++;
104
        }
105
        if (sz == 3) {
106
            struct packet pp = mouse_parse_packet(packet);
107
            mouse_print_packet(&pp);
108
            packetCounter++;
109
            sz = 0;
110
            if (packetCounter == cnt) good = 0;
111
        }
112
        tickdelay(micros_to_ticks(period*1e3));
113
    }
114
    */
115
    // Set Stream mode
116
    if ((ret = mouse_issue_cmd(SET_STREAM_MD))) return ret;
117
    // Disable data reporting
118
    if ((ret = mouse_set_data_report(false))) return ret;
119
    uint8_t cmd_byte = minix_get_dflt_kbc_cmd_byte();
120
    if ((ret = kbc_change_cmd(cmd_byte))) return ret;
121

    
122
    return SUCCESS;
123
}
124

    
125
int (mouse_test_async)(uint8_t idle_time) {
126
    /// loop stuff
127
    int ipc_status, r;
128
    message msg;
129
    /// Timer interrupt handling
130
    const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
131
    uint8_t timer_irq_bit = 0;
132
    int timer_id = 0;
133
    int timer_irq = BIT(timer_irq_bit);
134
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1;
135

    
136
    no_interrupts = 0;
137
    int time = 0;
138
    /// Mouse interrupt handling
139
    uint8_t mouse_irq_bit = 12;
140
    int mouse_id = 0;
141
    int mouse_irq = BIT(mouse_irq_bit);
142
    if (mouse_set_data_report(true)) return 1;
143

    
144
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
145
    /// cycle
146
    int good = 1;
147
    while (good) {
148
        /* Get a request message. */
149
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
150
            printf("driver_receive failed with %d", r);
151
            continue;
152
        }
153
        if (is_ipc_notify(ipc_status)) { /* received notification */
154
            switch (_ENDPOINT_P(msg.m_source)) {
155
                case HARDWARE: /* hardware interrupt notification */
156
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
157
                        timer_int_handler();
158
                        if (no_interrupts%frequency == 0) time++;
159
                        if(time >= idle_time) good = 0;
160
                    }
161
                    if (msg.m_notify.interrupts & mouse_irq) { /// subscribed interrupt
162
                        mouse_ih();
163
                        if(counter_mouse_ih >= 3){
164
                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
165
                            mouse_print_packet(&pp);
166
                            time = 0;
167
                            no_interrupts = 0;
168
                        }
169
                    }
170
                    break;
171
                default:
172
                    break; /* no other notifications expected: do nothing */
173
            }
174
        } else { /* received standart message, not a notification */
175
            /* no standart message expected: do nothing */
176
        }
177
    }
178

    
179
    if (unsubscribe_interrupt(&mouse_id)) return 1;
180
    if (mouse_set_data_report(false)) return 1;
181

    
182
    if (unsubscribe_interrupt(&timer_id)) return 1;
183

    
184
    return 0;
185
}
186

    
187
int (mouse_test_gesture)(uint8_t x_len, uint8_t tolerance) {
188
    int ret;
189
    /// loop stuff
190
    int ipc_status, r;
191
    message msg;
192
    /// Mouse interrupt handling
193
    uint8_t mouse_irq_bit = 12;
194
    int mouse_id = 0;
195
    int mouse_irq = BIT(mouse_irq_bit);
196
    if ((ret = mouse_set_data_report(true))) return ret;
197

    
198
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1;
199
    /// cycle
200
    int good = 1;
201

    
202
    // mouse event gesture
203
    struct mouse_ev *event;
204
    struct packet pp;
205
    int response;
206
    while (good) {
207
        /* Get a request message. */
208
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
209
            printf("driver_receive failed with %d", r);
210
            continue;
211
        }
212
        if (is_ipc_notify(ipc_status)) { /* received notification */
213
            switch (_ENDPOINT_P(msg.m_source)) {
214
                case HARDWARE: /* hardware interrupt notification */
215
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
216
                        mouse_ih();
217
                        if(counter_mouse_ih >= 3) {
218
                            pp = mouse_parse_packet(packet_mouse_ih);
219
                            mouse_print_packet(&pp);
220
                            event = mouse_get_event(&pp);
221

    
222
                            response = state_machine(event, x_len, tolerance);
223

    
224
                            if (response == SUCCESS)
225
                                good = 0;
226
                            else if (response == INVALID_STATE)
227
                                return response;
228
                        }
229
                    }
230
                    break;
231
                default:
232
                    break; /* no other notifications expected: do nothing */
233
            }
234
        } else { /* received standart message, not a notification */
235
            /* no standart message expected: do nothing */
236
        }
237
    }
238
    if (unsubscribe_interrupt(&mouse_id)) return 1;
239
    if (mouse_set_data_report(false)) return 1;
240

    
241
    return 0;
242
}