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

#define X_INCR 20
#define Y_INCR 5

#define GET_MONEY 0
#define EROGATING 1
#define EROGATED 2

#define PRICE_L 1.5

/*
 * X = PB10
 * Y = PB4
 * Z = PB5
 * T = PB6
 * output:
 * red = PB0
 * yellow = PC2
 */

int money_tot = 0;
int status = GET_MONEY;
float erogated = 0;
char s[5];
int flashing = 0;

int
main()
{
    GPIO_init(GPIOB);
    GPIO_init(GPIOC);

    GPIO_config_output(GPIOC, 2);
    GPIO_config_output(GPIOB, 0);
    TIM_init(TIM2);
    TIM_set(TIM2, 0);
    TIM_on(TIM2);
    TIM_enable_irq(TIM2, IRQ_UPDATE);
    TIM_config_timebase(TIM2, 8400, 5000);

    GPIO_config_input(GPIOB, 10); /* X */
    GPIO_config_input(GPIOB, 4); /* Y */
    GPIO_config_input(GPIOB, 5); /* Z */
    GPIO_config_input(GPIOB, 6); /* T */

    DISPLAY_init();

    GPIO_config_EXTI(GPIOB, EXTI10);
    EXTI_enable(EXTI10, FALLING_EDGE);
    GPIO_config_EXTI(GPIOB, EXTI4);
    EXTI_enable(EXTI4, FALLING_EDGE);
    GPIO_config_EXTI(GPIOB, EXTI5);
    EXTI_enable(EXTI5, FALLING_EDGE);
    GPIO_config_EXTI(GPIOB, EXTI6);
    EXTI_enable(EXTI6, FALLING_EDGE);

    for (;;) {
        if (status == GET_MONEY) {
            sprintf(s, "%4d", money_tot);
            DISPLAY_puts(0, s);
        } else if (status == EROGATING) {
            sprintf(s, "%g", erogated);
            DISPLAY_puts(0, s);
        }
    }

    return 0;
}

void
EXTI15_10_IRQHandler(void)
{
    if (EXTI_isset(EXTI10)) {
        if (status == GET_MONEY) {
            money_tot += X_INCR;
        }
        EXTI_clear(EXTI10);
    }
}

void
EXTI4_IRQHandler(void)
{
    if (EXTI_isset(EXTI4)) {
        if (status == GET_MONEY) {
            money_tot += Y_INCR;
        }
        EXTI_clear(EXTI4);
    }
}

void
EXTI9_5_IRQHandler(void)
{
    if (EXTI_isset(EXTI5)) {
        if (status == GET_MONEY) {
            money_tot = 0;
        }
        EXTI_clear(EXTI5);
    }

    if (EXTI_isset(EXTI6)) {
        if (status == GET_MONEY) {
            status = EROGATING;
        } else if (status == EROGATED) {
            GPIO_write(GPIOB, 0, 0);
            status = GET_MONEY;
            erogated = 0;
            money_tot = 0;
        }
        EXTI_clear(EXTI6);
    }
}

void
TIM2_IRQHandler(void)
{
    if (erogated >= (money_tot / PRICE_L)) {
        if (status == EROGATING) {
            status = EROGATED;
            GPIO_write(GPIOB, 0, 1);
            GPIO_write(GPIOC, 2, 0);
        }
    } else {
        if (status == EROGATING) {
            GPIO_toggle(GPIOC, 2);
            erogated += 0.375;
        }
    }
    TIM_update_clear(TIM2);
}