2014-05-03 22:27:38 +00:00
|
|
|
/*
|
2016-01-29 22:44:43 +00:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 22:27:38 +00:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2016-01-29 22:44:43 +00:00
|
|
|
* Copyright (c) 2013-2016 Damien P. George
|
2014-05-03 22:27:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-13 01:06:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-01-01 21:06:20 +00:00
|
|
|
#include "py/runtime.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2014-04-02 14:09:36 +00:00
|
|
|
#include "timer.h"
|
2014-03-13 01:06:26 +00:00
|
|
|
#include "led.h"
|
|
|
|
#include "pin.h"
|
|
|
|
|
2015-01-16 05:45:21 +00:00
|
|
|
#if defined(MICROPY_HW_LED1)
|
|
|
|
|
2014-04-29 21:55:34 +00:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class LED - LED object
|
|
|
|
///
|
|
|
|
/// The LED object controls an individual LED (Light Emitting Diode).
|
|
|
|
|
2016-05-13 10:01:21 +00:00
|
|
|
// the default is that LEDs are not inverted, and pin driven high turns them on
|
|
|
|
#ifndef MICROPY_HW_LED_INVERTED
|
|
|
|
#define MICROPY_HW_LED_INVERTED (0)
|
|
|
|
#endif
|
|
|
|
|
2014-04-01 17:20:52 +00:00
|
|
|
typedef struct _pyb_led_obj_t {
|
|
|
|
mp_obj_base_t base;
|
2014-07-03 12:25:24 +00:00
|
|
|
mp_uint_t led_id;
|
2023-08-08 16:14:13 +00:00
|
|
|
const machine_pin_obj_t *led_pin;
|
2014-04-01 17:20:52 +00:00
|
|
|
} pyb_led_obj_t;
|
|
|
|
|
|
|
|
STATIC const pyb_led_obj_t pyb_led_obj[] = {
|
2018-03-28 05:13:21 +00:00
|
|
|
{{&pyb_led_type}, 1, MICROPY_HW_LED1},
|
2020-02-27 04:36:53 +00:00
|
|
|
#if defined(MICROPY_HW_LED2)
|
2018-03-28 05:13:21 +00:00
|
|
|
{{&pyb_led_type}, 2, MICROPY_HW_LED2},
|
2020-02-27 04:36:53 +00:00
|
|
|
#if defined(MICROPY_HW_LED3)
|
2018-03-28 05:13:21 +00:00
|
|
|
{{&pyb_led_type}, 3, MICROPY_HW_LED3},
|
2020-02-27 04:36:53 +00:00
|
|
|
#if defined(MICROPY_HW_LED4)
|
2018-03-28 05:13:21 +00:00
|
|
|
{{&pyb_led_type}, 4, MICROPY_HW_LED4},
|
2020-07-02 07:36:27 +00:00
|
|
|
#if defined(MICROPY_HW_LED5)
|
|
|
|
{{&pyb_led_type}, 5, MICROPY_HW_LED5},
|
|
|
|
#if defined(MICROPY_HW_LED6)
|
|
|
|
{{&pyb_led_type}, 6, MICROPY_HW_LED6},
|
|
|
|
#endif
|
|
|
|
#endif
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-03-13 01:06:26 +00:00
|
|
|
};
|
2014-06-19 16:54:34 +00:00
|
|
|
#define NUM_LEDS MP_ARRAY_SIZE(pyb_led_obj)
|
2014-03-13 01:06:26 +00:00
|
|
|
|
|
|
|
void led_init(void) {
|
|
|
|
/* Turn off LEDs and initialize */
|
|
|
|
for (int led = 0; led < NUM_LEDS; led++) {
|
2023-08-08 16:14:13 +00:00
|
|
|
const machine_pin_obj_t *led_pin = pyb_led_obj[led].led_pin;
|
2015-08-02 23:05:16 +00:00
|
|
|
mp_hal_gpio_clock_enable(led_pin->gpio);
|
2014-04-01 17:20:52 +00:00
|
|
|
MICROPY_HW_LED_OFF(led_pin);
|
2016-10-18 03:43:05 +00:00
|
|
|
mp_hal_pin_output(led_pin);
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 22:44:43 +00:00
|
|
|
#if defined(MICROPY_HW_LED1_PWM) \
|
|
|
|
|| defined(MICROPY_HW_LED2_PWM) \
|
|
|
|
|| defined(MICROPY_HW_LED3_PWM) \
|
2020-07-02 07:36:27 +00:00
|
|
|
|| defined(MICROPY_HW_LED4_PWM) \
|
|
|
|
|| defined(MICROPY_HW_LED5_PWM) \
|
|
|
|
|| defined(MICROPY_HW_LED6_PWM)
|
2016-01-29 22:44:43 +00:00
|
|
|
|
|
|
|
// The following is semi-generic code to control LEDs using PWM.
|
2016-05-13 10:01:21 +00:00
|
|
|
// It currently supports TIM1, TIM2 and TIM3, channels 1-4.
|
2016-01-29 22:44:43 +00:00
|
|
|
// Configure by defining the relevant MICROPY_HW_LEDx_PWM macros in mpconfigboard.h.
|
|
|
|
// If they are not defined then PWM will not be available for that LED.
|
|
|
|
|
|
|
|
#define LED_PWM_ENABLED (1)
|
|
|
|
|
|
|
|
#ifndef MICROPY_HW_LED1_PWM
|
2016-05-13 10:01:21 +00:00
|
|
|
#define MICROPY_HW_LED1_PWM { NULL, 0, 0, 0 }
|
2016-01-29 22:44:43 +00:00
|
|
|
#endif
|
|
|
|
#ifndef MICROPY_HW_LED2_PWM
|
2016-05-13 10:01:21 +00:00
|
|
|
#define MICROPY_HW_LED2_PWM { NULL, 0, 0, 0 }
|
2016-01-29 22:44:43 +00:00
|
|
|
#endif
|
|
|
|
#ifndef MICROPY_HW_LED3_PWM
|
2016-05-13 10:01:21 +00:00
|
|
|
#define MICROPY_HW_LED3_PWM { NULL, 0, 0, 0 }
|
2016-01-29 22:44:43 +00:00
|
|
|
#endif
|
|
|
|
#ifndef MICROPY_HW_LED4_PWM
|
2016-05-13 10:01:21 +00:00
|
|
|
#define MICROPY_HW_LED4_PWM { NULL, 0, 0, 0 }
|
2016-01-29 22:44:43 +00:00
|
|
|
#endif
|
2020-07-02 07:36:27 +00:00
|
|
|
#ifndef MICROPY_HW_LED5_PWM
|
|
|
|
#define MICROPY_HW_LED5_PWM { NULL, 0, 0, 0 }
|
|
|
|
#endif
|
|
|
|
#ifndef MICROPY_HW_LED6_PWM
|
|
|
|
#define MICROPY_HW_LED6_PWM { NULL, 0, 0, 0 }
|
|
|
|
#endif
|
2016-01-29 22:44:43 +00:00
|
|
|
|
|
|
|
#define LED_PWM_TIM_PERIOD (10000) // TIM runs at 1MHz and fires every 10ms
|
|
|
|
|
2016-05-13 10:01:21 +00:00
|
|
|
// this gives the address of the CCR register for channels 1-4
|
2020-02-27 04:36:53 +00:00
|
|
|
#define LED_PWM_CCR(pwm_cfg) ((volatile uint32_t *)&(pwm_cfg)->tim->CCR1 + ((pwm_cfg)->tim_channel >> 2))
|
2016-05-13 10:01:21 +00:00
|
|
|
|
2016-01-29 22:44:43 +00:00
|
|
|
typedef struct _led_pwm_config_t {
|
|
|
|
TIM_TypeDef *tim;
|
|
|
|
uint8_t tim_id;
|
2016-05-13 10:01:21 +00:00
|
|
|
uint8_t tim_channel;
|
2016-01-29 22:44:43 +00:00
|
|
|
uint8_t alt_func;
|
|
|
|
} led_pwm_config_t;
|
|
|
|
|
|
|
|
STATIC const led_pwm_config_t led_pwm_config[] = {
|
|
|
|
MICROPY_HW_LED1_PWM,
|
|
|
|
MICROPY_HW_LED2_PWM,
|
|
|
|
MICROPY_HW_LED3_PWM,
|
|
|
|
MICROPY_HW_LED4_PWM,
|
2020-07-02 07:36:27 +00:00
|
|
|
MICROPY_HW_LED5_PWM,
|
|
|
|
MICROPY_HW_LED6_PWM,
|
2016-01-29 22:44:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC uint8_t led_pwm_state = 0;
|
|
|
|
|
|
|
|
static inline bool led_pwm_is_enabled(int led) {
|
|
|
|
return (led_pwm_state & (1 << led)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function has a large stack so it should not be inlined
|
|
|
|
STATIC void led_pwm_init(int led) __attribute__((noinline));
|
|
|
|
STATIC void led_pwm_init(int led) {
|
2023-08-08 16:14:13 +00:00
|
|
|
const machine_pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
|
2016-01-29 22:44:43 +00:00
|
|
|
const led_pwm_config_t *pwm_cfg = &led_pwm_config[led - 1];
|
|
|
|
|
|
|
|
// GPIO configuration
|
2016-12-22 03:50:52 +00:00
|
|
|
mp_hal_pin_config(led_pin, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, pwm_cfg->alt_func);
|
2016-01-29 22:44:43 +00:00
|
|
|
|
|
|
|
// TIM configuration
|
|
|
|
switch (pwm_cfg->tim_id) {
|
2020-02-27 04:36:53 +00:00
|
|
|
case 1:
|
|
|
|
__TIM1_CLK_ENABLE();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
__TIM2_CLK_ENABLE();
|
|
|
|
break;
|
2020-09-16 08:29:33 +00:00
|
|
|
#if defined(TIM3)
|
2020-02-27 04:36:53 +00:00
|
|
|
case 3:
|
|
|
|
__TIM3_CLK_ENABLE();
|
|
|
|
break;
|
2020-09-16 08:29:33 +00:00
|
|
|
#endif
|
2020-02-27 04:36:53 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
2016-01-29 22:44:43 +00:00
|
|
|
}
|
2016-12-22 03:02:09 +00:00
|
|
|
TIM_HandleTypeDef tim = {0};
|
2016-01-29 22:44:43 +00:00
|
|
|
tim.Instance = pwm_cfg->tim;
|
|
|
|
tim.Init.Period = LED_PWM_TIM_PERIOD - 1;
|
|
|
|
tim.Init.Prescaler = timer_get_source_freq(pwm_cfg->tim_id) / 1000000 - 1; // TIM runs at 1MHz
|
|
|
|
tim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
tim.Init.CounterMode = TIM_COUNTERMODE_UP;
|
2016-05-13 10:01:21 +00:00
|
|
|
tim.Init.RepetitionCounter = 0;
|
2016-01-29 22:44:43 +00:00
|
|
|
HAL_TIM_PWM_Init(&tim);
|
|
|
|
|
2016-05-13 10:01:21 +00:00
|
|
|
// PWM configuration
|
2016-01-29 22:44:43 +00:00
|
|
|
TIM_OC_InitTypeDef oc_init;
|
|
|
|
oc_init.OCMode = TIM_OCMODE_PWM1;
|
|
|
|
oc_init.Pulse = 0; // off
|
2016-05-13 10:01:21 +00:00
|
|
|
oc_init.OCPolarity = MICROPY_HW_LED_INVERTED ? TIM_OCPOLARITY_LOW : TIM_OCPOLARITY_HIGH;
|
2016-01-29 22:44:43 +00:00
|
|
|
oc_init.OCFastMode = TIM_OCFAST_DISABLE;
|
2016-05-13 10:01:21 +00:00
|
|
|
oc_init.OCNPolarity = TIM_OCNPOLARITY_HIGH; // needed for TIM1 and TIM8
|
|
|
|
oc_init.OCIdleState = TIM_OCIDLESTATE_SET; // needed for TIM1 and TIM8
|
|
|
|
oc_init.OCNIdleState = TIM_OCNIDLESTATE_SET; // needed for TIM1 and TIM8
|
|
|
|
HAL_TIM_PWM_ConfigChannel(&tim, &oc_init, pwm_cfg->tim_channel);
|
|
|
|
HAL_TIM_PWM_Start(&tim, pwm_cfg->tim_channel);
|
2016-01-29 22:44:43 +00:00
|
|
|
|
|
|
|
// indicate that this LED is using PWM
|
|
|
|
led_pwm_state |= 1 << led;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void led_pwm_deinit(int led) {
|
|
|
|
// make the LED's pin a standard GPIO output pin
|
2023-08-08 16:14:13 +00:00
|
|
|
const machine_pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
|
2016-01-29 22:44:43 +00:00
|
|
|
GPIO_TypeDef *g = led_pin->gpio;
|
|
|
|
uint32_t pin = led_pin->pin;
|
|
|
|
static const int mode = 1; // output
|
|
|
|
static const int alt = 0; // no alt func
|
|
|
|
g->MODER = (g->MODER & ~(3 << (2 * pin))) | (mode << (2 * pin));
|
|
|
|
g->AFR[pin >> 3] = (g->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
|
|
|
|
led_pwm_state &= ~(1 << led);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define LED_PWM_ENABLED (0)
|
|
|
|
#endif
|
|
|
|
|
2014-03-13 01:06:26 +00:00
|
|
|
void led_state(pyb_led_t led, int state) {
|
|
|
|
if (led < 1 || led > NUM_LEDS) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-29 22:44:43 +00:00
|
|
|
|
2023-08-08 16:14:13 +00:00
|
|
|
const machine_pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
|
2014-03-13 01:06:26 +00:00
|
|
|
if (state == 0) {
|
|
|
|
// turn LED off
|
2014-03-30 11:30:35 +00:00
|
|
|
MICROPY_HW_LED_OFF(led_pin);
|
2014-03-13 01:06:26 +00:00
|
|
|
} else {
|
|
|
|
// turn LED on
|
2014-03-30 11:30:35 +00:00
|
|
|
MICROPY_HW_LED_ON(led_pin);
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
2016-01-29 22:44:43 +00:00
|
|
|
|
|
|
|
#if LED_PWM_ENABLED
|
|
|
|
if (led_pwm_is_enabled(led)) {
|
|
|
|
led_pwm_deinit(led);
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void led_toggle(pyb_led_t led) {
|
|
|
|
if (led < 1 || led > NUM_LEDS) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-23 00:25:09 +00:00
|
|
|
|
2016-01-29 22:44:43 +00:00
|
|
|
#if LED_PWM_ENABLED
|
|
|
|
if (led_pwm_is_enabled(led)) {
|
|
|
|
// if PWM is enabled then LED has non-zero intensity, so turn it off
|
|
|
|
led_state(led, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-17 18:57:17 +00:00
|
|
|
// toggle the output data register to toggle the LED state
|
2023-08-08 16:14:13 +00:00
|
|
|
const machine_pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
|
2014-06-17 18:57:17 +00:00
|
|
|
led_pin->gpio->ODR ^= led_pin->pin_mask;
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 00:25:09 +00:00
|
|
|
int led_get_intensity(pyb_led_t led) {
|
2014-03-22 23:54:13 +00:00
|
|
|
if (led < 1 || led > NUM_LEDS) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-23 00:25:09 +00:00
|
|
|
|
2016-01-29 22:44:43 +00:00
|
|
|
#if LED_PWM_ENABLED
|
|
|
|
if (led_pwm_is_enabled(led)) {
|
2016-05-13 10:01:21 +00:00
|
|
|
const led_pwm_config_t *pwm_cfg = &led_pwm_config[led - 1];
|
|
|
|
mp_uint_t i = (*LED_PWM_CCR(pwm_cfg) * 255 + LED_PWM_TIM_PERIOD - 2) / (LED_PWM_TIM_PERIOD - 1);
|
2016-01-29 22:44:43 +00:00
|
|
|
if (i > 255) {
|
|
|
|
i = 255;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-08-08 16:14:13 +00:00
|
|
|
const machine_pin_obj_t *led_pin = pyb_led_obj[led - 1].led_pin;
|
2014-03-22 23:54:13 +00:00
|
|
|
GPIO_TypeDef *gpio = led_pin->gpio;
|
|
|
|
|
|
|
|
if (gpio->ODR & led_pin->pin_mask) {
|
|
|
|
// pin is high
|
2016-05-13 10:01:21 +00:00
|
|
|
return MICROPY_HW_LED_INVERTED ? 0 : 255;
|
2014-03-22 23:54:13 +00:00
|
|
|
} else {
|
|
|
|
// pin is low
|
2016-05-13 10:01:21 +00:00
|
|
|
return MICROPY_HW_LED_INVERTED ? 255 : 0;
|
2014-03-22 23:54:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 12:25:24 +00:00
|
|
|
void led_set_intensity(pyb_led_t led, mp_int_t intensity) {
|
2016-01-29 22:44:43 +00:00
|
|
|
#if LED_PWM_ENABLED
|
|
|
|
if (intensity > 0 && intensity < 255) {
|
2016-05-13 10:01:21 +00:00
|
|
|
const led_pwm_config_t *pwm_cfg = &led_pwm_config[led - 1];
|
|
|
|
if (pwm_cfg->tim != NULL) {
|
2016-01-29 22:44:43 +00:00
|
|
|
// set intensity using PWM pulse width
|
|
|
|
if (!led_pwm_is_enabled(led)) {
|
|
|
|
led_pwm_init(led);
|
|
|
|
}
|
2016-05-13 10:01:21 +00:00
|
|
|
*LED_PWM_CCR(pwm_cfg) = intensity * (LED_PWM_TIM_PERIOD - 1) / 255;
|
2016-01-29 22:44:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-23 00:25:09 +00:00
|
|
|
// intensity not supported for this LED; just turn it on/off
|
|
|
|
led_state(led, intensity > 0);
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:33:30 +00:00
|
|
|
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);
|
2017-03-02 04:02:57 +00:00
|
|
|
mp_hal_delay_ms(delay);
|
2014-03-20 23:33:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 01:06:26 +00:00
|
|
|
/******************************************************************************/
|
2017-06-30 07:22:17 +00:00
|
|
|
/* MicroPython bindings */
|
2014-03-13 01:06:26 +00:00
|
|
|
|
2015-04-09 22:56:15 +00:00
|
|
|
void led_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-07-08 13:25:11 +00:00
|
|
|
pyb_led_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2018-05-04 05:52:03 +00:00
|
|
|
mp_printf(print, "LED(%u)", self->led_id);
|
2014-03-13 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 21:55:34 +00:00
|
|
|
/// \classmethod \constructor(id)
|
|
|
|
/// Create an LED object associated with the given LED:
|
|
|
|
///
|
|
|
|
/// - `id` is the LED number, 1-4.
|
2017-01-04 13:10:42 +00:00
|
|
|
STATIC mp_obj_t led_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-03-24 11:48:39 +00:00
|
|
|
// check arguments
|
2014-04-19 23:16:30 +00:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
2014-03-24 11:48:39 +00:00
|
|
|
|
|
|
|
// get led number
|
2014-07-03 12:25:24 +00:00
|
|
|
mp_int_t led_id = mp_obj_get_int(args[0]);
|
2014-03-24 11:48:39 +00:00
|
|
|
|
|
|
|
// check led number
|
2014-04-30 18:23:21 +00:00
|
|
|
if (!(1 <= led_id && led_id <= NUM_LEDS)) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("LED(%d) doesn't exist"), led_id);
|
2014-03-24 11:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return static led object
|
2018-07-08 13:25:11 +00:00
|
|
|
return MP_OBJ_FROM_PTR(&pyb_led_obj[led_id - 1]);
|
2014-03-24 11:48:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 21:55:34 +00:00
|
|
|
/// \method on()
|
|
|
|
/// Turn the LED on.
|
2014-03-13 01:06:26 +00:00
|
|
|
mp_obj_t led_obj_on(mp_obj_t self_in) {
|
2018-07-08 13:25:11 +00:00
|
|
|
pyb_led_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-03-13 01:06:26 +00:00
|
|
|
led_state(self->led_id, 1);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-04-29 21:55:34 +00:00
|
|
|
/// \method off()
|
|
|
|
/// Turn the LED off.
|
2014-03-13 01:06:26 +00:00
|
|
|
mp_obj_t led_obj_off(mp_obj_t self_in) {
|
2018-07-08 13:25:11 +00:00
|
|
|
pyb_led_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-03-13 01:06:26 +00:00
|
|
|
led_state(self->led_id, 0);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-04-29 21:55:34 +00:00
|
|
|
/// \method toggle()
|
|
|
|
/// Toggle the LED between on and off.
|
2014-03-13 01:06:26 +00:00
|
|
|
mp_obj_t led_obj_toggle(mp_obj_t self_in) {
|
2018-07-08 13:25:11 +00:00
|
|
|
pyb_led_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-03-13 01:06:26 +00:00
|
|
|
led_toggle(self->led_id);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-04-29 21:55:34 +00:00
|
|
|
/// \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`.
|
2017-08-30 00:59:58 +00:00
|
|
|
mp_obj_t led_obj_intensity(size_t n_args, const mp_obj_t *args) {
|
2018-07-08 13:25:11 +00:00
|
|
|
pyb_led_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-03-23 00:25:09 +00:00
|
|
|
if (n_args == 1) {
|
|
|
|
return mp_obj_new_int(led_get_intensity(self->led_id));
|
2014-03-22 23:54:13 +00:00
|
|
|
} else {
|
2014-03-23 00:25:09 +00:00
|
|
|
led_set_intensity(self->led_id, mp_obj_get_int(args[1]));
|
2014-03-22 23:54:13 +00:00
|
|
|
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);
|
2014-03-23 00:25:09 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(led_obj_intensity_obj, 1, 2, led_obj_intensity);
|
2014-03-13 01:06:26 +00:00
|
|
|
|
2017-05-06 07:03:40 +00:00
|
|
|
STATIC const mp_rom_map_elem_t led_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&led_obj_on_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&led_obj_off_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&led_obj_toggle_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_intensity), MP_ROM_PTR(&led_obj_intensity_obj) },
|
2014-03-13 01:06:26 +00:00
|
|
|
};
|
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table);
|
|
|
|
|
2021-07-14 04:38:38 +00:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pyb_led_type,
|
|
|
|
MP_QSTR_LED,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 14:31:23 +00:00
|
|
|
make_new, led_obj_make_new,
|
2021-07-14 04:38:38 +00:00
|
|
|
print, led_obj_print,
|
2022-06-24 06:27:46 +00:00
|
|
|
locals_dict, &led_locals_dict
|
2021-07-14 04:38:38 +00:00
|
|
|
);
|
2015-01-16 05:45:21 +00:00
|
|
|
|
|
|
|
#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)
|