Skip to content

Commit

Permalink
Updated to modules (#24)
Browse files Browse the repository at this point in the history
* wip

* moving forward

* working again
  • Loading branch information
conejoninja committed Oct 10, 2022
1 parent e1b5d37 commit c6fe688
Show file tree
Hide file tree
Showing 15 changed files with 1,385 additions and 1,343 deletions.
61 changes: 22 additions & 39 deletions blocks/tinygo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,13 @@
*/
'use strict';

goog.provide('Blockly.TinyGo');
goog.module('Blockly.TinyGo');

const {createBlockDefinitionsFromJsonArray, defineBlocks} = goog.require('Blockly.common');
goog.require('Blockly.Types');

Blockly.TinyGo.init = function(workspace) {
Blockly.TinyGo.variables_ = [];
Blockly.TinyGo.pins_ = [];
Blockly.TinyGo.imports_ = [];
};

Blockly.TinyGo.addImport = function(id, path) {
Blockly.TinyGo.imports_[id] = path;
};

Blockly.TinyGo.addVariable = function(id, variable) {
Blockly.TinyGo.variables_[id] = variable;
};

Blockly.TinyGo.addDeclaration = function(id, data) {
Blockly.TinyGo.pins_[id] = data;
};

Blockly.TinyGo.configurePin = function(id, pinNumber, mode) {
Blockly.TinyGo.variables_[id] = 'const ' + id + ' = machine.Pin(' + pinNumber + ')';
Blockly.TinyGo.pins_[id] = id + '.Configure(machine.PinConfig{Mode: machine.Pin' + mode + '})';
Blockly.TinyGo.imports_['machine'] = 'machine';
};
const CATEGORY_NAME = 'TINYGO';
exports.CATEGORY_NAME = CATEGORY_NAME;

Blockly.Blocks['tinygo_led'] = {
init: function() {
Expand Down Expand Up @@ -65,15 +45,15 @@ Blockly.Blocks['tinygo_led'] = {
},
};

Blockly.TinyGo['tinygo_led'] = function(block) {
Blockly.Go['tinygo_led'] = function(block) {
const pins = block.connectorPinUsage();
const stateOutput = Blockly.TinyGo.valueToCode(
block, 'STATE', Blockly.TinyGo.ORDER_ATOMIC) || false;
const stateOutput = Blockly.Go.valueToCode(
block, 'STATE', Blockly.Go.ORDER_ATOMIC) || false;

// Blockly.TinyGo.reservePin(
// block, pins[0], Blockly.TinyGo.PinTypes.GROVE_LED, 'this Grove module');
// Blockly.Go.reservePin(
// block, pins[0], Blockly.Go.PinTypes.GROVE_LED, 'this Grove module');

Blockly.TinyGo.configurePin('ledPin' + pins[0], pins[0], 'Output');
Blockly.Go.configurePin('ledPin' + pins[0], pins[0], 'Output');
if (stateOutput == 'true') {
return 'ledPin' + pins[0] + '.High()\n';
}
Expand All @@ -88,7 +68,7 @@ Blockly.Types.LEDSTATE = new Blockly.Type({
});


Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
const blocks = createBlockDefinitionsFromJsonArray([
// Block for boolean data type: true and false.
{
"type": "tinygo_led_state",
Expand Down Expand Up @@ -255,15 +235,15 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
},
]);

Blockly.TinyGo['tinygo_led_state'] = function(block) {
Blockly.Go['tinygo_led_state'] = function(block) {
const code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
return [code, Blockly.TinyGo.ORDER_ATOMIC];
return [code, Blockly.Go.ORDER_ATOMIC];
};


Blockly.TinyGo['tinygo_goroutine'] = function(block) {
Blockly.Go['tinygo_goroutine'] = function(block) {
let code = '';
const branchCode = Blockly.TinyGo.statementToCode(block, 'GR0');
const branchCode = Blockly.Go.statementToCode(block, 'GR0');

const lines = branchCode.split('\n');
if (lines.length > 2) {
Expand All @@ -275,18 +255,21 @@ Blockly.TinyGo['tinygo_goroutine'] = function(block) {
return code;
};

Blockly.TinyGo['tinygo_time_sleep'] = function(block) {
Blockly.TinyGo.imports_['time'] = 'time';
Blockly.Go['tinygo_time_sleep'] = function(block) {
Blockly.Go.imports_['time'] = 'time';
const amount = block.getFieldValue('AMOUNT');
const unit = block.getFieldValue('UNIT');
const code = 'time.Sleep(' + amount + ' * ' + unit + ')\n';
return code;
};

Blockly.TinyGo['tinygo_led_complete'] = function(block) {
Blockly.Go['tinygo_led_complete'] = function(block) {
const state = block.getFieldValue('STATE');
const pin = block.getFieldValue('PIN');
Blockly.TinyGo.configurePin('ledPin' + pin, 'machine.D' + pin, 'Output');
Blockly.Go.configurePin('ledPin' + pin, 'machine.D' + pin, 'Output');

return 'ledPin' + pin + '.' + state + '()\n';
};

// Register provided blocks.
defineBlocks(blocks);
35 changes: 21 additions & 14 deletions generators/go.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ goog.require('Blockly.Types');
goog.require('Blockly.Generator');
goog.require('Blockly.utils.string');
goog.require('GoFmtServer');
goog.require('Blockly.TinyGo');
goog.require('Blockly.Names');


Expand Down Expand Up @@ -115,6 +114,8 @@ Blockly.Go.ORDER_OVERRIDES = [
*/
Blockly.Go.init = function (workspace) {
Blockly.Go.variables_ = Object.create(null);
Blockly.Go.pins_ = Object.create(null);
Blockly.Go.imports_ = Object.create(null);


// Create a dictionary of definitions to be printed before the code.
Expand Down Expand Up @@ -162,9 +163,6 @@ Blockly.Go.init = function (workspace) {
Blockly.Go.addVariable(varName,
'var ' + vName + ' ' + Blockly.Go.getGoType_(varsWithTypes[varName]));
}

Blockly.TinyGo.init(workspace);

};

/**
Expand All @@ -178,17 +176,13 @@ Blockly.Go.finish = function (code) {
defvars.push(Blockly.Go.variables_[i]);
}

for (var i in Blockly.TinyGo.variables_) {
defvars.push(Blockly.TinyGo.variables_[i]);
}

// Declare all of the variables.
let variables = defvars.join('\n');


defvars = [];
for (var i in Blockly.TinyGo.pins_) {
defvars.push(Blockly.TinyGo.pins_[i]);
for (var i in Blockly.Go.pins_) {
defvars.push(Blockly.Go.pins_[i]);
}

code = variables + '\n\nfunc main() {\n' + defvars.join('\n') + '\n' + code + '}';
Expand All @@ -200,8 +194,8 @@ Blockly.Go.finish = function (code) {
}

defvars = [];
for (var i in Blockly.TinyGo.imports_) {
defvars.push('"' + Blockly.TinyGo.imports_[i] + '"');
for (var i in Blockly.Go.imports_) {
defvars.push('"' + Blockly.Go.imports_[i] + '"');
}
let importsStr = '';
if (defvars.length > 0) {
Expand Down Expand Up @@ -359,7 +353,6 @@ Blockly.Go.getAdjusted = function (block, atId, opt_delta, opt_negate,
};

Blockly.Go.getGoType_ = function (typeBlockly) {
console.log("TYPEBLOCKLY", typeBlockly, Blockly.Types.COLOUR);
if (typeBlockly == undefined) {
return 'Invalid Blockly Type';
}
Expand Down Expand Up @@ -403,11 +396,25 @@ Blockly.Go.getGoType_ = function (typeBlockly) {
* @return {!boolean} Indicates if the declaration overwrote a previous one.
*/
Blockly.Go.addVariable = function (varName, code, overwrite) {
console.log("ADD VARIABLE", varName, code, overwrite);
var overwritten = false;
if (overwrite || (Blockly.Go.variables_[varName] === undefined)) {
Blockly.Go.variables_[varName] = code;
overwritten = true;
}
return overwritten;
};


Blockly.Go.addImport = function(id, path) {
Blockly.Go.imports_[id] = path;
};

Blockly.Go.addDeclaration = function(id, data) {
Blockly.Go.pins_[id] = data;
};

Blockly.Go.configurePin = function(id, pinNumber, mode) {
Blockly.Go.variables_[id] = 'const ' + id + ' = machine.Pin(' + pinNumber + ')';
Blockly.Go.pins_[id] = id + '.Configure(machine.PinConfig{Mode: machine.Pin' + mode + '})';
Blockly.Go.imports_['machine'] = 'machine';
};
2 changes: 2 additions & 0 deletions generators/go/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ goog.require('Blockly.Go.texts');
goog.require('Blockly.Go.variables');
goog.require('Blockly.Go.variablesDynamic');

goog.require('Blockly.TinyGo');

66 changes: 33 additions & 33 deletions generators/go/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,50 @@
*/
'use strict';

goog.provide('Blockly.Go.colour');
goog.module('Blockly.Go.colour');

goog.require('Blockly.Go');
const Go = goog.require('Blockly.Go');


Blockly.Go['colour_picker'] = function(block) {
Go['colour_picker'] = function(block) {
// Colour picker.
var code = Blockly.Go.quote_(block.getFieldValue('COLOUR'));
return [code, Blockly.Go.ORDER_ATOMIC];
var code = Go.quote_(block.getFieldValue('COLOUR'));
return [code, Go.ORDER_ATOMIC];
};

Blockly.Go['colour_array'] = function(block) {
Go['colour_array'] = function(block) {
var code = new Array(block.itemCount_);
for (var i = 0; i < block.itemCount_; i++) {
code[i] = Blockly.Go.valueToCode(block, 'ADD' + i,
Blockly.Go.ORDER_COMMA) || 'null';
code[i] = Go.valueToCode(block, 'ADD' + i,
Go.ORDER_COMMA) || 'null';
}
code = 'array(' + code.join(', ') + ')';
return [code, Blockly.Go.ORDER_FUNCTION_CALL];
return [code, Go.ORDER_FUNCTION_CALL];
};

Blockly.Go['colour_random'] = function(block) {
Go['colour_random'] = function(block) {
// Generate a random colour.
var functionName = Blockly.Go.provideFunction_(
var functionName = Go.provideFunction_(
'colour_random',
['func ' + Blockly.Go.FUNCTION_NAME_PLACEHOLDER_ + '() {',
['func ' + Go.FUNCTION_NAME_PLACEHOLDER_ + '() {',
' return \'#\' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), ' +
'6, \'0\', STR_PAD_LEFT);',
'}']);
var code = functionName + '()';
return [code, Blockly.Go.ORDER_FUNCTION_CALL];
return [code, Go.ORDER_FUNCTION_CALL];
};

Blockly.Go['colour_rgb'] = function(block) {
Go['colour_rgb'] = function(block) {
// Compose a colour from RGB components expressed as percentages.
var red = Blockly.Go.valueToCode(block, 'RED',
Blockly.Go.ORDER_COMMA) || 0;
var green = Blockly.Go.valueToCode(block, 'GREEN',
Blockly.Go.ORDER_COMMA) || 0;
var blue = Blockly.Go.valueToCode(block, 'BLUE',
Blockly.Go.ORDER_COMMA) || 0;
var functionName = Blockly.Go.provideFunction_(
var red = Go.valueToCode(block, 'RED',
Go.ORDER_COMMA) || 0;
var green = Go.valueToCode(block, 'GREEN',
Go.ORDER_COMMA) || 0;
var blue = Go.valueToCode(block, 'BLUE',
Go.ORDER_COMMA) || 0;
var functionName = Go.provideFunction_(
'colour_rgb',
['func ' + Blockly.Go.FUNCTION_NAME_PLACEHOLDER_ +
['func ' + Go.FUNCTION_NAME_PLACEHOLDER_ +
'($r, $g, $b) {',
' $r = round(max(min($r, 100), 0) * 2.55);',
' $g = round(max(min($g, 100), 0) * 2.55);',
Expand All @@ -65,20 +65,20 @@ Blockly.Go['colour_rgb'] = function(block) {
' return $hex;',
'}']);
var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
return [code, Blockly.Go.ORDER_FUNCTION_CALL];
return [code, Go.ORDER_FUNCTION_CALL];
};

Blockly.Go['colour_blend'] = function(block) {
Go['colour_blend'] = function(block) {
// Blend two colours together.
var c1 = Blockly.Go.valueToCode(block, 'COLOUR1',
Blockly.Go.ORDER_COMMA) || '\'#000000\'';
var c2 = Blockly.Go.valueToCode(block, 'COLOUR2',
Blockly.Go.ORDER_COMMA) || '\'#000000\'';
var ratio = Blockly.Go.valueToCode(block, 'RATIO',
Blockly.Go.ORDER_COMMA) || 0.5;
var functionName = Blockly.Go.provideFunction_(
var c1 = Go.valueToCode(block, 'COLOUR1',
Go.ORDER_COMMA) || '\'#000000\'';
var c2 = Go.valueToCode(block, 'COLOUR2',
Go.ORDER_COMMA) || '\'#000000\'';
var ratio = Go.valueToCode(block, 'RATIO',
Go.ORDER_COMMA) || 0.5;
var functionName = Go.provideFunction_(
'colour_blend',
['func ' + Blockly.Go.FUNCTION_NAME_PLACEHOLDER_ +
['func ' + Go.FUNCTION_NAME_PLACEHOLDER_ +
'($c1, $c2, $ratio) {',
' $ratio = max(min($ratio, 1), 0);',
' $r1 = hexdec(substr($c1, 1, 2));',
Expand All @@ -97,5 +97,5 @@ Blockly.Go['colour_blend'] = function(block) {
' return $hex;',
'}']);
var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
return [code, Blockly.Go.ORDER_FUNCTION_CALL];
return [code, Go.ORDER_FUNCTION_CALL];
};
Loading

0 comments on commit c6fe688

Please sign in to comment.