[WIP] - Support of ulab module for scientific calculations in PythonExtra #15

Draft
Slyvtt wants to merge 33 commits from Slyvtt/PythonExtra:ulab into dev
9 changed files with 325 additions and 63 deletions
Showing only changes of commit c207c7b6f7 - Show all commits

View File

@ -5,7 +5,7 @@ SH_LDFLAGS := -T fx9860g.ld -ljustui-fx -lm -lgint-fx -lc -lgint-fx -lgcc
SH_ASSETS := \
img_fkeys_main.png img_modifier_states.png \
font_5x7.png font_4x4.png font_4x6.png
font_5x7.png font_4x4.png font_4x6.png PoliceNW.png
SH_METADATA := fxconv-metadata.txt
SH_CONVFLAGS := --fx

BIN
ports/fx9860g3/PoliceNW.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -31,3 +31,12 @@ font_4x6.png:
grid.padding: 1
grid.border: 0
proportional: true
PoliceNW.png:
name: numworks
type: font
charset: print
grid.size: 10x16
grid.padding: 0
grid.border: 0
proportional: false

View File

@ -3,7 +3,7 @@ include ../../py/mkenv.mk
SH_CFLAGS := -DFXCG50
SH_LDFLAGS := -T fxcg50.ld -ljustui-cg -lm -lgint-cg -lc -lgint-cg -lgcc
SH_ASSETS := img_modifier_states.png font_9.png font_13.png font_19.png
SH_ASSETS := img_modifier_states.png font_9.png font_13.png font_19.png PoliceNW.png
SH_METADATA := fxconv-metadata.txt
SH_CONVFLAGS := --cg

BIN
ports/fxcg50/PoliceNW.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -29,3 +29,12 @@ font_19.png:
grid.padding: 0
grid.border: 0
proportional: true
PoliceNW.png:
name: numworks
type: font
charset: print
grid.size: 10x16
grid.padding: 0
grid.border: 0
proportional: false

View File

@ -15,6 +15,7 @@ SRC_C = \
ports/sh/debug.c \
ports/sh/keymap.c \
ports/sh/modcasioplot.c \
ports/sh/modkandinsky.c \
ports/sh/modgint.c \
ports/sh/mphalport.c \
ports/sh/pyexec.c \
@ -26,6 +27,7 @@ SRC_C = \
SRC_QSTR += \
ports/sh/main.c \
ports/sh/modcasioplot.c \
ports/sh/modkandinsky.c \
ports/sh/modgint.c \
ports/sh/pyexec.c \

239
ports/sh/modkandinsky.c Normal file
View File

