summaryrefslogtreecommitdiff
path: root/Year_3/LSM/ex_20211122.c
blob: a7980e1306da0f58e6c70f115b181d036b47b73b (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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "stm32_unict_lib.h"
#include <stdlib.h>
#include <string.h>

typedef enum {
    FIRST = 2,
    F_S,
    SECOND,
    S_T,
    THIRD
} floor_t;

typedef enum {
    CONFIG,
    IDLE,
    CLOSING,
    RUNNING,
    OPENING,
} status_t;

floor_t curr_floor = FIRST;
floor_t next_floor = FIRST;
short direction = 1;

unsigned q_index = 0;
floor_t* queue;

status_t status = IDLE;

short circular_200ms = 0; /* alternates 0 and 1 thanks to `0 ^ 1` */
short handle_1500ms = 0; /* until 15, then restart */

int time = 10; /* 10 * `time` = 1s */
int lift_time;
int time_i = 0;

void
init()
{
    int i;
    queue = malloc(sizeof(floor_t) * 10);
    for (i = 0; i < 10; ++i)
        queue[i] = 0;

    GPIO_init(GPIOB);
    GPIO_init(GPIOC);

    GPIO_config_input(GPIOB, 10); /* X */
    GPIO_config_EXTI(GPIOB, EXTI10);
    EXTI_enable(EXTI10, FALLING_EDGE);

    GPIO_config_input(GPIOB, 4); /* Y */
    GPIO_config_EXTI(GPIOB, EXTI4);
    EXTI_enable(EXTI4, FALLING_EDGE);

    GPIO_config_input(GPIOB, 5); /* Z */
    GPIO_config_EXTI(GPIOB, EXTI5);
    EXTI_enable(EXTI5, FALLING_EDGE);

    GPIO_config_input(GPIOB, 6); /* T */
    GPIO_config_EXTI(GPIOB, EXTI6);
    EXTI_enable(EXTI6, FALLING_EDGE);

    GPIO_config_output(GPIOC, 2); /* YELLOW */
    GPIO_config_output(GPIOC, 3); /* GREEN */
    GPIO_config_output(GPIOB, 0); /* RED */

    DISPLAY_init();

    CONSOLE_init();

    TIM_init(TIM2);
    TIM_set(TIM2, 0);
    TIM_on(TIM2);
    TIM_config_timebase(TIM2, 8400, 840);
    TIM_enable_irq(TIM2, IRQ_UPDATE);

    ADC_init(ADC1, ADC_RES_8, ADC_ALIGN_RIGHT);
    ADC_channel_config(ADC1, GPIOC, 1, 11);
    ADC_on(ADC1);
    ADC_sample_channel(ADC1, 11);

    ADC_start(ADC1);
    while (!ADC_completed(ADC1))
        ;
    int adc_read = ADC_read(ADC1);
    time = ((adc_read * (10 - 4) / 255.0) + 4);
}

int
main()
{
    init();

    char s[4];
    int i;
    int adc_read;

    sprintf(s, "%3d ", curr_floor);

    for (;;) {
        ADC_start(ADC1);
        if (status == CONFIG) {
            while (!ADC_completed(ADC1))
                ;
            adc_read = ADC_read(ADC1);
            time = ((adc_read * (10 - 4) / 255.0) + 4);
            sprintf(s, "%f", time / 10.0);
            DISPLAY_puts(0, s);
        } else {
            sprintf(s, "%3d%c", curr_floor / 2, ((curr_floor % 2) ? '-' : ' '));
            DISPLAY_puts(0, s);
            lift_time = (time / 10 / 2.0) / 0.1;
        }
    }

    free(queue);
    return 0;
}

void
check_queue()
{
    int i;

    for (i = q_index; i != (q_index - 1) % 10; i = (i + 1) % 10) {
        if (queue[i] != 0) {
            goto_floor(queue[i]);
            queue[i] = 0;
            return;
        }
    }

    if (queue[i] != 0) {
        goto_floor(queue[i]);
        queue[i] = 0;
    }
}

void
    goto_floor(floor)
{
    status = CLOSING;
    switch (floor) {
    case 1:
        next_floor = FIRST;
        break;
    case 2:
        next_floor = SECOND;
        break;
    case 3:
        next_floor = THIRD;
        break;
    }

    if (curr_floor < next_floor) {
        direction = 1;
    } else if (curr_floor > next_floor) {
        direction = -1;
    } else {
        direction = 0;
    }
}

void
    call_floor(floor)
{
    if (queue[(q_index - 1) % 10] == floor)
        return;

    if (status == IDLE) {
        goto_floor(floor);
    } else {
        queue[q_index] = floor;
        q_index = (q_index + 1) % 10;
    }
}

void
EXTI15_10_IRQHandler(void)
{
    if (EXTI_isset(EXTI10)) {
        call_floor(1);
        EXTI_clear(10);
    }
}

void
EXTI9_5_IRQHandler(void)
{
    if (EXTI_isset(EXTI5)) {
        call_floor(2);
        EXTI_clear(EXTI5);
    }

    if (EXTI_isset(EXTI6)) {
        if (status == IDLE) {
            status = CONFIG;
        } else if (status == CONFIG) {
            status = IDLE;
        }
        EXTI_clear(EXTI6);
    }
}

void
EXTI4_IRQHandler(void)
{
    if (EXTI_isset(EXTI4)) {
        call_floor(3);
        EXTI_clear(EXTI4);
    }
}

void
TIM2_IRQHandler(void)
{
    if (TIM_update_check(TIM2)) {
        if (status == RUNNING) {
            if (circular_200ms) {
                GPIO_toggle(GPIOC, 2);
            }

            time_i++;

            if (time_i >= lift_time) {
                time_i = 0;
                if (curr_floor == next_floor) {
                    GPIO_write(GPIOC, 2, 0);
                    status = OPENING;
                    time_i = 0;
                } else {
                    curr_floor += direction;
                }
            }
        } else if (status == CLOSING || status == OPENING) {
            if (circular_200ms) {
                if (status == CLOSING)
                    GPIO_toggle(GPIOB, 0);
                else
                    GPIO_toggle(GPIOC, 3);
            }

            handle_1500ms++;
            if (handle_1500ms == 15) {
                if (status == CLOSING) {
                    status = RUNNING;
                } else {
                    status = IDLE;
                }

                handle_1500ms = 0;
                GPIO_write(GPIOB, 0, 0);
                GPIO_write(GPIOC, 3, 0);

                if (status == IDLE)
                    check_queue();
            }
        }

        circular_200ms ^= 1;
        TIM_update_clear(TIM2);
    }
}