summaryrefslogtreecommitdiff
path: root/Year_3/LSM/ex_shopqueue.c
blob: b6b513064589788304007d27bcdebae37235e87f (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
#include "stm32_unict_lib.h"
#include <stdio.h>
#include <string.h>

int queue = 0;
int current_ticket = 0;
int average_s = 0;

int incr_s = -1;

int ms500 = 0;
int sum = 0;
int n = 0;

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_output(GPIOB, 0);

    TIM_init(TIM3);
    TIM_set(TIM3, 0);
    TIM_config_timebase(TIM3, 8400, 5000);
    TIM_config_IC(TIM3, 1, IC_FALLING_EDGE);
    TIM_enable_irq(TIM3, IRQ_UPDATE | IRQ_CC_1);
    TIM_on(TIM3);

    CONSOLE_init();
}

int
main()
{
    init();

    char s[2];

    for (;;) {
        if (incr_s >= 0) {
            if ((incr_s % 2) == 1) {
                sprintf(s, "%02d", queue);
            } else {
                sprintf(s, "  ");
            }
        } else {
            sprintf(s, "%02d", current_ticket);
        }

        DISPLAY_puts(0, s);

        sprintf(s, "%02d", average_s);
        DISPLAY_puts(2, s);
    }
    return 0;
}

void
EXTI15_10_IRQHandler(void)
{
    if (EXTI_isset(EXTI10)) {
        queue++;
        incr_s = 0;
        GPIO_write(GPIOB, 0, 1);
        TIM3->CNT = 0;

        EXTI_clear(EXTI10);
    }
}

void
EXTI4_IRQHandler(void)
{
    if (EXTI_isset(EXTI4)) {
        int seconds = ms500 / 2;
        sum += seconds;
        n++;

        average_s = sum / n;
        current_ticket++;
        ms500 = 0;

        EXTI_clear(EXTI4);
    }
}

void
TIM3_IRQHandler(void)
{
    char s[2];
    if (TIM_update_check(TIM3)) {
        ms500++;

        if (incr_s >= 0) {
            incr_s++;

            if (incr_s == 4) {
                incr_s = -1;
                GPIO_write(GPIOB, 0, 0);
            }
        }

        TIM_update_clear(TIM3);
    }
}