@ -0,0 +1,239 @@
//---------------------------------------------------------------------------//
// ____ PythonExtra //
//.-'`_ o `;__, A community port of MicroPython for CASIO calculators. //
//.-'` `---` ' License: MIT (except some files; see LICENSE) //
//---------------------------------------------------------------------------//
// pe.modkandinsky: Compatibility module for NumWorks Kandinsky library
#include "py/obj.h"
#include "py/runtime.h"
#include <gint/display.h>
#include <gint/drivers/r61524.h>
#include <gint/timer.h>
#include <stdlib.h>
#include <string.h>
extern font_t numworks;
static int callback(void) {
dupdate();
return TIMER_CONTINUE;
}
static mp_obj_t Kandinsky_make_color(color_t color) {
int r, g, b;
r = (color >> 8) & 0xf8;
g = (color >> 3) & 0xfc;
b = (color << 3) & 0xfc;
mp_obj_t items[3] = {
MP_OBJ_NEW_SMALL_INT(r),
MP_OBJ_NEW_SMALL_INT(g),
MP_OBJ_NEW_SMALL_INT(b),
};
return mp_obj_new_tuple(3, items);
}
static mp_obj_t Kandinsky_init(void) {
void pe_enter_graphics_mode(void);
pe_enter_graphics_mode();
dclear(C_WHITE);
int t = timer_configure(TIMER_TMU, 33333, GINT_CALL(callback));
if (t >= 0)
timer_start(t);
return mp_const_none;
}
static mp_obj_t Kandinsky_color(size_t n, mp_obj_t const *args) {
int r = mp_obj_get_int(args[0]);
int g = mp_obj_get_int(args[1]);
int b = mp_obj_get_int(args[2]);
int color = C_RGB(r >> 3, g >> 3, b >> 3);
return mp_obj_new_int(color);
}
int Internal_Get_Color_From_String(const char *str) {
if (strcmp(str, "red") == 0)
return C_RGB(31, 0, 0);
else if (strcmp(str, "r") == 0)
return C_RGB(31, 0, 0);
else if (strcmp(str, "green") == 0)
return C_RGB(0, 31, 0);
else if (strcmp(str, "g") == 0)
return C_RGB(0, 31, 0);
else if (strcmp(str, "blue") == 0)
return C_RGB(0, 0, 31);
else if (strcmp(str, "b") == 0)
return C_RGB(0, 0, 31);
else if (strcmp(str, "black") == 0)
return C_RGB(0, 0, 0);
else if (strcmp(str, "k") == 0)
return C_RGB(0, 0, 0);
else if (strcmp(str, "white") == 0)
return C_RGB(31, 31, 31);
else if (strcmp(str, "w") == 0)
return C_RGB(31, 31, 31);
else if (strcmp(str, "yellow") == 0)
return C_RGB(31, 31, 0);
else if (strcmp(str, "y") == 0)
return C_RGB(31, 31, 0);
// Data can be found here
// https://github.com/numworks/epsilon/blob/master/escher/include/escher/palette.h
// and here
// https://github.com/numworks/epsilon/blob/master/python/port/port.cpp#L221
else if (strcmp(str, "pink") == 0)
return C_RGB(0xFF >> 3, 0xAB >> 3, 0xB6 >> 3);
else if (strcmp(str, "magenta") == 0)
return C_RGB(31, 0, 31);
else if (strcmp(str, "grey") == 0)
return C_RGB(0xA7 >> 3, 0xA7 >> 3, 0xA7 >> 3);
else if (strcmp(str, "gray") == 0)
return C_RGB(0xA7 >> 3, 0xA7 >> 3, 0xA7 >> 3);
else if (strcmp(str, "purple") == 0)
return C_RGB(15, 0, 31);
else if (strcmp(str, "orange") == 0)
return C_RGB(31, 15, 0);
else if (strcmp(str, "cyan") == 0)
return C_RGB(31, 15, 0);
else
return C_RGB(0, 0, 0);
}
int Internal_Treat_Color(mp_obj_t color) {
const mp_obj_type_t *type = mp_obj_get_type(color);
if (type == &mp_type_str) {
size_t text_len;
char const *text = mp_obj_str_get_data(color, &text_len);
return Internal_Get_Color_From_String(text);
}
else if (type == &mp_type_int)
return mp_obj_get_int(color);
else if (type == &mp_type_tuple) {
size_t tuple_len;
mp_obj_t *items;
mp_obj_tuple_get(color, &tuple_len, &items);
int r = mp_obj_get_int(items[0]) >> 3;
int g = mp_obj_get_int(items[1]) >> 3;
int b = mp_obj_get_int(items[2]) >> 3;
return C_RGB(r, g, b);
} else
return C_BLACK;
}
static mp_obj_t Kandinsky_fill_rect(size_t n, mp_obj_t const *args) {
int x = mp_obj_get_int(args[0]);
int y = mp_obj_get_int(args[1]);
int w = mp_obj_get_int(args[2]);
int h = mp_obj_get_int(args[3]);
int color = Internal_Treat_Color(args[4]);
drect(x, y, x + w, y + h, color);
return mp_const_none;
}
static mp_obj_t Kandinsky_set_pixel(size_t n, mp_obj_t const *args) {
int x = mp_obj_get_int(args[0]);
int y = mp_obj_get_int(args[1]);
int color;
if (n == 3)
color = Internal_Treat_Color(args[2]);
else
color = C_BLACK;
dpixel(x, y, color);
return mp_const_none;
}
static mp_obj_t Kandinsky_get_pixel(mp_obj_t _x, mp_obj_t _y) {
int x = mp_obj_get_int(_x);
int y = mp_obj_get_int(_y);
if (x >= 0 && x < DWIDTH && y >= 0 && y < DHEIGHT) {
color_t color = gint_vram[DWIDTH * y + x];
return Kandinsky_make_color(color);
}
return mp_const_none;
}
static mp_obj_t Kandinsky_draw_string(size_t n, mp_obj_t const *args) {
int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
size_t text_len;
char const *text = mp_obj_str_get_data(args[0], &text_len);
char *text_free = NULL;
/* If there are \n in the text, turn them into spaces */
if (strchr(text, '\n')) {
text_free = strdup(text);
if (text_free) {
for (size_t i = 0; i < text_len; i++)
text_free[i] = (text_free[i] == '\n') ? ' ' : text_free[i];
}
}
color_t colortext = C_BLACK;
if (n >= 4) {
colortext = Internal_Treat_Color(args[3]);
}
color_t colorback = C_NONE;
if (n >= 5) {
colorback = Internal_Treat_Color(args[4]);
}
font_t const *old_font = dfont(&numworks);
dtext_opt(x, y, colortext, colorback, DTEXT_LEFT, DTEXT_TOP,
text_free ? text_free : text, text_len);
dfont(old_font);
free(text_free);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(Kandinsky_init_obj, Kandinsky_init);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(Kandinsky_set_pixel_obj, 3, 3,
Kandinsky_set_pixel);
MP_DEFINE_CONST_FUN_OBJ_2(Kandinsky_get_pixel_obj, Kandinsky_get_pixel);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(Kandinsky_draw_string_obj, 3, 5,
Kandinsky_draw_string);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(Kandinsky_fill_rect_obj, 5, 5,
Kandinsky_fill_rect);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(Kandinsky_color_obj, 3, 3, Kandinsky_color);
STATIC const mp_rom_map_elem_t kandinsky_module_globals_table[] = {
{MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_kandinsky)},
{MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&Kandinsky_init_obj)},
{MP_ROM_QSTR(MP_QSTR_fill_rect), MP_ROM_PTR(&Kandinsky_fill_rect_obj)},
{MP_ROM_QSTR(MP_QSTR_color), MP_ROM_PTR(&Kandinsky_color_obj)},
{MP_ROM_QSTR(MP_QSTR_set_pixel), MP_ROM_PTR(&Kandinsky_set_pixel_obj)},
{MP_ROM_QSTR(MP_QSTR_get_pixel), MP_ROM_PTR(&Kandinsky_get_pixel_obj)},
{MP_ROM_QSTR(MP_QSTR_draw_string), MP_ROM_PTR(&Kandinsky_draw_string_obj)},
};
STATIC MP_DEFINE_CONST_DICT(kandinsky_module_globals,
kandinsky_module_globals_table);
const mp_obj_module_t kandinsky_module = {
.base = {&mp_type_module},
.globals = (mp_obj_dict_t *)&kandinsky_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_kandinsky, kandinsky_module);

