Skip to content

Commit

Permalink
Updated UGFX widget bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Aram committed Jun 21, 2017
1 parent 4582b34 commit caaf6ab
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 28 deletions.
185 changes: 185 additions & 0 deletions esp32/modugfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,51 @@ uint8_t target_lut;

typedef struct _ugfx_obj_t { mp_obj_base_t base; } ugfx_obj_t;

static orientation_t get_orientation(int a){
if (a == 90)
return GDISP_ROTATE_90;
else if (a == 180)
return GDISP_ROTATE_180;
else if (a == 270)
return GDISP_ROTATE_270;
else
return GDISP_ROTATE_0;
}

// Our default style - a white background theme
const GWidgetStyle BWWhiteWidgetStyle = {
HTML2COLOR(0xFFFFFF), // window background
HTML2COLOR(0x000000), // focused

// enabled color set
{
HTML2COLOR(0x000000), // text
HTML2COLOR(0x000000), // edge
HTML2COLOR(0xFFFFFF), // fill
HTML2COLOR(0x000000) // progress - active area
},

// disabled color set
{
HTML2COLOR(0xC0C0C0), // text
HTML2COLOR(0x808080), // edge
HTML2COLOR(0xE0E0E0), // fill
HTML2COLOR(0xC0E0C0) // progress - active area
},

// pressed color set
{
HTML2COLOR(0xFF0000), // text
HTML2COLOR(0x404040), // edge
HTML2COLOR(0x808080), // fill
HTML2COLOR(0x00E000) // progress - active area
}
};


STATIC mp_obj_t ugfx_init(void) {
gwinSetDefaultFont(gdispOpenFont(gdispListFonts()->font->short_name));
gwinSetDefaultStyle(&BWWhiteWidgetStyle, FALSE);
gfxInit();
return mp_const_none;
}
Expand All @@ -87,6 +131,118 @@ STATIC mp_obj_t ugfx_set_lut(mp_obj_t selected_lut) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ugfx_set_lut_obj, ugfx_set_lut);

/// \method set_orientation(a)
///
/// Set orientation to 0, 90, 180 or 270 degrees
///
STATIC mp_obj_t ugfx_set_orientation(mp_uint_t n_args, const mp_obj_t *args) {
if (n_args > 0){
int a = mp_obj_get_int(args[0]);
gdispSetOrientation(get_orientation(a));

}

return mp_obj_new_int(gdispGetOrientation());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ugfx_set_orientation_obj, 0, 1, ugfx_set_orientation);

/// \method width()
///
/// Gets current width of the screen in pixels
///
STATIC mp_obj_t ugfx_width(void) {
return mp_obj_new_int(gdispGetWidth());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(ugfx_width_obj, ugfx_width);



/// \method height()
///
/// Gets current width of the screen in pixels
///
STATIC mp_obj_t ugfx_height(void) {
return mp_obj_new_int(gdispGetHeight());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(ugfx_height_obj, ugfx_height);


/// \method get_pixel()
///
/// Gets the colour of the given pixel at (x,y)
///
STATIC mp_obj_t ugfx_get_pixel(mp_obj_t x_in, mp_obj_t y_in) {
// extract arguments
//ugfx_obj_t *self = args[0];
//int x = mp_obj_get_int(x_in);
//int y = mp_obj_get_int(y_in);
return mp_obj_new_int(0);
//needs sorting, currently returns somewhat dodgy values
//return mp_obj_new_int(gdispGetPixelColor(x,y));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ugfx_get_pixel_obj, ugfx_get_pixel);


/// \method set_default_font()
///
/// Sets the default font used by widgets.
/// Note, it is only necessary to use a font object if font scaling is used, since
/// in this case memory will need to be cleared once the scaled font is no longer required
///
STATIC mp_obj_t ugfx_set_default_font(mp_obj_t font_obj) {
ugfx_font_obj_t *fo = font_obj;
if (MP_OBJ_IS_TYPE(font_obj, &ugfx_font_type)){
gwinSetDefaultFont(fo->font);
}else if (MP_OBJ_IS_STR(font_obj)){
const char *file = mp_obj_str_get_str(font_obj);
gwinSetDefaultFont(gdispOpenFont(file));
/*}else if (MP_OBJ_IS_INT(font_obj)){*/
/*if (mp_obj_get_int(font_obj) < sizeof(font_list)/sizeof(char*)){*/
/*gwinSetDefaultFont(gdispOpenFont(font_list[mp_obj_get_int(font_obj)]));*/
/*}*/
/*else*/
/*nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid font index"));*/
/*}*/
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ugfx_set_default_font_obj, ugfx_set_default_font);

/// \method set_default_style()
///
/// Sets the default style used by widgets.
///
STATIC mp_obj_t ugfx_set_default_style(mp_obj_t style_obj) {
ugfx_style_obj_t *st = style_obj;
if (MP_OBJ_IS_TYPE(style_obj, &ugfx_style_type))
gwinSetDefaultStyle(&(st->style),FALSE);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ugfx_set_default_style_obj, ugfx_set_default_style);

/// \method send_tab()
///
/// Sends a 'tab' signal to cycle through focus.
///
STATIC mp_obj_t ugfx_send_tab(void) {

GSourceListener *psl=0;
GEventKeyboard *pe;

while ((psl = geventGetSourceListener(ginputGetKeyboard(GKEYBOARD_ALL_INSTANCES), psl))){
pe = (GEventKeyboard *)geventGetEventBuffer(psl);


pe->type = GEVENT_KEYBOARD;
pe->bytecount = 1;
pe->c[0] = GKEY_TAB;
geventSendEvent(psl);
}

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(ugfx_send_tab_obj, ugfx_send_tab);


// PRIMITIVES

Expand Down Expand Up @@ -167,6 +323,25 @@ STATIC mp_obj_t ugfx_char(mp_uint_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ugfx_char_obj, 5, 5, ugfx_char);

/// \method text(x, y, str, colour)
///
/// Draw the given text to the position `(x, y)` using the given colour.
///
STATIC mp_obj_t ugfx_text(mp_uint_t n_args, const mp_obj_t *args) {
// extract arguments
//ugfx_obj_t *self = args[0];
mp_uint_t len;
const char *data = mp_obj_str_get_data(args[2], &len);
int x0 = mp_obj_get_int(args[0]);
int y0 = mp_obj_get_int(args[1]);
int col = mp_obj_get_int(args[3]);

gdispDrawString(x0, y0, data, gwinGetDefaultFont(), col);

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ugfx_text_obj, 4, 4, ugfx_text);

/// \method string(x, y, str, font, colour)
///
/// Draw the given text to the position `(x, y)` using the given font and
Expand Down Expand Up @@ -927,6 +1102,7 @@ STATIC const mp_rom_map_elem_t ugfx_module_globals_table[] = {
(mp_obj_t)&ugfx_get_char_width_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_char), (mp_obj_t)&ugfx_char_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_string), (mp_obj_t)&ugfx_string_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_text), (mp_obj_t)&ugfx_text_obj },
{MP_OBJ_NEW_QSTR(MP_QSTR_string_box), (mp_obj_t)&ugfx_string_box_obj},

{MP_OBJ_NEW_QSTR(MP_QSTR_pixel), (mp_obj_t)&ugfx_pixel_obj},
Expand All @@ -946,13 +1122,22 @@ STATIC const mp_rom_map_elem_t ugfx_module_globals_table[] = {
{MP_OBJ_NEW_QSTR(MP_QSTR_polygon), (mp_obj_t)&ugfx_polygon_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_fill_polygon), (mp_obj_t)&ugfx_fill_polygon_obj},

{ MP_OBJ_NEW_QSTR(MP_QSTR_display_image), (mp_obj_t)&ugfx_display_image_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_orientation), (mp_obj_t)&ugfx_set_orientation_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_width), (mp_obj_t)&ugfx_width_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_height), (mp_obj_t)&ugfx_height_obj },

