reachy-blocks / examples.js
gathint
Reachy Blocks: Scratch 2.0-style palette - pills + flat flyout
4795cb7
Raw
History Blame Contribute Delete
8.32 kB
// examples.js - 5 starter programs for the Examples dropdown.
//
// Each entry exposes a Blockly v12 workspace JSON (the same format as the
// Share modal's export). They load directly into the workspace via
// Blockly.serialization.workspaces.load.
(function () {
const numShadow = (n = 0) => ({ shadow: { type: 'math_number', fields: { NUM: n } } });
const txtShadow = (t = '') => ({ shadow: { type: 'text', fields: { TEXT: t } } });
const numBlock = (n) => ({ block: { type: 'math_number', fields: { NUM: n } } });
// Random integer between FROM and TO. Used by Random Dance.
const rand = (from, to) => ({
block: {
type: 'math_random_int',
inputs: {
FROM: numShadow(from),
TO: numShadow(to),
},
},
});
window.REACHY_EXAMPLES = [
// -------------------------------------------------------------
{
id: 'wave-hello',
name: 'Wave Hello',
description: 'Greet the robot. No API key needed.',
program: {
blocks: {
languageVersion: 0,
blocks: [{
type: 'reachy_say', x: 80, y: 80,
inputs: { TEXT: txtShadow("Hello! I'm Reachy.") },
next: { block: {
type: 'reachy_wait_seconds',
inputs: { SECONDS: numShadow(0.5) },
next: { block: {
type: 'reachy_wave_hello',
next: { block: {
type: 'reachy_say',
inputs: { TEXT: txtShadow('Nice to meet you!') },
} },
} },
} },
}],
},
},
},
// -------------------------------------------------------------
{
id: 'joke-teller',
name: 'Joke Teller',
description: 'AI picks a one-liner, robot says it, then waves.',
program: {
blocks: {
languageVersion: 0,
blocks: [{
type: 'variables_set', x: 80, y: 80,
fields: { VAR: { id: 'v_joke' } },
inputs: {
VALUE: { block: {
type: 'reachy_ask_ai',
inputs: { PROMPT: txtShadow('Tell me a one-line robot joke.') },
} },
},
next: { block: {
type: 'reachy_say',
inputs: {
TEXT: { block: { type: 'variables_get', fields: { VAR: { id: 'v_joke' } } } },
},
next: { block: { type: 'reachy_wave_hello' } },
} },
}],
},
variables: [{ name: 'joke', id: 'v_joke' }],
},
},
// -------------------------------------------------------------
{
id: 'receptionist',
name: 'Receptionist',
description: 'Full voice loop: listen, reply via AI, wave. Forever.',
program: {
blocks: {
languageVersion: 0,
blocks: [{
type: 'controls_whileUntil', x: 80, y: 80,
fields: { MODE: 'WHILE' },
inputs: {
BOOL: { block: { type: 'logic_boolean', fields: { BOOL: 'TRUE' } } },
DO: { block: {
type: 'variables_set',
fields: { VAR: { id: 'v_user_said' } },
inputs: { VALUE: { block: { type: 'reachy_listen' } } },
next: { block: {
type: 'reachy_say',
inputs: {
TEXT: { block: {
type: 'reachy_ask_ai_system',
inputs: {
SYSTEM: txtShadow('You are a friendly receptionist. Keep replies under 30 words.'),
USER: { block: { type: 'variables_get', fields: { VAR: { id: 'v_user_said' } } } },
},
} },
},
next: { block: { type: 'reachy_wave_hello' } },
} },
} },
},
}],
},
variables: [{ name: 'user_said', id: 'v_user_said' }],
},
},
// -------------------------------------------------------------
{
id: 'random-dance',
name: 'Random Dance',
description: 'Head + antennas wiggle to random poses. No API key needed.',
program: {
blocks: {
languageVersion: 0,
blocks: [{
type: 'controls_repeat_ext', x: 80, y: 80,
inputs: {
TIMES: numShadow(8),
DO: { block: {
type: 'reachy_move_head',
inputs: { ROLL: rand(-20, 20), PITCH: rand(-15, 15), YAW: rand(-40, 40) },
next: { block: {
type: 'reachy_set_antennas',
inputs: { RIGHT: rand(-90, 90), LEFT: rand(-90, 90) },
next: { block: {
type: 'reachy_wait_seconds',
inputs: { SECONDS: numShadow(0.4) },
} },
} },
} },
},
next: { block: {
type: 'reachy_move_head',
inputs: { ROLL: numShadow(0), PITCH: numShadow(0), YAW: numShadow(0) },
next: { block: {
type: 'reachy_set_antennas',
inputs: { RIGHT: numShadow(0), LEFT: numShadow(0) },
} },
} },
}],
},
},
},
// -------------------------------------------------------------
{
id: 'voice-mirror',
name: 'Voice Mirror',
description: 'Robot repeats what you say. Tests mic + voice without AI cost.',
program: {
blocks: {
languageVersion: 0,
blocks: [{
type: 'controls_whileUntil', x: 80, y: 80,
fields: { MODE: 'WHILE' },
inputs: {
BOOL: { block: { type: 'logic_boolean', fields: { BOOL: 'TRUE' } } },
DO: { block: {
type: 'variables_set',
fields: { VAR: { id: 'v_heard' } },
inputs: { VALUE: { block: { type: 'reachy_listen' } } },
next: { block: {
type: 'reachy_say',
inputs: {
TEXT: { block: { type: 'variables_get', fields: { VAR: { id: 'v_heard' } } } },
},
} },
} },
},
}],
},
variables: [{ name: 'heard', id: 'v_heard' }],
},
},
];
})();