Merge pull request #29 from adafruit/tylerdcooper-patch-7

docs: arithmetic block
This commit is contained in:
Loren Norman 2025-08-19 13:46:13 -04:00 committed by GitHub
commit 6924ca667d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,42 +5,36 @@ export default {
name: "Arithmetic", name: "Arithmetic",
colour: 120, colour: 120,
inputsInline: true, inputsInline: true,
description: "Perform the specified arithmetic operation on two specified operands.", description: "Perform mathematical calculations using sensor data, feed values, or any numbers in your Actions. Perfect for creating custom formulas like calculating averages, converting units (Celsius to Fahrenheit), computing percentages, or building complex calculations from multiple data sources.",
connections: { connections: {
mode: "value", mode: "value",
output: "expression", output: "expression",
}, },
template: `%A %OP %B`, template: `%A %OP %B`,
inputs: { inputs: {
A: { A: {
description: "The left side of the operation. Will be coerced to a number", description: "The first number in your calculation (left side). Examples: temperature reading (72.5), feed value, sensor data, or results from other calculations. Non-numeric values will be automatically converted to numbers where possible.",
check: "expression", check: "expression",
shadow: 'io_math_number' shadow: 'io_math_number'
}, },
B: { B: {
description: "The right side of the operation. Will be coerced to a number", description: "The second number in your calculation (right side). Examples: conversion factors (1.8 for temp conversion), target values (75 for comparison), other sensor readings, or mathematical constants. Also automatically converted to numbers.",
check: "expression", check: "expression",
shadow: 'io_math_number' shadow: 'io_math_number'
}, },
}, },
fields: { fields: {
OP: { OP: {
description: "The mathematical operation to perform.", description: "Choose the mathematical operation to perform:",
options: [ options: [
['+', 'ADD', "add two numbers"], ['+', 'ADD', "Addition: Combine two numbers (e.g., 25 + 5 = 30). For totaling values, adding offsets, or combining multiple sensor readings into sums."],
['-', 'MINUS', "subtract number B from number A"], ['-', 'MINUS', "Subtraction: Remove B from A (e.g., 30 - 5 = 25). For calculating differences, finding deltas between readings, or subtracting baseline values."],
['x', 'MULTIPLY', "multiply two numbers"], ['x', 'MULTIPLY', "Multiplication: A times B (e.g., 6 x 4 = 24). For unit conversions, scaling values, calculating areas/volumes, or applying multiplication factors."],
['/', 'DIVIDE', "divide number A by number B"], ['/', 'DIVIDE', "Division: A divided by B (e.g., 20 ÷ 4 = 5). For calculating averages, ratios, percentages, or converting between different unit scales."],
['^', 'POWER', "raise number A to the power of number B"], ['^', 'POWER', "Exponentiation: A raised to the power of B (e.g., 2^3 = 8). For advanced calculations, exponential growth models, or complex mathematical formulas."],
] ]
} }
}, },
generators: { generators: {
json: (block, generator) => { json: (block, generator) => {
const const
@ -54,7 +48,6 @@ export default {
operator = block.getFieldValue('OP'), operator = block.getFieldValue('OP'),
leftExp = generator.valueToCode(block, 'A', 0) || 'null', leftExp = generator.valueToCode(block, 'A', 0) || 'null',
rightExp = generator.valueToCode(block, 'B', 0) || 'null', rightExp = generator.valueToCode(block, 'B', 0) || 'null',
blockPayload = JSON.stringify({ blockPayload = JSON.stringify({
arithmetic: { arithmetic: {
left: JSON.parse(leftExp), left: JSON.parse(leftExp),
@ -64,11 +57,9 @@ export default {
right: JSON.parse(rightExp) right: JSON.parse(rightExp)
} }
}) })
return [ blockPayload, 0 ] return [ blockPayload, 0 ]
} }
}, },
regenerators: { regenerators: {
json: (blockObject, helpers) => { json: (blockObject, helpers) => {
const const
@ -87,7 +78,6 @@ export default {
A: helpers.expressionToBlock(payload.left, { shadow: 'io_math_number' }), A: helpers.expressionToBlock(payload.left, { shadow: 'io_math_number' }),
B: helpers.expressionToBlock(payload.right, { shadow: 'io_math_number' }), B: helpers.expressionToBlock(payload.right, { shadow: 'io_math_number' }),
} }
return { type: 'io_math_arithmetic', fields, inputs } return { type: 'io_math_arithmetic', fields, inputs }
} }
} }