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

Commit70af7a8

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 commit70af7a8

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed

‎extmod/modframebuf.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,25 @@ static mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo,
346346
returnmp_get_buffer(self->buf_obj,bufinfo,flags) ?0 :1;
347347
}
348348

349+
staticvoidframebuf_attr(mp_obj_tself_in,qstrattr,mp_obj_t*dest) {
350+
mp_obj_framebuf_t*self=MP_OBJ_TO_PTR(self_in);
351+
if (dest[0]==MP_OBJ_NULL) {
352+
// Load attribute
353+
if (attr==MP_QSTR_width) {
354+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->width);
355+
}elseif (attr==MP_QSTR_height) {
356+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->height);
357+
}elseif (attr==MP_QSTR_format) {
358+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->format);
359+
}elseif (attr==MP_QSTR_stride) {
360+
dest[0]=MP_OBJ_NEW_SMALL_INT(self->stride);
361+
}else {
362+
// Continue lookup in locals_dict.
363+
dest[1]=MP_OBJ_SENTINEL;
364+
}
365+
}
366+
}
367+
349368
staticmp_obj_tframebuf_fill(mp_obj_tself_in,mp_obj_tcol_in) {
350369
mp_obj_framebuf_t*self=MP_OBJ_TO_PTR(self_in);
351370
mp_int_tcol=mp_obj_get_int(col_in);
@@ -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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
try:
2+
importframebuf
3+
exceptImportError:
4+
print("SKIP")
5+
raiseSystemExit
6+
7+
# Test different formats
8+
formats= [
9+
(framebuf.MONO_VLSB,"MONO_VLSB"),
10+
(framebuf.MONO_HLSB,"MONO_HLSB"),
11+
(framebuf.MONO_HMSB,"MONO_HMSB"),
12+
(framebuf.RGB565,"RGB565"),
13+
(framebuf.GS2_HMSB,"GS2_HMSB"),
14+
(framebuf.GS4_HMSB,"GS4_HMSB"),
15+
(framebuf.GS8,"GS8"),
16+
]
17+
18+
# Test with different dimensions
19+
dimensions= [(8,4), (16,8), (128,64)]
20+
21+
forwidth,heightindimensions:
22+
forfmt,nameinformats:
23+
# Create appropriate buffer size based on format
24+
iffmt==framebuf.MONO_VLSB:
25+
size= ((height+7)//8)*width
26+
eliffmt==framebuf.MONO_HLSBorfmt==framebuf.MONO_HMSB:
27+
size= ((width+7)//8)*height
28+
eliffmt==framebuf.RGB565:
29+
size=width*height*2
30+
eliffmt==framebuf.GS2_HMSB:
31+
size= ((width+3)//4)*height
32+
eliffmt==framebuf.GS4_HMSB:
33+
size= ((width+1)//2)*height
34+
eliffmt==framebuf.GS8:
35+
size=width*height
36+
else:
37+
continue
38+
39+
buf=bytearray(size)
40+
fb=framebuf.FrameBuffer(buf,width,height,fmt)
41+
42+
print(name,"{}x{}:".format(width,height))
43+
print("width:",fb.width)
44+
print("height:",fb.height)
45+
print("format:",fb.format)
46+
print("stride:",fb.stride)
47+
print("format == {}: {}".format(name,fb.format==fmt))
48+
49+
# Try accessing a non-existent attribute (should fail)
50+
try:
51+
print(fb.non_existent)
52+
print("ERROR: should have raised AttributeError")
53+
exceptAttributeError:
54+
print("Raised AttributeError correctly")
55+
56+
print()

‎tests/extmod/framebuf_attr.py.exp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
MONO_VLSB 8x4:
2+
width: 8
3+
height: 4
4+
format: 0
5+
stride: 8
6+
format == MONO_VLSB: True
7+
Raised AttributeError correctly
8+
9+
MONO_HLSB 8x4:
10+
width: 8
11+
height: 4
12+
format: 3
13+
stride: 8
14+
format == MONO_HLSB: True
15+
Raised AttributeError correctly
16+
17+
MONO_HMSB 8x4:
18+
width: 8
19+
height: 4
20+
format: 4
21+
stride: 8
22+
format == MONO_HMSB: True
23+
Raised AttributeError correctly
24+
25+
RGB565 8x4:
26+
width: 8
27+
height: 4
28+
format: 1
29+
stride: 8
30+
format == RGB565: True
31+
Raised AttributeError correctly
32+
33+
GS2_HMSB 8x4:
34+
width: 8
35+
height: 4
36+
format: 5
37+
stride: 8
38+
format == GS2_HMSB: True
39+
Raised AttributeError correctly
40+
41+
GS4_HMSB 8x4:
42+
width: 8
43+
height: 4
44+
format: 2
45+
stride: 8
46+
format == GS4_HMSB: True
47+
Raised AttributeError correctly
48+
49+
GS8 8x4:
50+
width: 8
51+
height: 4
52+
format: 6
53+
stride: 8
54+
format == GS8: True
55+
Raised AttributeError correctly
56+
57+
MONO_VLSB 16x8:
58+
width: 16
59+
height: 8
60+
format: 0
61+
stride: 16
62+
format == MONO_VLSB: True
63+
Raised AttributeError correctly
64+
65+
MONO_HLSB 16x8:
66+
width: 16
67+
height: 8
68+
format: 3
69+
stride: 16
70+
format == MONO_HLSB: True
71+
Raised AttributeError correctly
72+
73+
MONO_HMSB 16x8:
74+
width: 16
75+
height: 8
76+
format: 4
77+
stride: 16
78+
format == MONO_HMSB: True
79+
Raised AttributeError correctly
80+
81+
RGB565 16x8:
82+
width: 16
83+
height: 8
84+
format: 1
85+
stride: 16
86+
format == RGB565: True
87+
Raised AttributeError correctly
88+
89+
GS2_HMSB 16x8:
90+
width: 16
91+
height: 8
92+
format: 5
93+
stride: 16
94+
format == GS2_HMSB: True
95+
Raised AttributeError correctly
96+
97+
GS4_HMSB 16x8:
98+
width: 16
99+
height: 8
100+
format: 2
101+
stride: 16
102+
format == GS4_HMSB: True
103+
Raised AttributeError correctly
104+
105+
GS8 16x8:
106+
width: 16
107+
height: 8
108+
format: 6
109+
stride: 16
110+
format == GS8: True
111+
Raised AttributeError correctly
112+
113+
MONO_VLSB 128x64:
114+
width: 128
115+
height: 64
116+
format: 0
117+
stride: 128
118+
format == MONO_VLSB: True
119+
Raised AttributeError correctly
120+
121+
MONO_HLSB 128x64:
122+
width: 128
123+
height: 64
124+
format: 3
125+
stride: 128
126+
format == MONO_HLSB: True
127+
Raised AttributeError correctly
128+
129+
MONO_HMSB 128x64:
130+
width: 128
131+
height: 64
132+
format: 4
133+
stride: 128
134+
format == MONO_HMSB: True
135+
Raised AttributeError correctly
136+
137+
RGB565 128x64:
138+
width: 128
139+
height: 64
140+
format: 1
141+
stride: 128
142+
format == RGB565: True
143+
Raised AttributeError correctly
144+
145+
GS2_HMSB 128x64:
146+
width: 128
147+
height: 64
148+
format: 5
149+
stride: 128
150+
format == GS2_HMSB: True
151+
Raised AttributeError correctly
152+
153+
GS4_HMSB 128x64:
154+
width: 128
155+
height: 64
156+
format: 2
157+
stride: 128
158+
format == GS4_HMSB: True
159+
Raised AttributeError correctly
160+
161+
GS8 128x64:
162+
width: 128
163+
height: 64
164+
format: 6
165+
stride: 128
166+
format == GS8: True
167+
Raised AttributeError correctly
168+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp