diff --git a/python/Makefile b/python/Makefile index c8d8fa8df..c368be85c 100644 --- a/python/Makefile +++ b/python/Makefile @@ -134,6 +134,11 @@ port_src += $(addprefix python/port/,\ mod/ion/modion_table.cpp \ mod/kandinsky/modkandinsky.cpp \ mod/kandinsky/modkandinsky_table.c \ + mod/matplotlib/modpyplot.cpp \ + mod/matplotlib/modpyplot_table.c \ + mod/matplotlib/plot_controller.cpp \ + mod/matplotlib/plot_store.cpp \ + mod/matplotlib/plot_view.cpp \ mod/time/modtime.c \ mod/time/modtime_table.c \ mod/turtle/modturtle.cpp \ diff --git a/python/port/genhdr/qstrdefs.in.h b/python/port/genhdr/qstrdefs.in.h index 17b4eea5c..be88dc99e 100644 --- a/python/port/genhdr/qstrdefs.in.h +++ b/python/port/genhdr/qstrdefs.in.h @@ -73,6 +73,12 @@ Q(fill_rect) Q(get_pixel) Q(set_pixel) +// Matplotlib QSTRs +Q(plot) +Q(pyplot) +Q(show) + + // Turtle QSTRs Q(turtle) Q(forward) diff --git a/python/port/mod/matplotlib/modpyplot.cpp b/python/port/mod/matplotlib/modpyplot.cpp new file mode 100644 index 000000000..065d6dd5c --- /dev/null +++ b/python/port/mod/matplotlib/modpyplot.cpp @@ -0,0 +1,17 @@ +extern "C" { +#include "modpyplot.h" +} +#include "port.h" +#include "plot_controller.h" + +Matplotlib::PlotController sPlotController; + +mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y) { + // Ensure x and y are arrays + // "Push" x and y on bigger arrays +} + +mp_obj_t modpyplot_show() { + MicroPython::ExecutionEnvironment * env = MicroPython::ExecutionEnvironment::currentExecutionEnvironment(); + env->displayViewController(&sPlotController); +} diff --git a/python/port/mod/matplotlib/modpyplot.h b/python/port/mod/matplotlib/modpyplot.h new file mode 100644 index 000000000..e5d2853f9 --- /dev/null +++ b/python/port/mod/matplotlib/modpyplot.h @@ -0,0 +1,10 @@ +#include + +mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y); +//mp_obj_t grid(); +//mp_obj_t scatter(mp_obj_t x, mp_obj_t y); +//mp_obj_t bar(); +//mp_obj_t hist(); +//mp_obj_t axis(mp_obj_t v); +//mp_obj_t text(mp_obj_t x, mp_obj_t y, mp_obj_t text); +mp_obj_t modpyplot_show(); diff --git a/python/port/mod/matplotlib/modpyplot_table.c b/python/port/mod/matplotlib/modpyplot_table.c new file mode 100644 index 000000000..41f0ef6f6 --- /dev/null +++ b/python/port/mod/matplotlib/modpyplot_table.c @@ -0,0 +1,17 @@ +#include "modpyplot.h" + +MP_DEFINE_CONST_FUN_OBJ_1(modpyplot_plot_obj, modpyplot_plot); +MP_DEFINE_CONST_FUN_OBJ_0(modpyplot_show_obj, modpyplot_show); + +STATIC const mp_rom_map_elem_t modpyplot_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyplot) }, + { MP_ROM_QSTR(MP_QSTR_plot), MP_ROM_PTR(&modpyplot_plot_obj) }, + { MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&modpyplot_show_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(modpyplot_module_globals, modpyplot_module_globals_table); + +const mp_obj_module_t modpyplot_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&modpyplot_module_globals, +}; diff --git a/python/port/mod/matplotlib/plot_controller.cpp b/python/port/mod/matplotlib/plot_controller.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/python/port/mod/matplotlib/plot_controller.h b/python/port/mod/matplotlib/plot_controller.h new file mode 100644 index 000000000..27ab135b9 --- /dev/null +++ b/python/port/mod/matplotlib/plot_controller.h @@ -0,0 +1,47 @@ +#ifndef PYTHON_MATPLOTLIB_PLOT_CONTROLLER_H +#define PYTHON_MATPLOTLIB_PLOT_CONTROLLER_H + +//#include +#include +#include +#include "plot_view.h" +#include "plot_store.h" + +#include + +namespace Matplotlib { + +class PlotController : public Shared::SimpleInteractiveCurveViewController { +public: + PlotController() : Shared::SimpleInteractiveCurveViewController(nullptr, &m_cursor), m_store(), m_view(&m_store) {} + //virtual View * view() override { return &m_view; } + + float cursorBottomMarginRatio() override { + return 0.0f; + } + virtual void reloadBannerView() override { + } + + bool handleEvent(Ion::Events::Event event) override { + printf("EVENT!!\n"); + return Shared::SimpleInteractiveCurveViewController::handleEvent(event); + } + + virtual bool handleEnter() override { + printf("ENTER !!\n"); + return false; + } + +protected: + Shared::CurveView * curveView() override { return &m_view; } + Shared::InteractiveCurveViewRange * interactiveCurveViewRange() override { return &m_store; } +private: + Shared::CurveViewCursor m_cursor; + PlotStore m_store; + PlotView m_view; +}; + +} + + +#endif diff --git a/python/port/mod/matplotlib/plot_store.cpp b/python/port/mod/matplotlib/plot_store.cpp new file mode 100644 index 000000000..7c9680232 --- /dev/null +++ b/python/port/mod/matplotlib/plot_store.cpp @@ -0,0 +1 @@ +#include "plot_store.h" diff --git a/python/port/mod/matplotlib/plot_store.h b/python/port/mod/matplotlib/plot_store.h new file mode 100644 index 000000000..b7435ea9b --- /dev/null +++ b/python/port/mod/matplotlib/plot_store.h @@ -0,0 +1,33 @@ +#ifndef PYTHON_MATPLOTLIB_PLOT_STORE_H +#define PYTHON_MATPLOTLIB_PLOT_STORE_H + +//#include +#include + +namespace Matplotlib { + +class PlotStore : public Shared::InteractiveCurveViewRange { +public: + PlotStore() : Shared::InteractiveCurveViewRange() {} + /* + float xMin() const override { return 0.0f; } + float xMax() const override { return 1.0f; } + float yMin() const override { return 0.0f; } + float yMax() const override { return 1.0f; } + */ +private: + /* + mp_obj_array_t * m_plots; + mp_obj_array_t * m_arrows; + mp_obj_array_t * m_scatters; + mp_obj_array_t * m_texts; + mp_obj_array_t * m_rects; + + bool m_grid; + */ + +}; + +} + +#endif diff --git a/python/port/mod/matplotlib/plot_view.cpp b/python/port/mod/matplotlib/plot_view.cpp new file mode 100644 index 000000000..ca6f6c35b --- /dev/null +++ b/python/port/mod/matplotlib/plot_view.cpp @@ -0,0 +1,13 @@ +#include "plot_view.h" + +namespace Matplotlib { + +void PlotView::drawRect(KDContext * ctx, KDRect rect) const { + ctx->fillRect(rect, KDColorWhite); + drawGrid(ctx, rect); + drawAxes(ctx, rect); + drawLabelsAndGraduations(ctx, rect, Axis::Vertical, true); + drawLabelsAndGraduations(ctx, rect, Axis::Horizontal, true); +} + +} diff --git a/python/port/mod/matplotlib/plot_view.h b/python/port/mod/matplotlib/plot_view.h new file mode 100644 index 000000000..2514c4fe2 --- /dev/null +++ b/python/port/mod/matplotlib/plot_view.h @@ -0,0 +1,18 @@ +#ifndef PYTHON_MATPLOTLIB_PLOT_VIEW_H +#define PYTHON_MATPLOTLIB_PLOT_VIEW_H + +#include +#include "plot_store.h" + +namespace Matplotlib { + +class PlotView : public Shared::LabeledCurveView { +public: + PlotView(PlotStore * s) : Shared::LabeledCurveView(s) {} + void drawRect(KDContext * ctx, KDRect rect) const override; +}; + +} + + +#endif diff --git a/python/port/mpconfigport.h b/python/port/mpconfigport.h index 8ef4cad6d..d6565ff01 100644 --- a/python/port/mpconfigport.h +++ b/python/port/mpconfigport.h @@ -130,12 +130,14 @@ typedef long mp_off_t; extern const struct _mp_obj_module_t modion_module; extern const struct _mp_obj_module_t modkandinsky_module; +extern const struct _mp_obj_module_t modpyplot_module; extern const struct _mp_obj_module_t modtime_module; extern const struct _mp_obj_module_t modturtle_module; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_ROM_QSTR(MP_QSTR_ion), MP_ROM_PTR(&modion_module) }, \ { MP_ROM_QSTR(MP_QSTR_kandinsky), MP_ROM_PTR(&modkandinsky_module) }, \ + { MP_ROM_QSTR(MP_QSTR_pyplot), MP_ROM_PTR(&modpyplot_module) }, \ { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&modtime_module) }, \ { MP_ROM_QSTR(MP_QSTR_turtle), MP_ROM_PTR(&modturtle_module) }, \