Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sd_read code emitter. #530

Merged
merged 1 commit into from
Jan 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 58 additions & 33 deletions src/modules/blockly/generators/propc/gpio.js
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,9 @@ Blockly.propc.wav_stop = function() {
return 'wav_stop();\n';
};

// ----------------------------------------------------------------
// ----------------- SD Card file blocks --------------------------
// ----------------------------------------------------------------

/**
* SD Card Initialization
Expand Down Expand Up @@ -2378,15 +2380,15 @@ Blockly.propc.sd_open = function() {
}

if (!this.disabled && !initFound && profile.sd_card) {
Blockly.propc.setups_['sd_card'] = 'sd_mount(' + profile.sd_card + ');';
Blockly.propc.setups_['sd_card'] = 'sd_mount(' + profile.sd_card + ');\r';
}

let code = head + 'fp = fopen("' + fp + '","' + mode + '");';
let code = head + 'fp = fopen("' + fp + '","' + mode + '");\r';
if (project.boardType.name !== 'activity-board' &&
project.boardType.name !== 'heb-wx' &&
allBlocks.toString().indexOf('SD initialize') === -1) {
code = '// WARNING: You must use a SD initialize block at the' +
' beginning of your program!';
' beginning of your program!\r';
}
return code;
};
Expand Down Expand Up @@ -2438,8 +2440,9 @@ Blockly.Blocks.sd_read = {
this.removeInput('VALUE');
}
if (mode === 'fwrite') {
// Ensure that the field only receives numeric data
this.appendValueInput('SIZE')
.setCheck(null)
.setCheck('Number')
.appendField('SD file')
.appendField(new Blockly.FieldDropdown([
['write', 'fwrite'],
Expand Down Expand Up @@ -2505,54 +2508,74 @@ Blockly.Blocks.sd_read = {
};

/**
* SD Card Read C code generator
* SD Card Read/Write/Close C code generator
*
* Generate code to read from an sd card, write to an sd card or to close a
* connection to the sd card reader.
*
* @return {string}
*/
Blockly.propc.sd_read = function() {
// Identify the block action (fread, fwrite, or fclose)
const mode = this.getFieldValue('MODE');

// Handle close stright away
if (mode === 'fclose') {
return ` if(fp) ${mode}(fp);`;
}

// Verify the required SD-Open block is in the project
const block = Blockly.getMainWorkspace().getBlocksByType(
'sd_open', false);

if ( block.length === 0 || (!block[0].isEnabled())) {
return '// WARNING: You must use a SD file open block before reading,' +
' writing, or closing an SD file!';
}

/**
* Verify that for boards that do not have a built-in card reader, there is
* an sd_init block in the project
*/
const project = getProjectInitialState();
const profile = getDefaultProfile();
let initFound = false;

const initSdBlock = Blockly.getMainWorkspace().getBlocksByType(
'sd_init', false);
if (initSdBlock.length > 0 && initSdBlock[0].isEnabled()) {
initFound = true;
}

if (project.boardType.name !== 'heb-wx' &&
project.boardType.name !== 'activity-board' &&
! initFound) {
return '// WARNING: You must use a SD initialize block at the' +
' beginning of your program!';
}

// Retreive the number of bytes to read/write. Default to one byte
const size = Blockly.propc.valueToCode(
this, 'SIZE', Blockly.propc.ORDER_NONE) || '1';
const mode = this.getFieldValue('MODE');

let value = '';
let code = '';

if (mode === 'fread') {
value = Blockly.propc.variableDB_.getName(
this.getFieldValue('VAR'),
Blockly.VARIABLE_CATEGORY_NAME);

value = '&' + value;
Blockly.propc.vartype_[value] = 'char *';
} else if (mode === 'fwrite') {
value = Blockly.propc.valueToCode(
this, 'VALUE', Blockly.propc.ORDER_NONE) || '';
}

if (mode === 'fclose') {
code = mode + '(fp);';
} else {
code = mode + '(' + value + ', 1, ' + size + ', fp);';
// code = mode + '(&' + value + ', 1, ' + size + ', fp);';
}

const allBlocks = Blockly.getMainWorkspace().getAllBlocks().toString();
if (allBlocks.indexOf('SD file open') === -1) {
code = '// WARNING: You must use a SD file open block before reading,' +
' writing, or closing an SD file!';
} else if (allBlocks.indexOf('SD initialize') === -1 &&
project.boardType.name !== 'heb-wx' &&
project.boardType.name !== 'activity-board') {
code = '// WARNING: You must use a SD initialize block at the' +
' beginning of your program!';
}

let initFound = false;
for (let x = 0; x < allBlocks.length; x++) {
if (allBlocks[x].type === 'sd_init') {
initFound = true;
}
}
code = ` ${mode}(${value}, 1, ${size}, fp);\r`;

// Silently mount the embedded sd card device
const profile = getDefaultProfile();
if (!this.disabled && !initFound && profile.sd_card) {
Blockly.propc.setups_['sd_card'] = 'sd_mount(' + profile.sd_card + ');';
}
Expand Down Expand Up @@ -2603,6 +2626,7 @@ Blockly.Blocks.sd_file_pointer = {
this.setPreviousStatement(true, 'Block');
this.setNextStatement(true, null);
} else {
// mode == get
this.appendDummyInput('FP');
this.getInput('FP')
.appendField('SD file')
Expand All @@ -2614,7 +2638,8 @@ Blockly.Blocks.sd_file_pointer = {
this.getSourceBlock().setSdMode(blockMode);
}), 'MODE')
.appendField('pointer');
this.setPreviousStatement(false, 'Block');

this.setPreviousStatement(false);
this.setNextStatement(false, null);
this.setOutput(true, 'Number');
}
Expand Down Expand Up @@ -2654,7 +2679,7 @@ Blockly.propc.sd_file_pointer = function() {
} else if (this.getFieldValue('MODE') === 'set') {
const fp = Blockly.propc.valueToCode(
this, 'FP', Blockly.propc.ORDER_NONE) || '0';
code = 'fp = ' + fp + ';';
code = 'fp = ' + fp + ';\r';
} else {
code = ['fp', Blockly.propc.ORDER_ATOMIC];
}
Expand Down