led.c 7.25 KB
Newer Older
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
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
28
#include <stdio.h>

29
30
#include "py/nlr.h"
#include "py/runtime.h"
31
#include "py/mphal.h"
32
#include "timer.h"
33
34
#include "led.h"
#include "pin.h"
35
#include "genhdr/pins.h"
36

37
38
#if defined(MICROPY_HW_LED1)

39
40
41
42
43
// default is to not PWM LED4
#if !defined(MICROPY_HW_LED4_PWM)
#define MICROPY_HW_LED4_PWM (0)
#endif

44
45
46
47
48
/// \moduleref pyb
/// \class LED - LED object
///
/// The LED object controls an individual LED (Light Emitting Diode).

49
50
typedef struct _pyb_led_obj_t {
    mp_obj_base_t base;
51
    mp_uint_t led_id;
52
53
54
55
56
    const pin_obj_t *led_pin;
} pyb_led_obj_t;

STATIC const pyb_led_obj_t pyb_led_obj[] = {
    {{&pyb_led_type}, 1, &MICROPY_HW_LED1},
57
#if defined(MICROPY_HW_LED2)
58
    {{&pyb_led_type}, 2, &MICROPY_HW_LED2},
59
#if defined(MICROPY_HW_LED3)
60
    {{&pyb_led_type}, 3, &MICROPY_HW_LED3},
61
#if defined(MICROPY_HW_LED4)
62
    {{&pyb_led_type}, 4, &MICROPY_HW_LED4},
63
64
65
66
#endif
#endif
#endif
};
67
#define NUM_LEDS MP_ARRAY_SIZE(pyb_led_obj)
68
69
70
71
72
73
74

void led_init(void) {
    /* GPIO structure */
    GPIO_InitTypeDef GPIO_InitStructure;

    /* Configure I/O speed, mode, output type and pull */
    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
75
    GPIO_InitStructure.Mode = MICROPY_HW_LED_OTYPE;
76
77
78
79
    GPIO_InitStructure.Pull = GPIO_NOPULL;

    /* Turn off LEDs and initialize */
    for (int led = 0; led < NUM_LEDS; led++) {
80
        const pin_obj_t *led_pin = pyb_led_obj[led].led_pin;
81
        mp_hal_gpio_clock_enable(led_pin->gpio);
82
83
84
        MICROPY_HW_LED_OFF(led_pin);
        GPIO_InitStructure.Pin = led_pin->pin_mask;
        HAL_GPIO_Init(led_pin->gpio, &GPIO_InitStructure);
85
86
87
88
89
90
91
    }
}

void led_state(pyb_led_t led, int state) {
    if (led < 1 || led > NUM_LEDS) {
        return;
    }
92
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
93
    //printf("led_state(%d,%d)\n", led, state);
94
95
    if (state == 0) {
        // turn LED off
96
        MICROPY_HW_LED_OFF(led_pin);
97
98
    } else {
        // turn LED on
99
        MICROPY_HW_LED_ON(led_pin);
100
101
102
103
104
105
106
    }
}

void led_toggle(pyb_led_t led) {
    if (led < 1 || led > NUM_LEDS) {
        return;
    }
107

108
    // toggle the output data register to toggle the LED state
109
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
110
    led_pin->gpio->ODR ^= led_pin->pin_mask;
111
112
}

113
int led_get_intensity(pyb_led_t led) {
114
115
116
    if (led < 1 || led > NUM_LEDS) {
        return 0;
    }
117

118
    const pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
119
120
121
122
123
    GPIO_TypeDef *gpio = led_pin->gpio;

    // TODO convert high/low to on/off depending on board
    if (gpio->ODR & led_pin->pin_mask) {
        // pin is high
124
        return 255;
125
126
127
128
129
130
    } else {
        // pin is low
        return 0;
    }
}

131
void led_set_intensity(pyb_led_t led, mp_int_t intensity) {
132
133
134
135
    // intensity not supported for this LED; just turn it on/off
    led_state(led, intensity > 0);
}

136
137
138
139
140
141
142
143
void led_debug(int n, int delay) {
    led_state(1, n & 1);
    led_state(2, n & 2);
    led_state(3, n & 4);
    led_state(4, n & 8);
    HAL_Delay(delay);
}

144
145
146
/******************************************************************************/
/* Micro Python bindings                                                      */

147
void led_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
148
    pyb_led_obj_t *self = self_in;
149
    mp_printf(print, "LED(%lu)", self->led_id);
150
151
}

152
153
154
155
/// \classmethod \constructor(id)
/// Create an LED object associated with the given LED:
///
///   - `id` is the LED number, 1-4.
156
STATIC mp_obj_t led_obj_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
157
    // check arguments
Damien George's avatar
Damien George committed
158
    mp_arg_check_num(n_args, n_kw, 1, 1, false);
159
160

    // get led number
161
    mp_int_t led_id = mp_obj_get_int(args[0]);
162
163

    // check led number
164
    if (!(1 <= led_id && led_id <= NUM_LEDS)) {
165
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LED(%d) does not exist", led_id));
166
167
168
    }

    // return static led object
169
    return (mp_obj_t)&pyb_led_obj[led_id - 1];
170
171
}

172
173
/// \method on()
/// Turn the LED on.
174
175
176
177
178
179
mp_obj_t led_obj_on(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_state(self->led_id, 1);
    return mp_const_none;
}

180
181
/// \method off()
/// Turn the LED off.
182
183
184
185
186
187
mp_obj_t led_obj_off(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_state(self->led_id, 0);
    return mp_const_none;
}

188
189
/// \method toggle()
/// Toggle the LED between on and off.
190
191
192
193
194
195
mp_obj_t led_obj_toggle(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_toggle(self->led_id);
    return mp_const_none;
}

196
197
198
199
/// \method intensity([value])
/// Get or set the LED intensity.  Intensity ranges between 0 (off) and 255 (full on).
/// If no argument is given, return the LED intensity.
/// If an argument is given, set the LED intensity and return `None`.
200
mp_obj_t led_obj_intensity(mp_uint_t n_args, const mp_obj_t *args) {
201
    pyb_led_obj_t *self = args[0];
202
203
    if (n_args == 1) {
        return mp_obj_new_int(led_get_intensity(self->led_id));
204
    } else {
205
        led_set_intensity(self->led_id, mp_obj_get_int(args[1]));
206
207
208
209
210
211
212
        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);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle);
213
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(led_obj_intensity_obj, 1, 2, led_obj_intensity);
214

215
216
217
218
219
STATIC const mp_map_elem_t led_locals_dict_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR_on), (mp_obj_t)&led_obj_on_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_off), (mp_obj_t)&led_obj_off_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_toggle), (mp_obj_t)&led_obj_toggle_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_intensity), (mp_obj_t)&led_obj_intensity_obj },
220
221
};

222
223
STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table);

224
const mp_obj_type_t pyb_led_type = {
225
    { &mp_type_type },
226
    .name = MP_QSTR_LED,
227
    .print = led_obj_print,
228
    .make_new = led_obj_make_new,
229
    .locals_dict = (mp_obj_t)&led_locals_dict,
230
};
231
232
233
234
235
236
237
238
239
240
241

#else
// For boards with no LEDs, we leave an empty function here so that we don't
// have to put conditionals everywhere.
void led_init(void) {
}
void led_state(pyb_led_t led, int state) {
}
void led_toggle(pyb_led_t led) {
}
#endif  // defined(MICROPY_HW_LED1)