Skip to content

Commit a6708d1

Browse files
committed
Change BitmapTransformA-F() methods to accept a single float, converting to the appropriate fixed-point internally.
No more fiddly 8.8 / 1.15 fixed-point math in user code.
1 parent 5f66400 commit a6708d1

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

shared-bindings/_eve/__init__.c

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -236,114 +236,125 @@ static mp_obj_t _bitmapswizzle(size_t n_args, const mp_obj_t *args) {
236236
}
237237
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitmapswizzle_obj, 5, 5, _bitmapswizzle);
238238

239-
//| def BitmapTransformA(self, p: int, v: int) -> None:
239+
//| def BitmapTransformA(self, v: float) -> None:
240240
//| """Set the :math:`a` component of the bitmap transform matrix
241241
//|
242-
//| :param int p: precision control: 0 is 8.8, 1 is 1.15. Range 0-1. The initial value is 0
243-
//| :param int v: The :math:`a` component of the bitmap transform matrix, in signed 8.8 or 1.15 bit fixed-point form. Range 0-131071. The initial value is 256
242+
//| :param float v: The :math:`a` component of the bitmap transform matrix
244243
//|
245-
//| The initial value is **p** = 0, **v** = 256. This represents the value 1.0.
244+
//| The initial value 1.0.
246245
//|
247246
//| These values are part of the graphics context and are saved and restored by :meth:`SaveContext` and :meth:`RestoreContext`.
248247
//| """
249248
//| ...
250249

