Skip to content

Commit

Permalink
[#143] Updated script-space module to use generated C++ defs
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Nov 20, 2023
1 parent 8ff63c8 commit 62b4952
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define SRC_MAIN_C___ZSCRIPT_MODULES_SCRIPTSPACE_SCRIPTSPACECAPABILITIESCOMMAND_HPP_

#include "../../ZscriptIncludes.hpp"

#include <net/zscript/model/modules/base/ScriptSpaceModule.hpp>
#include "../../execution/ZscriptCommandContext.hpp"
#include "../ZscriptModule.hpp"

Expand All @@ -19,14 +21,13 @@ namespace Zscript {
namespace GenericCore {

template<class ZP>
class ScriptSpaceCapabilitiesCommand {
class ScriptSpaceCapabilitiesCommand: public script_space_module::Capabilities_CommandDefs {
public:

static void execute(ZscriptCommandContext<ZP> ctx) {
CommandOutStream<ZP> out = ctx.getOutStream();
out.writeField('C', MODULE_CAPABILITIES(002));
out.writeField('P', Zscript<ZP>::zscript.getScriptSpaceCount());

out.writeField(RespCommandsSet__C, MODULE_CAPABILITIES(002));
out.writeField(RespScriptSpaceCount__P, Zscript<ZP>::zscript.getScriptSpaceCount());
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

#define MODULE_EXISTS_002 EXISTENCE_MARKER_UTIL
#define MODULE_SWITCH_002 MODULE_SWITCH_UTIL(ScriptSpaceModule<ZP>::execute)

#include "ScriptSpaceSetupCommand.hpp"
#include "ScriptSpaceWriteCommand.hpp"
#include "ScriptSpaceCapabilitiesCommand.hpp"

namespace Zscript {

namespace GenericCore {

template<class ZP>
class ScriptSpaceModule: public ZscriptModule<ZP> {

public:

static void execute(ZscriptCommandContext<ZP> ctx, uint8_t bottomBits) {
switch (bottomBits) {
case 0x0:
Expand All @@ -47,8 +48,11 @@ class ScriptSpaceModule: public ZscriptModule<ZP> {
}

};

}

}

#define ZSCRIPT_SUPPORT_SCRIPT_SPACE
#define ZSCRIPT_SUPPORT_NOTIFICATIONS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,56 @@
#define SRC_MAIN_C___ZSCRIPT_MODULES_SCRIPTSPACE_SCRIPTSPACESETUPCOMMAND_HPP_

#include "../../ZscriptIncludes.hpp"

#include <net/zscript/model/modules/base/ScriptSpaceModule.hpp>
#include "../../execution/ZscriptCommandContext.hpp"
#include "../ZscriptModule.hpp"

#define COMMAND_EXISTS_0021 EXISTENCE_MARKER_UTIL

namespace Zscript {

template<class ZP>
class ScriptSpace;

namespace GenericCore {

template<class ZP>
class ScriptSpaceSetupCommand {
class ScriptSpaceSetupCommand: public script_space_module::ScriptSpaceSetup_CommandDefs {
public:

static void execute(ZscriptCommandContext<ZP> ctx) {
uint16_t spaceIndex = 0;
if (!ctx.getField('P', &spaceIndex)) {
if (!ctx.getField(ReqScriptSpaceId__P, &spaceIndex)) {
ctx.status(ResponseStatus::MISSING_KEY);
return;
} else if (spaceIndex >= Zscript<ZP>::zscript.getScriptSpaceCount()) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}

CommandOutStream<ZP> out = ctx.getOutStream();
ScriptSpace<ZP> *target = Zscript<ZP>::zscript.getScriptSpaces()[spaceIndex];
out.writeField('P', target->getCurrentLength());
out.writeField(RespCurrentWritePosition__P, target->getCurrentLength());
if (target->isRunning()) {
out.writeField('R', 0);
out.writeField(RespRunning__R, 0);
}
if (target->canBeWrittenTo()) {
out.writeField('W', 0);
out.writeField(RespWriteAllowed__W, 0);
}
out.writeField('L', target->getMaxLength());
out.writeField(RespAvailableLength__L, target->getMaxLength());
uint16_t runOpt = 0;
if (ctx.getField('R', &runOpt)) {
if (ctx.getField(ReqRun__R, &runOpt)) {
if (runOpt != 0) {
target->run();
} else {
target->stop();
}
}

}

};

}

}

#endif /* SRC_MAIN_C___ZSCRIPT_MODULES_SCRIPTSPACE_SCRIPTSPACESETUPCOMMAND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@

#ifndef SRC_MAIN_C___ZSCRIPT_MODULES_SCRIPTSPACE_SCRIPTSPACEWRITECOMMAND_HPP_
#define SRC_MAIN_C___ZSCRIPT_MODULES_SCRIPTSPACE_SCRIPTSPACEWRITECOMMAND_HPP_

#include "../../ZscriptIncludes.hpp"

#include <net/zscript/model/modules/base/ScriptSpaceModule.hpp>
#include "../../execution/ZscriptCommandContext.hpp"
#include "../ZscriptModule.hpp"
#include "../../tokenizer/ZscriptTokenizer.hpp"

#define COMMAND_EXISTS_0022 EXISTENCE_MARKER_UTIL

namespace Zscript {

template<class ZP>
class ScriptSpace;

namespace GenericCore {
template<class ZP>
class ScriptSpaceWriteCommand {

template<class ZP>
class ScriptSpaceWriteCommand: public script_space_module::ScriptSpaceWrite_CommandDefs {
private:
static void writeToWriter(ZscriptCommandContext<ZP> ctx, ScriptSpace<ZP> *target, TokenRingBuffer<ZP> writer) {
ZscriptTokenizer<ZP> tok(writer.getWriter(), 2);

Expand All @@ -32,33 +38,35 @@ class ScriptSpaceWriteCommand {
}
target->commitChanges(&writer);
}
public:

public:
static void execute(ZscriptCommandContext<ZP> ctx) {
uint16_t spaceIndex;
if (!ctx.getField('P', &spaceIndex)) {
if (!ctx.getField(ReqScriptSpaceId__P, &spaceIndex)) {
ctx.status(ResponseStatus::MISSING_KEY);
return;
} else if (spaceIndex >= Zscript<ZP>::zscript.getScriptSpaceCount()) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}

CommandOutStream<ZP> out = ctx.getOutStream();
ScriptSpace<ZP> *target = Zscript<ZP>::zscript.getScriptSpaces()[spaceIndex];
if (target->isRunning()) {
ctx.status(ResponseStatus::COMMAND_FAIL);
return;
}
if (ctx.hasField('R')) {
if (ctx.hasField(ReqReset__R)) {
writeToWriter(ctx, target, target->overwrite());
} else {
writeToWriter(ctx, target, target->append());
}
out.writeField('L', target->getRemainingLength());
out.writeField(RespAvailableLength__L, target->getRemainingLength());
}

};

}

}

#endif /* SRC_MAIN_C___ZSCRIPT_MODULES_SCRIPTSPACE_SCRIPTSPACEWRITECOMMAND_HPP_ */

0 comments on commit 62b4952

Please sign in to comment.