mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[code] Added default Fibonacci script.
Change-Id: I2766fa542ee64722018486a874e9855a5976a788
This commit is contained in:
@@ -12,6 +12,7 @@ app_objs += $(addprefix apps/code/,\
|
||||
script.o\
|
||||
script_parameter_controller.o\
|
||||
script_store.o\
|
||||
script_template.o\
|
||||
)
|
||||
|
||||
i18n_files += $(addprefix apps/code/,\
|
||||
|
||||
@@ -17,10 +17,6 @@ const Image * App::Descriptor::icon() {
|
||||
return ImageStore::CodeIcon;
|
||||
}
|
||||
|
||||
App::Snapshot::Snapshot() {
|
||||
m_scriptStore.addNewScript(ScriptStore::DefaultScript::Factorial);
|
||||
}
|
||||
|
||||
App * App::Snapshot::unpack(Container * container) {
|
||||
return new App(container, this);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ public:
|
||||
};
|
||||
class Snapshot : public ::App::Snapshot {
|
||||
public:
|
||||
Snapshot();
|
||||
App * unpack(Container * container) override;
|
||||
void reset() override;
|
||||
Descriptor * descriptor() override;
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
|
||||
namespace Code {
|
||||
|
||||
constexpr char ScriptStore::k_defaultScriptName[];
|
||||
|
||||
ScriptStore::ScriptStore() :
|
||||
m_accordion(m_scriptData, k_scriptDataSize)
|
||||
{
|
||||
addScriptFromTemplate(ScriptTemplate::Factorial());
|
||||
addScriptFromTemplate(ScriptTemplate::Fibonacci());
|
||||
}
|
||||
|
||||
const Script ScriptStore::scriptAtIndex(int index, EditableZone zone) {
|
||||
@@ -59,47 +59,8 @@ int ScriptStore::numberOfScripts() {
|
||||
return (m_accordion.numberOfBuffers())/Script::NumberOfStringsPerScript;
|
||||
}
|
||||
|
||||
bool ScriptStore::addNewScript(DefaultScript defaultScript) {
|
||||
const char autoImportationString[2] = {Script::DefaultAutoImportationMarker, 0};
|
||||
if (!m_accordion.appendBuffer(autoImportationString)) {
|
||||
return false;
|
||||
}
|
||||
const char * name = nullptr;
|
||||
switch (defaultScript) {
|
||||
case DefaultScript::Empty:
|
||||
name = k_defaultScriptName;
|
||||
break;
|
||||
case DefaultScript::Mandelbrot:
|
||||
name = "mandelbrot.py";
|
||||
break;
|
||||
case DefaultScript::Factorial:
|
||||
name = "factorial.py";
|
||||
break;
|
||||
}
|
||||
if (!m_accordion.appendBuffer(name)) {
|
||||
// Delete the Auto Importation Marker
|
||||
m_accordion.deleteLastBuffer();
|
||||
return false;
|
||||
}
|
||||
bool didCopy = false;
|
||||
switch (defaultScript) {
|
||||
case DefaultScript::Empty:
|
||||
didCopy = copyEmptyScriptOnFreeSpace();
|
||||
break;
|
||||
case DefaultScript::Mandelbrot:
|
||||
didCopy = copyMandelbrotScriptOnFreeSpace();
|
||||
break;
|
||||
case DefaultScript::Factorial:
|
||||
didCopy = copyFactorialScriptOnFreeSpace();
|
||||
break;
|
||||
}
|
||||
if (didCopy) {
|
||||
return true;
|
||||
}
|
||||
// Delete the Auto Importation Marker and the Name Of the Script
|
||||
m_accordion.deleteLastBuffer();
|
||||
m_accordion.deleteLastBuffer();
|
||||
return false;
|
||||
bool ScriptStore::addNewScript() {
|
||||
return addScriptFromTemplate(ScriptTemplate::Empty());
|
||||
}
|
||||
|
||||
bool ScriptStore::renameScriptAtIndex(int index, const char * newName) {
|
||||
@@ -143,6 +104,32 @@ const char * ScriptStore::contentOfScript(const char * name) {
|
||||
return script.content();
|
||||
}
|
||||
|
||||
|
||||
bool ScriptStore::addScriptFromTemplate(const ScriptTemplate * scriptTemplate) {
|
||||
const char autoImportationString[2] = {Script::DefaultAutoImportationMarker, 0};
|
||||
if (!m_accordion.appendBuffer(autoImportationString)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_accordion.appendBuffer(scriptTemplate->name())) {
|
||||
// Delete the Auto Importation Marker
|
||||
m_accordion.deleteLastBuffer();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (copyStaticScriptOnFreeSpace(scriptTemplate)) {
|
||||
return true;
|
||||
}
|
||||
// Delete the Auto Importation Marker and the Name Of the Script
|
||||
m_accordion.deleteLastBuffer();
|
||||
m_accordion.deleteLastBuffer();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScriptStore::copyStaticScriptOnFreeSpace(const ScriptTemplate * scriptTemplate) {
|
||||
return m_accordion.appendBuffer(scriptTemplate->content());
|
||||
}
|
||||
|
||||
int ScriptStore::accordionIndexOfMarkersOfScriptAtIndex(int index) const {
|
||||
return index * Script::NumberOfStringsPerScript;
|
||||
}
|
||||
@@ -155,46 +142,4 @@ int ScriptStore::accordionIndexOfContentOfScriptAtIndex(int index) const {
|
||||
return index * Script::NumberOfStringsPerScript + 2;
|
||||
}
|
||||
|
||||
bool ScriptStore::copyMandelbrotScriptOnFreeSpace() {
|
||||
const char script[] = R"(# This script draws a Mandelbrot fractal set
|
||||
# N_iteration: degree of precision
|
||||
import kandinsky
|
||||
N_iteration = 10
|
||||
for x in range(320):
|
||||
for y in range(240):
|
||||
# Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i)
|
||||
z_r = 0
|
||||
z_i = 0
|
||||
# Rescale to fit the drawing screen 320x222
|
||||
c_r = 2.7*x/319-2.1
|
||||
c_i = -1.87*y/221+0.935
|
||||
i = 0
|
||||
while (i < N_iteration) and ((z_r * z_r) + (z_i * z_i) < 4):
|
||||
i = i + 1
|
||||
stock = z_r
|
||||
z_r = z_r * z_r - z_i * z_i + c_r
|
||||
z_i = 2 * stock * z_i + c_i
|
||||
# Choose the color of the dot from the Mandelbrot sequence
|
||||
rgb = int(255*i/N_iteration)
|
||||
col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25))
|
||||
# Draw a pixel colored in 'col' at position (x,y)
|
||||
kandinsky.set_pixel(x,y,col))";
|
||||
|
||||
return m_accordion.appendBuffer(script);
|
||||
}
|
||||
bool ScriptStore::copyFactorialScriptOnFreeSpace() {
|
||||
const char script[] = R"(def factorial(n):
|
||||
if n == 0:
|
||||
return 1
|
||||
else:
|
||||
return n * factorial(n-1))";
|
||||
|
||||
return m_accordion.appendBuffer(script);
|
||||
}
|
||||
|
||||
bool ScriptStore::copyEmptyScriptOnFreeSpace() {
|
||||
const char script[] = " ";
|
||||
return m_accordion.appendBuffer(script);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define CODE_SCRIPT_STORE_H
|
||||
|
||||
#include "script.h"
|
||||
#include "script_template.h"
|
||||
#include <escher/accordion.h>
|
||||
#include <python/port/port.h>
|
||||
|
||||
@@ -15,17 +16,11 @@ public:
|
||||
Content
|
||||
};
|
||||
|
||||
enum class DefaultScript {
|
||||
Empty,
|
||||
Mandelbrot,
|
||||
Factorial
|
||||
};
|
||||
|
||||
ScriptStore();
|
||||
const Script scriptAtIndex(int index, EditableZone zone = EditableZone::None);
|
||||
const Script scriptNamed(const char * name);
|
||||
int numberOfScripts();
|
||||
bool addNewScript(DefaultScript defaultScript = DefaultScript::Empty);
|
||||
bool addNewScript();
|
||||
bool renameScriptAtIndex(int index, const char * newName);
|
||||
void switchAutoImportAtIndex(int index);
|
||||
void deleteScriptAtIndex(int index);
|
||||
@@ -37,13 +32,12 @@ public:
|
||||
private:
|
||||
static constexpr char k_defaultScriptName[] = ".py";
|
||||
static constexpr size_t k_scriptDataSize = 1024;
|
||||
bool addScriptFromTemplate(const ScriptTemplate * scriptTemplate);
|
||||
bool copyStaticScriptOnFreeSpace(const ScriptTemplate * scriptTemplate);
|
||||
int accordionIndexOfScriptAtIndex(int index) const;
|
||||
int accordionIndexOfMarkersOfScriptAtIndex(int index) const;
|
||||
int accordionIndexOfNameOfScriptAtIndex(int index) const;
|
||||
int accordionIndexOfContentOfScriptAtIndex(int index) const;
|
||||
bool copyMandelbrotScriptOnFreeSpace();
|
||||
bool copyFactorialScriptOnFreeSpace();
|
||||
bool copyEmptyScriptOnFreeSpace();
|
||||
char m_scriptData[k_scriptDataSize];
|
||||
Accordion m_accordion;
|
||||
};
|
||||
|
||||
72
apps/code/script_template.cpp
Normal file
72
apps/code/script_template.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "script_template.h"
|
||||
|
||||
namespace Code {
|
||||
|
||||
constexpr ScriptTemplate emptyScriptTemplate(".py", R"()");
|
||||
|
||||
constexpr ScriptTemplate factorialScriptTemplate("factorial.py", R"(def factorial(n):
|
||||
if n == 0:
|
||||
return 1
|
||||
else:
|
||||
return n * factorial(n-1))");
|
||||
|
||||
constexpr ScriptTemplate fibonacciScriptTemplate("fibonacci.py", R"(def fibo(n):
|
||||
a=0
|
||||
b=1
|
||||
for i in range(1,n+1):
|
||||
c=a+b
|
||||
a=b
|
||||
b=c
|
||||
return a
|
||||
|
||||
def fibo2(n):
|
||||
if n==0:
|
||||
return 0
|
||||
elif n==1 or n==2:
|
||||
return 1
|
||||
return fibo2(n-1)+fibo2(n-2))");
|
||||
|
||||
constexpr ScriptTemplate mandelbrotScriptTemplate("mandelbrot.py", R"(# This script draws a Mandelbrot fractal set
|
||||
# N_iteration: degree of precision
|
||||
import kandinsky
|
||||
N_iteration = 10
|
||||
def drawMandelbrot():
|
||||
for x in range(320):
|
||||
for y in range(240):
|
||||
# Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i)
|
||||
z_r = 0
|
||||
z_i = 0
|
||||
# Rescale to fit the drawing screen 320x222
|
||||
c_r = 2.7*x/319-2.1
|
||||
c_i = -1.87*y/221+0.935
|
||||
i = 0
|
||||
while (i < N_iteration) and ((z_r * z_r) + (z_i * z_i) < 4):
|
||||
i = i + 1
|
||||
stock = z_r
|
||||
z_r = z_r * z_r - z_i * z_i + c_r
|
||||
z_i = 2 * stock * z_i + c_i
|
||||
# Choose the color of the dot from the Mandelbrot sequence
|
||||
rgb = int(255*i/N_iteration)
|
||||
col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25))
|
||||
# Draw a pixel colored in 'col' at position (x,y)
|
||||
kandinsky.set_pixel(x,y,col))");
|
||||
|
||||
const ScriptTemplate * ScriptTemplate::Empty() {
|
||||
return &emptyScriptTemplate;
|
||||
}
|
||||
|
||||
const ScriptTemplate * ScriptTemplate::Factorial() {
|
||||
return &factorialScriptTemplate;
|
||||
}
|
||||
|
||||
const ScriptTemplate * ScriptTemplate::Fibonacci() {
|
||||
return &fibonacciScriptTemplate;
|
||||
}
|
||||
|
||||
const ScriptTemplate * ScriptTemplate::Mandelbrot() {
|
||||
return &mandelbrotScriptTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
20
apps/code/script_template.h
Normal file
20
apps/code/script_template.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef CODE_SCRIPT_TEMPLATE_H
|
||||
#define CODE_SCRIPT_TEMPLATE_H
|
||||
|
||||
namespace Code {
|
||||
|
||||
class ScriptTemplate {
|
||||
public:
|
||||
constexpr ScriptTemplate(const char * name, const char * content) : m_name(name), m_content(content) {}
|
||||
static const ScriptTemplate * Empty();
|
||||
static const ScriptTemplate * Factorial();
|
||||
static const ScriptTemplate * Fibonacci();
|
||||
static const ScriptTemplate * Mandelbrot();
|
||||
const char * name() const { return m_name; }
|
||||
const char * content() const { return m_content; }
|
||||
private:
|
||||
const char * m_name;
|
||||
const char * m_content;
|
||||
};}
|
||||
|
||||
#endif
|
||||
@@ -20,6 +20,7 @@ int Accordion::numberOfBuffers() {
|
||||
const char * Accordion::bufferAtIndex(int index) {
|
||||
assert(index >= 0 && index < numberOfBuffers());
|
||||
cleanFreeSpace();
|
||||
moveFreeSpaceAtEndOfHistory();
|
||||
int startOfBuffer = startOfBufferAtIndex(index);
|
||||
return &m_history[startOfBuffer];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user