ocoge/blocks/sensors/sfmv17.js

288 lines
8.0 KiB
JavaScript
Raw Normal View History

/****************************** */
/** SFM-V1.7 Fingerprint Sensor */
/****************************** */
// 初期化
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_init",
"message0": "指紋センサ(ポート %1 )に接続",
"args0": [
{
"type": "input_value",
"name": "port",
"check": "String"
}
],
"previousStatement": null,
"nextStatement": null,
"tooltip": "指紋センサ SFM-V1.7 とのシリアル通信を開始します。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_init'] = function (block, generator) {
var value_port = generator.valueToCode(block, 'port', javascript.Order.ATOMIC);
Blockly.JavaScript.provideFunction_(
'require_sfmv17', [`const _sfm = require('@ocoge/sfmv17');`]
);
var code = `await _sfm.init(_rg, ${value_port}, 115200);\n`;
return code;
};
// リングカラー
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_setringcolor",
"message0": "指紋センサ LED リング色: %1 %2 %3 周期 %4 秒",
"args0": [
{
"type": "field_dropdown",
"name": "start_color",
"options": [
[
"OFF",
"0x07"
],
[
"赤",
"0x03"
],
[
"緑",
"0x05"
],
[
"青",
"0x06"
],
[
"黄",
"0x01"
],
[
"紫",
"0x02"
],
[
"青緑(シアン)",
"0x04"
]
]
},
{
"type": "field_dropdown",
"name": "end_color",
"options": [
[
"OFF",
"0x07"
],
[
"赤",
"0x03"
],
[
"緑",
"0x05"
],
[
"青",
"0x06"
],
[
"黄",
"0x01"
],
[
"紫",
"0x02"
],
[
"青緑(シアン)",
"0x04"
]
]
},
{
"type": "input_dummy"
},
{
"type": "input_value",
"name": "period_sec",
"check": "Number"
}
],
"inputsInline": true,
"previousStatement": null,
"nextStatement": null,
"tooltip": "指紋センサの LED リングの色を一定周期で切り替えます。周期は 0.3 秒から 2 秒までを指定できます。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_setringcolor'] = function (block, generator) {
var dropdown_start_color = block.getFieldValue('start_color');
var dropdown_end_color = block.getFieldValue('end_color');
var value_period_sec = generator.valueToCode(block, 'period_sec', javascript.Order.ATOMIC);
var code = `await _sfm.setRingColor(${dropdown_start_color}, ${dropdown_end_color}, ${value_period_sec}*1000);\n`;
return code;
};
// 記録済みユーザ数取得
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_getusercount",
"message0": "指紋登録数",
"output": "Number",
"tooltip": "データベースに記録された指紋の登録数を返します。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_getusercount'] = function (block, generator) {
var code = `await _sfm.getUserCount()`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
// 指紋認識
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_recognize",
"message0": "指紋 ID",
"output": "Number",
"tooltip": "指紋を認識します。データベースの指紋と一致した場合その ID を、一致しなければ 0 を、エラーの場合は -1 を返します。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_recognize'] = function (block, generator) {
var code = 'await _sfm.recognition_1vN()';
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
// 指紋登録
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_registration",
"message0": "指紋登録 %1",
"args0": [
{
"type": "field_dropdown",
"name": "step",
"options": [
[
"ステップ1",
"1"
],
[
"ステップ2",
"2"
],
[
"ステップ3",
"3"
]
]
}
],
"output": null,
"tooltip": "指紋をモジュール内データベースに登録します。ステップ1からステップ3を行います。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_registration'] = function (block, generator) {
var dropdown_step = block.getFieldValue('step');
var code = `await _sfm.register_3c3r(${dropdown_step})`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
// 全ユーザ一括削除
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_deletealluser",
"message0": "指紋一括削除",
"previousStatement": null,
"nextStatement": null,
"tooltip": "指紋センサに登録されている指紋を全て削除します。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_deletealluser'] = function (block, generator) {
var code = `await _sfm.deleteAllUser();\n`;
return code;
};
// 切断
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_stop",
"message0": "指紋センサから切断",
"previousStatement": null,
"nextStatement": null,
"tooltip": "指紋センサとのシリアル通信を終了してポートを開放します。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_stop'] = function (block, generator) {
var code = `await _sfm.stop();\n`;
return code;
};
flyout_contents = flyout_contents.concat([
{
"kind": "label",
"text": "指紋センサ SFM-V1.7",
"web-line": "4.0",
"web-line-width": "200"
},
{
"kind": "block",
"type": "oc_sfmv17_init",
"inputs": {
"port": {
"shadow": {
"type": "text",
"fields": {
"TEXT": "/dev/ttyS0"
}
}
}
}
},
{
"kind": "block",
"type": "oc_sfmv17_setringcolor",
"fields": {
"start_color": "0x03"
},
"fields": {
"end_color": "0x07"
},
"inputs": {
"period_sec": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": "0.5"
}
}
}
}
},
{
"kind": "block",
"type": "oc_sfmv17_getusercount"
},
{
"kind": "block",
"type": "oc_sfmv17_recognize"
},
{
"kind": "block",
"type": "oc_sfmv17_registration",
"fields": {
"step": "1"
}
},
{
"kind": "block",
"type": "oc_sfmv17_deletealluser"
},
{
"kind": "block",
"type": "oc_sfmv17_stop"
}
]);