Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf691ae9

Browse files
pi-anlClaude
and
Claude
committed
extmod/modframebuf: Add attributes for width, height, format and stride.
Implement an attr handler for FrameBuffer objects to allow direct access tothe width, height, format and stride properties, which makes it easier toreuse framebuffers without needing to track their properties separately.🤖 Generated with [Claude Code](https://claude.ai/code)Co-Authored-By: Claude <noreply@anthropic.com>Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent79abdad commitf691ae9

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

‎docs/library/framebuf.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ Drawing text
120120
dimensions of 8x8 pixels and there is currently no way to change the font.
121121

122122

123+
Attributes
124+
----------
125+
126+
..attribute::FrameBuffer.width
127+
128+
Gets the width of the FrameBuffer in pixels.
129+
130+
..attribute::FrameBuffer.height
131+
132+
Gets the height of the FrameBuffer in pixels.
133+
134+
..attribute::FrameBuffer.format
135+
136+
Gets the format of the FrameBuffer, which is one of the constants defined in this module.
137+
138+
..attribute::FrameBuffer.stride
139+
140+
Gets the stride of the FrameBuffer in pixels (horizontal offset between lines).
141+
123142
Other methods
124143
-------------
125144

‎examples/natmod/framebuf/framebuf.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ void *memset(void *s, int c, size_t n) {
1111

1212
mp_obj_full_type_tmp_type_framebuf;
1313

14+
/**
15+
* When building native modules tools/mpy_ld.py is used to preprocess the
16+
* source for things like QSTR registration. As this tool currently does
17+
* not support #include statements we list QSTR_* from extmod/modframebuf.c
18+
* here to ensure they're included in the build.
19+
* MP_QSTR_width
20+
* MP_QSTR_height
21+
* MP_QSTR_format
22+
* MP_QSTR_stride
23+
*/
1424
#include"extmod/modframebuf.c"
1525

1626
mp_map_elem_tframebuf_locals_dict_table[12];
@@ -23,6 +33,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
2333
mp_type_framebuf.name=MP_QSTR_FrameBuffer;
2434
MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf,make_new,framebuf_make_new,0);
2535
MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf,buffer,framebuf_get_buffer,1);
36+
// MP_OBJ_TYPE_SET_SLOT(&mp_type_framebuf, attr, framebuf_attr, 2);
2637
framebuf_locals_dict_table[0]= (mp_map_elem_t){MP_OBJ_NEW_QSTR(MP_QSTR_fill),MP_OBJ_FROM_PTR(&framebuf_fill_obj) };
2738
framebuf_locals_dict_table[1]= (mp_map_elem_t){MP_OBJ_NEW_QSTR(MP_QSTR_fill_rect),MP_OBJ_FROM_PTR(&framebuf_fill_rect_obj) };
2839
framebuf_locals_dict_table[2]= (mp_map_elem_t){MP_OBJ_NEW_QSTR(MP_QSTR_pixel),MP_OBJ_FROM_PTR(&framebuf_pixel_obj) };

‎extmod/modframebuf.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,25 @@ static void framebuf_args(const mp_obj_t *args_in, mp_int_t *args_out, int n) {
341341
}
342342
}
343343

