led.c 4.04 KB
Newer Older
1
#include <stdio.h>
2
3
#include <stm32f4xx.h>
#include <stm32f4xx_gpio.h>
4
5
6

#include "misc.h"
#include "mpconfig.h"
7
#include "qstr.h"
8
#include "obj.h"
9
10
#include "led.h"

11
/* LED numbers, used internally */
Damien George's avatar
Damien George committed
12
13
14
15
#define PYB_LED_1   (1)
#define PYB_LED_2   (2)
#define PYB_LED_3   (3)
#define PYB_LED_4   (4)
16

17
void led_init(void) {
18
    /* GPIO structure */
19
    GPIO_InitTypeDef GPIO_InitStructure;
20
21

    /* Configure I/O speed, mode, output type and pull */
22
23
24
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
25
26
27
28
29
    GPIO_InitStructure.GPIO_OType = PYB_OTYPE;

    /* Turn off LEDs */
    PYB_LED_OFF(PYB_LED1_PORT, PYB_LED1_PIN);
    PYB_LED_OFF(PYB_LED2_PORT, PYB_LED2_PIN);
mux's avatar
mux committed
30
31
    PYB_LED_OFF(PYB_LED3_PORT, PYB_LED3_PIN);
    PYB_LED_OFF(PYB_LED4_PORT, PYB_LED4_PIN);
32
33
34
35
36
37
38
39
40
41
42
43
44

    /* Initialize LEDs */
    GPIO_InitStructure.GPIO_Pin = PYB_LED1_PIN;
    GPIO_Init(PYB_LED1_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PYB_LED2_PIN;
    GPIO_Init(PYB_LED2_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PYB_LED3_PIN;
    GPIO_Init(PYB_LED3_PORT, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = PYB_LED4_PIN;
    GPIO_Init(PYB_LED4_PORT, &GPIO_InitStructure);
45
46
47
48
49
}

void led_state(pyb_led_t led, int state) {
    GPIO_TypeDef *port;
    uint32_t pin;
50

51
    switch (led) {
52
        case PYB_LED_1:
53
54
            pin  = PYB_LED1_PIN;
            port = PYB_LED1_PORT;
55
56
            break;
        case PYB_LED_2:
57
58
            pin  = PYB_LED2_PIN;
            port = PYB_LED2_PORT;
59
60
            break;
        case PYB_LED_3:
61
62
            pin  = PYB_LED3_PIN;
            port = PYB_LED3_PORT;
63
64
            break;
        case PYB_LED_4:
65
66
            pin  = PYB_LED4_PIN;
            port = PYB_LED4_PORT;
67
            break;
68
        default:
69
            return;
70
    }
71

72
    if (state == 0) {
73
74
        // turn LED off
        PYB_LED_OFF(port, pin);
75
    } else {
76
77
        // turn LED on
        PYB_LED_ON(port, pin);
78
79
80
81
82
83
    }
}

void led_toggle(pyb_led_t led) {
    GPIO_TypeDef *port;
    uint32_t pin;
84

85
    switch (led) {
86
        case PYB_LED_1:
87
88
            pin  = PYB_LED1_PIN;
            port = PYB_LED1_PORT;
89
90
            break;
        case PYB_LED_2:
91
92
            pin  = PYB_LED2_PIN;
            port = PYB_LED2_PORT;
93
94
            break;
        case PYB_LED_3:
95
96
            pin  = PYB_LED3_PIN;
            port = PYB_LED3_PORT;
97
98
            break;
        case PYB_LED_4:
99
100
            pin  = PYB_LED4_PIN;
            port = PYB_LED4_PORT;
101
            break;
102
        default:
103
            return;
104
    }
105

106
    if (!(port->ODR & pin)) {
107
        // turn LED off
108
        PYB_LED_OFF(port, pin);
109
110
    } else {
        // turn LED on (output low)
111
        PYB_LED_ON(port, pin);
112
113
    }
}
114
115
116
117
118
119
120
121
122

/******************************************************************************/
/* Micro Python bindings                                                      */

typedef struct _pyb_led_obj_t {
    mp_obj_base_t base;
    uint led_id;
} pyb_led_obj_t;

123
void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
124
125
126
127
128
129
    pyb_led_obj_t *self = self_in;
    print(env, "<LED %lu>", self->led_id);
}

mp_obj_t led_obj_on(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
130
    led_state(self->led_id, 1);
131
132
133
134
135
    return mp_const_none;
}

mp_obj_t led_obj_off(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
136
    led_state(self->led_id, 0);
137
138
139
140
141
142
    return mp_const_none;
}

static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);

143
144
145
146
147
148
static const mp_method_t led_methods[] = {
    { "on", &led_obj_on_obj },
    { "off", &led_obj_off_obj },
    { NULL, NULL },
};

149
150
151
static const mp_obj_type_t led_obj_type = {
    { &mp_const_type },
    "Led",
152
    .print = led_obj_print,
153
    .methods = led_methods,
154
155
};

156
static mp_obj_t pyb_Led(mp_obj_t led_id) {
157
158
159
160
161
    pyb_led_obj_t *o = m_new_obj(pyb_led_obj_t);
    o->base.type = &led_obj_type;
    o->led_id = mp_obj_get_int(led_id);
    return o;
}
162
163

MP_DEFINE_CONST_FUN_OBJ_1(pyb_Led_obj, pyb_Led);