Skip to content

Commit

Permalink
Lib/MicroPython: Added pyb LED support
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviermartin committed Feb 27, 2017
1 parent faaf536 commit 8dd2332
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions Lib/MicroPython/CMakeLists.txt
Expand Up @@ -43,6 +43,7 @@ set(MICROPYTHON_BUILD_HEADER_DIR ${MICROPYTHON_BUILD_DIR}/genhdr)
# List of sources # List of sources
set(micropython_lib_SRCS micropython.c set(micropython_lib_SRCS micropython.c
modpyb.c modpyb.c
led.c
uart_core.c) uart_core.c)


if(NOT IS_DIRECTORY ${MICROPYTHON_SRC_DIR}) if(NOT IS_DIRECTORY ${MICROPYTHON_SRC_DIR})
Expand Down
6 changes: 6 additions & 0 deletions Lib/MicroPython/board/nordic/mpplatformport.h
Expand Up @@ -27,4 +27,10 @@
#ifndef __MP_PLATFORM_PORT_H__ #ifndef __MP_PLATFORM_PORT_H__
#define __MP_PLATFORM_PORT_H__ #define __MP_PLATFORM_PORT_H__


//
// Platform Specific Settings
//
#define PYB_LED_COUNT 4
#define PYB_LED_HAS_INTENSITY 0

#endif #endif
144 changes: 144 additions & 0 deletions Lib/MicroPython/led.c
@@ -0,0 +1,144 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2016 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.
*/

#include "board.h"

#include "py/nlr.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "led.h"

#include <stdio.h>

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

STATIC const pyb_led_obj_t pyb_led_obj[] = {
{ {&pyb_led_type}, 1 },
#if PYB_LED_COUNT > 1
{ {&pyb_led_type}, 2 },
#if PYB_LED_COUNT > 2
{ {&pyb_led_type}, 3 },
#if PYB_LED_COUNT > 3
{ {&pyb_led_type}, 4 },
#endif
#endif
#endif
};

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

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

/// \classmethod \constructor(id)
/// Create an LED object associated with the given LED:
///
/// - `id` is the LED number, 1-4.
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) {
// check arguments
mp_arg_check_num(n_args, n_kw, 1, 1, false);

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

// check led number
if (!(1 <= led_id && led_id <= PYB_LED_COUNT)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LED(%d) does not exist", led_id));
}

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

/// \method on()
/// Turn the LED on.
mp_obj_t led_obj_on(mp_obj_t self_in) {
pyb_led_obj_t *self = self_in;
led_on(self->led_id);
return mp_const_none;
}

/// \method off()
/// Turn the LED off.
mp_obj_t led_obj_off(mp_obj_t self_in) {
pyb_led_obj_t *self = self_in;
led_off(self->led_id);
return mp_const_none;
}

/// \method toggle()
/// Toggle the LED between on and off.
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;
}

/// \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`.
mp_obj_t led_obj_intensity(mp_uint_t n_args, const mp_obj_t *args) {
pyb_led_obj_t *self = args[0];
if (n_args == 1) {
#if PYB_LED_HAS_INTENSITY
return mp_obj_new_int(led_get(self->led_id));
#else
return mp_obj_new_int(led_get(self->led_id) * 255);
#endif
} else {
led_set(self->led_id, mp_obj_get_int(args[1]));
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);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(led_obj_intensity_obj, 1, 2, led_obj_intensity);

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 },
};

STATIC MP_DEFINE_CONST_DICT(led_locals_dict, led_locals_dict_table);

const mp_obj_type_t pyb_led_type = {
{ &mp_type_type },
.name = MP_QSTR_LED,
.print = led_obj_print,
.make_new = led_obj_make_new,
.locals_dict = (mp_obj_t)&led_locals_dict,
};
27 changes: 27 additions & 0 deletions Lib/MicroPython/led.h
@@ -0,0 +1,27 @@
/*
* 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.
*/

extern const mp_obj_type_t pyb_led_type;
3 changes: 3 additions & 0 deletions Lib/MicroPython/modpyb.c
Expand Up @@ -52,8 +52,11 @@
#include "py/mpconfig.h" #include "py/mpconfig.h"
#include "py/obj.h" #include "py/obj.h"


#include "led.h"

STATIC const mp_map_elem_t pyb_module_globals_table[] = { STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) }, { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_LED), (mp_obj_t)&pyb_led_type },
}; };


STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table); STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
Expand Down

0 comments on commit 8dd2332

Please sign in to comment.