ocoge/blocks/sensors/sfmv17.js

333 lines
9.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/****************************** */
/** 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];
};
// ユーザ削除 / ID 0 で全削除
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_deleteuser",
"message0": "ID %1 の指紋を削除",
"args0": [
{
"type": "input_value",
"name": "uid"
}
],
"inputsInline": true,
"previousStatement": null,
"nextStatement": null,
"tooltip": "登録済みの指紋から ID で指定したものを削除します。ID 0 を指定すると全部の指紋を削除します。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_deleteuser'] = function (block, generator) {
var value_uid = generator.valueToCode(block, 'uid', javascript.Order.ATOMIC);
var code = `await _sfm.deleteUser(${value_uid});\n`;
return code;
};
// 指紋画像を取得
Blockly.defineBlocksWithJsonArray([{
"type": "oc_sfmv17_getimage",
"message0": "指紋画像をキャンバスに表示",
"previousStatement": null,
"nextStatement": null,
"tooltip": "指紋センサで読み取った指紋をキャンバスに表示します。",
"helpUrl": "",
"style": "sensor_blocks"
}]);
javascript.javascriptGenerator.forBlock['oc_sfmv17_getimage'] = function (block, generator) {
var code = ` let r = await _sfm.getImage();
console.log(r);
console.log(r.length)
let imgdata = _ctx.createImageData(160, 160);
for (let pixel=0; pixel<r.length; pixel++){
for (let rgb=0; rgb<3; rgb++)
imgdata.data[pixel*4+rgb] = r[pixel];
imgdata.data[pixel*4+3] = 0xff;
}
_ctx.putImageData(imgdata,0,0);
`;
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_deleteuser",
"inputs": {
"uid": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": "0"
}
}
}
}
},
{
"kind": "block",
"type": "oc_sfmv17_getimage"
},
{
"kind": "block",
"type": "oc_sfmv17_stop"
}
]);