Skip to content

Commit 1fda504

Browse files
committed
add setFont
1 parent df89ac6 commit 1fda504

File tree

4 files changed

+158
-56
lines changed

4 files changed

+158
-56
lines changed

m5stack/cmodules/m5unified/m5unified.c

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,88 @@
44
#define MAKE_METHOD_0(prefix, func) extern mp_obj_t prefix##_##func(mp_obj_t ); STATIC MP_DEFINE_CONST_FUN_OBJ_1( prefix##_##func##_obj, prefix##_##func );
55
#define MAKE_METHOD_1(prefix, func) extern mp_obj_t prefix##_##func(mp_obj_t,mp_obj_t ); STATIC MP_DEFINE_CONST_FUN_OBJ_2( prefix##_##func##_obj, prefix##_##func );
66
#define MAKE_METHOD_2(prefix, func) extern mp_obj_t prefix##_##func(mp_obj_t,mp_obj_t,mp_obj_t); STATIC MP_DEFINE_CONST_FUN_OBJ_3( prefix##_##func##_obj, prefix##_##func );
7+
#define MAKE_METHOD_KW(prefix, func, args) extern mp_obj_t prefix##_##func(size_t,const mp_obj_t*,mp_map_t*); STATIC MP_DEFINE_CONST_FUN_OBJ_KW( prefix##_##func##_obj, args, prefix##_##func );
78

