Skip to content

Commit 3c9983e

Browse files
committed
codal_port/modlove: Add love module from micro:bit v1.
Taken from ahttps://github.com/bbcmicrobit/micropython commit a92ca9b1f907c07a01116b0eb464ca4743a28bf1 Signed-off-by: Damien George <[email protected]>
1 parent ea28ef4 commit 3c9983e

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

src/codal_port/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ SRC_C += \
7676
microbit_uart.c \
7777
modantigravity.c \
7878
modaudio.c \
79+
modlove.c \
7980
modmachine.c \
8081
modmicrobit.c \
8182
modmusic.c \

src/codal_port/drv_image.h

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void image_blit(microbit_image_obj_t *src, greyscale_t *dest, mp_int_t x, mp_int
9797
mp_obj_t microbit_string_facade(mp_obj_t string);
9898

9999
microbit_image_obj_t *microbit_image_for_char(char c);
100+
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval);
100101

101102
// ref argument exists so that we can pull a string out of an object and not have it GC'ed while oterating over it
102103
mp_obj_t scrolling_string_image_iterable(const char* str, mp_uint_t len, mp_obj_t ref, bool monospace, bool repeat);

src/codal_port/microbit_image.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ microbit_image_obj_t *microbit_image_for_char(char c) {
448448
return (microbit_image_obj_t *)result;
449449
}
450450

451-
STATIC microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval) {
451+
microbit_image_obj_t *microbit_image_dim(microbit_image_obj_t *lhs, mp_float_t fval) {
452452
if (fval < 0) {
453453
mp_raise_ValueError("brightness multiplier must not be negative");
454454
}

src/codal_port/modlove.c

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
};

src/codal_port/mpconfigport.h

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102

103103
extern const struct _mp_obj_module_t antigravity_module;
104104
extern const struct _mp_obj_module_t audio_module;
105+
extern const struct _mp_obj_module_t love_module;
105106
extern const struct _mp_obj_module_t machine_module;
106107
extern const struct _mp_obj_module_t microbit_module;
107108
extern const struct _mp_obj_module_t music_module;
@@ -113,6 +114,7 @@ extern const struct _mp_obj_module_t utime_module;
113114
#define MICROPY_PORT_BUILTIN_MODULES \
114115
{ MP_ROM_QSTR(MP_QSTR_antigravity), MP_ROM_PTR(&antigravity_module) }, \
115116
{ MP_ROM_QSTR(MP_QSTR_audio), MP_ROM_PTR(&audio_module) }, \
117+
{ MP_ROM_QSTR(MP_QSTR_love), MP_ROM_PTR(&love_module) }, \
116118
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&machine_module) }, \
117119
{ MP_ROM_QSTR(MP_QSTR_microbit), MP_ROM_PTR(&microbit_module) }, \
118120
{ MP_ROM_QSTR(MP_QSTR_music), MP_ROM_PTR(&music_module) }, \

0 commit comments

Comments
 (0)