diff --git a/packages/hikkaku/src/blocks/boost.ts b/packages/hikkaku/src/blocks/boost.ts new file mode 100644 index 0000000..e4eec7b --- /dev/null +++ b/packages/hikkaku/src/blocks/boost.ts @@ -0,0 +1,397 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource, menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { + HikkakuBool, + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type BoostMotorId = 'A' | 'B' | 'C' | 'D' | 'AB' | 'ABCD' +export type BoostMotorReporterId = 'A' | 'B' | 'C' | 'D' +export type BoostMotorDirection = 'forward' | 'backward' | 'reverse' +export type BoostColor = + | 'red' + | 'blue' + | 'green' + | 'yellow' + | 'white' + | 'black' + | 'any' +export type BoostTiltDirection = 'up' | 'down' | 'left' | 'right' +export type BoostTiltDirectionAny = 'up' | 'down' | 'left' | 'right' | 'any' + +/** + * Turns a Boost motor on for the given duration. + * + * Input: `motorId`, `duration`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @param duration See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostMotorOnFor } from 'hikkaku/blocks' + * + * boostMotorOnFor('A', 1) + * ``` + */ +export const boostMotorOnFor = ( + motorId: PrimitiveSource, + duration: PrimitiveSource, +) => { + return block('boost_motorOnFor', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + DURATION: fromPrimitiveSource(InputType.Number, duration, 1), + }, + }) +} + +export const menuOfMotorId = (motorId: BoostMotorId = 'A') => { + return valueBlock('boost_menu_MOTOR_ID', { + fields: { + MOTOR_ID: [motorId, null], + }, + isShadow: true, + }) +} + +/** + * Turns a Boost motor on for the given number of rotations. + * + * Input: `motorId`, `rotation`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @param rotation See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostMotorOnForRotation } from 'hikkaku/blocks' + * + * boostMotorOnForRotation('A', 1) + * ``` + */ +export const boostMotorOnForRotation = ( + motorId: PrimitiveSource, + rotation: PrimitiveSource, +) => { + return block('boost_motorOnForRotation', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + ROTATION: fromPrimitiveSource(InputType.Number, rotation, 1), + }, + }) +} + +/** + * Turns a Boost motor on indefinitely. + * + * Input: `motorId`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostMotorOn } from 'hikkaku/blocks' + * + * boostMotorOn('A') + * ``` + */ +export const boostMotorOn = (motorId: PrimitiveSource) => { + return block('boost_motorOn', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + }, + }) +} + +/** + * Turns a Boost motor off. + * + * Input: `motorId`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostMotorOff } from 'hikkaku/blocks' + * + * boostMotorOff('A') + * ``` + */ +export const boostMotorOff = (motorId: PrimitiveSource) => { + return block('boost_motorOff', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + }, + }) +} + +/** + * Sets the power of a Boost motor. + * + * Input: `motorId`, `power`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @param power See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostSetMotorPower } from 'hikkaku/blocks' + * + * boostSetMotorPower('ABCD', 100) + * ``` + */ +export const boostSetMotorPower = ( + motorId: PrimitiveSource, + power: PrimitiveSource, +) => { + return block('boost_setMotorPower', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + POWER: fromPrimitiveSource(InputType.Number, power, 100), + }, + }) +} + +/** + * Sets the direction of a Boost motor. + * + * Input: `motorId`, `direction`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @param direction See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostSetMotorDirection } from 'hikkaku/blocks' + * + * boostSetMotorDirection('A', 'forward') + * ``` + */ +export const boostSetMotorDirection = ( + motorId: PrimitiveSource, + direction: PrimitiveSource, +) => { + return block('boost_setMotorDirection', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + MOTOR_DIRECTION: menuInput(direction, menuOfMotorDirection), + }, + }) +} + +export const menuOfMotorDirection = ( + direction: BoostMotorDirection = 'forward', +) => { + return valueBlock('boost_menu_MOTOR_DIRECTION', { + fields: { + MOTOR_DIRECTION: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Returns the position of a Boost motor. + * + * Input: `motorId`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param motorId See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { boostGetMotorPosition } from 'hikkaku/blocks' + * + * boostGetMotorPosition('A') + * ``` + */ +export const boostGetMotorPosition = ( + motorId: PrimitiveSource, +) => { + return valueBlock('boost_getMotorPosition', { + inputs: { + MOTOR_REPORTER_ID: menuInput(motorId, menuOfMotorReporterId), + }, + }) +} + +export const menuOfMotorReporterId = ( + motorId: BoostMotorReporterId = 'A', +) => { + return valueBlock('boost_menu_MOTOR_REPORTER_ID', { + fields: { + MOTOR_REPORTER_ID: [motorId, null], + }, + isShadow: true, + }) +} + +/** + * Hat block that triggers when Boost sensor sees a specific color. + * + * Input: `color`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param color See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostWhenColor } from 'hikkaku/blocks' + * + * boostWhenColor('red', () => {}) + * ``` + */ +export const boostWhenColor = ( + color: PrimitiveSource, + stack?: () => void, +) => { + const res = block('boost_whenColor', { + topLevel: true, + inputs: { + COLOR: menuInput(color, menuOfColor), + }, + }) + attachStack(res.id, stack) + return res +} + +export const menuOfColor = (color: BoostColor = 'any') => { + return valueBlock('boost_menu_COLOR', { + fields: { + COLOR: [color, null], + }, + isShadow: true, + }) +} + +/** + * Boolean check for whether Boost sensor sees a specific color. + * + * Input: `color`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param color See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { boostSeeingColor } from 'hikkaku/blocks' + * + * boostSeeingColor('red') + * ``` + */ +export const boostSeeingColor = (color: PrimitiveSource) => { + return valueBlock('boost_seeingColor', { + inputs: { + COLOR: menuInput(color, menuOfColor), + }, + }) +} + +/** + * Hat block that triggers when Boost is tilted. + * + * Input: `tiltDirection`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param tiltDirection See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostWhenTilted } from 'hikkaku/blocks' + * + * boostWhenTilted('any', () => {}) + * ``` + */ +export const boostWhenTilted = ( + tiltDirection: PrimitiveSource, + stack?: () => void, +) => { + const res = block('boost_whenTilted', { + topLevel: true, + inputs: { + TILT_DIRECTION_ANY: menuInput(tiltDirection, menuOfTiltDirectionAny), + }, + }) + attachStack(res.id, stack) + return res +} + +export const menuOfTiltDirectionAny = ( + direction: BoostTiltDirectionAny = 'any', +) => { + return valueBlock('boost_menu_TILT_DIRECTION_ANY', { + fields: { + TILT_DIRECTION_ANY: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Returns the tilt angle of the Boost sensor. + * + * Input: `tiltDirection`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param tiltDirection See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { boostGetTiltAngle } from 'hikkaku/blocks' + * + * boostGetTiltAngle('up') + * ``` + */ +export const boostGetTiltAngle = ( + tiltDirection: PrimitiveSource, +) => { + return valueBlock('boost_getTiltAngle', { + inputs: { + TILT_DIRECTION: menuInput(tiltDirection, menuOfTiltDirection), + }, + }) +} + +export const menuOfTiltDirection = (direction: BoostTiltDirection = 'up') => { + return valueBlock('boost_menu_TILT_DIRECTION', { + fields: { + TILT_DIRECTION: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Sets the Boost LED light color. + * + * Input: `hue`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param hue See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { boostSetLightHue } from 'hikkaku/blocks' + * + * boostSetLightHue(50) + * ``` + */ +export const boostSetLightHue = (hue: PrimitiveSource) => { + return block('boost_setLightHue', { + inputs: { + HUE: fromPrimitiveSource(InputType.Number, hue, 50), + }, + }) +} diff --git a/packages/hikkaku/src/blocks/ev3.ts b/packages/hikkaku/src/blocks/ev3.ts new file mode 100644 index 0000000..f743088 --- /dev/null +++ b/packages/hikkaku/src/blocks/ev3.ts @@ -0,0 +1,314 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource, menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { + HikkakuBool, + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type Ev3MotorPort = '0' | '1' | '2' | '3' +export type Ev3SensorPort = '0' | '1' | '2' | '3' + +/** + * Turns an EV3 motor clockwise for the given time. + * + * Input: `port`, `time`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param port See function signature for accepted input values. + * @param time See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { ev3MotorTurnClockwise } from 'hikkaku/blocks' + * + * ev3MotorTurnClockwise('0', 1) + * ``` + */ +export const ev3MotorTurnClockwise = ( + port: PrimitiveSource, + time: PrimitiveSource, +) => { + return block('ev3_motorTurnClockwise', { + inputs: { + PORT: menuInput(port, menuOfMotorPorts), + TIME: fromPrimitiveSource(InputType.Number, time, 1), + }, + }) +} + +/** + * Turns an EV3 motor counter-clockwise for the given time. + * + * Input: `port`, `time`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param port See function signature for accepted input values. + * @param time See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { ev3MotorTurnCounterClockwise } from 'hikkaku/blocks' + * + * ev3MotorTurnCounterClockwise('0', 1) + * ``` + */ +export const ev3MotorTurnCounterClockwise = ( + port: PrimitiveSource, + time: PrimitiveSource, +) => { + return block('ev3_motorTurnCounterClockwise', { + inputs: { + PORT: menuInput(port, menuOfMotorPorts), + TIME: fromPrimitiveSource(InputType.Number, time, 1), + }, + }) +} + +export const menuOfMotorPorts = (port: Ev3MotorPort = '0') => { + return valueBlock('ev3_menu_motorPorts', { + fields: { + motorPorts: [port, null], + }, + isShadow: true, + }) +} + +/** + * Sets the power of an EV3 motor. + * + * Input: `port`, `power`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param port See function signature for accepted input values. + * @param power See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { ev3MotorSetPower } from 'hikkaku/blocks' + * + * ev3MotorSetPower('0', 100) + * ``` + */ +export const ev3MotorSetPower = ( + port: PrimitiveSource, + power: PrimitiveSource, +) => { + return block('ev3_motorSetPower', { + inputs: { + PORT: menuInput(port, menuOfMotorPorts), + POWER: fromPrimitiveSource(InputType.Number, power, 100), + }, + }) +} + +/** + * Returns the position of an EV3 motor. + * + * Input: `port`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param port See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { ev3GetMotorPosition } from 'hikkaku/blocks' + * + * ev3GetMotorPosition('0') + * ``` + */ +export const ev3GetMotorPosition = (port: PrimitiveSource) => { + return valueBlock('ev3_getMotorPosition', { + inputs: { + PORT: menuInput(port, menuOfMotorPorts), + }, + }) +} + +/** + * Hat block that triggers when an EV3 button is pressed. + * + * Input: `port`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param port See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { ev3WhenButtonPressed } from 'hikkaku/blocks' + * + * ev3WhenButtonPressed('0', () => {}) + * ``` + */ +export const ev3WhenButtonPressed = ( + port: PrimitiveSource, + stack?: () => void, +) => { + const res = block('ev3_whenButtonPressed', { + topLevel: true, + inputs: { + PORT: menuInput(port, menuOfSensorPorts), + }, + }) + attachStack(res.id, stack) + return res +} + +export const menuOfSensorPorts = (port: Ev3SensorPort = '0') => { + return valueBlock('ev3_menu_sensorPorts', { + fields: { + sensorPorts: [port, null], + }, + isShadow: true, + }) +} + +/** + * Hat block that triggers when distance is less than a threshold. + * + * Input: `distance`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param distance See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { ev3WhenDistanceLessThan } from 'hikkaku/blocks' + * + * ev3WhenDistanceLessThan(5, () => {}) + * ``` + */ +export const ev3WhenDistanceLessThan = ( + distance: PrimitiveSource, + stack?: () => void, +) => { + const res = block('ev3_whenDistanceLessThan', { + topLevel: true, + inputs: { + DISTANCE: fromPrimitiveSource(InputType.Number, distance, 5), + }, + }) + attachStack(res.id, stack) + return res +} + +/** + * Hat block that triggers when brightness is less than a threshold. + * + * Input: `distance`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param distance See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { ev3WhenBrightnessLessThan } from 'hikkaku/blocks' + * + * ev3WhenBrightnessLessThan(50, () => {}) + * ``` + */ +export const ev3WhenBrightnessLessThan = ( + distance: PrimitiveSource, + stack?: () => void, +) => { + const res = block('ev3_whenBrightnessLessThan', { + topLevel: true, + inputs: { + DISTANCE: fromPrimitiveSource(InputType.Number, distance, 50), + }, + }) + attachStack(res.id, stack) + return res +} + +/** + * Boolean check for whether an EV3 button is pressed. + * + * Input: `port`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param port See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { ev3ButtonPressed } from 'hikkaku/blocks' + * + * ev3ButtonPressed('0') + * ``` + */ +export const ev3ButtonPressed = (port: PrimitiveSource) => { + return valueBlock('ev3_buttonPressed', { + inputs: { + PORT: menuInput(port, menuOfSensorPorts), + }, + }) +} + +/** + * Returns the EV3 distance sensor value. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { ev3GetDistance } from 'hikkaku/blocks' + * + * ev3GetDistance() + * ``` + */ +export const ev3GetDistance = () => { + return valueBlock('ev3_getDistance', {}) +} + +/** + * Returns the EV3 brightness sensor value. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { ev3GetBrightness } from 'hikkaku/blocks' + * + * ev3GetBrightness() + * ``` + */ +export const ev3GetBrightness = () => { + return valueBlock('ev3_getBrightness', {}) +} + +/** + * Beeps a note on EV3 for some time. + * + * Input: `note`, `time`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param note See function signature for accepted input values. + * @param time See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { ev3Beep } from 'hikkaku/blocks' + * + * ev3Beep(60, 0.5) + * ``` + */ +export const ev3Beep = ( + note: PrimitiveSource, + time: PrimitiveSource, +) => { + return block('ev3_beep', { + inputs: { + NOTE: fromPrimitiveSource(InputType.Number, note, 60), + TIME: fromPrimitiveSource(InputType.Number, time, 0.5), + }, + }) +} diff --git a/packages/hikkaku/src/blocks/face_sensing.ts b/packages/hikkaku/src/blocks/face_sensing.ts new file mode 100644 index 0000000..f8d25bd --- /dev/null +++ b/packages/hikkaku/src/blocks/face_sensing.ts @@ -0,0 +1,233 @@ +import { menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { + HikkakuBool, + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type FaceSensingPart = + | 'nose' + | 'leftEye' + | 'rightEye' + | 'leftEar' + | 'rightEar' + | 'mouth' + | 'betweenEyes' + | 'topOfHead' +export type FaceSensingTilt = 'left' | 'right' + +/** + * Moves the sprite to a face part. + * + * Input: `part`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param part See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { faceSensingGoToPart } from 'hikkaku/blocks' + * + * faceSensingGoToPart('nose') + * ``` + */ +export const faceSensingGoToPart = (part: PrimitiveSource) => { + return block('faceSensing_goToPart', { + inputs: { + PART: menuInput(part, faceSensingMenuOfPart), + }, + }) +} + +export const faceSensingMenuOfPart = (part: FaceSensingPart = 'nose') => { + return valueBlock('faceSensing_menu_PART', { + fields: { + PART: [part, null], + }, + isShadow: true, + }) +} + +/** + * Points the sprite in the direction of face tilt. + * + * Input: none. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { faceSensingPointInFaceTiltDirection } from 'hikkaku/blocks' + * + * faceSensingPointInFaceTiltDirection() + * ``` + */ +export const faceSensingPointInFaceTiltDirection = () => { + return block('faceSensing_pointInFaceTiltDirection', {}) +} + +/** + * Sets the size of the sprite to the face size. + * + * Input: none. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { faceSensingSetSizeToFaceSize } from 'hikkaku/blocks' + * + * faceSensingSetSizeToFaceSize() + * ``` + */ +export const faceSensingSetSizeToFaceSize = () => { + return block('faceSensing_setSizeToFaceSize', {}) +} + +/** + * Hat block that triggers when face tilts in a direction. + * + * Input: `direction`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param direction See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { faceSensingWhenTilted } from 'hikkaku/blocks' + * + * faceSensingWhenTilted('left', () => {}) + * ``` + */ +export const faceSensingWhenTilted = ( + direction: PrimitiveSource, + stack?: () => void, +) => { + const res = block('faceSensing_whenTilted', { + topLevel: true, + inputs: { + DIRECTION: menuInput(direction, faceSensingMenuOfTilt), + }, + }) + attachStack(res.id, stack) + return res +} + +export const faceSensingMenuOfTilt = (direction: FaceSensingTilt = 'left') => { + return valueBlock('faceSensing_menu_TILT', { + fields: { + TILT: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Hat block that triggers when this sprite touches a face part. + * + * Input: `part`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param part See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { faceSensingWhenSpriteTouchesPart } from 'hikkaku/blocks' + * + * faceSensingWhenSpriteTouchesPart('nose', () => {}) + * ``` + */ +export const faceSensingWhenSpriteTouchesPart = ( + part: PrimitiveSource, + stack?: () => void, +) => { + const res = block('faceSensing_whenSpriteTouchesPart', { + topLevel: true, + inputs: { + PART: menuInput(part, faceSensingMenuOfPart), + }, + }) + attachStack(res.id, stack) + return res +} + +/** + * Hat block that triggers when a face is detected. + * + * Input: `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { faceSensingWhenFaceDetected } from 'hikkaku/blocks' + * + * faceSensingWhenFaceDetected(() => {}) + * ``` + */ +export const faceSensingWhenFaceDetected = (stack?: () => void) => { + const res = block('faceSensing_whenFaceDetected', { + topLevel: true, + }) + attachStack(res.id, stack) + return res +} + +/** + * Boolean check for whether a face is detected. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { faceSensingFaceIsDetected } from 'hikkaku/blocks' + * + * faceSensingFaceIsDetected() + * ``` + */ +export const faceSensingFaceIsDetected = () => { + return valueBlock('faceSensing_faceIsDetected', {}) +} + +/** + * Returns the face tilt value. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { faceSensingFaceTilt } from 'hikkaku/blocks' + * + * faceSensingFaceTilt() + * ``` + */ +export const faceSensingFaceTilt = () => { + return valueBlock('faceSensing_faceTilt', {}) +} + +/** + * Returns the face size value. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { faceSensingFaceSize } from 'hikkaku/blocks' + * + * faceSensingFaceSize() + * ``` + */ +export const faceSensingFaceSize = () => { + return valueBlock('faceSensing_faceSize', {}) +} diff --git a/packages/hikkaku/src/blocks/gdx_for.ts b/packages/hikkaku/src/blocks/gdx_for.ts new file mode 100644 index 0000000..2dea5ac --- /dev/null +++ b/packages/hikkaku/src/blocks/gdx_for.ts @@ -0,0 +1,263 @@ +import { InputType } from 'sb3-types/enum' +import { menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { + HikkakuBool, + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type GdxForGesture = 'shaken' | 'startedFalling' | 'turnedFaceUp' | 'turnedFaceDown' +export type GdxForPushPull = 'pushed' | 'pulled' +export type GdxForAxis = 'x' | 'y' | 'z' +export type GdxForTilt = 'front' | 'back' | 'left' | 'right' +export type GdxForTiltAny = 'front' | 'back' | 'left' | 'right' | 'any' + +/** + * Hat block that triggers when a GDX-FOR gesture is detected. + * + * Input: `gesture`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param gesture See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { gdxforWhenGesture } from 'hikkaku/blocks' + * + * gdxforWhenGesture('shaken', () => {}) + * ``` + */ +export const gdxforWhenGesture = ( + gesture: PrimitiveSource, + stack?: () => void, +) => { + const res = block('gdxfor_whenGesture', { + topLevel: true, + inputs: { + GESTURE: menuInput(gesture, gdxforMenuOfGestures), + }, + }) + attachStack(res.id, stack) + return res +} + +export const gdxforMenuOfGestures = (gesture: GdxForGesture = 'shaken') => { + return valueBlock('gdxfor_menu_gestureOptions', { + fields: { + gestureOptions: [gesture, null], + }, + isShadow: true, + }) +} + +/** + * Hat block that triggers when force sensor is pushed or pulled. + * + * Input: `pushPull`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param pushPull See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { gdxforWhenForcePushedOrPulled } from 'hikkaku/blocks' + * + * gdxforWhenForcePushedOrPulled('pushed', () => {}) + * ``` + */ +export const gdxforWhenForcePushedOrPulled = ( + pushPull: PrimitiveSource, + stack?: () => void, +) => { + const res = block('gdxfor_whenForcePushedOrPulled', { + topLevel: true, + inputs: { + PUSH_PULL: menuInput(pushPull, gdxforMenuOfPushPull), + }, + }) + attachStack(res.id, stack) + return res +} + +export const gdxforMenuOfPushPull = (pushPull: GdxForPushPull = 'pushed') => { + return valueBlock('gdxfor_menu_pushPullOptions', { + fields: { + pushPullOptions: [pushPull, null], + }, + isShadow: true, + }) +} + +/** + * Returns the GDX-FOR force sensor value. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { gdxforGetForce } from 'hikkaku/blocks' + * + * gdxforGetForce() + * ``` + */ +export const gdxforGetForce = () => { + return valueBlock('gdxfor_getForce', {}) +} + +/** + * Hat block that triggers when GDX-FOR is tilted. + * + * Input: `tilt`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param tilt See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { gdxforWhenTilted } from 'hikkaku/blocks' + * + * gdxforWhenTilted('any', () => {}) + * ``` + */ +export const gdxforWhenTilted = ( + tilt: PrimitiveSource, + stack?: () => void, +) => { + const res = block('gdxfor_whenTilted', { + topLevel: true, + inputs: { + TILT: menuInput(tilt, gdxforMenuOfTiltAny), + return res +} + +export const gdxforMenuOfTiltAny = (tilt: GdxForTiltAny = 'any') => { + return valueBlock('gdxfor_menu_tiltAnyOptions', { + fields: { + tiltAnyOptions: [tilt, null], + }, + isShadow: true, + }) +} + +/** + * Boolean check for whether GDX-FOR is tilted. + * + * Input: `tilt`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param tilt See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { gdxforIsTilted } from 'hikkaku/blocks' + * + * gdxforIsTilted('any') + * ``` + */ +export const gdxforIsTilted = (tilt: PrimitiveSource) => { + return valueBlock('gdxfor_isTilted', { + inputs: { + TILT: menuInput(tilt, gdxforMenuOfTiltAny), of the GDX-FOR sensor. + * + * Input: `tilt`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param tilt See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { gdxforGetTilt } from 'hikkaku/blocks' + * + * gdxforGetTilt('front') + * ``` + */ +export const gdxforGetTilt = (tilt: PrimitiveSource) => { + return valueBlock('gdxfor_getTilt', { + inputs: { + TILT: menuInput(tilt, gdxforMenuOfTilt), + }, + }) +} + +export const gdxforMenuOfTilt = (tilt: GdxForTilt = 'front') => { + return valueBlock('gdxfor_menu_tiltOptions', { + fields: { + tiltOptions: [tilt, null], + }, + isShadow: true, + }) +} + +/** + * Boolean check for whether GDX-FOR is free falling. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { gdxforIsFreeFalling } from 'hikkaku/blocks' + * + * gdxforIsFreeFalling() + * ``` + */ +export const gdxforIsFreeFalling = () => { + return valueBlock('gdxfor_isFreeFalling', {}) +} + +/** + * Returns the spin speed of the GDX-FOR sensor. + * + * Input: `direction`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param direction See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { gdxforGetSpinSpeed } from 'hikkaku/blocks' + * + * gdxforGetSpinSpeed('z') + * ``` + */ +export const gdxforGetSpinSpeed = (direction: PrimitiveSource) => { + return valueBlock('gdxfor_getSpinSpeed', { + inputs: { + DIRECTION: menuInput(direction, gdxforMenuOfAxis), = (direction: GdxForAxis = 'z') => { + return valueBlock('gdxfor_menu_axisOptions', { + fields: { + axisOptions: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Returns the acceleration of the GDX-FOR sensor. + * + * Input: `direction`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param direction See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { gdxforGetAcceleration } from 'hikkaku/blocks' + * + * gdxforGetAcceleration('x') + * ``` + */ +export const gdxforGetAcceleration = ( + direction: PrimitiveSource, +) => { + return valueBlock('gdxfor_getAcceleration', { + inputs: { + DIRECTION: menuInput(direction, gdxforMenuOfAxis), \ No newline at end of file diff --git a/packages/hikkaku/src/blocks/index.ts b/packages/hikkaku/src/blocks/index.ts index 0eebc3f..01e0372 100644 --- a/packages/hikkaku/src/blocks/index.ts +++ b/packages/hikkaku/src/blocks/index.ts @@ -1,7 +1,13 @@ +export * from './boost' export * from './control' export * from './data' +export * from './ev3' export * from './events' +export * from './face_sensing' +export * from './gdx_for' export * from './looks' +export * from './makeymakey' +export * from './microbit' export * from './motion' export * from './music' export * from './operator' @@ -9,3 +15,8 @@ export * from './pen' export * from './procedures' export * from './sensing' export * from './sound' +export * from './speech2text' +export * from './text2speech' +export * from './translate' +export * from './video_sensing' +export * from './wedo2' diff --git a/packages/hikkaku/src/blocks/makeymakey.ts b/packages/hikkaku/src/blocks/makeymakey.ts new file mode 100644 index 0000000..b979c7a --- /dev/null +++ b/packages/hikkaku/src/blocks/makeymakey.ts @@ -0,0 +1,95 @@ +import { InputType } from 'sb3-types/enum' +import { menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { HikkakuString, PrimitiveSource } from '../core/types' + +export type MakeyMakeyKey = + | 'SPACE' + | 'UP' + | 'DOWN' + | 'LEFT' + | 'RIGHT' + | 'w' + | 'a' + | 's' + | 'd' + | 'f' + | 'g' + +/** + * Hat block that triggers when a Makey Makey key is pressed. + * + * Input: `key`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param key See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { whenMakeyKeyPressed } from 'hikkaku/blocks' + * + * whenMakeyKeyPressed('SPACE', () => {}) + * ``` + */ +export const whenMakeyKeyPressed = ( + key: PrimitiveSource, + stack?: () => void, +) => { + const res = block('makeymakey_whenMakeyKeyPressed', { + topLevel: true, + inputs: { + KEY: menuInput(key, menuOfKey), + }, + }) + attachStack(res.id, stack) + return res +} + +export const menuOfKey = (key: MakeyMakeyKey = 'SPACE') => { + return valueBlock('makeymakey_menu_KEY', { + fields: { + KEY: [key, null], + }, + isShadow: true, + }) +} + +/** + * Hat block that triggers when a sequence of Makey Makey keys is pressed in order. + * + * Input: `sequence`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param sequence See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { whenCodePressed } from 'hikkaku/blocks' + * + * whenCodePressed('LEFT UP RIGHT', () => {}) + * ``` + */ +export const whenCodePressed = ( + sequence: PrimitiveSource, + stack?: () => void, +) => { + const res = block('makeymakey_whenCodePressed', { + topLevel: true, + inputs: { + SEQUENCE: menuInput(sequence, menuOfSequence), + }, + }) + attachStack(res.id, stack) + return res +} + +export const menuOfSequence = (sequence = 'LEFT UP RIGHT') => { + return valueBlock('makeymakey_menu_SEQUENCE', { + fields: { + SEQUENCE: [sequence, null], + }, + isShadow: true, + }) +} diff --git a/packages/hikkaku/src/blocks/microbit.ts b/packages/hikkaku/src/blocks/microbit.ts new file mode 100644 index 0000000..50c86ff --- /dev/null +++ b/packages/hikkaku/src/blocks/microbit.ts @@ -0,0 +1,334 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource, menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { + HikkakuBool, + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type MicroBitButton = 'A' | 'B' | 'any' +export type MicroBitGesture = + | 'moved' + | 'shaken' + | 'jumped' +export type MicroBitTiltDirection = 'front' | 'back' | 'left' | 'right' +export type MicroBitTiltDirectionAny = + | 'front' + | 'back' + | 'left' + | 'right' + | 'any' + +/** + * Hat block that triggers when a micro:bit button is pressed. + * + * Input: `button`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param button See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { microbitWhenButtonPressed } from 'hikkaku/blocks' + * + * microbitWhenButtonPressed('A', () => {}) + * ``` + */ +export const microbitWhenButtonPressed = ( + button: PrimitiveSource, + stack?: () => void, +) => { + const res = block('microbit_whenButtonPressed', { + topLevel: true, + inputs: { + BTN: menuInput(button, microbitMenuOfButtons), + }, + }) + attachStack(res.id, stack) + return res +} + +export const microbitMenuOfButtons = (button: MicroBitButton = 'A') => { + return valueBlock('microbit_menu_buttons', { + fields: { + buttons: [button, null], + }, + isShadow: true, + }) +} + +/** + * Boolean check for whether a micro:bit button is pressed. + * + * Input: `button`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param button See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { microbitIsButtonPressed } from 'hikkaku/blocks' + * + * microbitIsButtonPressed('A') + * ``` + */ +export const microbitIsButtonPressed = ( + button: PrimitiveSource, +) => { + return valueBlock('microbit_isButtonPressed', { + inputs: { + BTN: menuInput(button, microbitMenuOfButtons), + }, + }) +} + +/** + * Hat block that triggers when a micro:bit gesture is detected. + * + * Input: `gesture`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param gesture See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { microbitWhenGesture } from 'hikkaku/blocks' + * + * microbitWhenGesture('moved', () => {}) + * ``` + */ +export const microbitWhenGesture = ( + gesture: PrimitiveSource, + stack?: () => void, +) => { + const res = block('microbit_whenGesture', { + topLevel: true, + inputs: { + GESTURE: menuInput(gesture, microbitMenuOfGestures), + }, + }) + attachStack(res.id, stack) + return res +} + +export const microbitMenuOfGestures = (gesture: MicroBitGesture = 'moved') => { + return valueBlock('microbit_menu_gestures', { + fields: { + gestures: [gesture, null], + }, + isShadow: true, + }) +} + +/** + * Displays a symbol on the micro:bit LED matrix. + * + * Input: `matrix`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param matrix See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { microbitDisplaySymbol } from 'hikkaku/blocks' + * + * microbitDisplaySymbol('0101010101100010101000100') + * ``` + */ +export const microbitDisplaySymbol = ( + matrix: PrimitiveSource, +) => { + return block('microbit_displaySymbol', { + inputs: { + MATRIX: fromPrimitiveSource( + InputType.String, + matrix, + '0101010101100010101000100', + ), + }, + }) +} + +/** + * Displays text on the micro:bit. + * + * Input: `text`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param text See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { microbitDisplayText } from 'hikkaku/blocks' + * + * microbitDisplayText('Hello!') + * ``` + */ +export const microbitDisplayText = (text: PrimitiveSource) => { + return block('microbit_displayText', { + inputs: { + TEXT: fromPrimitiveSource(InputType.String, text, 'Hello!'), + }, + }) +} + +/** + * Clears the micro:bit display. + * + * Input: none. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { microbitDisplayClear } from 'hikkaku/blocks' + * + * microbitDisplayClear() + * ``` + */ +export const microbitDisplayClear = () => { + return block('microbit_displayClear', {}) +} + +/** + * Hat block that triggers when micro:bit is tilted. + * + * Input: `direction`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param direction See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { microbitWhenTilted } from 'hikkaku/blocks' + * + * microbitWhenTilted('any', () => {}) + * ``` + */ +export const microbitWhenTilted = ( + direction: PrimitiveSource, + stack?: () => void, +) => { + const res = block('microbit_whenTilted', { + topLevel: true, + inputs: { + DIRECTION: menuInput(direction, microbitMenuOfTiltDirectionAny), + }, + }) + attachStack(res.id, stack) + return res +} + +export const microbitMenuOfTiltDirectionAny = ( + direction: MicroBitTiltDirectionAny = 'any', +) => { + return valueBlock('microbit_menu_tiltDirectionAny', { + fields: { + tiltDirectionAny: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Boolean check for whether micro:bit is tilted. + * + * Input: `direction`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param direction See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { microbitIsTilted } from 'hikkaku/blocks' + * + * microbitIsTilted('any') + * ``` + */ +export const microbitIsTilted = (direction: PrimitiveSource) => { + return valueBlock('microbit_isTilted', { + inputs: { + DIRECTION: menuInput(direction, microbitMenuOfTiltDirectionAny), + }, + }) +} + +/** + * Returns the tilt angle of the micro:bit. + * + * Input: `direction`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param direction See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { microbitGetTiltAngle } from 'hikkaku/blocks' + * + * microbitGetTiltAngle('front') + * ``` + */ +export const microbitGetTiltAngle = ( + direction: PrimitiveSource, +) => { + return valueBlock('microbit_getTiltAngle', { + inputs: { + DIRECTION: menuInput(direction, microbitMenuOfTiltDirection), + }, + }) +} + +export const microbitMenuOfTiltDirection = ( + direction: MicroBitTiltDirection = 'front', +) => { + return valueBlock('microbit_menu_tiltDirection', { + fields: { + tiltDirection: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Hat block that triggers when a micro:bit pin is connected. + * + * Input: `pin`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param pin See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { microbitWhenPinConnected } from 'hikkaku/blocks' + * + * microbitWhenPinConnected('0', () => {}) + * ``` + */ +export const microbitWhenPinConnected = ( + pin: PrimitiveSource, + stack?: () => void, +) => { + const res = block('microbit_whenPinConnected', { + topLevel: true, + inputs: { + PIN: menuInput(pin, microbitMenuOfTouchPins), + }, + }) + attachStack(res.id, stack) + return res +} + +export const microbitMenuOfTouchPins = (pin = '0') => { + return valueBlock('microbit_menu_touchPins', { + fields: { + touchPins: [pin, null], + }, + isShadow: true, + }) +} diff --git a/packages/hikkaku/src/blocks/speech2text.ts b/packages/hikkaku/src/blocks/speech2text.ts new file mode 100644 index 0000000..f60b012 --- /dev/null +++ b/packages/hikkaku/src/blocks/speech2text.ts @@ -0,0 +1,70 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { HikkakuString, PrimitiveSource } from '../core/types' + +/** + * Listens to the microphone and waits for speech recognition. + * + * Input: none. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { listenAndWait } from 'hikkaku/blocks' + * + * listenAndWait() + * ``` + */ +export const listenAndWait = () => { + return block('speech2text_listenAndWait', {}) +} + +/** + * Hat block that triggers when the given phrase is heard. + * + * Input: `phrase`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param phrase See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { whenIHearHat } from 'hikkaku/blocks' + * + * whenIHearHat("let's go", () => {}) + * ``` + */ +export const whenIHearHat = ( + phrase: PrimitiveSource, + stack?: () => void, +) => { + const res = block('speech2text_whenIHearHat', { + topLevel: true, + inputs: { + PHRASE: fromPrimitiveSource(InputType.String, phrase, "let's go"), + }, + }) + attachStack(res.id, stack) + return res +} + +/** + * Returns the speech recognized by the microphone. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { getSpeech } from 'hikkaku/blocks' + * + * getSpeech() + * ``` + */ +export const getSpeech = () => { + return valueBlock('speech2text_getSpeech', {}) +} diff --git a/packages/hikkaku/src/blocks/text2speech.ts b/packages/hikkaku/src/blocks/text2speech.ts new file mode 100644 index 0000000..f30e610 --- /dev/null +++ b/packages/hikkaku/src/blocks/text2speech.ts @@ -0,0 +1,115 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource, menuInput } from '../core/block-helper' +import { block, valueBlock } from '../core/composer' +import type { + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type Text2SpeechVoice = 'ALTO' | 'TENOR' | 'SQUEAK' | 'GIANT' | 'KITTEN' + +/** + * Speaks the given words and waits. + * + * Input: `words`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param words See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { speakAndWait } from 'hikkaku/blocks' + * + * speakAndWait('hello') + * ``` + */ +export const speakAndWait = (words: PrimitiveSource) => { + return block('text2speech_speakAndWait', { + inputs: { + WORDS: fromPrimitiveSource(InputType.String, words, 'hello'), + }, + }) +} + +/** + * Sets the voice for text-to-speech. + * + * Input: `voice`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param voice See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { setVoice } from 'hikkaku/blocks' + * + * setVoice('ALTO') + * ``` + */ +export const setVoice = (voice: PrimitiveSource) => { + return block('text2speech_setVoice', { + inputs: { + VOICE: menuInput(voice, menuOfVoice), + }, + }) +} + +export const menuOfVoice = (voice: Text2SpeechVoice = 'ALTO') => { + return valueBlock('text2speech_menu_voices', { + fields: { + voices: [voice, null], + }, + isShadow: true, + }) +} + +/** + * Sets the language for text-to-speech. + * + * Input: `language`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param language See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { setLanguage } from 'hikkaku/blocks' + * + * setLanguage('en') + * ``` + */ +export const setLanguage = (language: PrimitiveSource) => { + return block('text2speech_setLanguage', { + inputs: { + LANGUAGE: menuInput(language, menuOfLanguage), + }, + }) +} + +export const menuOfLanguage = (language = 'en') => { + return valueBlock('text2speech_menu_languages', { + fields: { + languages: [language, null], + }, + isShadow: true, + }) +} + +/** + * Returns the current text-to-speech language. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { getLanguage } from 'hikkaku/blocks' + * + * getLanguage() + * ``` + */ +export const getLanguage = () => { + return valueBlock('text2speech_getLanguage', {}) +} diff --git a/packages/hikkaku/src/blocks/translate.ts b/packages/hikkaku/src/blocks/translate.ts new file mode 100644 index 0000000..39c946d --- /dev/null +++ b/packages/hikkaku/src/blocks/translate.ts @@ -0,0 +1,59 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource, menuInput } from '../core/block-helper' +import { valueBlock } from '../core/composer' +import type { HikkakuString, PrimitiveSource } from '../core/types' + +/** + * Translates words to the given language. + * + * Input: `words`, `language`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param words See function signature for accepted input values. + * @param language See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { getTranslate } from 'hikkaku/blocks' + * + * getTranslate('hello', 'fr') + * ``` + */ +export const getTranslate = ( + words: PrimitiveSource, + language: PrimitiveSource, +) => { + return valueBlock('translate_getTranslate', { + inputs: { + WORDS: fromPrimitiveSource(InputType.String, words, 'hello'), + LANGUAGE: menuInput(language, menuOfLanguages), + }, + }) +} + +export const menuOfLanguages = (language = 'fr') => { + return valueBlock('translate_menu_languages', { + fields: { + languages: [language, null], + }, + isShadow: true, + }) +} + +/** + * Returns the language of the viewer. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { getViewerLanguage } from 'hikkaku/blocks' + * + * getViewerLanguage() + * ``` + */ +export const getViewerLanguage = () => { + return valueBlock('translate_getViewerLanguage', {}) +} diff --git a/packages/hikkaku/src/blocks/video_sensing.ts b/packages/hikkaku/src/blocks/video_sensing.ts new file mode 100644 index 0000000..2f9cc43 --- /dev/null +++ b/packages/hikkaku/src/blocks/video_sensing.ts @@ -0,0 +1,160 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource, menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { + HikkakuBool, + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type VideoSensingAttribute = 'motion' | 'direction' +export type VideoSensingSubject = 'this sprite' | 'Stage' +export type VideoState = 'off' | 'on' | 'on-flipped' + +/** + * Hat block that triggers when video motion exceeds a threshold. + * + * Input: `reference`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param reference See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { whenMotionGreaterThan } from 'hikkaku/blocks' + * + * whenMotionGreaterThan(10, () => {}) + * ``` + */ +export const whenMotionGreaterThan = ( + reference: PrimitiveSource, + stack?: () => void, +) => { + const res = block('videoSensing_whenMotionGreaterThan', { + topLevel: true, + inputs: { + REFERENCE: fromPrimitiveSource(InputType.Number, reference, 10), + }, + }) + attachStack(res.id, stack) + return res +} + +/** + * Reports video motion/direction on a subject. + * + * Input: `attribute`, `subject`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param attribute See function signature for accepted input values. + * @param subject See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { videoOn } from 'hikkaku/blocks' + * + * videoOn('motion', 'this sprite') + * ``` + */ +export const videoOn = ( + attribute: PrimitiveSource, + subject: PrimitiveSource, +) => { + return valueBlock('videoSensing_videoOn', { + inputs: { + ATTRIBUTE: menuInput(attribute, menuOfAttribute), + SUBJECT: menuInput(subject, menuOfSubject), + }, + }) +} + +export const menuOfAttribute = ( + attribute: VideoSensingAttribute = 'motion', +) => { + return valueBlock('videoSensing_menu_ATTRIBUTE', { + fields: { + ATTRIBUTE: [attribute, null], + }, + isShadow: true, + }) +} + +export const menuOfSubject = (subject: VideoSensingSubject = 'this sprite') => { + return valueBlock('videoSensing_menu_SUBJECT', { + fields: { + SUBJECT: [subject, null], + }, + isShadow: true, + }) +} + +/** + * Turns video on or off. + * + * Input: `state`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param state See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { videoToggle } from 'hikkaku/blocks' + * + * videoToggle('on') + * ``` + */ +export const videoToggle = (state: PrimitiveSource) => { + return block('videoSensing_videoToggle', { + inputs: { + VIDEO_STATE: menuInput(state, menuOfVideoState), + }, + }) +} + +export const menuOfVideoState = (state: VideoState = 'on') => { + return valueBlock('videoSensing_menu_VIDEO_STATE', { + fields: { + VIDEO_STATE: [state, null], + }, + isShadow: true, + }) +} + +/** + * Sets video transparency. + * + * Input: `transparency`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param transparency See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { setVideoTransparency } from 'hikkaku/blocks' + * + * setVideoTransparency(50) + * ``` + */ +export const setVideoTransparency = ( + transparency: PrimitiveSource, +) => { + return block('videoSensing_setVideoTransparency', { + inputs: { + TRANSPARENCY: fromPrimitiveSource(InputType.Number, transparency, 50), + }, + }) +} + +/** + * Boolean check for video on state. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + */ +export const isVideoOn = () => { + return valueBlock('videoSensing_videoOn', {}) +} diff --git a/packages/hikkaku/src/blocks/wedo2.ts b/packages/hikkaku/src/blocks/wedo2.ts new file mode 100644 index 0000000..e5f54d4 --- /dev/null +++ b/packages/hikkaku/src/blocks/wedo2.ts @@ -0,0 +1,374 @@ +import { InputType } from 'sb3-types/enum' +import { fromPrimitiveSource, menuInput } from '../core/block-helper' +import { attachStack, block, valueBlock } from '../core/composer' +import type { + HikkakuBool, + HikkakuNumber, + HikkakuString, + PrimitiveSource, +} from '../core/types' + +export type WeDo2MotorId = 'motor' | 'motor1' | 'motor2' | 'all motors' +export type WeDo2MotorDirection = 'this way' | 'that way' | 'reverse' +export type WeDo2TiltDirection = 'up' | 'down' | 'left' | 'right' +export type WeDo2TiltDirectionAny = 'up' | 'down' | 'left' | 'right' | 'any' +export type WeDo2DistanceOp = '<' | '>' + +/** + * Turns a WeDo 2.0 motor on for the given duration. + * + * Input: `motorId`, `duration`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @param duration See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2MotorOnFor } from 'hikkaku/blocks' + * + * wedo2MotorOnFor('motor', 1) + * ``` + */ +export const wedo2MotorOnFor = ( + motorId: PrimitiveSource, + duration: PrimitiveSource, +) => { + return block('wedo2_motorOnFor', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + DURATION: fromPrimitiveSource(InputType.Number, duration, 1), + }, + }) +} + +export const menuOfMotorId = (motorId: WeDo2MotorId = 'motor') => { + return valueBlock('wedo2_menu_MOTOR_ID', { + fields: { + MOTOR_ID: [motorId, null], + }, + isShadow: true, + }) +} + +/** + * Turns a WeDo 2.0 motor on indefinitely. + * + * Input: `motorId`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2MotorOn } from 'hikkaku/blocks' + * + * wedo2MotorOn('motor') + * ``` + */ +export const wedo2MotorOn = (motorId: PrimitiveSource) => { + return block('wedo2_motorOn', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + }, + }) +} + +/** + * Turns a WeDo 2.0 motor off. + * + * Input: `motorId`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2MotorOff } from 'hikkaku/blocks' + * + * wedo2MotorOff('motor') + * ``` + */ +export const wedo2MotorOff = (motorId: PrimitiveSource) => { + return block('wedo2_motorOff', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + }, + }) +} + +/** + * Sets the power of a WeDo 2.0 motor and turns it on. + * + * Input: `motorId`, `power`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @param power See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2StartMotorPower } from 'hikkaku/blocks' + * + * wedo2StartMotorPower('motor', 100) + * ``` + */ +export const wedo2StartMotorPower = ( + motorId: PrimitiveSource, + power: PrimitiveSource, +) => { + return block('wedo2_startMotorPower', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + POWER: fromPrimitiveSource(InputType.Number, power, 100), + }, + }) +} + +/** + * Sets the direction of a WeDo 2.0 motor. + * + * Input: `motorId`, `direction`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param motorId See function signature for accepted input values. + * @param direction See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2SetMotorDirection } from 'hikkaku/blocks' + * + * wedo2SetMotorDirection('motor', 'this way') + * ``` + */ +export const wedo2SetMotorDirection = ( + motorId: PrimitiveSource, + direction: PrimitiveSource, +) => { + return block('wedo2_setMotorDirection', { + inputs: { + MOTOR_ID: menuInput(motorId, menuOfMotorId), + MOTOR_DIRECTION: menuInput(direction, menuOfMotorDirection), + }, + }) +} + +export const menuOfMotorDirection = ( + direction: WeDo2MotorDirection = 'this way', +) => { + return valueBlock('wedo2_menu_MOTOR_DIRECTION', { + fields: { + MOTOR_DIRECTION: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Sets the WeDo 2.0 LED light color. + * + * Input: `hue`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param hue See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2SetLightHue } from 'hikkaku/blocks' + * + * wedo2SetLightHue(50) + * ``` + */ +export const wedo2SetLightHue = (hue: PrimitiveSource) => { + return block('wedo2_setLightHue', { + inputs: { + HUE: fromPrimitiveSource(InputType.Number, hue, 50), + }, + }) +} + +/** + * Plays a note on WeDo 2.0 for some time. + * + * Input: `note`, `duration`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param note See function signature for accepted input values. + * @param duration See function signature for accepted input values. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2PlayNoteFor } from 'hikkaku/blocks' + * + * wedo2PlayNoteFor(60, 0.5) + * ``` + */ +export const wedo2PlayNoteFor = ( + note: PrimitiveSource, + duration: PrimitiveSource, +) => { + return block('wedo2_playNoteFor', { + inputs: { + NOTE: fromPrimitiveSource(InputType.Number, note, 60), + DURATION: fromPrimitiveSource(InputType.Number, duration, 0.5), + }, + }) +} + +/** + * Hat block that triggers based on distance. + * + * Input: `op`, `reference`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param op See function signature for accepted input values. + * @param reference See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2WhenDistance } from 'hikkaku/blocks' + * + * wedo2WhenDistance('<', 50, () => {}) + * ``` + */ +export const wedo2WhenDistance = ( + op: PrimitiveSource, + reference: PrimitiveSource, + stack?: () => void, +) => { + const res = block('wedo2_whenDistance', { + topLevel: true, + inputs: { + OP: menuInput(op, menuOfOp), + REFERENCE: fromPrimitiveSource(InputType.Number, reference, 50), + }, + }) + attachStack(res.id, stack) + return res +} + +export const menuOfOp = (op: WeDo2DistanceOp = '<') => { + return valueBlock('wedo2_menu_OP', { + fields: { + OP: [op, null], + }, + isShadow: true, + }) +} + +/** + * Hat block that triggers when WeDo 2.0 is tilted. + * + * Input: `tiltDirection`, `stack`. + * Output: Scratch statement block definition that is appended to the current script stack. + * + * @param tiltDirection See function signature for accepted input values. + * @param stack See function signature for accepted input values. Optional. + * @returns Scratch statement block definition that is appended to the current script stack. + * @example + * ```ts + * import { wedo2WhenTilted } from 'hikkaku/blocks' + * + * wedo2WhenTilted('any', () => {}) + * ``` + */ +export const wedo2WhenTilted = ( + tiltDirection: PrimitiveSource, + stack?: () => void, +) => { + const res = block('wedo2_whenTilted', { + topLevel: true, + inputs: { + TILT_DIRECTION_ANY: menuInput(tiltDirection, menuOfTiltDirectionAny), + }, + }) + attachStack(res.id, stack) + return res +} + +export const menuOfTiltDirectionAny = ( + direction: WeDo2TiltDirectionAny = 'any', +) => { + return valueBlock('wedo2_menu_TILT_DIRECTION_ANY', { + fields: { + TILT_DIRECTION_ANY: [direction, null], + }, + isShadow: true, + }) +} + +/** + * Returns the WeDo 2.0 distance sensor value. + * + * Input: none. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { wedo2GetDistance } from 'hikkaku/blocks' + * + * wedo2GetDistance() + * ``` + */ +export const wedo2GetDistance = () => { + return valueBlock('wedo2_getDistance', {}) +} + +/** + * Boolean check for whether WeDo 2.0 is tilted. + * + * Input: `tiltDirection`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param tiltDirection See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { wedo2IsTilted } from 'hikkaku/blocks' + * + * wedo2IsTilted('any') + * ``` + */ +export const wedo2IsTilted = (tiltDirection: PrimitiveSource) => { + return valueBlock('wedo2_isTilted', { + inputs: { + TILT_DIRECTION_ANY: menuInput(tiltDirection, menuOfTiltDirectionAny), + }, + }) +} + +/** + * Returns the tilt angle of the WeDo 2.0 sensor. + * + * Input: `tiltDirection`. + * Output: Scratch reporter block definition that can be used as an input value in other blocks. + * + * @param tiltDirection See function signature for accepted input values. + * @returns Scratch reporter block definition that can be used as an input value in other blocks. + * @example + * ```ts + * import { wedo2GetTiltAngle } from 'hikkaku/blocks' + * + * wedo2GetTiltAngle('up') + * ``` + */ +export const wedo2GetTiltAngle = ( + tiltDirection: PrimitiveSource, +) => { + return valueBlock('wedo2_getTiltAngle', { + inputs: { + TILT_DIRECTION: menuInput(tiltDirection, menuOfTiltDirection), + }, + }) +} + +export const menuOfTiltDirection = (direction: WeDo2TiltDirection = 'up') => { + return valueBlock('wedo2_menu_TILT_DIRECTION', { + fields: { + TILT_DIRECTION: [direction, null], + }, + isShadow: true, + }) +}