summaryrefslogtreecommitdiff
path: root/Year_3/LSM/ex_20190409.c
blob: 4a960c4acf4d09766596ad2e7c72cb7695aed9a0 (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
265
266
267
268
269
270
271
272
273
274
#include "stm32_unict_lib.h"
#include <math.h>
#include <stdio.h>
#include <string.h>

typedef struct {
    char name[10];
    float price;
    char display[2];
    short s_range;
    short e_range;
} drinks_t;

typedef enum {
    SELECTING,
    EROGATING,
    RESTING,
} status_t;

typedef struct {
    int five_c;
    int ten_c;
    int twenty_c;
} rest_t;

drinks_t drinks[3];
float total = 0.0;

int flag_add = 0;
status_t status = SELECTING;
int inc_erogating = 0;
int value;

rest_t rest;

int q_rest[30] = { 0 };
int q_index = 0;
int q_jndex = 0;
int total_to_return;

void
init()
{
    DISPLAY_init();
    GPIO_init(GPIOB);
    GPIO_init(GPIOC);
    GPIO_config_input(GPIOB, 10);
    GPIO_config_EXTI(GPIOB, EXTI10);
    EXTI_enable(EXTI10, FALLING_EDGE);

    GPIO_config_input(GPIOB, 4);
    GPIO_config_EXTI(GPIOB, EXTI4);
    EXTI_enable(EXTI4, FALLING_EDGE);

    GPIO_config_input(GPIOB, 5);
    GPIO_config_EXTI(GPIOB, EXTI5);
    EXTI_enable(EXTI5, FALLING_EDGE);

    GPIO_config_input(GPIOB, 6);
    GPIO_config_EXTI(GPIOB, EXTI6);
    EXTI_enable(EXTI6, FALLING_EDGE);

    GPIO_config_output(GPIOB, 0);
    GPIO_config_output(GPIOC, 2);
    GPIO_config_output(GPIOC, 3);

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

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

    CONSOLE_init();
}

int
search_index_range(int value)
{
    int i;

    for (i = 0; i < 3; ++i) {
        if (drinks[i].s_range <= value && drinks[i].e_range >= value)
            break;
    }

    return i;
}

void
get_rest(int drink_index)
{
    total_to_return = floor((total - drinks[drink_index].price) * 100);

    while (rest.twenty_c > 0) {
        if ((total_to_return - 20) >= 0) {
            total_to_return -= 20;
            rest.twenty_c--;
            q_rest[q_index++] = 20;
        } else {
            break;
        }
    }

    while (rest.ten_c > 0) {
        if ((total_to_return - 10) >= 0) {
            total_to_return -= 10;
            rest.ten_c--;
            q_rest[q_index++] = 10;
        } else {
            break;
        }
    }

    while (rest.five_c > 0) {
        if ((total_to_return - 5) >= 0) {
            total_to_return -= 5;
            rest.five_c--;
            q_rest[q_index++] = 5;
        } else {
            break;
        }
    }
}

int
main()
{
    init();

    strcpy(drinks[0].name, "Coffee");
    strcpy(drinks[0].display, "Ca");
    drinks[0].price = 0.35;
    drinks[0].s_range = 0;
    drinks[0].e_range = 28;

    strcpy(drinks[1].name, "The");
    strcpy(drinks[1].display, "te");
    drinks[1].price = 0.40;
    drinks[1].s_range = 29;
    drinks[1].e_range = 38;

    strcpy(drinks[2].name, "Choco");
    strcpy(drinks[2].display, "Ci");
    drinks[2].price = 0.45;
    drinks[2].s_range = 39;
    drinks[2].e_range = 64;

    rest.five_c = 10;
    rest.ten_c = 10;
    rest.twenty_c = 10;

    char s[5];
    char s2[2];

    for (;;) {
        ADC_start(ADC1);
        while (!ADC_completed(ADC1))
            ;
        value = ADC_read(ADC1);
        int index = search_index_range(value);

        sprintf(s, "%s", drinks[index].display);
        DISPLAY_puts(0, s);

        if (status == RESTING) {
            if (inc_erogating) {
                printf("%d %d %d\n", inc_erogating, q_rest[q_jndex], q_jndex);
                sprintf(s2, "%02d", q_rest[q_jndex++]);
            } else {
                sprintf(s2, "  ");
            }
            DISPLAY_puts(2, s2);
            delay_ms(500);
        } else {
            sprintf(s, "%.1f", total);
            DISPLAY_puts(2, s);

            sprintf(s, "%.1f", total - floor(total));
            memcpy(s, &s[2], 1);
            s[1] = '\0';
            DISPLAY_puts(3, s);
        }
    }
    return 0;
}

void
EXTI15_10_IRQHandler(void)
{
    if (EXTI_isset(EXTI10)) {
        if (status == SELECTING) {
            total += 0.5;
            flag_add = 1;
        }

        EXTI_clear(EXTI10);
    }
}

void
EXTI9_5_IRQHandler(void)
{
    if (EXTI_isset(EXTI5)) {
        if (status == SELECTING) {
            total += 0.1;
            flag_add = 1;
        }

        EXTI_clear(EXTI5);
    }

    if (EXTI_isset(EXTI6)) {
        if (status == SELECTING) {
            status = EROGATING;
            GPIO_write(GPIOC, 3, 1);
        }

        EXTI_clear(EXTI6);
    }
}

void
EXTI4_IRQHandler(void)
{
    if (EXTI_isset(EXTI4)) {
        if (status == SELECTING) {
            total += 0.2;
            flag_add = 1;
        }

        EXTI_clear(EXTI4);
    }
}

void
TIM2_IRQHandler(void)
{
    if (TIM_update_check(TIM2)) {
        if (status == SELECTING) {
            if (flag_add == 1) {
                GPIO_write(GPIOB, 0, 1);
                flag_add = 0;
            } else {
                GPIO_write(GPIOB, 0, 0);
            }
        } else if (status == EROGATING) {
            if (inc_erogating == 1) {
                GPIO_write(GPIOC, 3, 0);
                status = RESTING;
                inc_erogating = 0;
                get_rest(search_index_range(value));
                total = 0.0;
                GPIO_write(GPIOA, 7, 0);
            }
            inc_erogating ^= 1;
        } else {
            GPIO_write(GPIOC, 2, 1);
            inc_erogating ^= 1;

            if (q_index == q_jndex) {
                GPIO_write(GPIOC, 2, 0);
                q_index = q_jndex = 0;
                status = SELECTING;
            }
        }

        TIM_update_clear(TIM2);
    }
}