Skip to content

Commit

Permalink
Merge pull request #2106 from xodio/feat-stdlib-interrupts-and-micros
Browse files Browse the repository at this point in the history
Add interrupt nodes and `micros` type with some utility nodes in the standard library
  • Loading branch information
brusherru committed Mar 4, 2021
2 parents 8ec7fbe + 0f12a6f commit 16dd97c
Show file tree
Hide file tree
Showing 19 changed files with 796 additions and 33 deletions.
2 changes: 2 additions & 0 deletions packages/xod-client-browser/test-func/utils/getPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { SERVER_URL } from '../server.config';
export default async function getPage(browser) {
const page = await browser.newPage();

await page.setDefaultNavigationTimeout(60000);

await page.goto(SERVER_URL);
await page.setViewport({ width: 1024, height: 768 });
await page.waitForSelector('.Workarea');
Expand Down
8 changes: 4 additions & 4 deletions packages/xod-project/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { mapObjIndexed } from 'ramda';

import PIN_TYPE from './internal/pinTypes';
import {
BINDABLE_CUSTOM_TYPE_CONSTRUCTORS,
CUSTOM_TYPE_CONSTRUCTORS,
BINDABLE_CUSTOM_TYPE_DEFAULT_VALUES,
BINDABLE_CUSTOM_TYPES_CAST_NODES,
CUSTOM_TYPES_CAST_NODES,
} from './custom-types';

export { PIN_TYPE };
Expand Down Expand Up @@ -104,7 +104,7 @@ const STATIC_TYPES_CAST_NODES = mapObjIndexed((castsTo, fromType) =>
*/
export const CAST_NODES = {
...STATIC_TYPES_CAST_NODES,
...BINDABLE_CUSTOM_TYPES_CAST_NODES,
...CUSTOM_TYPES_CAST_NODES,
};

// node types that provide a constant value
Expand All @@ -114,7 +114,7 @@ export const CONST_NODETYPES = {
[PIN_TYPE.STRING]: 'xod/core/constant-string',
[PIN_TYPE.BYTE]: 'xod/core/constant-byte',
[PIN_TYPE.PORT]: 'xod/core/constant-port',
...BINDABLE_CUSTOM_TYPE_CONSTRUCTORS,
...CUSTOM_TYPE_CONSTRUCTORS,
};

// node types that provide a constant pulse,
Expand Down
71 changes: 42 additions & 29 deletions packages/xod-project/src/custom-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ import PIN_TYPE from './internal/pinTypes';

// =============================================================================
//
// Add custom type specifications in BINDABLE_CUSTOM_TYPES_SPECS
// Add custom type specifications in CUSTOM_TYPES_SPECS
// Key — an alias for easy access by a Developer
// Value — specification object
//
// =============================================================================

export const BINDABLE_CUSTOM_TYPES_SPECS = {
export const CUSTOM_TYPES_SPECS = {
COLOR: {
typeName: 'xod/color/color',
nodeConstructor: 'xod/color/color',
defaultValue: '#000000',
validateLiteral: R.test(/^#[0-9A-F]{6}$/),
casts: {
[PIN_TYPE.STRING]: 'xod/color/format-color',
},
...{
isBindable: true,
// required fields:
defaultValue: '#000000',
validateLiteral: R.test(/^#[0-9A-F]{6}$/),
},
},
MICROS: {
typeName: 'xod/core/micros',
nodeConstructor: 'xod/core/micros',
casts: {
[PIN_TYPE.STRING]: 'xod/core/cast-to-string(micros)',
},
isBindable: false,
},
};

Expand All @@ -29,54 +41,55 @@ export const BINDABLE_CUSTOM_TYPES_SPECS = {

const indexByTypename = R.indexBy(R.prop('typeName'));

const filterBindable = R.filter(R.prop('isBindable'));

// =============================================================================
// Derived constants
// =============================================================================

const CUSTOM_TYPES_LIST = R.values(BINDABLE_CUSTOM_TYPES_SPECS);

// :: StrMap DataType
export const BINDABLE_CUSTOM_TYPES = R.map(
R.prop('typeName'),
BINDABLE_CUSTOM_TYPES_SPECS
);
const CUSTOM_TYPES_LIST = R.values(CUSTOM_TYPES_SPECS);

// :: Map DataType PatchPath
export const BINDABLE_CUSTOM_TYPE_CONSTRUCTORS = R.compose(
export const CUSTOM_TYPE_CONSTRUCTORS = R.compose(
R.pluck('nodeConstructor'),
indexByTypename
)(CUSTOM_TYPES_LIST);

// :: Map DataType (Map DataType PatchPath)
export const CUSTOM_TYPES_CAST_NODES = R.compose(
R.pluck('casts'),
indexByTypename
)(CUSTOM_TYPES_LIST);

// :: StrMap DataType
export const BINDABLE_CUSTOM_TYPES = R.compose(
R.pluck('typeName'),
filterBindable
)(CUSTOM_TYPES_SPECS);

// :: Map DataType DataValue
export const BINDABLE_CUSTOM_TYPE_DEFAULT_VALUES = R.compose(
R.pluck('defaultValue'),
indexByTypename
indexByTypename,
filterBindable
)(CUSTOM_TYPES_LIST);

// :: [(String -> Boolean)]
// :: Map DataType (String -> Boolean)
export const BINDABLE_CUSTOM_TYPE_VALIDATORS = R.compose(
R.pluck('validateLiteral'),
indexByTypename
indexByTypename,
filterBindable
)(CUSTOM_TYPES_LIST);

// :: [DataType]
export const BINDABLE_CUSTOM_TYPES_LIST = R.pluck(
'typeName',
CUSTOM_TYPES_LIST
);

// :: Map DataType (Map DataType PatchPath)
export const BINDABLE_CUSTOM_TYPES_CAST_NODES = R.compose(
R.pluck('casts'),
R.indexBy(R.prop('typeName')),
R.values
)(BINDABLE_CUSTOM_TYPES_SPECS);
export const BINDABLE_CUSTOM_TYPES_LIST = R.compose(
R.pluck('typeName'),
filterBindable
)(CUSTOM_TYPES_LIST);

// =============================================================================
// Functions
// =============================================================================

// :: String -> Boolean
export const isBindableCustomType = isAmong(
R.pluck('typeName', CUSTOM_TYPES_LIST)
);
export const isBindableCustomType = isAmong(R.values(BINDABLE_CUSTOM_TYPES));
37 changes: 37 additions & 0 deletions workspace/__lib__/xod/core/cast-to-string(micros)/patch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

#pragma XOD dirtieness disable

node {
void uint32ToCharArray(uint32_t value, char* str) {
if (value == 0) {
str[0] = '0';
str[1] = '\0';
}

uint8_t idx = 0;
uint8_t len = 0;

auto v = value;
while (v > 0) {
len++;
v = v / 10;
}
v = value;
while (v > 0) {
auto q = v % 10;
v = (v - q) / 10;
str[len - idx - 1] = '0' + q;
idx++;
}
str[len] = '\0';
}

char str[11];
CStringView view = CStringView(str);

void evaluate(Context ctx) {
auto micros = getValue<input_IN>(ctx);
uint32ToCharArray(micros, str);
emitValue<output_OUT>(ctx, XString(&view));
}
}
41 changes: 41 additions & 0 deletions workspace/__lib__/xod/core/cast-to-string(micros)/patch.xodp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"description": "Transforms micros to a string",
"nodes": [
{
"id": "HJR1uifqM",
"position": {
"units": "slots",
"x": 5,
"y": 1
},
"type": "xod/patch-nodes/utility"
},
{
"id": "__in__",
"position": {
"units": "slots",
"x": 0,
"y": 0
},
"type": "@/input-micros"
},
{
"id": "__out__",
"position": {
"units": "slots",
"x": 0,
"y": 3
},
"type": "xod/patch-nodes/output-string"
},
{
"id": "noNativeImpl",
"position": {
"units": "slots",
"x": 3,
"y": 1
},
"type": "xod/patch-nodes/not-implemented-in-xod"
}
]
}
12 changes: 12 additions & 0 deletions workspace/__lib__/xod/core/delta-micros/patch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

node {
typeof_IN lastMicros = 0;

void evaluate(Context ctx) {
if (!isInputDirty<input_UPD>(ctx)) return;

typeof_IN newLast = getValue<input_IN>(ctx);
emitValue<output_OUT>(ctx, newLast - lastMicros);
lastMicros = newLast;
}
}
45 changes: 45 additions & 0 deletions workspace/__lib__/xod/core/delta-micros/patch.xodp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"description": "Outputs time difference in microseconds between the current and previous updates.",
"nodes": [
{
"description": "Time in microseconds",
"id": "S1dci4Szu",
"position": {
"units": "slots",
"x": 1,
"y": 0
},
"type": "@/input-micros"
},
{
"description": "Delta time in microseconds",
"id": "ryooiEHfd",
"position": {
"units": "slots",
"x": 1,
"y": 2
},
"type": "@/output-micros"
},
{
"id": "Syy2iEBGd",
"position": {
"units": "slots",
"x": 2,
"y": 1
},
"type": "xod/patch-nodes/not-implemented-in-xod"
},
{
"description": "Triggers new update",
"id": "ryC6nNSMd",
"label": "UPD",
"position": {
"units": "slots",
"x": 3,
"y": 0
},
"type": "xod/patch-nodes/input-pulse"
}
]
}
11 changes: 11 additions & 0 deletions workspace/__lib__/xod/core/micros/patch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

node {
meta {
using Type = uint32_t;
}
void evaluate(Context ctx) {
if (isInputDirty<input_UPD>(ctx)) {
emitValue<output_OUT>(ctx, micros());
}
}
}
35 changes: 35 additions & 0 deletions workspace/__lib__/xod/core/micros/patch.xodp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"description": "Outputs time spent since the program start in microseconds.",
"nodes": [
{
"id": "S1Dx-ZNzd",
"position": {
"units": "slots",
"x": 1,
"y": 1
},
"type": "xod/patch-nodes/not-implemented-in-xod"
},
{
"description": "Time in microseconds.",
"id": "BJ5gWbEfu",
"position": {
"units": "slots",
"x": 1,
"y": 2
},
"type": "xod/patch-nodes/output-self"
},
{
"description": "Trigger updating the time in microseconds.",
"id": "S1DmXHHfu",
"label": "UPD",
"position": {
"units": "slots",
"x": 1,
"y": 0
},
"type": "xod/patch-nodes/input-pulse"
}
]
}
7 changes: 7 additions & 0 deletions workspace/__lib__/xod/core/to-seconds/patch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

node {
void evaluate(Context ctx) {
Number micros = getValue<input_IN>(ctx);
emitValue<output_OUT>(ctx, micros / 1000000 );
}
}
32 changes: 32 additions & 0 deletions workspace/__lib__/xod/core/to-seconds/patch.xodp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"description": "Transforms micros to a Number as seconds. The value may lose accuracy.",
"nodes": [
{
"id": "HkqdI2iMu",
"position": {
"units": "slots",
"x": 0,
"y": 0
},
"type": "@/input-micros"
},
{
"id": "ryROL3jz_",
"position": {
"units": "slots",
"x": 0,
"y": 1
},
"type": "xod/patch-nodes/not-implemented-in-xod"
},
{
"id": "SJbYIhsGO",
"position": {
"units": "slots",
"x": 0,
"y": 2
},
"type": "xod/patch-nodes/output-number"
}
]
}
Loading

0 comments on commit 16dd97c

Please sign in to comment.