Skip to content

Commit

Permalink
Added bytes and bytearray
Browse files Browse the repository at this point in the history
  • Loading branch information
QuirkyCort committed Sep 10, 2024
1 parent 353a79d commit f0dffcd
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion public/customBlocks.json

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions public/customBlocks/data/bytearray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "bytearray",
"message0": "bytearray %1",
"args0": [
{
"type": "field_input",
"name": "bytestring",
"text": ""
}
],
"inputsInline": true,
"output": "Bytes",
"colour": 230,
"tooltip": "#blk-bytearray_tooltip#",
"helpUrl": ""
}
16 changes: 16 additions & 0 deletions public/customBlocks/data/bytearray_by_size.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "bytearray_by_size",
"message0": "bytearray #blk-of_size# %1",
"args0": [
{
"type": "input_value",
"name": "size",
"check": "Number"
}
],
"inputsInline": true,
"output": "Bytes",
"colour": 230,
"tooltip": "#blk-bytearray_by_size_tooltip#",
"helpUrl": ""
}
16 changes: 16 additions & 0 deletions public/customBlocks/data/bytes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "bytes",
"message0": "bytes %1",
"args0": [
{
"type": "field_input",
"name": "bytestring",
"text": ""
}
],
"inputsInline": true,
"output": "Bytes",
"colour": 230,
"tooltip": "#blk-bytes_tooltip#",
"helpUrl": ""
}
55 changes: 55 additions & 0 deletions public/js/ioty_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ var ioty_generator = new function() {
Blockly.Python['run_python_and_return'] = self.run_python_and_return;

Blockly.Python['type_cast'] = self.type_cast;
Blockly.Python['bytes'] = self.bytes;
Blockly.Python['bytearray'] = self.bytearray;
Blockly.Python['bytearray_by_size'] = self.bytearray_by_size;
Blockly.Python['decode'] = self.decode;
Blockly.Python['encode'] = self.encode;
Blockly.Python['unpack'] = self.unpack;
Expand Down Expand Up @@ -1131,6 +1134,58 @@ var ioty_generator = new function() {
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};

this.bytes = function(block) {
const bytestring = block.getFieldValue('bytestring');

let outstring = '';
let escaped = false;
for (c of bytestring) {
if (c == "'" && escaped == false) {
outstring += '\\';
}
outstring += c;
if (c == '\\' && escaped == false) {
escaped = true;
} else {
escaped = false;
}
}

let code = 'b\'' + outstring + '\'';

return [code, Blockly.Python.ORDER_ATOMIC];
};

this.bytearray = function(block) {
const bytestring = block.getFieldValue('bytestring');

let outstring = '';
let escaped = false;
for (c of bytestring) {
if (c == "'" && escaped == false) {
outstring += '\\';
}
outstring += c;
if (c == '\\' && escaped == false) {
escaped = true;
} else {
escaped = false;
}
}

let code = 'bytearray(b\'' + outstring + '\')';

return [code, Blockly.Python.ORDER_ATOMIC];
};

this.bytearray_by_size = function(block) {
var size = Blockly.Python.valueToCode(block, 'size', Blockly.Python.ORDER_ATOMIC);

let code = 'bytearray(' + size + ')';

return [code, Blockly.Python.ORDER_ATOMIC];
};

this.decode = function(block) {
var value = Blockly.Python.valueToCode(block, 'value', Blockly.Python.ORDER_ATOMIC);

Expand Down
12 changes: 12 additions & 0 deletions public/js/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,18 @@ let MSGS = {
'#blk-type_cast_tooltip#': {
en: 'Convert from one data type into another.',
},
'#blk-bytes_tooltip#': {
en: 'Returns a bytes object. Bytes objects can be indexed like a list, but cannot be modified.',
},
'#blk-bytearray_tooltip#': {
en: 'Returns a bytearray. Bytearray works just like a bytes object, but can be modified.',
},
'#blk-of_size#': {
en: 'of size',
},
'#blk-bytearray_by_size_tooltip#': {
en: 'Returns a bytearray of the specified size initialized to all 0.',
},
'#blk-neopixel_init#': {
en: 'Initialize Neopixel at pin',
},
Expand Down
9 changes: 9 additions & 0 deletions public/toolbox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@
</category>
<category name="#blk-data#" colour="#5b67a5">
<block type="type_cast"></block>
<block type="bytes"></block>
<block type="bytearray"></block>
<block type="bytearray_by_size">
<value name="size">
<shadow type="math_number">
<field name="NUM">0</field>
</shadow>
</value>
</block>
<block type="decode"></block>
<block type="encode"></block>
<block type="unpack"></block>
Expand Down

0 comments on commit f0dffcd

Please sign in to comment.