|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2015 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/runtime.h" |
| 28 | +#include "py/mphal.h" |
| 29 | +#include "modmicrobit.h" |
| 30 | + |
| 31 | +STATIC const mp_float_t bright_levels[7] = { |
| 32 | + 0.0, 1.0 / 9, 2.0 / 9, 4.0 / 9, 6.0 / 9, 7.0 / 9, 1.0, |
| 33 | +}; |
| 34 | + |
| 35 | +STATIC void love(int interval_ms) { |
| 36 | + microbit_image_obj_t *hearts[MP_ARRAY_SIZE(bright_levels)]; |
| 37 | + for (uint i = 0; i < MP_ARRAY_SIZE(bright_levels); i++) { |
| 38 | + hearts[i] = microbit_image_dim(HEART_IMAGE, bright_levels[i]); |
| 39 | + } |
| 40 | + |
| 41 | + for (int iteration = 0; iteration < 8; iteration++) { |
| 42 | + // pause between double beats of the heart |
| 43 | + if (iteration && (iteration & 1) == 0) { |
| 44 | + mp_hal_delay_ms(20 * interval_ms); |
| 45 | + } |
| 46 | + // pulse heart to max brightness |
| 47 | + for(uint step = 0; step < MP_ARRAY_SIZE(bright_levels); ++step) { |
| 48 | + microbit_display_show(hearts[step]); |
| 49 | + mp_hal_delay_ms(interval_ms); |
| 50 | + } |
| 51 | + // pulse heart to min brightness |
| 52 | + for(int step = MP_ARRAY_SIZE(bright_levels) - 1; step >= 0; --step) { |
| 53 | + microbit_display_show(hearts[step]); |
| 54 | + mp_hal_delay_ms(interval_ms); |
| 55 | + } |
| 56 | + } |
| 57 | + microbit_display_clear(); |
| 58 | +} |
| 59 | + |
| 60 | +STATIC mp_obj_t love_badaboom(void) { |
| 61 | + // make |
| 62 | + love(25); |
| 63 | + // ! war |
| 64 | + return mp_const_none; |
| 65 | +} |
| 66 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(love_badaboom_obj, love_badaboom); |
| 67 | + |
| 68 | +STATIC const mp_map_elem_t love_module_globals_table[] = { |
| 69 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_love) }, |
| 70 | + { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&love_badaboom_obj }, |
| 71 | + { MP_OBJ_NEW_QSTR(MP_QSTR_badaboom), (mp_obj_t)&love_badaboom_obj }, |
| 72 | +}; |
| 73 | +STATIC MP_DEFINE_CONST_DICT(love_module_globals, love_module_globals_table); |
| 74 | + |
| 75 | +const mp_obj_module_t love_module = { |
| 76 | + .base = { &mp_type_module }, |
| 77 | + .globals = (mp_obj_dict_t *)&love_module_globals, |
| 78 | +}; |
0 commit comments