344+
staticvoidframebuf_attr(mp_obj_tself_in,qstrattr,mp_obj_t*dest) {
345+
mp_obj_framebuf_t*self=MP_OBJ_TO_PTR(self_in);
346+
if (dest[0]==MP_OBJ_NULL) {
347+
// Load attribute
348+
if (attr==MP_QSTR_width) {
349+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->width);
350+
}elseif (attr==MP_QSTR_height) {
351+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->height);
352+
}elseif (attr==MP_QSTR_format) {
353+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->format);
354+
}elseif (attr==MP_QSTR_stride) {
355+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->stride);
356+
}else {
357+
// Continue lookup in locals_dict.
358+
dest[1]=MP_OBJ_SENTINEL;
359+
}
360+
}
361+
}
362+
344363
staticmp_int_tframebuf_get_buffer(mp_obj_tself_in,mp_buffer_info_t*bufinfo,mp_uint_tflags) {
345364
mp_obj_framebuf_t*self=MP_OBJ_TO_PTR(self_in);
346365
returnmp_get_buffer(self->buf_obj,bufinfo,flags) ?0 :1;
@@ -869,6 +888,7 @@ static MP_DEFINE_CONST_OBJ_TYPE(
869888
MP_TYPE_FLAG_NONE,
870889
make_new,framebuf_make_new,
871890
buffer,framebuf_get_buffer,
891+
attr,framebuf_attr,
872892
locals_dict,&framebuf_locals_dict
873893
);
874894
#endif

‎tests/extmod/framebuf_attr.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
try:
2+
importframebuf
3+
exceptImportError:
4+
print("SKIP")
5+
raiseSystemExit
6+
7+
importunittest
8+
9+
10+
# Create a framebuffer for testing
11+
defcreate_test_framebuf(width=100,height=32,fmt=None,stride=None):
12+
iffmtisNone:
13+
fmt=framebuf.MONO_HLSB
14+
15+
# Calculate buffer size based on format
16+
ifstrideisNone:
17+
stride=width
18+
19+
iffmt==framebuf.MONO_VLSB:
20+
size= ((height+7)//8)*stride
21+
eliffmt==framebuf.MONO_HLSBorfmt==framebuf.MONO_HMSB:
22+
size= ((stride+7)//8)*height
23+
eliffmt==framebuf.RGB565:
24+
size=stride*height*2
25+
eliffmt==framebuf.GS2_HMSB:
26+
size= ((stride+3)//4)*height
27+
eliffmt==framebuf.GS4_HMSB:
28+
size= ((stride+1)//2)*height
29+
eliffmt==framebuf.GS8:
30+
size=stride*height
31+
else:
32+
size= ((stride+7)//8)*height# Default to MONO_HLSB size
33+
34+
buf=bytearray(size)
35+
ifstride==width:
36+
returnframebuf.FrameBuffer(buf,width,height,fmt)
37+
else:
38+
returnframebuf.FrameBuffer(buf,width,height,fmt,stride)
39+
40+
41+
classTestFramebufAttrs(unittest.TestCase):
42+
"""Test FrameBuffer attributes and format constants"""
43+
44+
deftest_format_constants(self):
45+
"""Test that format constants have correct values"""
46+
self.assertEqual(framebuf.MONO_VLSB,0)
47+
self.assertEqual(framebuf.RGB565,1)
48+
self.assertEqual(framebuf.GS4_HMSB,2)
49+
self.assertEqual(framebuf.MONO_HLSB,3)
50+
self.assertEqual(framebuf.MONO_HMSB,4)
51+
self.assertEqual(framebuf.GS2_HMSB,5)
52+
self.assertEqual(framebuf.GS8,6)
53+
54+
deftest_basic_attributes(self):
55+
"""Test access to basic attributes like width, height, format and stride"""
56+
test_configs= [
57+
(8,4,framebuf.MONO_VLSB),
58+
(16,8,framebuf.MONO_HLSB),
59+
(32,16,framebuf.MONO_HMSB),
60+
(8,4,framebuf.RGB565),
61+
(16,8,framebuf.GS2_HMSB),
62+
(8,4,framebuf.GS4_HMSB),
63+
(16,8,framebuf.GS8),
64+
]
65+
66+
forwidth,height,fmtintest_configs:
67+
withself.subTest(width=width,height=height,format=fmt):
68+
fb=create_test_framebuf(width,height,fmt)
69+
self.assertEqual(fb.width,width)
70+
self.assertEqual(fb.height,height)
71+
self.assertEqual(fb.format,fmt)
72+
self.assertEqual(fb.stride,width)# Default stride equals width
73+
74+
deftest_custom_stride_mono_vlsb(self):
75+
"""Test creating MONO_VLSB framebuffer with custom stride"""
76+
width,height=16,8
77+
stride=width+4# Set a custom stride larger than width
78+
fmt=framebuf.MONO_VLSB
79+
80+
fb=create_test_framebuf(width,height,fmt,stride)
81+
self.assertEqual(fb.width,width)
82+
self.assertEqual(fb.height,height)
83+
self.assertEqual(fb.format,fmt)
84+
self.assertEqual(fb.stride,stride)
85+
86+
deftest_attribute_error(self):
87+
"""Test that accessing a non-existent attribute raises AttributeError"""
88+
fb=create_test_framebuf(8,1,framebuf.MONO_HLSB)
89+
withself.assertRaises(AttributeError):
90+
fb.non_existent
91+
92+
93+
if__name__=='__main__':
94+
unittest.main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp