From 5860695f5d781bb2259e8005fbd124c388431c4a Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 22 Feb 2016 18:41:50 +0200 Subject: [PATCH] Move helper functions to new files. --- src/Makefile | 1 + src/omv/Makefile | 1 + src/omv/py/py_helper.c | 62 ++++++++++++++++++++++++++++++++++++++ src/omv/py/py_helper.h | 15 ++++++++++ src/omv/py/py_image.c | 68 ++---------------------------------------- 5 files changed, 82 insertions(+), 65 deletions(-) create mode 100644 src/omv/py/py_helper.c create mode 100644 src/omv/py/py_helper.h diff --git a/src/Makefile b/src/Makefile index c21248510..42515aca8 100755 --- a/src/Makefile +++ b/src/Makefile @@ -152,6 +152,7 @@ OBJ += $(addprefix $(BUILD)/$(OMV_DIR)/img/,\ ) OBJ += $(addprefix $(BUILD)/$(OMV_DIR)/py/, \ + py_helper.o \ py_sensor.o \ py_image.o \ py_time.o \ diff --git a/src/omv/Makefile b/src/omv/Makefile index 9ef70aeb0..65cbaa6ac 100644 --- a/src/omv/Makefile +++ b/src/omv/Makefile @@ -55,6 +55,7 @@ SRCS += $(addprefix img/, \ ) SRCS += $(addprefix py/, \ + py_helper.c \ py_sensor.c \ py_image.c \ py_time.c \ diff --git a/src/omv/py/py_helper.c b/src/omv/py/py_helper.c new file mode 100644 index 000000000..2e2ce5cac --- /dev/null +++ b/src/omv/py/py_helper.c @@ -0,0 +1,62 @@ +#include "mp.h" +#include "imlib.h" +#include "py_helper.h" + +int get_int_kw(mp_map_t *kw_args, mp_obj_t kw, int default_val) +{ + mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, kw, MP_MAP_LOOKUP); + + if (kw_arg != NULL) { + default_val = mp_obj_get_int(kw_arg->value); + } + return default_val; +} + +float get_float_kw(mp_map_t *kw_args, mp_obj_t kw, float default_val) +{ + mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, kw, MP_MAP_LOOKUP); + + if (kw_arg != NULL) { + default_val = mp_obj_get_float(kw_arg->value); + } + return default_val; +} + +int get_color_kw(mp_map_t *kw_args, int default_color) +{ + mp_map_elem_t *kw_color = mp_map_lookup(kw_args, + MP_OBJ_NEW_QSTR(MP_QSTR_color), MP_MAP_LOOKUP); + + if (kw_color != NULL) { + if (mp_obj_is_integer(kw_color->value)) { + default_color = mp_obj_get_int(kw_color->value); + } else { + mp_obj_t *arg_color; + mp_obj_get_array_fixed_n(kw_color->value, 3, &arg_color); + default_color = IM_RGB565(IM_R825(mp_obj_get_int(arg_color[0])), + IM_G826(mp_obj_get_int(arg_color[1])), + IM_B825(mp_obj_get_int(arg_color[2]))); + } + } + return default_color; +} + +void get_rectangle_kw(mp_map_t *kw_args, image_t *img, rectangle_t *r) +{ + mp_map_elem_t *kw_rectangle = mp_map_lookup(kw_args, + MP_OBJ_NEW_QSTR(MP_QSTR_roi), MP_MAP_LOOKUP); + + if (kw_rectangle == NULL) { + r->x = 0; + r->y = 0; + r->w = img->w; + r->h = img->h; + } else { + mp_obj_t *arg_rectangle; + mp_obj_get_array_fixed_n(kw_rectangle->value, 4, &arg_rectangle); + r->x = mp_obj_get_int(arg_rectangle[0]); + r->y = mp_obj_get_int(arg_rectangle[1]); + r->w = mp_obj_get_int(arg_rectangle[2]); + r->h = mp_obj_get_int(arg_rectangle[3]); + } +} diff --git a/src/omv/py/py_helper.h b/src/omv/py/py_helper.h new file mode 100644 index 000000000..a155b9848 --- /dev/null +++ b/src/omv/py/py_helper.h @@ -0,0 +1,15 @@ +/* + * This file is part of the OpenMV project. + * Copyright (c) 2013/2014 Ibrahim Abdelkader + * This work is licensed under the MIT license, see the file LICENSE for details. + * + * MicroPython helper functions. + * + */ +#ifndef __PY_HELPER_H__ +#define __PY_HELPER_H__ +int get_int_kw(mp_map_t *kw_args, mp_obj_t kw, int default_val); +float get_float_kw(mp_map_t *kw_args, mp_obj_t kw, float default_val); +int get_color_kw(mp_map_t *kw_args, int default_color); +void get_rectangle_kw(mp_map_t *kw_args, image_t *img, rectangle_t *r); +#endif // __PY_HELPER__ diff --git a/src/omv/py/py_image.c b/src/omv/py/py_image.c index 6f1ece558..d97af3b41 100644 --- a/src/omv/py/py_image.c +++ b/src/omv/py/py_image.c @@ -12,9 +12,10 @@ #include "sensor.h" #include "ff.h" #include "xalloc.h" -#include "py_assert.h" -#include "py_image.h" #include "arm_math.h" +#include "py_assert.h" +#include "py_helper.h" +#include "py_image.h" #include "omv_boardconfig.h" #define JPEG_INIT_BUF (5*1024) @@ -156,69 +157,6 @@ static mp_obj_t py_image_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t va } } -// Start Helper Functions ///////////////////////////////////////////////////// - -static int get_int_kw(mp_map_t *kw_args, mp_obj_t kw, int default_val) -{ - mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, kw, MP_MAP_LOOKUP); - - if (kw_arg != NULL) { - default_val = mp_obj_get_int(kw_arg->value); - } - return default_val; -} - -static float get_float_kw(mp_map_t *kw_args, mp_obj_t kw, float default_val) -{ - mp_map_elem_t *kw_arg = mp_map_lookup(kw_args, kw, MP_MAP_LOOKUP); - - if (kw_arg != NULL) { - default_val = mp_obj_get_float(kw_arg->value); - } - return default_val; -} - -static int get_color_kw(mp_map_t *kw_args, int default_color) -{ - mp_map_elem_t *kw_color = mp_map_lookup(kw_args, - MP_OBJ_NEW_QSTR(MP_QSTR_color), MP_MAP_LOOKUP); - - if (kw_color != NULL) { - if (mp_obj_is_integer(kw_color->value)) { - default_color = mp_obj_get_int(kw_color->value); - } else { - mp_obj_t *arg_color; - mp_obj_get_array_fixed_n(kw_color->value, 3, &arg_color); - default_color = IM_RGB565(IM_R825(mp_obj_get_int(arg_color[0])), - IM_G826(mp_obj_get_int(arg_color[1])), - IM_B825(mp_obj_get_int(arg_color[2]))); - } - } - return default_color; -} - -static void get_rectangle_kw(mp_map_t *kw_args, image_t *img, rectangle_t *r) -{ - mp_map_elem_t *kw_rectangle = mp_map_lookup(kw_args, - MP_OBJ_NEW_QSTR(MP_QSTR_roi), MP_MAP_LOOKUP); - - if (kw_rectangle == NULL) { - r->x = 0; - r->y = 0; - r->w = img->w; - r->h = img->h; - } else { - mp_obj_t *arg_rectangle; - mp_obj_get_array_fixed_n(kw_rectangle->value, 4, &arg_rectangle); - r->x = mp_obj_get_int(arg_rectangle[0]); - r->y = mp_obj_get_int(arg_rectangle[1]); - r->w = mp_obj_get_int(arg_rectangle[2]); - r->h = mp_obj_get_int(arg_rectangle[3]); - } -} - -// End Helper Functions /////////////////////////////////////////////////////// - static mp_obj_t py_image_width(mp_obj_t img_obj) { image_t *arg_img = py_image_cobj(img_obj);