diff --git a/packages/xod-arduino/src/formatTweakMessage.js b/packages/xod-arduino/src/formatTweakMessage.js index bbb6fdc61..186e0b981 100644 --- a/packages/xod-arduino/src/formatTweakMessage.js +++ b/packages/xod-arduino/src/formatTweakMessage.js @@ -7,6 +7,24 @@ import { def } from './types'; import { byteLiteralToDecimal } from './templates'; +// Convert char literals to decimal byte literals +// E.G. `'a'` -> `97d` +const charLiteralToByteLiteral = R.when( + XP.isLikeCharLiteral, + R.compose( + R.concat(R.__, 'd'), + R.toString, + a => a.charCodeAt(0), + R.nth(1), + R.match(XP.charLiteralRegExp) + ) +); + +const formatByteLiteral = R.compose( + byteLiteralToDecimal, + charLiteralToByteLiteral +); + export default def( 'formatTweakMessage :: PatchPath -> NodeId -> DataValue -> String', (nodeType, nodeId, value) => { @@ -17,7 +35,7 @@ export default def( case XP.PIN_TYPE.BOOLEAN: return `${prefix}:${value === 'True' ? '1' : '0'}\r\n`; case XP.PIN_TYPE.BYTE: - return `${prefix}:${byteLiteralToDecimal(value)}\r\n`; + return `${prefix}:${formatByteLiteral(value)}\r\n`; case XP.PIN_TYPE.PULSE: return `${prefix}\r\n`; case XP.PIN_TYPE.STRING: diff --git a/packages/xod-project/src/utils.js b/packages/xod-project/src/utils.js index f03ce04a7..fe9f3f9fa 100644 --- a/packages/xod-project/src/utils.js +++ b/packages/xod-project/src/utils.js @@ -187,9 +187,11 @@ export const isValidNumberDataValue = R.test(numberDataTypeRegExp); export const isValidPortLiteral = R.test(/^(P[A-F]|A|D)\d{0,3}$/g); +export const charLiteralRegExp = /^'\\?(.)'$/; + export const isLikeCharLiteral = def( 'isLikeCharLiteral :: String -> Boolean', - R.test(/^'\\?.'$/) + R.test(charLiteralRegExp) ); const unescapedCharLiterals = ["'''", "'\\'"];