{MP_OBJ_NEW_QSTR(MP_QSTR_fonts_list), (mp_obj_t)&ugfx_fonts_list_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_fonts_dump), (mp_obj_t)&ugfx_fonts_dump_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_fonts_load), (mp_obj_t)&ugfx_fonts_load_obj},

{MP_OBJ_NEW_QSTR(MP_QSTR_input_init), (mp_obj_t)&ugfx_input_init_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_input_attach), (mp_obj_t)&ugfx_input_attach_obj},
{MP_OBJ_NEW_QSTR(MP_QSTR_demo), (mp_obj_t)&ugfx_demo_obj},
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_default_font), (mp_obj_t)&ugfx_set_default_font_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_default_style), (mp_obj_t)&ugfx_set_default_style_obj },

{ MP_OBJ_NEW_QSTR(MP_QSTR_send_tab), (mp_obj_t)&ugfx_send_tab_obj },

{ MP_OBJ_NEW_QSTR(MP_QSTR_display_image), (mp_obj_t)&ugfx_display_image_obj },

Expand Down
10 changes: 5 additions & 5 deletions esp32/ugfx_containers.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ STATIC mp_obj_t ugfx_polygon(mp_uint_t n_args, const mp_obj_t *args) {

mp_obj_t *mp_arr;
mp_obj_t *mp_arr2;
uint len;
uint len2;
size_t len;
size_t len2;
mp_obj_get_array(args[3], &len, &mp_arr);

if (len <= 20){
Expand Down Expand Up @@ -357,8 +357,8 @@ STATIC mp_obj_t ugfx_fill_polygon(mp_uint_t n_args, const mp_obj_t *args) {

mp_obj_t *mp_arr;
mp_obj_t *mp_arr2;
uint len;
uint len2;
size_t len;
size_t len2;
mp_obj_get_array(args[3], &len, &mp_arr);

if (len <= 20){
Expand Down Expand Up @@ -451,7 +451,7 @@ STATIC mp_obj_t ugfx_container_make_new(const mp_obj_type_t *type, mp_uint_t n_a
//wi.g.parent = ;
wi.text = 0;//text;

if (MP_OBJ_IS_TYPE(vals[4].u_obj, &ugfx_style_type)) {
if (n_args > 4 && MP_OBJ_IS_TYPE(vals[4].u_obj, &ugfx_style_type)) {
ugfx_style_obj_t *st = vals[4].u_obj;
wi.customStyle = &(st->style);
ctr->style = &(st->style);
Expand Down
Loading

0 comments on commit caaf6ab

Please sign in to comment.