251-
static mp_obj_t _bitmaptransforma(mp_obj_t self, mp_obj_t a0, mp_obj_t a1) {
252-
uint32_t p = mp_obj_get_int_truncated(a0);
253-
uint32_t v = mp_obj_get_int_truncated(a1);
250+
static void _transform(uint32_t *p, uint32_t *v, mp_obj_t a0) {
251+
mp_float_t a = mp_obj_get_float(a0);
252+
253+
if ((-2.0 <= a) && (a < 2.0)) {
254+
*p = 1;
255+
*v = (int)(32768.0 * a);
256+
} else {
257+
*p = 0;
258+
*v = (int)(256.0 * a);
259+
}
260+
}
261+
static mp_obj_t _bitmaptransforma(mp_obj_t self, mp_obj_t a0) {
262+
uint32_t p, v;
263+
_transform(&p, &v, a0);
254264
common_hal__eve_BitmapTransformA(EVEHAL(self), p, v);
255265
return mp_const_none;
256266
}
257-
static MP_DEFINE_CONST_FUN_OBJ_3(bitmaptransforma_obj, _bitmaptransforma);
267+
static MP_DEFINE_CONST_FUN_OBJ_2(bitmaptransforma_obj, _bitmaptransforma);
258268

259-
//| def BitmapTransformB(self, p: int, v: int) -> None:
269+
//| def BitmapTransformB(self, v: float) -> None:
260270
//| """Set the :math:`b` component of the bitmap transform matrix
261271
//|
262-
//| :param int p: precision control: 0 is 8.8, 1 is 1.15. Range 0-1. The initial value is 0
263-
//| :param int v: The :math:`b` component of the bitmap transform matrix, in signed 8.8 or 1.15 bit fixed-point form. Range 0-131071. The initial value is 0
272+
//| :param float v: The :math:`b` component of the bitmap transform matrix
264273
//|
265-
//| The initial value is **p** = 0, **v** = 0. This represents the value 0.0.
274+
//| The initial value 0.0.
266275
//|
267276
//| These values are part of the graphics context and are saved and restored by :meth:`SaveContext` and :meth:`RestoreContext`.
268277
//| """
269278
//| ...
270279

271-
static mp_obj_t _bitmaptransformb(mp_obj_t self, mp_obj_t a0, mp_obj_t a1) {
272-
uint32_t p = mp_obj_get_int_truncated(a0);
273-
uint32_t v = mp_obj_get_int_truncated(a1);
280+
static mp_obj_t _bitmaptransformb(mp_obj_t self, mp_obj_t a0) {
281+
uint32_t p, v;
282+
_transform(&p, &v, a0);
274283
common_hal__eve_BitmapTransformB(EVEHAL(self), p, v);
275284
return mp_const_none;
276285
}
277-
static MP_DEFINE_CONST_FUN_OBJ_3(bitmaptransformb_obj, _bitmaptransformb);
286+
static MP_DEFINE_CONST_FUN_OBJ_2(bitmaptransformb_obj, _bitmaptransformb);
278287

279-
//| def BitmapTransformC(self, v: int) -> None:
288+
//| def BitmapTransformC(self, v: float) -> None:
280289
//| """Set the :math:`c` component of the bitmap transform matrix
281290
//|
282-
//| :param int v: The :math:`c` component of the bitmap transform matrix, in signed 15.8 bit fixed-point form. Range 0-16777215. The initial value is 0
291+
//| :param int v: The :math:`c` component of the bitmap transform matrix
292+
//|
293+
//| The initial value 0.0.
283294
//|
284295
//| This value is part of the graphics context and is saved and restored by :meth:`SaveContext` and :meth:`RestoreContext`.
285296
//| """
286297
//| ...
287298

288299
static mp_obj_t _bitmaptransformc(mp_obj_t self, mp_obj_t a0) {
289-
uint32_t v = mp_obj_get_int_truncated(a0);
290-
common_hal__eve_BitmapTransformC(EVEHAL(self), v);
300+
mp_float_t v = mp_obj_get_float(a0);
301+
common_hal__eve_BitmapTransformC(EVEHAL(self), (int)(256.0 * v));
291302
return mp_const_none;
292303
}
293304
static MP_DEFINE_CONST_FUN_OBJ_2(bitmaptransformc_obj, _bitmaptransformc);
294305

295-
//| def BitmapTransformD(self, p: int, v: int) -> None:
306+
//| def BitmapTransformD(self, v: float) -> None:
296307
//| """Set the :math:`d` component of the bitmap transform matrix
297308
//|
298-
//| :param int p: precision control: 0 is 8.8, 1 is 1.15. Range 0-1. The initial value is 0
299-
//| :param int v: The :math:`d` component of the bitmap transform matrix, in signed 8.8 or 1.15 bit fixed-point form. Range 0-131071. The initial value is 0
309+
//| :param float v: The :math:`d` component of the bitmap transform matrix
300310
//|
301-
//| The initial value is **p** = 0, **v** = 0. This represents the value 0.0.
311+
//| The initial value 0.0.
302312
//|
303313
//| These values are part of the graphics context and are saved and restored by :meth:`SaveContext` and :meth:`RestoreContext`.
304314
//| """
305315
//| ...
306316

307-
static mp_obj_t _bitmaptransformd(mp_obj_t self, mp_obj_t a0, mp_obj_t a1) {
308-
uint32_t p = mp_obj_get_int_truncated(a0);
309-
uint32_t v = mp_obj_get_int_truncated(a1);
317+
static mp_obj_t _bitmaptransformd(mp_obj_t self, mp_obj_t a0) {
318+
uint32_t p, v;
319+
_transform(&p, &v, a0);
310320
common_hal__eve_BitmapTransformD(EVEHAL(self), p, v);
311321
return mp_const_none;
312322
}
313-
static MP_DEFINE_CONST_FUN_OBJ_3(bitmaptransformd_obj, _bitmaptransformd);
323+
static MP_DEFINE_CONST_FUN_OBJ_2(bitmaptransformd_obj, _bitmaptransformd);
314324

315-
//| def BitmapTransformE(self, p: int, v: int) -> None:
325+
//| def BitmapTransformE(self, v: float) -> None:
316326
//| """Set the :math:`e` component of the bitmap transform matrix
317327
//|
318-
//| :param int p: precision control: 0 is 8.8, 1 is 1.15. Range 0-1. The initial value is 0
319-
//| :param int v: The :math:`e` component of the bitmap transform matrix, in signed 8.8 or 1.15 bit fixed-point form. Range 0-131071. The initial value is 256
328+
//| :param float v: The :math:`e` component of the bitmap transform matrix
320329
//|
321-
//| The initial value is **p** = 0, **v** = 256. This represents the value 1.0.
330+
//| The initial value 1.0.
322331
//|
323332
//| These values are part of the graphics context and are saved and restored by :meth:`SaveContext` and :meth:`RestoreContext`.
324333
//| """
325334
//| ...
326335

327-
static mp_obj_t _bitmaptransforme(mp_obj_t self, mp_obj_t a0, mp_obj_t a1) {
328-
uint32_t p = mp_obj_get_int_truncated(a0);
329-
uint32_t v = mp_obj_get_int_truncated(a1);
336+
static mp_obj_t _bitmaptransforme(mp_obj_t self, mp_obj_t a0) {
337+
uint32_t p, v;
338+
_transform(&p, &v, a0);
330339
common_hal__eve_BitmapTransformE(EVEHAL(self), p, v);
331340
return mp_const_none;
332341
}
333-
static MP_DEFINE_CONST_FUN_OBJ_3(bitmaptransforme_obj, _bitmaptransforme);
342+
static MP_DEFINE_CONST_FUN_OBJ_2(bitmaptransforme_obj, _bitmaptransforme);
334343

335344
//| def BitmapTransformF(self, v: int) -> None:
336345
//| """Set the :math:`f` component of the bitmap transform matrix
337346
//|
338-
//| :param int v: The :math:`f` component of the bitmap transform matrix, in signed 15.8 bit fixed-point form. Range 0-16777215. The initial value is 0
347+
//| :param int v: The :math:`f` component of the bitmap transform matrix
348+
//|
349+
//| The initial value 0.0.
339350
//|
340351
//| This value is part of the graphics context and is saved and restored by :meth:`SaveContext` and :meth:`RestoreContext`.
341352
//| """
342353
//| ...
343354

344355
static mp_obj_t _bitmaptransformf(mp_obj_t self, mp_obj_t a0) {
345-
uint32_t v = mp_obj_get_int_truncated(a0);
346-
common_hal__eve_BitmapTransformF(EVEHAL(self), v);
356+
mp_float_t v = mp_obj_get_float(a0);
357+
common_hal__eve_BitmapTransformF(EVEHAL(self), (int)(256.0 * v));
347358
return mp_const_none;
348359
}
349360
static MP_DEFINE_CONST_FUN_OBJ_2(bitmaptransformf_obj, _bitmaptransformf);

0 commit comments

Comments
 (0)