View File

@ -5,106 +5,109 @@
//---------------------------------------------------------------------------//
// pe.mpconfigport: MicroPython's main port configuration file
#include <stdint.h>
#include <alloca.h>
#include "widget_shell.h"
#include <alloca.h>
#include <stdint.h>
/* Debugging options: PythonExtra debug tools (pretty much required for any
other one), MicroPython's verbose logging. */
/* PythonExtra's main debug flag */
#define PE_DEBUG (0)
#define MICROPY_DEBUG_VERBOSE (0)
#define PE_DEBUG (0)
#define MICROPY_DEBUG_VERBOSE (0)
/* Custom flag to remove DEBUG_printf in alloc/GC (very verbose) */
#define MICROPY_DEBUG_VERBOSE_ALLOC (0)
#define MICROPY_DEBUG_VERBOSE_ALLOC (0)
#if PE_DEBUG
extern const struct _mp_print_t mp_debug_print;
#define MICROPY_DEBUG_PRINTER (&mp_debug_print)
#define MICROPY_DEBUG_PRINTER (&mp_debug_print)
#endif
/* Custom option to use relative imports. For instance when working at the fs
root, 'import b' in '/folder/a.py' will import 'folder/b.py' not '/b.py'. */
#define MICROPY_RELATIVE_FILE_IMPORTS (1)
#define MICROPY_RELATIVE_FILE_IMPORTS (1)
/* General feature set selection
Other options: BASIC_FEATURES, EXTRA_FEATURES, FULL_FEATURES, EVERYTHING */
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES)
/* Main features */
#define MICROPY_ENABLE_COMPILER (1)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_GC_SPLIT_HEAP (1)
#define MP_ENDIANNESS_BIG (1)
#define MICROPY_READER_POSIX (1)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
#define MICROPY_REPL_EVENT_DRIVEN (1)
#define MICROPY_ENABLE_COMPILER (1)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_GC_SPLIT_HEAP (1)
#define MP_ENDIANNESS_BIG (1)
#define MICROPY_READER_POSIX (1)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
#define MICROPY_REPL_EVENT_DRIVEN (1)
/* Other features that we select against MICROPY_CONFIG_ROM_LEVEL */
#define MICROPY_PY_FSTRINGS (1) /* in EXTRA_FEATURES */
#define MICROPY_HELPER_REPL (1) /* in EXTRA_FEATURES */
#define MICROPY_ENABLE_SOURCE_LINE (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_BUILTINS_STR_UNICODE (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_BUILTINS_HELP_MODULES (1) /* in EXTRA_FEATURES */
#define MICROPY_KBD_EXCEPTION (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_SYS_PS1_PS2 (1) /* in EXTRA_FEATURES */
#define MICROPY_MODULE_BUILTIN_INIT (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_ALL_SPECIAL_METHODS (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_FSTRINGS (1) /* in EXTRA_FEATURES */
#define MICROPY_HELPER_REPL (1) /* in EXTRA_FEATURES */
#define MICROPY_ENABLE_SOURCE_LINE (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_BUILTINS_STR_UNICODE (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_BUILTINS_HELP_MODULES (1) /* in EXTRA_FEATURES */
#define MICROPY_KBD_EXCEPTION (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_SYS_PS1_PS2 (1) /* in EXTRA_FEATURES */
#define MICROPY_MODULE_BUILTIN_INIT (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_ALL_SPECIAL_METHODS (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_BUILTINS_ROUND_INT (1) /* in EXTRA_FEATURES */
#define MICROPY_PY_BUILTINS_ROUND_INT (1) /* in EXTRA_FEATURES */
// #define MICROPY_PY_SYS_STDFILES (1) /* in EXTRA_FEATURES */
#define MICROPY_ALLOC_PATH_MAX (256)
#define MICROPY_ALLOC_PARSE_CHUNK_INIT (32)
#define MICROPY_MEM_STATS (0)
#define MICROPY_GC_ALLOC_THRESHOLD (1)
#define MICROPY_ENABLE_DOC_STRING (0)
#define MICROPY_ALLOC_PATH_MAX (256)
#define MICROPY_ALLOC_PARSE_CHUNK_INIT (32)
#define MICROPY_MEM_STATS (0)
#define MICROPY_GC_ALLOC_THRESHOLD (1)
#define MICROPY_ENABLE_DOC_STRING (0)
#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (1)
#define MICROPY_PY_BUILTINS_BYTEARRAY (1)
#define MICROPY_PY_BUILTINS_ENUMERATE (1)
#define MICROPY_PY_BUILTINS_FILTER (1)
#define MICROPY_PY_BUILTINS_FROZENSET (1)
#define MICROPY_PY_BUILTINS_HELP (1)
#define MICROPY_PY_BUILTINS_INPUT (1)
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
#define MICROPY_PY_BUILTINS_MIN_MAX (1)
#define MICROPY_PY_BUILTINS_PROPERTY (1)
#define MICROPY_PY_BUILTINS_REVERSED (1)
#define MICROPY_PY_BUILTINS_SET (1)
#define MICROPY_PY_BUILTINS_SLICE (1)
#define MICROPY_PY_BUILTINS_BYTEARRAY (1)
#define MICROPY_PY_BUILTINS_ENUMERATE (1)
#define MICROPY_PY_BUILTINS_FILTER (1)
#define MICROPY_PY_BUILTINS_FROZENSET (1)
#define MICROPY_PY_BUILTINS_HELP (1)
#define MICROPY_PY_BUILTINS_INPUT (1)
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
#define MICROPY_PY_BUILTINS_MIN_MAX (1)
#define MICROPY_PY_BUILTINS_PROPERTY (1)
#define MICROPY_PY_BUILTINS_REVERSED (1)
#define MICROPY_PY_BUILTINS_SET (1)
#define MICROPY_PY_BUILTINS_SLICE (1)
/* Extra built-in modules */
#define MICROPY_PY_ARRAY (1)
#define MICROPY_PY_COLLECTIONS (1)
#define MICROPY_PY_MATH (1)
#define MICROPY_PY_CMATH (1)
#define MICROPY_PY_GC (1)
#define MICROPY_PY_IO (1)
#define MICROPY_PY_STRUCT (1)
#define MICROPY_PY_RANDOM (1)
#define MICROPY_PY_RANDOM_EXTRA_FUNCS (1)
#define MICROPY_PY_SYS (1)
#define MICROPY_PY_TIME (1)
#define MICROPY_PY_ARRAY (1)
#define MICROPY_PY_COLLECTIONS (1)
#define MICROPY_PY_MATH (1)
#define MICROPY_PY_CMATH (1)
#define MICROPY_PY_GC (1)
#define MICROPY_PY_IO (1)
#define MICROPY_PY_STRUCT (1)
#define MICROPY_PY_RANDOM (1)
#define MICROPY_PY_RANDOM_EXTRA_FUNCS (1)
#define MICROPY_PY_SYS (1)
#define MICROPY_PY_TIME (1)
// TODO: Enable the os module:
// #define MICROPY_PY_UOS (1)
// TODO: Enable other modules
// #define MICROPY_PY_URE (1) // + other flags?
/* Enable alias of u-modules */
#define MICROPY_MODULE_WEAK_LINKS (1)
#define MICROPY_MODULE_WEAK_LINKS (1)
/* Command executed automatically after every shell input */
void pe_after_python_exec(
int input_kind, int exec_flags, void *ret_val, int *ret);
void pe_after_python_exec(int input_kind, int exec_flags, void *ret_val,
int *ret);
#define MICROPY_BOARD_AFTER_PYTHON_EXEC pe_after_python_exec
/* Command executed regularly during execution */
extern void pe_draw(void);
extern widget_shell *pe_shell;
#define MICROPY_VM_HOOK_LOOP \
{ if(pe_shell->widget.update) pe_draw(); }
#define MICROPY_VM_HOOK_LOOP \
{ \
if (pe_shell->widget.update) \
pe_draw(); \
}
/* extra built in names to add to the global namespace
#define MICROPY_PORT_BUILTINS \
@ -116,6 +119,6 @@ typedef uintptr_t mp_uint_t;
typedef long mp_off_t;
#define MICROPY_HW_BOARD_NAME "sh7305"
#define MICROPY_HW_MCU_NAME "sh-4a"
#define MICROPY_HW_MCU_NAME "sh-4a"
#define MP_STATE_PORT MP_STATE_VM
#define MP_STATE_PORT MP_STATE_VM