89
#define MAKE_TABLE( prefix, func ) \
910
{ MP_ROM_QSTR(MP_QSTR_##func), MP_ROM_PTR(&prefix##_##func##_obj) }
1011

1112
//-------- GFX common wrapper
12-
MAKE_METHOD_0( gfx, width );
13-
MAKE_METHOD_0( gfx, height );
14-
MAKE_METHOD_0( gfx, getRotation );
15-
MAKE_METHOD_0( gfx, getColorDepth );
16-
MAKE_METHOD_1( gfx, setRotation );
17-
MAKE_METHOD_1( gfx, setColorDepth );
18-
MAKE_METHOD_1( gfx, print );
19-
MAKE_METHOD_2( gfx, setCursor );
20-
MAKE_METHOD_V( gfx, fillScreen , 1, 2 );
21-
MAKE_METHOD_V( gfx, drawPixel , 3, 4 );
22-
MAKE_METHOD_V( gfx, drawCircle , 4, 5 );
23-
MAKE_METHOD_V( gfx, fillCircle , 4, 5 );
24-
MAKE_METHOD_V( gfx, drawLine , 5, 6 );
25-
MAKE_METHOD_V( gfx, drawRect , 5, 6 );
26-
MAKE_METHOD_V( gfx, fillRect , 5, 6 );
27-
MAKE_METHOD_V( gfx, printf , 2, 32);
28-
MAKE_METHOD_V( gfx, newCanvas , 3, 5 );
13+
MAKE_METHOD_0( gfx, width );
14+
MAKE_METHOD_0( gfx, height );
15+
MAKE_METHOD_0( gfx, getRotation );
16+
MAKE_METHOD_0( gfx, getColorDepth);
17+
MAKE_METHOD_0( gfx, getCursor );
18+
MAKE_METHOD_1( gfx, setRotation );
19+
MAKE_METHOD_1( gfx, setColorDepth);
20+
MAKE_METHOD_1( gfx, setFont );
21+
MAKE_METHOD_1( gfx, print );
22+
MAKE_METHOD_2( gfx, setCursor );
23+
MAKE_METHOD_V( gfx, clear , 1, 2 );
24+
MAKE_METHOD_V( gfx, fillScreen , 1, 2 );
25+
MAKE_METHOD_V( gfx, drawPixel , 3, 4 );
26+
MAKE_METHOD_V( gfx, drawCircle , 4, 5 );
27+
MAKE_METHOD_V( gfx, fillCircle , 4, 5 );
28+
MAKE_METHOD_V( gfx, drawLine , 5, 6 );
29+
MAKE_METHOD_V( gfx, drawRect , 5, 6 );
30+
MAKE_METHOD_V( gfx, fillRect , 5, 6 );
31+
MAKE_METHOD_V( gfx, drawRoundRect, 6, 7 );
32+
MAKE_METHOD_V( gfx, fillRoundRect, 6, 7 );
33+
MAKE_METHOD_V( gfx, printf , 2, 32);
34+
MAKE_METHOD_V( gfx, newCanvas , 3, 5 );
2935

3036
#define TABLE_PARTS_GFX_BASE \
37+
MAKE_TABLE( gfx, clear ), \
3138
MAKE_TABLE( gfx, drawCircle ), \
3239
MAKE_TABLE( gfx, drawLine ), \
3340
MAKE_TABLE( gfx, drawPixel ), \
3441
MAKE_TABLE( gfx, drawRect ), \
42+
MAKE_TABLE( gfx, drawRoundRect), \
3543
MAKE_TABLE( gfx, fillCircle ), \
3644
MAKE_TABLE( gfx, fillRect ), \
45+
MAKE_TABLE( gfx, fillRoundRect), \
3746
MAKE_TABLE( gfx, fillScreen ), \
3847
MAKE_TABLE( gfx, getColorDepth), \
48+
MAKE_TABLE( gfx, getCursor ), \
3949
MAKE_TABLE( gfx, getRotation ), \
4050
MAKE_TABLE( gfx, height ), \
4151
MAKE_TABLE( gfx, newCanvas ), \
4252
MAKE_TABLE( gfx, print ), \
4353
MAKE_TABLE( gfx, printf ), \
4454
MAKE_TABLE( gfx, setColorDepth), \
4555
MAKE_TABLE( gfx, setCursor ), \
56+
MAKE_TABLE( gfx, setFont ), \
4657
MAKE_TABLE( gfx, setRotation ), \
4758
MAKE_TABLE( gfx, width ), \
48-
{ MP_ROM_QSTR(MP_QSTR_BLACK ), MP_ROM_INT( 0x000000 ) }, \
49-
{ MP_ROM_QSTR(MP_QSTR_NAVY ), MP_ROM_INT( 0x000080 ) }, \
50-
{ MP_ROM_QSTR(MP_QSTR_DARKGREEN ), MP_ROM_INT( 0x008000 ) }, \
51-
{ MP_ROM_QSTR(MP_QSTR_DARKCYAN ), MP_ROM_INT( 0x008080 ) }, \
52-
{ MP_ROM_QSTR(MP_QSTR_MAROON ), MP_ROM_INT( 0x800000 ) }, \
53-
{ MP_ROM_QSTR(MP_QSTR_PURPLE ), MP_ROM_INT( 0x800080 ) }, \
54-
{ MP_ROM_QSTR(MP_QSTR_OLIVE ), MP_ROM_INT( 0x808000 ) }, \
55-
{ MP_ROM_QSTR(MP_QSTR_LIGHTGREY ), MP_ROM_INT( 0xC0C0C0 ) }, \
56-
{ MP_ROM_QSTR(MP_QSTR_DARKGREY ), MP_ROM_INT( 0x808080 ) }, \
57-
{ MP_ROM_QSTR(MP_QSTR_BLUE ), MP_ROM_INT( 0x0000FF ) }, \
58-
{ MP_ROM_QSTR(MP_QSTR_GREEN ), MP_ROM_INT( 0x00FF00 ) }, \
59-
{ MP_ROM_QSTR(MP_QSTR_CYAN ), MP_ROM_INT( 0x00FFFF ) }, \
60-
{ MP_ROM_QSTR(MP_QSTR_RED ), MP_ROM_INT( 0xFF0000 ) }, \
61-
{ MP_ROM_QSTR(MP_QSTR_MAGENTA ), MP_ROM_INT( 0xFF00FF ) }, \
62-
{ MP_ROM_QSTR(MP_QSTR_YELLOW ), MP_ROM_INT( 0xFFFF00 ) }, \
63-
{ MP_ROM_QSTR(MP_QSTR_WHITE ), MP_ROM_INT( 0xFFFFFF ) }, \
64-
{ MP_ROM_QSTR(MP_QSTR_ORANGE ), MP_ROM_INT( 0xFFA500 ) }, \
65-
{ MP_ROM_QSTR(MP_QSTR_GREENYELLOW), MP_ROM_INT( 0xADFF2F ) }, \
66-
{ MP_ROM_QSTR(MP_QSTR_PINK ), MP_ROM_INT( 0xFFC0CB ) }
59+
{ MP_ROM_QSTR(MP_QSTR_FONT0 ), MP_ROM_PTR(&gfx_font_0_obj) }, \
60+
{ MP_ROM_QSTR(MP_QSTR_FONT2 ), MP_ROM_PTR(&gfx_font_2_obj) }, \
61+
{ MP_ROM_QSTR(MP_QSTR_FONT4 ), MP_ROM_PTR(&gfx_font_4_obj) }, \
62+
{ MP_ROM_QSTR(MP_QSTR_FONT6 ), MP_ROM_PTR(&gfx_font_6_obj) }, \
63+
{ MP_ROM_QSTR(MP_QSTR_FONT7 ), MP_ROM_PTR(&gfx_font_7_obj) }, \
64+
{ MP_ROM_QSTR(MP_QSTR_FONT8 ), MP_ROM_PTR(&gfx_font_8_obj) }, \
65+
{ MP_ROM_QSTR(MP_QSTR_FONT_DejaVu9 ), MP_ROM_PTR(&gfx_font_DejaVu9_obj ) }, \
66+
{ MP_ROM_QSTR(MP_QSTR_FONT_DejaVu12), MP_ROM_PTR(&gfx_font_DejaVu12_obj) }, \
67+
{ MP_ROM_QSTR(MP_QSTR_FONT_DejaVu18), MP_ROM_PTR(&gfx_font_DejaVu18_obj) }, \
68+
{ MP_ROM_QSTR(MP_QSTR_FONT_DejaVu24), MP_ROM_PTR(&gfx_font_DejaVu24_obj) }, \
69+
{ MP_ROM_QSTR(MP_QSTR_BLACK ), MP_ROM_INT( 0x000000 ) }, \
70+
{ MP_ROM_QSTR(MP_QSTR_NAVY ), MP_ROM_INT( 0x000080 ) }, \
71+
{ MP_ROM_QSTR(MP_QSTR_DARKGREEN ), MP_ROM_INT( 0x008000 ) }, \
72+
{ MP_ROM_QSTR(MP_QSTR_DARKCYAN ), MP_ROM_INT( 0x008080 ) }, \
73+
{ MP_ROM_QSTR(MP_QSTR_MAROON ), MP_ROM_INT( 0x800000 ) }, \
74+
{ MP_ROM_QSTR(MP_QSTR_PURPLE ), MP_ROM_INT( 0x800080 ) }, \
75+
{ MP_ROM_QSTR(MP_QSTR_OLIVE ), MP_ROM_INT( 0x808000 ) }, \
76+
{ MP_ROM_QSTR(MP_QSTR_LIGHTGREY ), MP_ROM_INT( 0xC0C0C0 ) }, \
77+
{ MP_ROM_QSTR(MP_QSTR_DARKGREY ), MP_ROM_INT( 0x808080 ) }, \
78+
{ MP_ROM_QSTR(MP_QSTR_BLUE ), MP_ROM_INT( 0x0000FF ) }, \
79+
{ MP_ROM_QSTR(MP_QSTR_GREEN ), MP_ROM_INT( 0x00FF00 ) }, \
80+
{ MP_ROM_QSTR(MP_QSTR_CYAN ), MP_ROM_INT( 0x00FFFF ) }, \
81+
{ MP_ROM_QSTR(MP_QSTR_RED ), MP_ROM_INT( 0xFF0000 ) }, \
82+
{ MP_ROM_QSTR(MP_QSTR_MAGENTA ), MP_ROM_INT( 0xFF00FF ) }, \
83+
{ MP_ROM_QSTR(MP_QSTR_YELLOW ), MP_ROM_INT( 0xFFFF00 ) }, \
84+
{ MP_ROM_QSTR(MP_QSTR_WHITE ), MP_ROM_INT( 0xFFFFFF ) }, \
85+
{ MP_ROM_QSTR(MP_QSTR_ORANGE ), MP_ROM_INT( 0xFFA500 ) }, \
86+
{ MP_ROM_QSTR(MP_QSTR_GREENYELLOW ), MP_ROM_INT( 0xADFF2F ) }, \
87+
{ MP_ROM_QSTR(MP_QSTR_PINK ), MP_ROM_INT( 0xFFC0CB ) }
88+
6789

6890
//-------- GFX device wrapper
6991
MAKE_METHOD_0( gfx, startWrite );

m5stack/libs/m5unified/mpy_m5gfx.cpp

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef union {
1111

1212
extern "C"
1313
{
14+
#include <py/obj.h>
1415
#include "mpy_m5gfx.h"
1516

1617
static inline LovyanGFX* getGfx(const mp_obj_t* args)
@@ -74,6 +75,30 @@ extern "C"
7475
return mp_const_none;
7576
}
7677

78+
mp_obj_t gfx_getCursor(mp_obj_t self)
79+
{
80+
auto gfx = getGfx(&self);
81+
mp_obj_t tuple[2] = { mp_obj_new_int(gfx->getCursorX())
82+
, mp_obj_new_int(gfx->getCursorY())
83+
};
84+
return mp_obj_new_tuple(2, tuple);
85+
}
86+
87+
mp_obj_t gfx_setFont(mp_obj_t self, mp_obj_t font)
88+
{
89+
auto gfx = getGfx(&self);
90+
gfx->setFont((const m5gfx::IFont*) ((font_obj_t*)MP_OBJ_TO_PTR(font))->font);
91+
return mp_const_none;
92+
}
93+
94+
mp_obj_t gfx_clear(size_t n_args, const mp_obj_t *args)
95+
{
96+
auto gfx = getGfx(args);
97+
if (n_args >= 2) { gfx->setBaseColor((uint32_t)mp_obj_get_int(args[1])); }
98+
gfx->clear();
99+
return mp_const_none;
100+
}
101+
77102
mp_obj_t gfx_fillScreen(size_t n_args, const mp_obj_t *args)
78103
{
79104
auto gfx = getGfx(args);
@@ -150,6 +175,32 @@ extern "C"
150175
return mp_const_none;
151176
}
152177

178+
mp_obj_t gfx_drawRoundRect(size_t n_args, const mp_obj_t *args)
179+
{
180+
auto gfx = getGfx(args);
181+
if (n_args >= 7) { gfx->setColor((uint32_t)mp_obj_get_int(args[6])); }
182+
gfx->drawRoundRect( mp_obj_get_int(args[1])
183+
, mp_obj_get_int(args[2])
184+
, mp_obj_get_int(args[3])
185+
, mp_obj_get_int(args[4])
186+
, mp_obj_get_int(args[5])
187+
);
188+
return mp_const_none;
189+
}
190+
191+
mp_obj_t gfx_fillRoundRect(size_t n_args, const mp_obj_t *args)
192+
{
193+
auto gfx = getGfx(args);
194+
if (n_args >= 7) { gfx->setColor((uint32_t)mp_obj_get_int(args[6])); }
195+
gfx->fillRoundRect( mp_obj_get_int(args[1])
196+
, mp_obj_get_int(args[2])
197+
, mp_obj_get_int(args[3])
198+
, mp_obj_get_int(args[4])
199+
, mp_obj_get_int(args[5])
200+
);
201+
return mp_const_none;
202+
}
203+
153204
mp_obj_t gfx_printf(size_t n_args, const mp_obj_t *args)
154205
{
155206
auto types = (ffi_type**)alloca(n_args * sizeof(ffi_type *));
@@ -184,14 +235,17 @@ extern "C"
184235
return mp_const_none;
185236
}
186237

187-
mp_obj_t gfx_push(mp_obj_t self, mp_obj_t x, mp_obj_t y)
238+
mp_obj_t gfx_newCanvas( size_t n_args, const mp_obj_t *args)
188239
{
189-
auto gfx = getGfx(&self);
190-
if (gfx)
191-
{
192-
((M5Canvas*)gfx)->pushSprite(mp_obj_get_int(x), mp_obj_get_int(y));
193-
}
194-
return mp_const_none;
240+
auto gfx = getGfx(args);
241+
auto canvas = new M5Canvas(gfx);
242+
if (n_args >= 5) { canvas->setPsram(mp_obj_get_int(args[4])); }
243+
if (n_args >= 4) { canvas->setColorDepth(mp_obj_get_int(args[3])); }
244+
if (n_args >= 3) { canvas->createSprite(mp_obj_get_int(args[1]), mp_obj_get_int(args[2])); }
245+
gfx_obj_t *res = m_new_obj_with_finaliser(gfx_obj_t);
246+
res->base.type = &gfxcanvas_type;
247+
res->gfx = canvas;
248+
return MP_OBJ_FROM_PTR(res);
195249
}
196250

197251
//-------- GFX device wrapper
@@ -212,6 +266,16 @@ extern "C"
212266

213267
//-------- GFX canvas wrapper
214268

269+
mp_obj_t gfx_push(mp_obj_t self, mp_obj_t x, mp_obj_t y)
270+
{
271+
auto gfx = getGfx(&self);
272+
if (gfx)
273+
{
274+
((M5Canvas*)gfx)->pushSprite(mp_obj_get_int(x), mp_obj_get_int(y));
275+
}
276+
return mp_const_none;
277+
}
278+
215279
mp_obj_t gfx_delete(mp_obj_t self)
216280
{
217281
auto gfx = getGfx(&self);
@@ -224,18 +288,15 @@ extern "C"
224288
return mp_const_none;
225289
}
226290

227-
mp_obj_t gfx_newCanvas( size_t n_args, const mp_obj_t *args)
228-
{
229-
auto gfx = getGfx(args);
230-
auto canvas = new M5Canvas(gfx);
231-
if (n_args >= 5) { canvas->setPsram(mp_obj_get_int(args[4])); }
232-
if (n_args >= 4) { canvas->setColorDepth(mp_obj_get_int(args[3])); }
233-
if (n_args >= 3) { canvas->createSprite(mp_obj_get_int(args[1]), mp_obj_get_int(args[2])); }
234-
gfx_obj_t *res = m_new_obj_with_finaliser(gfx_obj_t);
235-
res->base.type = &gfxcanvas_type;
236-
res->gfx = canvas;
237-
return MP_OBJ_FROM_PTR(res);
238-
}
239-
291+
const font_obj_t gfx_font_0_obj = {{ &mp_type_object }, &m5gfx::fonts::Font0 };
292+
const font_obj_t gfx_font_2_obj = {{ &mp_type_object }, &m5gfx::fonts::Font2 };
293+
const font_obj_t gfx_font_4_obj = {{ &mp_type_object }, &m5gfx::fonts::Font4 };
294+
const font_obj_t gfx_font_6_obj = {{ &mp_type_object }, &m5gfx::fonts::Font6 };
295+
const font_obj_t gfx_font_7_obj = {{ &mp_type_object }, &m5gfx::fonts::Font7 };
296+
const font_obj_t gfx_font_8_obj = {{ &mp_type_object }, &m5gfx::fonts::Font8 };
297+
const font_obj_t gfx_font_DejaVu9_obj = {{ &mp_type_object }, &m5gfx::fonts::DejaVu9 };
298+
const font_obj_t gfx_font_DejaVu12_obj = {{ &mp_type_object }, &m5gfx::fonts::DejaVu12 };
299+
const font_obj_t gfx_font_DejaVu18_obj = {{ &mp_type_object }, &m5gfx::fonts::DejaVu18 };
300+
const font_obj_t gfx_font_DejaVu24_obj = {{ &mp_type_object }, &m5gfx::fonts::DejaVu24 };
240301

241302
}

m5stack/libs/m5unified/mpy_m5gfx.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ typedef struct _gfx_obj_t {
99
void* gfx;
1010
} gfx_obj_t;
1111

12+
typedef struct _font_obj_t {
13+
mp_obj_base_t base;
14+
const void* font;
15+
} font_obj_t;
16+
17+
1218
extern const mp_obj_type_t gfxdevice_type;
1319
extern const mp_obj_type_t gfxcanvas_type;
1420

21+
extern const font_obj_t gfx_font_0_obj;
22+
extern const font_obj_t gfx_font_2_obj;
23+
extern const font_obj_t gfx_font_4_obj;
24+
extern const font_obj_t gfx_font_6_obj;
25+
extern const font_obj_t gfx_font_7_obj;
26+
extern const font_obj_t gfx_font_8_obj;
27+
extern const font_obj_t gfx_font_DejaVu9_obj ;
28+
extern const font_obj_t gfx_font_DejaVu12_obj;
29+
extern const font_obj_t gfx_font_DejaVu18_obj;
30+
extern const font_obj_t gfx_font_DejaVu24_obj;
31+

m5stack/libs/m5unified/mpy_m5unified.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#define M5ATOMDISPLAY_LOGICAL_WIDTH 320
22
#define M5ATOMDISPLAY_LOGICAL_HEIGHT 240
33
#include <M5AtomDisplay.h>
4+
#include <M5UnitOLED.h>
5+
#include <M5UnitLCD.h>
46
#include <M5Unified.h>
57

68
extern "C"

0 commit comments

Comments
 (0)